ScreenPickerTool.cpp 6.99 KB
Newer Older
Roman Tolchenov's avatar
re #79  
Roman Tolchenov committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/***************************************************************************
    File                 : ScreenPickerTool.cpp
    Project              : QtiPlot
    --------------------------------------------------------------------
    Copyright            : (C) 2006,2007 by Ion Vasilief, Knut Franke
    Email (use @ for *)  : ion_vasilief*yahoo.fr, knut.franke*gmx.de
    Description          : Tool for selecting arbitrary points on a plot.

 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *  This program is free software; you can redistribute it and/or modify   *
 *  it under the terms of the GNU General Public License as published by   *
 *  the Free Software Foundation; either version 2 of the License, or      *
 *  (at your option) any later version.                                    *
 *                                                                         *
 *  This program is distributed in the hope that it will be useful,        *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 *  GNU General Public License for more details.                           *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the Free Software           *
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
 *   Boston, MA  02110-1301  USA                                           *
 *                                                                         *
 ***************************************************************************/
#include "ScreenPickerTool.h"
#include "ApplicationWindow.h"
#include "Graph.h"
#include "Plot.h"
#include "PlotCurve.h"
34
35
#include "SymbolBox.h"
#include "Table.h"
Roman Tolchenov's avatar
Roman Tolchenov committed
36
#include <MantidQtWidgets/Common/pixmaps.h>
37
#include <QKeyEvent>
38
39
40
#include <qwt_symbol.h>

using namespace MantidQt::API;
Roman Tolchenov's avatar
re #79  
Roman Tolchenov committed
41

42
43
44
45
46
47
48
49
50
51
52
53
54
55
ScreenPickerTool::ScreenPickerTool(Graph *graph, const QObject *status_target,
                                   const char *status_slot)
    : QwtPlotPicker(graph->plotWidget()->canvas()), PlotToolInterface(graph) {
  d_selection_marker.setLineStyle(QwtPlotMarker::Cross);
  d_selection_marker.setLinePen(QPen(Qt::red, 1));
  setTrackerMode(QwtPicker::AlwaysOn);
  setSelectionFlags(QwtPicker::PointSelection | QwtPicker::ClickSelection);
  d_graph->plotWidget()->canvas()->setCursor(
      QCursor(getQPixmap("cursor_xpm"), -1, -1));

  if (status_target)
    connect(this, SIGNAL(statusText(const QString &)), status_target,
            status_slot);
  emit statusText(tr("Click on plot or move cursor to display coordinates!"));
Roman Tolchenov's avatar
re #79  
Roman Tolchenov committed
56
57
}

58
59
60
61
ScreenPickerTool::~ScreenPickerTool() {
  d_selection_marker.detach();
  d_graph->plotWidget()->canvas()->unsetCursor();
  d_graph->plotWidget()->replot();
Roman Tolchenov's avatar
re #79  
Roman Tolchenov committed
62
63
}

64
65
66
67
68
69
70
71
72
73
void ScreenPickerTool::append(const QPoint &point) {
  QwtDoublePoint pos = invTransform(point);
  QString info;
  info.sprintf("x=%g; y=%g", pos.x(), pos.y());
  emit statusText(info);

  d_selection_marker.setValue(pos);
  if (d_selection_marker.plot() == NULL)
    d_selection_marker.attach(d_graph->plotWidget());
  d_graph->plotWidget()->replot();
Roman Tolchenov's avatar
re #79  
Roman Tolchenov committed
74
75
}

76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
bool ScreenPickerTool::eventFilter(QObject *obj, QEvent *event) {
  switch (event->type()) {
  case QEvent::MouseButtonDblClick:
    emit selected(d_selection_marker.value());
    return true;
  case QEvent::KeyPress: {
    QKeyEvent *ke = (QKeyEvent *)event;
    switch (ke->key()) {
    case Qt::Key_Enter:
    case Qt::Key_Return: {
      QwtDoublePoint pos =
          invTransform(canvas()->mapFromGlobal(QCursor::pos()));
      d_selection_marker.setValue(pos);
      if (d_selection_marker.plot() == NULL)
        d_selection_marker.attach(d_graph->plotWidget());
      d_graph->plotWidget()->replot();
      emit selected(d_selection_marker.value());
      QString info;
      emit statusText(info.sprintf("x=%g; y=%g", pos.x(), pos.y()));
      return true;
    }
    default:
      break;
    }
  }
  default:
    break;
  }
  return QwtPlotPicker::eventFilter(obj, event);
Roman Tolchenov's avatar
re #79  
Roman Tolchenov committed
105
}
106
107
108
109
110
111
/**
 *  This overrides the base class trackerText() function so that we can
 *  adjust the text returned.
 *
 *  @param pos  The current mouse location.
 */
