qwt_plot_picker.cpp
Go to the documentation of this file.
1 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
2  * Qwt Widget Library
3  * Copyright (C) 1997 Josef Wilgen
4  * Copyright (C) 2002 Uwe Rathmann
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the Qwt License, Version 1.0
8  *****************************************************************************/
9 
10 #include "qwt_plot_picker.h"
11 #include "qwt_plot.h"
12 #include "qwt_text.h"
13 #include "qwt_scale_div.h"
14 #include "qwt_scale_map.h"
15 #include "qwt_picker_machine.h"
16 
30 QwtPlotPicker::QwtPlotPicker( QWidget *canvas ):
31  QwtPicker( canvas ),
32  d_xAxis( -1 ),
33  d_yAxis( -1 )
34 {
35  if ( !canvas )
36  return;
37 
38  // attach axes
39 
40  int xAxis = QwtPlot::xBottom;
41 
42  const QwtPlot *plot = QwtPlotPicker::plot();
43  if ( !plot->axisEnabled( QwtPlot::xBottom ) &&
44  plot->axisEnabled( QwtPlot::xTop ) )
45  {
46  xAxis = QwtPlot::xTop;
47  }
48 
49  int yAxis = QwtPlot::yLeft;
50  if ( !plot->axisEnabled( QwtPlot::yLeft ) &&
51  plot->axisEnabled( QwtPlot::yRight ) )
52  {
53  yAxis = QwtPlot::yRight;
54  }
55 
56  setAxis( xAxis, yAxis );
57 }
58 
69  QwtPicker( canvas ),
70  d_xAxis( xAxis ),
71  d_yAxis( yAxis )
72 {
73 }
74 
91  QWidget *canvas ):
92  QwtPicker( rubberBand, trackerMode, canvas ),
93  d_xAxis( xAxis ),
94  d_yAxis( yAxis )
95 {
96 }
97 
100 {
101 }
102 
105 {
106  return parentWidget();
107 }
108 
110 const QWidget *QwtPlotPicker::canvas() const
111 {
112  return parentWidget();
113 }
114 
117 {
118  QWidget *w = canvas();
119  if ( w )
120  w = w->parentWidget();
121 
122  return qobject_cast<QwtPlot *>( w );
123 }
124 
127 {
128  const QWidget *w = canvas();
129  if ( w )
130  w = w->parentWidget();
131 
132  return qobject_cast<const QwtPlot *>( w );
133 }
134 
140 {
141  QRectF rect;
142 
143  if ( plot() )
144  {
145  const QwtScaleDiv &xs = plot()->axisScaleDiv( xAxis() );
146  const QwtScaleDiv &ys = plot()->axisScaleDiv( yAxis() );
147 
148  rect = QRectF( xs.lowerBound(), ys.lowerBound(),
149  xs.range(), ys.range() );
150  rect = rect.normalized();
151  }
152 
153  return rect;
154 }
155 
163 {
164  const QwtPlot *plt = plot();
165  if ( !plt )
166  return;
167 
168  if ( xAxis != d_xAxis || yAxis != d_yAxis )
169  {
170  d_xAxis = xAxis;
171  d_yAxis = yAxis;
172  }
173 }
174 
177 {
178  return d_xAxis;
179 }
180 
183 {
184  return d_yAxis;
185 }
186 
193 QwtText QwtPlotPicker::trackerText( const QPoint &pos ) const
194 {
195  if ( plot() == NULL )
196  return QwtText();
197 
198  return trackerTextF( invTransform( pos ) );
199 }
200 
213 QwtText QwtPlotPicker::trackerTextF( const QPointF &pos ) const
214 {
215  QString text;
216 
217  switch ( rubberBand() )
218  {
219  case HLineRubberBand:
220  text = QString::number( pos.y(), 'f', 4 );
221  break;
222  case VLineRubberBand:
223  text = QString::number( pos.x(), 'f', 4 );
224  break;
225  default:
226  text = QString::number( pos.x(), 'f', 4 )
227  + ", " + QString::number( pos.y(), 'f', 4 );
228  }
229  return QwtText( text );
230 }
231 
241 void QwtPlotPicker::append( const QPoint &pos )
242 {
243  QwtPicker::append( pos );
244  Q_EMIT appended( invTransform( pos ) );
245 }
246 
256 void QwtPlotPicker::move( const QPoint &pos )
257 {
258  QwtPicker::move( pos );
259  Q_EMIT moved( invTransform( pos ) );
260 }
261 
270 bool QwtPlotPicker::end( bool ok )
271 {
272  ok = QwtPicker::end( ok );
273  if ( !ok )
274  return false;
275 
277  if ( !plot )
278  return false;
279 
280  const QPolygon points = selection();
281  if ( points.count() == 0 )
282  return false;
283 
284  QwtPickerMachine::SelectionType selectionType =
286 
287  if ( stateMachine() )
288  selectionType = stateMachine()->selectionType();
289 
290  switch ( selectionType )
291  {
293  {
294  const QPointF pos = invTransform( points.first() );
295  Q_EMIT selected( pos );
296  break;
297  }
299  {
300  if ( points.count() >= 2 )
301  {
302  const QPoint p1 = points.first();
303  const QPoint p2 = points.last();
304 
305  const QRect rect = QRect( p1, p2 ).normalized();
306  Q_EMIT selected( invTransform( rect ) );
307  }
308  break;
309  }
311  {
312  QVector<QPointF> dpa( points.count() );
313  for ( int i = 0; i < points.count(); i++ )
314  dpa[i] = invTransform( points[i] );
315 
316  Q_EMIT selected( dpa );
317  }
318  default:
319  break;
320  }
321 
322  return true;
323 }
324 
331 QRectF QwtPlotPicker::invTransform( const QRect &rect ) const
332 {
333  const QwtScaleMap xMap = plot()->canvasMap( d_xAxis );
334  const QwtScaleMap yMap = plot()->canvasMap( d_yAxis );
335 
336  return QwtScaleMap::invTransform( xMap, yMap, rect );
337 }
338 
344 QRect QwtPlotPicker::transform( const QRectF &rect ) const
345 {
346  const QwtScaleMap xMap = plot()->canvasMap( d_xAxis );
347  const QwtScaleMap yMap = plot()->canvasMap( d_yAxis );
348 
349  return QwtScaleMap::transform( xMap, yMap, rect ).toRect();
350 }
351 
357 QPointF QwtPlotPicker::invTransform( const QPoint &pos ) const
358 {
359  QwtScaleMap xMap = plot()->canvasMap( d_xAxis );
360  QwtScaleMap yMap = plot()->canvasMap( d_yAxis );
361 
362  return QPointF(
363  xMap.invTransform( pos.x() ),
364  yMap.invTransform( pos.y() )
365  );
366 }
367 
373 QPoint QwtPlotPicker::transform( const QPointF &pos ) const
374 {
375  QwtScaleMap xMap = plot()->canvasMap( d_xAxis );
376  QwtScaleMap yMap = plot()->canvasMap( d_yAxis );
377 
378  const QPointF p( xMap.transform( pos.x() ),
379  yMap.transform( pos.y() ) );
380 
381  return p.toPoint();
382 }
383 
384 #if QWT_MOC_INCLUDE
385 #include "moc_qwt_plot_picker.cpp"
386 #endif
virtual bool end(bool ok=true)
Close a selection setting the state to inactive.
int yAxis() const
Return y axis.
DisplayMode
Display mode.
Definition: qwt_picker.h:162
const QwtScaleDiv & axisScaleDiv(int axisId) const
Return the scale division of a specified axis.
DisplayMode trackerMode() const
X axis above the canvas.
Definition: qwt_plot.h:106
virtual QwtText trackerText(const QPoint &) const QWT_OVERRIDE
QWidget * canvas()
QRect transform(const QRectF &) const
The state machine not usable for any type of selection.
QWidget * parentWidget()
Return the parent widget, where the selection happens.
Definition: qwt_picker.cpp:274
QwtPlotPicker(QWidget *canvas)
Create a plot picker.
QRectF scaleRect() const
Y axis right of the canvas.
Definition: qwt_plot.h:100
QwtPlot * plot()
A class representing a scale division.
Definition: qwt_scale_div.h:33
virtual QwtText trackerTextF(const QPointF &) const
Translate a position into a position string.
bool axisEnabled(int axisId) const
QRectF invTransform(const QRect &) const
A 2-D plotting widget.
Definition: qwt_plot.h:75
QPolygon selection() const
Definition: qwt_picker.cpp:805
void appended(const QPointF &pos)
virtual void setAxis(int xAxis, int yAxis)
Y axis left of the canvas.
Definition: qwt_plot.h:97
void moved(const QPointF &pos)
The state machine is for selecting a polygon (many points).
const QwtPickerMachine * stateMachine() const
Definition: qwt_picker.cpp:268
The state machine is for selecting a single point.
double lowerBound() const
RubberBand rubberBand() const
A class representing a text.
Definition: qwt_text.h:51
void selected(const QPointF &pos)
virtual QwtScaleMap canvasMap(int axisId) const
Definition: qwt_plot.cpp:786
virtual void move(const QPoint &)
A horizontal line ( only for QwtPickerMachine::PointSelection )
Definition: qwt_picker.h:134
A scale map.
Definition: qwt_scale_map.h:26
double invTransform(double p) const
virtual ~QwtPlotPicker()
Destructor.
int xAxis() const
Return x axis.
SelectionType selectionType() const
Return the selection type.
virtual bool end(bool ok=true) QWT_OVERRIDE
A vertical line ( only for QwtPickerMachine::PointSelection )
Definition: qwt_picker.h:137
double range() const
double transform(double s) const
virtual void append(const QPoint &) QWT_OVERRIDE
virtual void move(const QPoint &) QWT_OVERRIDE
QwtPicker provides selections on a widget.
Definition: qwt_picker.h:104
The state machine is for selecting a rectangle (2 points).
X axis below the canvas.
Definition: qwt_plot.h:103
virtual void append(const QPoint &)


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 03:48:10