source: ntrip/trunk/BNC/qwt/qwt_plot_picker.cpp@ 8127

Last change on this file since 8127 was 8127, checked in by stoecker, 7 years ago

update qwt and qwtpolar, many QT5 fixes (unfinished)

File size: 8.9 KB
Line 
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
17/*!
18 \brief Create a plot picker
19
20 The picker is set to those x- and y-axis of the plot
21 that are enabled. If both or no x-axis are enabled, the picker
22 is set to QwtPlot::xBottom. If both or no y-axis are
23 enabled, it is set to QwtPlot::yLeft.
24
25 \param canvas Plot canvas to observe, also the parent object
26
27 \sa QwtPlot::autoReplot(), QwtPlot::replot(), scaleRect()
28*/
29
30QwtPlotPicker::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
59/*!
60 Create a plot picker
61
62 \param xAxis Set the x axis of the picker
63 \param yAxis Set the y axis of the picker
64 \param canvas Plot canvas to observe, also the parent object
65
66 \sa QwtPlot::autoReplot(), QwtPlot::replot(), scaleRect()
67*/
68QwtPlotPicker::QwtPlotPicker( int xAxis, int yAxis, QWidget *canvas ):
69 QwtPicker( canvas ),
70 d_xAxis( xAxis ),
71 d_yAxis( yAxis )
72{
73}
74
75/*!
76 Create a plot picker
77
78 \param xAxis X axis of the picker
79 \param yAxis Y axis of the picker
80 \param rubberBand Rubber band style
81 \param trackerMode Tracker mode
82 \param canvas Plot canvas to observe, also the parent object
83
84 \sa QwtPicker, QwtPicker::setSelectionFlags(), QwtPicker::setRubberBand(),
85 QwtPicker::setTrackerMode
86
87 \sa QwtPlot::autoReplot(), QwtPlot::replot(), scaleRect()
88*/
89QwtPlotPicker::QwtPlotPicker( int xAxis, int yAxis,
90 RubberBand rubberBand, DisplayMode trackerMode,
91 QWidget *canvas ):
92 QwtPicker( rubberBand, trackerMode, canvas ),
93 d_xAxis( xAxis ),
94 d_yAxis( yAxis )
95{
96}
97
98//! Destructor
99QwtPlotPicker::~QwtPlotPicker()
100{
101}
102
103//! \return Observed plot canvas
104QWidget *QwtPlotPicker::canvas()
105{
106 return parentWidget();
107}
108
109//! \return Observed plot canvas
110const QWidget *QwtPlotPicker::canvas() const
111{
112 return parentWidget();
113}
114
115//! \return Plot widget, containing the observed plot canvas
116QwtPlot *QwtPlotPicker::plot()
117{
118 QWidget *w = canvas();
119 if ( w )
120 w = w->parentWidget();
121
122 return qobject_cast<QwtPlot *>( w );
123}
124
125//! \return Plot widget, containing the observed plot canvas
126const QwtPlot *QwtPlotPicker::plot() const
127{
128 const QWidget *w = canvas();
129 if ( w )
130 w = w->parentWidget();
131
132 return qobject_cast<const QwtPlot *>( w );
133}
134
135/*!
136 \return Normalized bounding rectangle of the axes
137 \sa QwtPlot::autoReplot(), QwtPlot::replot().
138*/
139QRectF QwtPlotPicker::scaleRect() const
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
156/*!
157 Set the x and y axes of the picker
158
159 \param xAxis X axis
160 \param yAxis Y axis
161*/
162void QwtPlotPicker::setAxis( int xAxis, int yAxis )
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
175//! Return x axis
176int QwtPlotPicker::xAxis() const
177{
178 return d_xAxis;
179}
180
181//! Return y axis
182int QwtPlotPicker::yAxis() const
183{
184 return d_yAxis;
185}
186
187/*!
188 Translate a pixel position into a position string
189
190 \param pos Position in pixel coordinates
191 \return Position string
192*/
193QwtText QwtPlotPicker::trackerText( const QPoint &pos ) const
194{
195 return trackerTextF( invTransform( pos ) );
196}
197
198/*!
199 \brief Translate a position into a position string
200
201 In case of HLineRubberBand the label is the value of the
202 y position, in case of VLineRubberBand the value of the x position.
203 Otherwise the label contains x and y position separated by a ',' .
204
205 The format for the double to string conversion is "%.4f".
206
207 \param pos Position
208 \return Position string
209*/
210QwtText QwtPlotPicker::trackerTextF( const QPointF &pos ) const
211{
212 QString text;
213
214 switch ( rubberBand() )
215 {
216 case HLineRubberBand:
217 text.sprintf( "%.4f", pos.y() );
218 break;
219 case VLineRubberBand:
220 text.sprintf( "%.4f", pos.x() );
221 break;
222 default:
223 text.sprintf( "%.4f, %.4f", pos.x(), pos.y() );
224 }
225 return QwtText( text );
226}
227
228/*!
229 Append a point to the selection and update rubber band and tracker.
230
231 \param pos Additional point
232 \sa isActive, begin(), end(), move(), appended()
233
234 \note The appended(const QPoint &), appended(const QDoublePoint &)
235 signals are emitted.
236*/
237void QwtPlotPicker::append( const QPoint &pos )
238{
239 QwtPicker::append( pos );
240 Q_EMIT appended( invTransform( pos ) );
241}
242
243/*!
244 Move the last point of the selection
245
246 \param pos New position
247 \sa isActive, begin(), end(), append()
248
249 \note The moved(const QPoint &), moved(const QDoublePoint &)
250 signals are emitted.
251*/
252void QwtPlotPicker::move( const QPoint &pos )
253{
254 QwtPicker::move( pos );
255 Q_EMIT moved( invTransform( pos ) );
256}
257
258/*!
259 Close a selection setting the state to inactive.
260
261 \param ok If true, complete the selection and emit selected signals
262 otherwise discard the selection.
263 \return True if the selection has been accepted, false otherwise
264*/
265
266bool QwtPlotPicker::end( bool ok )
267{
268 ok = QwtPicker::end( ok );
269 if ( !ok )
270 return false;
271
272 QwtPlot *plot = QwtPlotPicker::plot();
273 if ( !plot )
274 return false;
275
276 const QPolygon points = selection();
277 if ( points.count() == 0 )
278 return false;
279
280 QwtPickerMachine::SelectionType selectionType =
281 QwtPickerMachine::NoSelection;
282
283 if ( stateMachine() )
284 selectionType = stateMachine()->selectionType();
285
286 switch ( selectionType )
287 {
288 case QwtPickerMachine::PointSelection:
289 {
290 const QPointF pos = invTransform( points.first() );
291 Q_EMIT selected( pos );
292 break;
293 }
294 case QwtPickerMachine::RectSelection:
295 {
296 if ( points.count() >= 2 )
297 {
298 const QPoint p1 = points.first();
299 const QPoint p2 = points.last();
300
301 const QRect rect = QRect( p1, p2 ).normalized();
302 Q_EMIT selected( invTransform( rect ) );
303 }
304 break;
305 }
306 case QwtPickerMachine::PolygonSelection:
307 {
308 QVector<QPointF> dpa( points.count() );
309 for ( int i = 0; i < points.count(); i++ )
310 dpa[i] = invTransform( points[i] );
311
312 Q_EMIT selected( dpa );
313 }
314 default:
315 break;
316 }
317
318 return true;
319}
320
321/*!
322 Translate a rectangle from pixel into plot coordinates
323
324 \return Rectangle in plot coordinates
325 \sa transform()
326*/
327QRectF QwtPlotPicker::invTransform( const QRect &rect ) const
328{
329 const QwtScaleMap xMap = plot()->canvasMap( d_xAxis );
330 const QwtScaleMap yMap = plot()->canvasMap( d_yAxis );
331
332 return QwtScaleMap::invTransform( xMap, yMap, rect );
333}
334
335/*!
336 Translate a rectangle from plot into pixel coordinates
337 \return Rectangle in pixel coordinates
338 \sa invTransform()
339*/
340QRect QwtPlotPicker::transform( const QRectF &rect ) const
341{
342 const QwtScaleMap xMap = plot()->canvasMap( d_xAxis );
343 const QwtScaleMap yMap = plot()->canvasMap( d_yAxis );
344
345 return QwtScaleMap::transform( xMap, yMap, rect ).toRect();
346}
347
348/*!
349 Translate a point from pixel into plot coordinates
350 \return Point in plot coordinates
351 \sa transform()
352*/
353QPointF QwtPlotPicker::invTransform( const QPoint &pos ) const
354{
355 QwtScaleMap xMap = plot()->canvasMap( d_xAxis );
356 QwtScaleMap yMap = plot()->canvasMap( d_yAxis );
357
358 return QPointF(
359 xMap.invTransform( pos.x() ),
360 yMap.invTransform( pos.y() )
361 );
362}
363
364/*!
365 Translate a point from plot into pixel coordinates
366 \return Point in pixel coordinates
367 \sa invTransform()
368*/
369QPoint QwtPlotPicker::transform( const QPointF &pos ) const
370{
371 QwtScaleMap xMap = plot()->canvasMap( d_xAxis );
372 QwtScaleMap yMap = plot()->canvasMap( d_yAxis );
373
374 const QPointF p( xMap.transform( pos.x() ),
375 yMap.transform( pos.y() ) );
376
377 return p.toPoint();
378}
Note: See TracBrowser for help on using the repository browser.