112
113
QwtText ScreenPickerTool::trackerText(const QPoint &pos) const {
  return QwtPlotPicker::trackerText(pos);
114
}
Roman Tolchenov's avatar
re #79  
Roman Tolchenov committed
115

Nick Draper's avatar
Nick Draper committed
116
117
118
119
120
121
/**
 *  This overrides the base class trackerText() function so that we can
 *  adjust the text returned.
 *
 *  @param pos  The current mouse location.
 */
122
123
124
125
QwtText ScreenPickerTool::trackerText(const QwtDoublePoint &pos) const {
  QString info;
  info.sprintf("%g; %g", pos.x(), pos.y());
  return QwtText(info);
Nick Draper's avatar
Nick Draper committed
126
127
}

128
129
130
131
132
133
DrawPointTool::DrawPointTool(ApplicationWindow *app, Graph *graph,
                             const QObject *status_target,
                             const char *status_slot)
    : ScreenPickerTool(graph, status_target, status_slot), d_app(app) {
  d_curve = NULL;
  d_table = NULL;
Roman Tolchenov's avatar
re #79  
Roman Tolchenov committed
134
135
}

136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
void DrawPointTool::appendPoint(const QwtDoublePoint &pos) {
  if (!d_app)
    return;

  QString info;
  emit statusText(info.sprintf("x=%g; y=%g", pos.x(), pos.y()));

  if (!d_table) {
    d_table = d_app->newHiddenTable(d_app->generateUniqueName(tr("Draw")), "",
                                    30, 2, "");
    d_app->modifiedProject();
  }

  int rows = 0;
  if (d_curve)
    rows = d_curve->dataSize();

  if (d_table->numRows() <= rows)
    d_table->setNumRows(rows + 10);

  d_table->setCell(rows, 0, pos.x());
  d_table->setCell(rows, 1, pos.y());

  if (!d_curve) {
    d_curve = new DataCurve(d_table, d_table->colName(0), d_table->colName(1));
    d_curve->setAxis(QwtPlot::xBottom, QwtPlot::yLeft);
    d_curve->setPen(QPen(Qt::black, d_app->defaultCurveLineWidth));
    d_curve->setSymbol(
        QwtSymbol(QwtSymbol::Ellipse, QBrush(Qt::black),
                  QPen(Qt::black, d_app->defaultCurveLineWidth),
                  QSize(d_app->defaultSymbolSize, d_app->defaultSymbolSize)));
167
    d_graph->insertPlotItem(d_curve, GraphOptions::LineSymbols);
168
169
170
171
  }

  d_curve->setFullRange();
  d_graph->updatePlot();
Roman Tolchenov's avatar
re #79  
Roman Tolchenov committed
172
}
Peterson, Peter's avatar
Peterson, Peter committed
173

174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
bool DrawPointTool::eventFilter(QObject *obj, QEvent *event) {
  switch (event->type()) {
  case QEvent::MouseButtonDblClick:
    appendPoint(d_selection_marker.value());
    return true;
  case QEvent::KeyPress: {
    QKeyEvent *ke = (QKeyEvent *)event;
    switch (ke->key()) {
    case Qt::Key_Enter:
    case Qt::Key_Return: {
      QwtDoublePoint pos =
          invTransform(canvas()->mapFromGlobal(QCursor::pos()));
      d_selection_marker.setValue(pos);
      if (d_selection_marker.plot() == NULL)
        d_selection_marker.attach(d_graph->plotWidget());
      d_graph->plotWidget()->replot();
      emit selected(d_selection_marker.value());

      appendPoint(pos);
      return true;
    }
    default:
      break;
    }
  }
  default:
    break;
  }
  return QwtPlotPicker::eventFilter(obj, event);
Roman Tolchenov's avatar
re #79  
Roman Tolchenov committed
203
}