scidavis  2.3.0
About: SciDAVis is a free application for Scientific Data Analysis and Visualization (a fork off of QtiPlot).
  Fossies Dox: scidavis-2.3.0.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

RangeSelectorTool.cpp
Go to the documentation of this file.
1 
13 
31 #include "RangeSelectorTool.h"
32 #include "Graph.h"
33 #include "Plot.h"
34 #include "PlotCurve.h"
35 
36 #include <qwt_symbol.h>
37 #include <QPoint>
38 #include <QMessageBox>
39 #include <QEvent>
40 #include <QKeyEvent>
41 #include <QLocale>
42 
43 RangeSelectorTool::RangeSelectorTool(Graph *graph, const QObject *status_target, const char *status_slot)
44  : QwtPlotPicker(graph->plotWidget()->canvas()),
45  PlotToolInterface(graph)
46 {
47  d_selected_curve = NULL;
48  for (int i=d_graph->curves(); i>=0; --i) {
50  if (d_selected_curve && d_selected_curve->rtti() == QwtPlotItem::Rtti_PlotCurve
51  && d_selected_curve->dataSize() > 0)
52  break;
53  d_selected_curve = NULL;
54  }
55  if (!d_selected_curve) {
56  QMessageBox::critical(d_graph, tr("Warning"),
57  tr("All the curves on this plot are empty!"));
58  return;
59  }
60 
61  d_active_point = 0;
62  d_inactive_point = d_selected_curve->dataSize() - 1;
63  int marker_size = 20;
64  /* If we want this, it should also be added setSelectedCurve and pointSelected
65  if (d_selected_curve->symbol().style() != QwtSymbol::NoSymbol) {
66  QSize ssize = d_selected_curve->symbol().size();
67  marker_size += qMax(ssize.width(), ssize.height());
68  }
69  */
70  d_active_marker.setSymbol(QwtSymbol(QwtSymbol::Ellipse, QBrush(QColor(255,255,0,128)),
71  QPen(Qt::red,2), QSize(marker_size,marker_size)));
72  d_active_marker.setLineStyle(QwtPlotMarker::VLine);
73  d_active_marker.setLinePen(QPen(Qt::red, 1, Qt::DashLine));
74  d_inactive_marker.setSymbol(QwtSymbol(QwtSymbol::Ellipse, QBrush(QColor(255,255,0,128)),
75  QPen(Qt::black,2), QSize(marker_size,marker_size)));
76  d_inactive_marker.setLineStyle(QwtPlotMarker::VLine);
77  d_inactive_marker.setLinePen(QPen(Qt::black, 1, Qt::DashLine));
84 
85  setTrackerMode(QwtPicker::AlwaysOn);
86  setSelectionFlags(QwtPicker::PointSelection | QwtPicker::ClickSelection);
87  d_graph->plotWidget()->canvas()->setCursor(QCursor(QPixmap(":/vizor.xpm"), -1, -1));
88  d_graph->plotWidget()->canvas()->setFocus();
89  d_graph->plotWidget()->replot();
90 
91  if (status_target)
92  connect(this, SIGNAL(statusText(const QString&)), status_target, status_slot);
93  emit statusText(tr("Click or use Ctrl+arrow key to select range (arrows select active cursor)!"));
94 }
95 
97 {
98  d_active_marker.detach();
99  d_inactive_marker.detach();
100  d_graph->plotWidget()->canvas()->unsetCursor();
101  d_graph->plotWidget()->replot();
102 }
103 
104 void RangeSelectorTool::pointSelected(const QPoint &pos)
105 {
106  int dist, point;
107  const int curve_key = d_graph->plotWidget()->closestCurve(pos.x(), pos.y(), dist, point);
108  if (curve_key < 0 || dist >= 5) // 5 pixels tolerance
109  return;
110  QwtPlotCurve *curve = (QwtPlotCurve *)d_graph->plotWidget()->curve(curve_key);
111  if (!curve)
112  return;
113 
114  if (curve == d_selected_curve)
115  setActivePoint(point);
116  else {
117  // try to be intelligent about selecting the inactive point
118  double min_x = curve->data().boundingRect().left();
119  double max_x = curve->data().boundingRect().right();
120  int n = curve->dataSize();
121  double second_x;
122  if (curve->x(point) == min_x)
123  second_x = max_x;
124  else if (curve->x(point) == max_x)
125  second_x = min_x;
126  else if ( d_active_marker.xValue() < d_inactive_marker.xValue() )
127  second_x = max_x;
128  else
129  second_x = min_x;
130  if (second_x == max_x) { // start at selected point and try larger indices first
131  for (int i=0; i<n; ++i)
132  if (qFuzzyCompare(curve->x((i + point) % n), max_x))
133  d_inactive_point = (i + point) % n;
134  } else { // start at selected point and try smaller indices first
135  for (int i=n-1; i>=0; --i)
136  if (qFuzzyCompare(curve->x((i + point) % n),max_x))
137  d_inactive_point = (i + point) % n;
138  }
139  d_selected_curve = curve;
140  d_inactive_marker.setValue(curve->x(d_inactive_point), curve->y(d_inactive_point));
141  d_active_point = point;
143  emitStatusText();
144  emit changed();
145  }
146  d_graph->plotWidget()->replot();
147 }
148 
149 void RangeSelectorTool::setSelectedCurve(QwtPlotCurve *curve)
150 {
151  if (!curve || d_selected_curve == curve)
152  return;
153  d_selected_curve = curve;
154  d_active_point = 0;
155  d_inactive_point = d_selected_curve->dataSize() - 1;
158  emitStatusText();
159  emit changed();
160 }
161 
163 {
164  if (point == d_active_point)
165  return;
166  d_active_point = point;
168  emitStatusText();
169  emit changed();
170 }
171 
173 {
174  if (((PlotCurve *)d_selected_curve)->type() == Graph::Function)
175  {
176  emit statusText(QString("%1 <=> %2[%3]: x=%4; y=%5")
177  .arg(d_active_marker.xValue() > d_inactive_marker.xValue() ? tr("Right") : tr("Left"))
178  .arg(d_selected_curve->title().text())
179  .arg(d_active_point + 1)
180  .arg(QLocale().toString(d_selected_curve->x(d_active_point), 'G', 15))
181  .arg(QLocale().toString(d_selected_curve->y(d_active_point), 'G', 15)));
182  }
183  else
184  {
185  Table *t = ((DataCurve*)d_selected_curve)->table();
186  if (!t)
187  return;
188 
189  int row = ((DataCurve*)d_selected_curve)->tableRow(d_active_point);
190 
191  emit statusText(QString("%1 <=> %2[%3]: x=%4; y=%5")
192  .arg(d_active_marker.xValue() > d_inactive_marker.xValue() ? tr("Right") : tr("Left"))
193  .arg(d_selected_curve->title().text())
194  .arg(row + 1)
195  .arg(t->text(row, t->colIndex(((DataCurve*)d_selected_curve)->xColumnName())))
196  .arg(t->text(row, t->colIndex(d_selected_curve->title().text()))));
197  }
198 }
199 
201 {
202  QwtDoublePoint tmp = d_active_marker.value();
203  d_active_marker.setValue(d_inactive_marker.value());
204  d_inactive_marker.setValue(tmp);
205  int tmp2 = d_active_point;
207  d_inactive_point = tmp2;
208  d_graph->plotWidget()->replot();
209 
210  emitStatusText();
211 }
212 
213 bool RangeSelectorTool::eventFilter(QObject *obj, QEvent *event)
214 {
215  switch(event->type()) {
216  case QEvent::KeyPress:
217  if (keyEventFilter((QKeyEvent*)event))
218  return true;
219  break;
220  default:
221  break;
222  }
223  return QwtPlotPicker::eventFilter(obj, event);
224 }
225 
227 {
228  switch(ke->key()) {
229  case Qt::Key_Up:
230  {
231  int n_curves = d_graph->curves();
232  int start = d_graph->curveIndex(d_selected_curve) + 1;
233  for (int i = start; i < start + n_curves; ++i)
234  if (d_graph->curve(i % n_curves)->dataSize() > 0) {
235  setSelectedCurve(d_graph->curve(i % n_curves));
236  break;
237  }
238  d_graph->plotWidget()->replot();
239  return true;
240  }
241  case Qt::Key_Down:
242  {
243  int n_curves = d_graph->curves();
244  int start = d_graph->curveIndex(d_selected_curve) + n_curves - 1;
245  for (int i = start; i > start - n_curves; --i)
246  if (d_graph->curve(i % n_curves)->dataSize() > 0) {
247  setSelectedCurve(d_graph->curve(i % n_curves));
248  break;
249  }
250  d_graph->plotWidget()->replot();
251  return true;
252  }
253  case Qt::Key_Right:
254  case Qt::Key_Plus:
255  {
256  if (ke->modifiers() & Qt::ControlModifier) {
257  int n_points = d_selected_curve->dataSize();
258  setActivePoint((d_active_point + 1) % n_points);
259  d_graph->plotWidget()->replot();
260  } else
262  return true;
263  }
264  case Qt::Key_Left:
265  case Qt::Key_Minus:
266  {
267  if (ke->modifiers() & Qt::ControlModifier) {
268  int n_points = d_selected_curve->dataSize();
269  setActivePoint((d_active_point - 1 + n_points) % n_points);
270  d_graph->plotWidget()->replot();
271  } else
273  return true;
274  }
275  default:
276  break;
277  }
278  return false;
279 }
PlotToolInterface
Definition: PlotToolInterface.h:60
RangeSelectorTool::d_active_marker
QwtPlotMarker d_active_marker
Definition: RangeSelectorTool.h:89
Plot.h
Table::text
QString text(int row, int col)
Definition: Table.cpp:931
RangeSelectorTool::d_inactive_point
int d_inactive_point
Definition: RangeSelectorTool.h:90
RangeSelectorTool::~RangeSelectorTool
virtual ~RangeSelectorTool()
Definition: RangeSelectorTool.cpp:96
Plot::closestCurve
int closestCurve(int xpos, int ypos, int &dist, int &point)
Definition: Plot.cpp:427
Graph::Function
@ Function
Definition: Graph.h:131
RangeSelectorTool::setSelectedCurve
void setSelectedCurve(QwtPlotCurve *curve)
Caller is responsible for replot.
Definition: RangeSelectorTool.cpp:149
RangeSelectorTool::d_inactive_marker
QwtPlotMarker d_inactive_marker
Definition: RangeSelectorTool.h:89
Plot::curve
QwtPlotCurve * curve(int index)
Definition: Plot.cpp:418
RangeSelectorTool::statusText
void statusText(const QString &)
RangeSelectorTool.h
toString
S toString(const QString &x)
Deal with conversion between QString and std::string/std::wstring in a generic way.
Table
MDI window providing a spreadsheet table with column logic.
Definition: Table.h:51
RangeSelectorTool::d_active_point
int d_active_point
Definition: RangeSelectorTool.h:90
RangeSelectorTool::pointSelected
virtual void pointSelected(const QPoint &point)
Definition: RangeSelectorTool.cpp:104
RangeSelectorTool::switchActiveMarker
void switchActiveMarker()
Definition: RangeSelectorTool.cpp:200
Graph::plotWidget
Plot * plotWidget() const
Accessor method for d_plot.
Definition: Graph.h:155
python-sipcmd.dist
dist
Definition: python-sipcmd.py:52
Graph::curves
int curves() const
Definition: Graph.h:193
PlotCurve.h
PlotCurve
Abstract 2D plot curve class.
Definition: PlotCurve.h:37
PlotToolInterface::d_graph
Graph * d_graph
Definition: PlotToolInterface.h:66
Graph.h
RangeSelectorTool::emitStatusText
void emitStatusText()
Definition: RangeSelectorTool.cpp:172
RangeSelectorTool::changed
void changed()
Emitted whenever the selected curve and/or range have changed.
RangeSelectorTool::keyEventFilter
bool keyEventFilter(QKeyEvent *ke)
Definition: RangeSelectorTool.cpp:226
RangeSelectorTool::RangeSelectorTool
RangeSelectorTool(Graph *graph, const QObject *status_target=NULL, const char *status_slot="")
Definition: RangeSelectorTool.cpp:43
DataCurve
Definition: PlotCurve.h:52
RangeSelectorTool::eventFilter
virtual bool eventFilter(QObject *obj, QEvent *event)
Definition: RangeSelectorTool.cpp:213
Graph
A 2D-plotting widget.
Definition: Graph.h:119
Table::colIndex
int colIndex(const QString &name)
Definition: Table.cpp:1017
RangeSelectorTool::d_selected_curve
QwtPlotCurve * d_selected_curve
Definition: RangeSelectorTool.h:91
RangeSelectorTool::setActivePoint
void setActivePoint(int index)
Caller is responsible for replot.
Definition: RangeSelectorTool.cpp:162
Graph::curve
QwtPlotCurve * curve(int index) const
get curve by index
Definition: Graph.cpp:2821
Graph::curveIndex
int curveIndex(long key) const
Definition: Graph.h:199