Qwt User's Guide: qwt_plot_picker.cpp Source File

Qwt User's Guide 6.3.0

Loading...
Searching...
No Matches
qwt_plot_picker.cpp
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
17class QwtPlotPicker::PrivateData
18{
19 public:
20 PrivateData():
21 xAxisId( -1 ),
22 yAxisId( -1 )
23 {
24 }
25
26 QwtAxisId xAxisId;
27 QwtAxisId yAxisId;
28};
29
43QwtPlotPicker::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
76 QwtPlotPicker::QwtPlotPicker( QwtAxisId xAxisId, QwtAxisId yAxisId, QWidget* canvas )
77 : QwtPicker( canvas )
78{
79 m_data = new PrivateData;
80 m_data->xAxisId = xAxisId;
81 m_data->yAxisId = yAxisId;
82}
83
98 QwtPlotPicker::QwtPlotPicker( QwtAxisId xAxisId, QwtAxisId yAxisId,
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
108QwtPlotPicker::~QwtPlotPicker()
109{
110 delete m_data;
111}
112
114QWidget* QwtPlotPicker::canvas()
115{
116 return parentWidget();
117}
118
120 const QWidget* QwtPlotPicker::canvas() const
121{
122 return parentWidget();
123}
124
126 QwtPlot* QwtPlotPicker::plot()
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
149QRectF QwtPlotPicker::scaleRect() const
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
172 void QwtPlotPicker::setAxes( QwtAxisId xAxisId, QwtAxisId yAxisId )
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
186QwtAxisId QwtPlotPicker::xAxis() const
187{
188 return m_data->xAxisId;
189}
190
192QwtAxisId QwtPlotPicker::yAxis() const
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
286 QwtPlot* plot = QwtPlotPicker::plot();
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 =
295 QwtPickerMachine::NoSelection;
296
297 if ( stateMachine() )
298 selectionType = stateMachine()->selectionType();
299
300 switch ( selectionType )
301 {
302 case QwtPickerMachine::PointSelection:
303 {
304 const QPointF pos = invTransform( points.first() );
305 Q_EMIT selected( pos );
306 break;
307 }
308 case QwtPickerMachine::RectSelection:
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 }
320 case QwtPickerMachine::PolygonSelection:
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
341QRectF 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
354QRect 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
367QPointF 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
383QPoint 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#include "moc_qwt_plot_picker.cpp"
QVector
Definition qwt_clipper.h:23
QwtPicker
QwtPicker provides selections on a widget.
Definition qwt_picker.h:104
QwtPicker::DisplayMode
DisplayMode
Display mode.
Definition qwt_picker.h:162
QwtPicker::rubberBand
RubberBand rubberBand() const
Definition qwt_picker.cpp:299
QwtPicker::move
virtual void move(const QPoint &)
Definition qwt_picker.cpp:1357
QwtPicker::append
virtual void append(const QPoint &)
Definition qwt_picker.cpp:1339
QwtPicker::RubberBand
RubberBand
Definition qwt_picker.h:128
QwtPicker::VLineRubberBand
@ VLineRubberBand
A vertical line ( only for QwtPickerMachine::PointSelection )
Definition qwt_picker.h:136
QwtPicker::HLineRubberBand
@ HLineRubberBand
A horizontal line ( only for QwtPickerMachine::PointSelection )
Definition qwt_picker.h:133
QwtPicker::end
virtual bool end(bool ok=true)
Close a selection setting the state to inactive.
Definition qwt_picker.cpp:1291
QwtPicker::parentWidget
QWidget * parentWidget()
Return the parent widget, where the selection happens.
Definition qwt_picker.cpp:263
QwtPicker::stateMachine
const QwtPickerMachine * stateMachine() const
Definition qwt_picker.cpp:257
QwtPicker::selection
QPolygon selection() const
Definition qwt_picker.cpp:792
QwtPickerMachine::SelectionType
SelectionType
Definition qwt_picker_machine.h:36
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
QwtPickerMachine::PolygonSelection
@ PolygonSelection
The state machine is for selecting a polygon (many points).
Definition qwt_picker_machine.h:47
QwtPickerMachine::PointSelection
@ PointSelection
The state machine is for selecting a single point.
Definition qwt_picker_machine.h:41
QwtPickerMachine::selectionType
SelectionType selectionType() const
Return the selection type.
Definition qwt_picker_machine.cpp:28
QwtPlot
A 2-D plotting widget.
Definition qwt_plot.h:79
QwtPlot::isAxisVisible
bool isAxisVisible(QwtAxisId) const
Definition qwt_plot_axis.cpp:225
QwtPlot::canvasMap
virtual QwtScaleMap canvasMap(QwtAxisId) const
Definition qwt_plot.cpp:800
QwtPlot::axisScaleDiv
const QwtScaleDiv & axisScaleDiv(QwtAxisId) const
Return the scale division of a specified axis.
Definition qwt_plot_axis.cpp:283
QwtPlotPicker::transform
QRect transform(const QRectF &) const
Definition qwt_plot_picker.cpp:354
QwtPlotPicker::~QwtPlotPicker
virtual ~QwtPlotPicker()
Destructor.
Definition qwt_plot_picker.cpp:108
QwtPlotPicker::end
virtual bool end(bool ok=true) override
Definition qwt_plot_picker.cpp:280
QwtPlotPicker::selected
void selected(const QPointF &pos)
QwtPlotPicker::yAxis
QwtAxisId yAxis() const
Return y axis.
Definition qwt_plot_picker.cpp:192
QwtPlotPicker::moved
void moved(const QPointF &pos)
QwtPlotPicker::append
virtual void append(const QPoint &) override
Definition qwt_plot_picker.cpp:251
QwtPlotPicker::appended
void appended(const QPointF &pos)
QwtPlotPicker::xAxis
QwtAxisId xAxis() const
Return x axis.
Definition qwt_plot_picker.cpp:186
QwtPlotPicker::setAxes
virtual void setAxes(QwtAxisId xAxisId, QwtAxisId yAxisId)
Definition qwt_plot_picker.cpp:172
QwtPlotPicker::move
virtual void move(const QPoint &) override
Definition qwt_plot_picker.cpp:266
QwtPlotPicker::trackerText
virtual QwtText trackerText(const QPoint &) const override
Definition qwt_plot_picker.cpp:203
QwtPlotPicker::QwtPlotPicker
QwtPlotPicker(QWidget *canvas)
Create a plot picker.
Definition qwt_plot_picker.cpp:43
QwtPlotPicker::trackerTextF
virtual QwtText trackerTextF(const QPointF &) const
Translate a position into a position string.
Definition qwt_plot_picker.cpp:223
QwtPlotPicker::scaleRect
QRectF scaleRect() const
Definition qwt_plot_picker.cpp:149
QwtPlotPicker::invTransform
QRectF invTransform(const QRect &) const
Definition qwt_plot_picker.cpp:341
QwtPlotPicker::plot
QwtPlot * plot()
Definition qwt_plot_picker.cpp:126
QwtPlotPicker::canvas
QWidget * canvas()
Definition qwt_plot_picker.cpp:114
QwtScaleDiv
A class representing a scale division.
Definition qwt_scale_div.h:34
QwtScaleDiv::lowerBound
double lowerBound() const
Definition qwt_scale_div.cpp:131
QwtScaleDiv::range
double range() const
Definition qwt_scale_div.cpp:159
QwtScaleMap
A scale map.
Definition qwt_scale_map.h:27
QwtScaleMap::transform
double transform(double s) const
Definition qwt_scale_map.h:137
QwtScaleMap::invTransform
double invTransform(double p) const
Definition qwt_scale_map.h:154
QwtText
A class representing a text.
Definition qwt_text.h:52
QwtAxis
Definition qwt_axis.h:19