qwt_plot_picker.cpp
Go to the documentation of this file.
1 /******************************************************************************
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 
18 {
19  public:
20  PrivateData():
21  xAxisId( -1 ),
22  yAxisId( -1 )
23  {
24  }
25 
28 };
29 
43 QwtPlotPicker::QwtPlotPicker( QWidget* canvas )
44  : QwtPicker( canvas )
45 {
46  m_data = new PrivateData;
47 
48  if ( !canvas )
49  return;
50 
51  const QwtPlot* plot = QwtPlotPicker::plot();
52  // attach axes
53 
54  using namespace QwtAxis;
55 
56  int xAxis = XBottom;
57  if ( !plot->isAxisVisible( XBottom ) && plot->isAxisVisible( XTop ) )
58  xAxis = XTop;
59 
60  int yAxis = YLeft;
61  if ( !plot->isAxisVisible( YLeft ) && plot->isAxisVisible( YRight ) )
62  yAxis = YRight;
63 
64  setAxes( xAxis, yAxis );
65 }
66 
77  : QwtPicker( canvas )
78 {
79  m_data = new PrivateData;
80  m_data->xAxisId = xAxisId;
81  m_data->yAxisId = yAxisId;
82 }
83 
99  RubberBand rubberBand, DisplayMode trackerMode, QWidget* canvas )
100  : QwtPicker( rubberBand, trackerMode, canvas )
101 {
102  m_data = new PrivateData;
103  m_data->xAxisId = xAxisId;
104  m_data->yAxisId = yAxisId;
105 }
106 
109 {
110  delete m_data;
111 }
112 
115 {
116  return parentWidget();
117 }
118 
120 const QWidget* QwtPlotPicker::canvas() const
121 {
122  return parentWidget();
123 }
124 
127 {
128  QWidget* w = canvas();
129  if ( w )
130  w = w->parentWidget();
131 
132  return qobject_cast< QwtPlot* >( w );
133 }
134 
136 const QwtPlot* QwtPlotPicker::plot() const
137 {
138  const QWidget* w = canvas();
139  if ( w )
140  w = w->parentWidget();
141 
142  return qobject_cast< const QwtPlot* >( w );
143 }
144 
150 {
151  QRectF rect;
152 
153  if ( plot() )
154  {
155  const QwtScaleDiv& xs = plot()->axisScaleDiv( xAxis() );
156  const QwtScaleDiv& ys = plot()->axisScaleDiv( yAxis() );
157 
158  rect = QRectF( xs.lowerBound(), ys.lowerBound(),
159  xs.range(), ys.range() );
160  rect = rect.normalized();
161  }
162 
163  return rect;
164 }
165 
173 {
174  const QwtPlot* plt = plot();
175  if ( !plt )
176  return;
177 
178  if ( xAxisId != m_data->xAxisId || yAxisId != m_data->yAxisId )
179  {
180  m_data->xAxisId = xAxisId;
181  m_data->yAxisId = yAxisId;
182  }
183 }
184 
187 {
188  return m_data->xAxisId;
189 }
190 
193 {
194  return m_data->yAxisId;
195 }
196 
203 QwtText QwtPlotPicker::trackerText( const QPoint& pos ) const
204 {
205  if ( plot() == NULL )
206  return QwtText();
207 
208  return trackerTextF( invTransform( pos ) );
209 }
210 
223 QwtText QwtPlotPicker::trackerTextF( const QPointF& pos ) const
224 {
225  QString text;
226 
227  switch ( rubberBand() )
228  {
229  case HLineRubberBand:
230  text = QString::number( pos.y(), 'f', 4 );
231  break;
232  case VLineRubberBand:
233  text = QString::number( pos.x(), 'f', 4 );
234  break;
235  default:
236  text = QString::number( pos.x(), 'f', 4 )
237  + ", " + QString::number( pos.y(), 'f', 4 );
238  }
239  return QwtText( text );
240 }
241 
251 void QwtPlotPicker::append( const QPoint& pos )
252 {
253  QwtPicker::append( pos );
254  Q_EMIT appended( invTransform( pos ) );
255 }
256 
266 void QwtPlotPicker::move( const QPoint& pos )
267 {
268  QwtPicker::move( pos );
269  Q_EMIT moved( invTransform( pos ) );
270 }
271 
280 bool QwtPlotPicker::end( bool ok )
281 {
282  ok = QwtPicker::end( ok );
283  if ( !ok )
284  return false;
285 
287  if ( !plot )
288  return false;
289 
290  const QPolygon points = selection();
291  if ( points.count() == 0 )
292  return false;
293 
294  QwtPickerMachine::SelectionType selectionType =
296 
297  if ( stateMachine() )
298  selectionType = stateMachine()->selectionType();
299 
300  switch ( selectionType )
301  {
303  {
304  const QPointF pos = invTransform( points.first() );
305  Q_EMIT selected( pos );
306  break;
307  }
309  {
310  if ( points.count() >= 2 )
311  {
312  const QPoint p1 = points.first();
313  const QPoint p2 = points.last();
314 
315  const QRect rect = QRect( p1, p2 ).normalized();
316  Q_EMIT selected( invTransform( rect ) );
317  }
318  break;
319  }
321  {
322  QVector< QPointF > dpa( points.count() );
323  for ( int i = 0; i < points.count(); i++ )
324  dpa[i] = invTransform( points[i] );
325 
326  Q_EMIT selected( dpa );
327  }
328  default:
329  break;
330  }
331 
332  return true;
333 }
334 
341 QRectF QwtPlotPicker::invTransform( const QRect& rect ) const
342 {
343  const QwtScaleMap xMap = plot()->canvasMap( xAxis() );
344  const QwtScaleMap yMap = plot()->canvasMap( yAxis() );
345 
346  return QwtScaleMap::invTransform( xMap, yMap, rect );
347 }
348 
354 QRect QwtPlotPicker::transform( const QRectF& rect ) const
355 {
356  const QwtScaleMap xMap = plot()->canvasMap( xAxis() );
357  const QwtScaleMap yMap = plot()->canvasMap( yAxis() );
358 
359  return QwtScaleMap::transform( xMap, yMap, rect ).toRect();
360 }
361 
367 QPointF QwtPlotPicker::invTransform( const QPoint& pos ) const
368 {
369  const QwtScaleMap xMap = plot()->canvasMap( xAxis() );
370  const QwtScaleMap yMap = plot()->canvasMap( yAxis() );
371 
372  return QPointF(
373  xMap.invTransform( pos.x() ),
374  yMap.invTransform( pos.y() )
375  );
376 }
377 
383 QPoint QwtPlotPicker::transform( const QPointF& pos ) const
384 {
385  const QwtScaleMap xMap = plot()->canvasMap( xAxis() );
386  const QwtScaleMap yMap = plot()->canvasMap( yAxis() );
387 
388  const QPointF p( xMap.transform( pos.x() ), yMap.transform( pos.y() ) );
389 
390  return p.toPoint();
391 }
392 
393 #if QWT_MOC_INCLUDE
394 #include "moc_qwt_plot_picker.cpp"
395 #endif
QwtPlotPicker::~QwtPlotPicker
virtual ~QwtPlotPicker()
Destructor.
Definition: qwt_plot_picker.cpp:108
QwtPicker::rubberBand
RubberBand rubberBand
Definition: qwt_picker.h:116
QwtPickerMachine::NoSelection
@ NoSelection
The state machine not usable for any type of selection.
Definition: qwt_picker_machine.h:38
QwtPickerMachine::RectSelection
@ RectSelection
The state machine is for selecting a rectangle (2 points).
Definition: qwt_picker_machine.h:44
QwtScaleDiv::range
double range() const
Definition: qwt_scale_div.cpp:159
QwtPlotPicker::PrivateData
Definition: qwt_plot_picker.cpp:17
QwtScaleMap::invTransform
double invTransform(double p) const
Definition: qwt_scale_map.h:154
QwtPicker::DisplayMode
DisplayMode
Display mode.
Definition: qwt_picker.h:161
qwt_scale_div.h
QwtPlotPicker::PrivateData::xAxisId
QwtAxisId xAxisId
Definition: qwt_plot_picker.cpp:33
QwtPlotPicker::appended
void appended(const QPointF &pos)
QwtPlotPicker::invTransform
QRectF invTransform(const QRect &) const
Definition: qwt_plot_picker.cpp:341
QVector
Definition: qwt_clipper.h:23
QwtPicker::RubberBand
RubberBand
Definition: qwt_picker.h:127
QwtPlotPicker::selected
void selected(const QPointF &pos)
QwtPlotPicker::PrivateData::yAxisId
QwtAxisId yAxisId
Definition: qwt_plot_picker.cpp:34
QwtPlot
A 2-D plotting widget.
Definition: qwt_plot.h:78
QwtPlotPicker::move
virtual void move(const QPoint &) QWT_OVERRIDE
Definition: qwt_plot_picker.cpp:266
QwtPlotPicker::plot
QwtPlot * plot()
Definition: qwt_plot_picker.cpp:126
QwtScaleDiv::lowerBound
double lowerBound() const
Definition: qwt_scale_div.cpp:131
QwtPicker::VLineRubberBand
@ VLineRubberBand
A vertical line ( only for QwtPickerMachine::PointSelection )
Definition: qwt_picker.h:136
QwtAxis::YLeft
@ YLeft
Y axis left of the canvas.
Definition: qwt_axis.h:38
ok
ROSCPP_DECL bool ok()
QwtPickerMachine::PointSelection
@ PointSelection
The state machine is for selecting a single point.
Definition: qwt_picker_machine.h:41
QwtPlotPicker::transform
QRect transform(const QRectF &) const
Definition: qwt_plot_picker.cpp:354
QwtPlotPicker::m_data
PrivateData * m_data
Definition: qwt_plot_picker.h:113
QwtPlotPicker::xAxis
QwtAxisId xAxis() const
Return x axis.
Definition: qwt_plot_picker.cpp:186
qwt_scale_map.h
QwtPlotPicker::canvas
QWidget * canvas()
Definition: qwt_plot_picker.cpp:114
QwtText
A class representing a text.
Definition: qwt_text.h:51
QwtPlotPicker::moved
void moved(const QPointF &pos)
QwtPlotPicker::trackerText
virtual QwtText trackerText(const QPoint &) const QWT_OVERRIDE
Definition: qwt_plot_picker.cpp:203
QwtPlotPicker::end
virtual bool end(bool ok=true) QWT_OVERRIDE
Definition: qwt_plot_picker.cpp:280
QwtPickerMachine::PolygonSelection
@ PolygonSelection
The state machine is for selecting a polygon (many points).
Definition: qwt_picker_machine.h:47
QwtPlotPicker::append
virtual void append(const QPoint &) QWT_OVERRIDE
Definition: qwt_plot_picker.cpp:251
QwtPicker::HLineRubberBand
@ HLineRubberBand
A horizontal line ( only for QwtPickerMachine::PointSelection )
Definition: qwt_picker.h:133
QwtScaleMap::transform
double transform(double s) const
Definition: qwt_scale_map.h:137
qwt_picker_machine.h
QwtPlotPicker::PrivateData::PrivateData
PrivateData()
Definition: qwt_plot_picker.cpp:27
QwtPlot::axisScaleDiv
const QwtScaleDiv & axisScaleDiv(QwtAxisId) const
Return the scale division of a specified axis.
Definition: qwt_plot_axis.cpp:283
QwtPickerMachine::SelectionType
SelectionType
Definition: qwt_picker_machine.h:35
QwtAxisId
int QwtAxisId
Axis identifier.
Definition: qwt_axis_id.h:26
QwtPicker::append
virtual void append(const QPoint &)
Definition: qwt_picker.cpp:1339
QwtScaleMap
A scale map.
Definition: qwt_scale_map.h:26
QwtPicker::move
virtual void move(const QPoint &)
Definition: qwt_picker.cpp:1357
QwtPlotPicker::yAxis
QwtAxisId yAxis() const
Return y axis.
Definition: qwt_plot_picker.cpp:192
qwt_plot_picker.h
QwtPicker::selection
QPolygon selection() const
Definition: qwt_picker.cpp:792
QwtPicker::end
virtual bool end(bool ok=true)
Close a selection setting the state to inactive.
Definition: qwt_picker.cpp:1291
QwtPickerMachine::selectionType
SelectionType selectionType() const
Return the selection type.
Definition: qwt_picker_machine.cpp:28
QwtPicker
QwtPicker provides selections on a widget.
Definition: qwt_picker.h:103
QwtAxis
Definition: qwt_axis.h:18
QwtAxis::XTop
@ XTop
X axis above the canvas.
Definition: qwt_axis.h:47
QwtPlotPicker::trackerTextF
virtual QwtText trackerTextF(const QPointF &) const
Translate a position into a position string.
Definition: qwt_plot_picker.cpp:223
QwtPicker::parentWidget
QWidget * parentWidget()
Return the parent widget, where the selection happens.
Definition: qwt_picker.cpp:263
QwtScaleDiv
A class representing a scale division.
Definition: qwt_scale_div.h:33
QwtPlotPicker::QwtPlotPicker
QwtPlotPicker(QWidget *canvas)
Create a plot picker.
Definition: qwt_plot_picker.cpp:43
qwt_text.h
QwtPlot::canvasMap
virtual QwtScaleMap canvasMap(QwtAxisId) const
Definition: qwt_plot.cpp:800
QwtPlotPicker::scaleRect
QRectF scaleRect() const
Definition: qwt_plot_picker.cpp:149
QwtPlot::isAxisVisible
bool isAxisVisible(QwtAxisId) const
Definition: qwt_plot_axis.cpp:225
QwtAxis::YRight
@ YRight
Y axis right of the canvas.
Definition: qwt_axis.h:41
qwt_plot.h
QwtPicker::stateMachine
const QwtPickerMachine * stateMachine() const
Definition: qwt_picker.cpp:248
QwtPlotPicker::setAxes
virtual void setAxes(QwtAxisId xAxisId, QwtAxisId yAxisId)
Definition: qwt_plot_picker.cpp:172
QwtAxis::XBottom
@ XBottom
X axis below the canvas.
Definition: qwt_axis.h:44


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:46