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_scale_div.h"
13 #include "qwt_painter.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.sprintf( "%.4f", pos.y() );
221  break;
222  case VLineRubberBand:
223  text.sprintf( "%.4f", pos.x() );
224  break;
225  default:
226  text.sprintf( "%.4f, %.4f", pos.x(), pos.y() );
227  }
228  return QwtText( text );
229 }
230 
240 void QwtPlotPicker::append( const QPoint &pos )
241 {
242  QwtPicker::append( pos );
243  Q_EMIT appended( invTransform( pos ) );
244 }
245 
255 void QwtPlotPicker::move( const QPoint &pos )
256 {
257  QwtPicker::move( pos );
258  Q_EMIT moved( invTransform( pos ) );
259 }
260 
269 bool QwtPlotPicker::end( bool ok )
270 {
271  ok = QwtPicker::end( ok );
272  if ( !ok )
273  return false;
274 
276  if ( !plot )
277  return false;
278 
279  const QPolygon points = selection();
280  if ( points.count() == 0 )
281  return false;
282 
283  QwtPickerMachine::SelectionType selectionType =
285 
286  if ( stateMachine() )
287  selectionType = stateMachine()->selectionType();
288 
289  switch ( selectionType )
290  {
292  {
293  const QPointF pos = invTransform( points.first() );
294  Q_EMIT selected( pos );
295  break;
296  }
298  {
299  if ( points.count() >= 2 )
300  {
301  const QPoint p1 = points.first();
302  const QPoint p2 = points.last();
303 
304  const QRect rect = QRect( p1, p2 ).normalized();
305  Q_EMIT selected( invTransform( rect ) );
306  }
307  break;
308  }
310  {
311  QVector<QPointF> dpa( points.count() );
312  for ( int i = 0; i < points.count(); i++ )
313  dpa[i] = invTransform( points[i] );
314 
315  Q_EMIT selected( dpa );
316  }
317  default:
318  break;
319  }
320 
321  return true;
322 }
323 
330 QRectF QwtPlotPicker::invTransform( const QRect &rect ) const
331 {
332  const QwtScaleMap xMap = plot()->canvasMap( d_xAxis );
333  const QwtScaleMap yMap = plot()->canvasMap( d_yAxis );
334 
335  return QwtScaleMap::invTransform( xMap, yMap, rect );
336 }
337 
343 QRect QwtPlotPicker::transform( const QRectF &rect ) const
344 {
345  const QwtScaleMap xMap = plot()->canvasMap( d_xAxis );
346  const QwtScaleMap yMap = plot()->canvasMap( d_yAxis );
347 
348  return QwtScaleMap::transform( xMap, yMap, rect ).toRect();
349 }
350 
356 QPointF QwtPlotPicker::invTransform( const QPoint &pos ) const
357 {
358  QwtScaleMap xMap = plot()->canvasMap( d_xAxis );
359  QwtScaleMap yMap = plot()->canvasMap( d_yAxis );
360 
361  return QPointF(
362  xMap.invTransform( pos.x() ),
363  yMap.invTransform( pos.y() )
364  );
365 }
366 
372 QPoint QwtPlotPicker::transform( const QPointF &pos ) const
373 {
374  QwtScaleMap xMap = plot()->canvasMap( d_xAxis );
375  QwtScaleMap yMap = plot()->canvasMap( d_yAxis );
376 
377  const QPointF p( xMap.transform( pos.x() ),
378  yMap.transform( pos.y() ) );
379 
380  return p.toPoint();
381 }
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:153
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:105
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
virtual bool end(bool ok=true)
Y axis right of the canvas.
Definition: qwt_plot.h:99
QwtPlot * plot()
A class representing a scale division.
Definition: qwt_scale_div.h:36
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:74
QPolygon selection() const
Definition: qwt_picker.cpp:801
void appended(const QPointF &pos)
virtual void setAxis(int xAxis, int yAxis)
Y axis left of the canvas.
Definition: qwt_plot.h:96
void moved(const QPointF &pos)
virtual QwtText trackerText(const QPoint &) const
The state machine is for selecting a polygon (many points).
virtual void append(const QPoint &)
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:790
virtual void move(const QPoint &)
A horizontal line ( only for QwtPickerMachine::PointSelection )
Definition: qwt_picker.h:125
A scale map.
Definition: qwt_scale_map.h:30
double invTransform(double p) const
virtual ~QwtPlotPicker()
Destructor.
TFSIMD_FORCE_INLINE const tfScalar & w() const
int xAxis() const
Return x axis.
SelectionType selectionType() const
Return the selection type.
virtual void move(const QPoint &)
A vertical line ( only for QwtPickerMachine::PointSelection )
Definition: qwt_picker.h:128
double range() const
double transform(double s) const
int i
QwtPicker provides selections on a widget.
Definition: qwt_picker.h:95
The state machine is for selecting a rectangle (2 points).
X axis below the canvas.
Definition: qwt_plot.h:102
virtual void append(const QPoint &)


plotjuggler
Author(s): Davide Faconti
autogenerated on Sat Jul 6 2019 03:44:17