1 /***************************************************************************
2     File                 : ScreenPickerTool.cpp
3     Project              : SciDAVis
4     --------------------------------------------------------------------
5     Copyright            : (C) 2006,2007 by Ion Vasilief,
6                            Tilman Benkert, Knut Franke
7     Email (use @ for *)  : ion_vasilief*yahoo.fr, thzs*gmx.net,
8                            knut.franke*gmx.de
9     Description          : Tool for selecting arbitrary points on a plot.
10 
11  ***************************************************************************/
12 
13 /***************************************************************************
14  *                                                                         *
15  *  This program is free software; you can redistribute it and/or modify   *
16  *  it under the terms of the GNU General Public License as published by   *
17  *  the Free Software Foundation; either version 2 of the License, or      *
18  *  (at your option) any later version.                                    *
19  *                                                                         *
20  *  This program is distributed in the hope that it will be useful,        *
21  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
22  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
23  *  GNU General Public License for more details.                           *
24  *                                                                         *
25  *   You should have received a copy of the GNU General Public License     *
26  *   along with this program; if not, write to the Free Software           *
27  *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
28  *   Boston, MA  02110-1301  USA                                           *
29  *                                                                         *
30  ***************************************************************************/
31 #include "ScreenPickerTool.h"
32 #include "Graph.h"
33 #include "Plot.h"
34 #include <qwt_symbol.h>
35 #include <qwt_scale_draw.h>
36 #include <QKeyEvent>
37 
ScreenPickerTool(Graph * graph,const QObject * status_target,const char * status_slot)38 ScreenPickerTool::ScreenPickerTool(Graph *graph, const QObject *status_target,
39                                    const char *status_slot)
40     : QwtPlotPicker(graph->plotWidget()->canvas()), PlotToolInterface(graph)
41 {
42     //	d_selection_marker.setSymbol(QwtSymbol(QwtSymbol::Cross, QBrush(Qt::NoBrush),
43     // QPen(Qt::red,1), QSize(15,15)));
44     d_selection_marker.setLineStyle(QwtPlotMarker::Cross);
45     d_selection_marker.setLinePen(QPen(Qt::red, 1));
46     setTrackerMode(QwtPicker::AlwaysOn);
47     setSelectionFlags(QwtPicker::PointSelection | QwtPicker::ClickSelection);
48     d_graph->plotWidget()->canvas()->setCursor(QCursor(QPixmap(":/cursor.xpm"), -1, -1));
49 
50     if (status_target)
51         connect(this, SIGNAL(statusText(const QString &)), status_target, status_slot);
52     emit statusText(tr("Click on plot or move cursor to display coordinates!"));
53 }
54 
~ScreenPickerTool()55 ScreenPickerTool::~ScreenPickerTool()
56 {
57     d_selection_marker.detach();
58     d_graph->plotWidget()->canvas()->unsetCursor();
59     d_graph->plotWidget()->replot();
60 }
61 
append(const QPoint & point)62 void ScreenPickerTool::append(const QPoint &point)
63 {
64     //	QwtPlotPicker::append(point);
65 
66     QwtDoublePoint pos = invTransform(point);
67     emit statusText(trackerText(pos).text());
68 
69     d_selection_marker.setValue(pos);
70     if (d_selection_marker.plot() == NULL)
71         d_selection_marker.attach(d_graph->plotWidget());
72     d_graph->plotWidget()->replot();
73 }
74 
eventFilter(QObject * obj,QEvent * event)75 bool ScreenPickerTool::eventFilter(QObject *obj, QEvent *event)
76 {
77     switch (event->type()) {
78     case QEvent::MouseButtonDblClick:
79         emit selected(d_selection_marker.value());
80         return true;
81     case QEvent::KeyPress: {
82         QKeyEvent *ke = (QKeyEvent *)event;
83         switch (ke->key()) {
84         case Qt::Key_Enter:
85         case Qt::Key_Return:
86             emit selected(d_selection_marker.value());
87             return true;
88         default:
89             break;
90         }
91     }
92     default:
93         break;
94     }
95     return QwtPlotPicker::eventFilter(obj, event);
96 }
97 
trackerText(const QwtDoublePoint & point) const98 QwtText ScreenPickerTool::trackerText(const QwtDoublePoint &point) const
99 {
100     return plot()->axisScaleDraw(xAxis())->label(point.x()).text() + ", "
101             + plot()->axisScaleDraw(yAxis())->label(point.y()).text();
102 }
103