source: ntrip/branches/BNC_2.12/qwt/qwt_plot_picker.cpp@ 9402

Last change on this file since 9402 was 4271, checked in by mervart, 12 years ago
File size: 9.0 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( QwtPlotCanvas *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, QwtPlotCanvas *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 Rubberband 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 QwtPlotCanvas *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
104QwtPlotCanvas *QwtPlotPicker::canvas()
105{
106 return qobject_cast<QwtPlotCanvas *>( parentWidget() );
107}
108
109//! Return Observed plot canvas
110const QwtPlotCanvas *QwtPlotPicker::canvas() const
111{
112 return qobject_cast<const QwtPlotCanvas *>( parentWidget() );
113}
114
115//! Return plot widget, containing the observed plot canvas
116QwtPlot *QwtPlotPicker::plot()
117{
118 QwtPlotCanvas *w = canvas();
119 if ( w )
120 return w->plot();
121
122 return NULL;
123}
124
125//! Return plot widget, containing the observed plot canvas
126const QwtPlot *QwtPlotPicker::plot() const
127{
128 const QwtPlotCanvas *w = canvas();
129 if ( w )
130 return w->plot();
131
132 return NULL;
133}
134
135/*!
136 Return normalized bounding rect of the axes
137
138 \sa QwtPlot::autoReplot(), QwtPlot::replot().
139*/
140QRectF QwtPlotPicker::scaleRect() const
141{
142 QRectF rect;
143
144 if ( plot() )
145 {
146 const QwtScaleDiv *xs = plot()->axisScaleDiv( xAxis() );
147 const QwtScaleDiv *ys = plot()->axisScaleDiv( yAxis() );
148
149 if ( xs && ys )
150 {
151 rect = QRectF( xs->lowerBound(), ys->lowerBound(),
152 xs->range(), ys->range() );
153 rect = rect.normalized();
154 }
155 }
156
157 return rect;
158}
159
160/*!
161 Set the x and y axes of the picker
162
163 \param xAxis X axis
164 \param yAxis Y axis
165*/
166void QwtPlotPicker::setAxis( int xAxis, int yAxis )
167{
168 const QwtPlot *plt = plot();
169 if ( !plt )
170 return;
171
172 if ( xAxis != d_xAxis || yAxis != d_yAxis )
173 {
174 d_xAxis = xAxis;
175 d_yAxis = yAxis;
176 }
177}
178
179//! Return x axis
180int QwtPlotPicker::xAxis() const
181{
182 return d_xAxis;
183}
184
185//! Return y axis
186int QwtPlotPicker::yAxis() const
187{
188 return d_yAxis;
189}
190
191/*!
192 Translate a pixel position into a position string
193
194 \param pos Position in pixel coordinates
195 \return Position string
196*/
197QwtText QwtPlotPicker::trackerText( const QPoint &pos ) const
198{
199 return trackerTextF( invTransform( pos ) );
200}
201
202/*!
203 \brief Translate a position into a position string
204
205 In case of HLineRubberBand the label is the value of the
206 y position, in case of VLineRubberBand the value of the x position.
207 Otherwise the label contains x and y position separated by a ',' .
208
209 The format for the double to string conversion is "%.4f".
210
211 \param pos Position
212 \return Position string
213*/
214QwtText QwtPlotPicker::trackerTextF( const QPointF &pos ) const
215{
216 QString text;
217
218 switch ( rubberBand() )
219 {
220 case HLineRubberBand:
221 text.sprintf( "%.4f", pos.y() );
222 break;
223 case VLineRubberBand:
224 text.sprintf( "%.4f", pos.x() );
225 break;
226 default:
227 text.sprintf( "%.4f, %.4f", pos.x(), pos.y() );
228 }
229 return QwtText( text );
230}
231
232/*!
233 Append a point to the selection and update rubberband and tracker.
234
235 \param pos Additional point
236 \sa isActive, begin(), end(), move(), appended()
237
238 \note The appended(const QPoint &), appended(const QDoublePoint &)
239 signals are emitted.
240*/
241void QwtPlotPicker::append( const QPoint &pos )
242{
243 QwtPicker::append( pos );
244 Q_EMIT appended( invTransform( pos ) );
245}
246
247/*!
248 Move the last point of the selection
249
250 \param pos New position
251 \sa isActive, begin(), end(), append()
252
253 \note The moved(const QPoint &), moved(const QDoublePoint &)
254 signals are emitted.
255*/
256void QwtPlotPicker::move( const QPoint &pos )
257{
258 QwtPicker::move( pos );
259 Q_EMIT moved( invTransform( pos ) );
260}
261
262/*!
263 Close a selection setting the state to inactive.
264
265 \param ok If true, complete the selection and emit selected signals
266 otherwise discard the selection.
267 \return true if the selection is accepted, false otherwise
268*/
269
270bool QwtPlotPicker::end( bool ok )
271{
272 ok = QwtPicker::end( ok );
273 if ( !ok )
274 return false;
275
276 QwtPlot *plot = QwtPlotPicker::plot();
277 if ( !plot )
278 return false;
279
280 const QPolygon pa = selection();
281 if ( pa.count() == 0 )
282 return false;
283
284 QwtPickerMachine::SelectionType selectionType =
285 QwtPickerMachine::NoSelection;
286
287 if ( stateMachine() )
288 selectionType = stateMachine()->selectionType();
289
290 switch ( selectionType )
291 {
292 case QwtPickerMachine::PointSelection:
293 {
294 const QPointF pos = invTransform( pa[0] );
295 Q_EMIT selected( pos );
296 break;
297 }
298 case QwtPickerMachine::RectSelection:
299 {
300 if ( pa.count() >= 2 )
301 {
302 const QPoint p1 = pa[0];
303 const QPoint p2 = pa[int( pa.count() - 1 )];
304
305 const QRect rect = QRect( p1, p2 ).normalized();
306 Q_EMIT selected( invTransform( rect ) );
307 }
308 break;
309 }
310 case QwtPickerMachine::PolygonSelection:
311 {
312 QVector<QPointF> dpa( pa.count() );
313 for ( int i = 0; i < int( pa.count() ); i++ )
314 dpa[i] = invTransform( pa[i] );
315
316 Q_EMIT selected( dpa );
317 }
318 default:
319 break;
320 }
321
322 return true;
323}
324
325/*!
326 Translate a rectangle from pixel into plot coordinates
327
328 \return Rectangle in plot coordinates
329 \sa transform()
330*/
331QRectF QwtPlotPicker::invTransform( const QRect &rect ) const
332{
333 const QwtScaleMap xMap = plot()->canvasMap( d_xAxis );
334 const QwtScaleMap yMap = plot()->canvasMap( d_yAxis );
335
336 return QwtScaleMap::invTransform( xMap, yMap, rect );
337}
338
339/*!
340 Translate a rectangle from plot into pixel coordinates
341 \return Rectangle in pixel coordinates
342 \sa invTransform()
343*/
344QRect QwtPlotPicker::transform( const QRectF &rect ) const
345{
346 const QwtScaleMap xMap = plot()->canvasMap( d_xAxis );
347 const QwtScaleMap yMap = plot()->canvasMap( d_yAxis );
348
349 return QwtScaleMap::transform( xMap, yMap, rect ).toRect();
350}
351
352/*!
353 Translate a point from pixel into plot coordinates
354 \return Point in plot coordinates
355 \sa transform()
356*/
357QPointF QwtPlotPicker::invTransform( const QPoint &pos ) const
358{
359 QwtScaleMap xMap = plot()->canvasMap( d_xAxis );
360 QwtScaleMap yMap = plot()->canvasMap( d_yAxis );
361
362 return QPointF(
363 xMap.invTransform( pos.x() ),
364 yMap.invTransform( pos.y() )
365 );
366}
367
368/*!
369 Translate a point from plot into pixel coordinates
370 \return Point in pixel coordinates
371 \sa invTransform()
372*/
373QPoint QwtPlotPicker::transform( const QPointF &pos ) const
374{
375 QwtScaleMap xMap = plot()->canvasMap( d_xAxis );
376 QwtScaleMap yMap = plot()->canvasMap( d_yAxis );
377
378 const QPointF p( xMap.transform( pos.x() ),
379 yMap.transform( pos.y() ) );
380
381 return p.toPoint();
382}
Note: See TracBrowser for help on using the repository browser.