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
44 : QwtPicker( canvas )
45{
46 m_data = new PrivateData;
47
48 if ( !canvas )
49 return;
50
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
65}
66
76QwtPlotPicker::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
98QwtPlotPicker::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
109{
110 delete m_data;
111}
112
115{
116 return parentWidget();
117}
118
120const 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
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
172void 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
203QwtText QwtPlotPicker::trackerText( const QPoint& pos ) const
204{
205 if ( plot() == NULL )
206 return QwtText();
207
208 return trackerTextF( invTransform( pos ) );
209}
210
223QwtText 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
251void QwtPlotPicker::append( const QPoint& pos )
252{
253 QwtPicker::append( pos );
254 Q_EMIT appended( invTransform( pos ) );
255}
256
266void QwtPlotPicker::move( const QPoint& pos )
267{
268 QwtPicker::move( pos );
269 Q_EMIT moved( invTransform( pos ) );
270}
271
280bool 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
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"
QwtPicker provides selections on a widget.
Definition qwt_picker.h:104
DisplayMode
Display mode.
Definition qwt_picker.h:162
RubberBand rubberBand() const
virtual void move(const QPoint &)
virtual void append(const QPoint &)
@ VLineRubberBand
A vertical line ( only for QwtPickerMachine::PointSelection )
Definition qwt_picker.h:136
@ HLineRubberBand
A horizontal line ( only for QwtPickerMachine::PointSelection )
Definition qwt_picker.h:133
virtual bool end(bool ok=true)
Close a selection setting the state to inactive.
QWidget * parentWidget()
Return the parent widget, where the selection happens.
const QwtPickerMachine * stateMachine() const
QPolygon selection() const
@ NoSelection
The state machine not usable for any type of selection.
@ RectSelection
The state machine is for selecting a rectangle (2 points).
@ PolygonSelection
The state machine is for selecting a polygon (many points).
@ PointSelection
The state machine is for selecting a single point.
SelectionType selectionType() const
Return the selection type.
A 2-D plotting widget.
Definition qwt_plot.h:79
bool isAxisVisible(QwtAxisId) const
virtual QwtScaleMap canvasMap(QwtAxisId) const
Definition qwt_plot.cpp:800
const QwtScaleDiv & axisScaleDiv(QwtAxisId) const
Return the scale division of a specified axis.
QRect transform(const QRectF &) const
virtual ~QwtPlotPicker()
Destructor.
virtual bool end(bool ok=true) override
void selected(const QPointF &pos)
QwtAxisId yAxis() const
Return y axis.
void moved(const QPointF &pos)
virtual void append(const QPoint &) override
void appended(const QPointF &pos)
QwtAxisId xAxis() const
Return x axis.
virtual void setAxes(QwtAxisId xAxisId, QwtAxisId yAxisId)
virtual void move(const QPoint &) override
virtual QwtText trackerText(const QPoint &) const override
QwtPlotPicker(QWidget *canvas)
Create a plot picker.
virtual QwtText trackerTextF(const QPointF &) const
Translate a position into a position string.
QRectF scaleRect() const
QRectF invTransform(const QRect &) const
A class representing a scale division.
double lowerBound() const
double range() const
A scale map.
double transform(double s) const
double invTransform(double p) const
A class representing a text.
Definition qwt_text.h:52