Qwt User's Guide 6.2.0

qwt_compass.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_compass.h"
11 #include "qwt_compass_rose.h"
12 #include "qwt_text.h"
13
14 #include <qevent.h>
15 #include <qmap.h>
16
17 class QwtCompassScaleDraw::PrivateData
18 {
19 public:
20 QMap< double, QString > labelMap;
21 };
22
28 QwtCompassScaleDraw::QwtCompassScaleDraw()
29 {
30 m_data = new PrivateData;
31
32 enableComponent( QwtAbstractScaleDraw::Backbone, false );
33 enableComponent( QwtAbstractScaleDraw::Ticks, false );
34
35 QMap< double, QString >& map = m_data->labelMap;
36
37 map.insert( 0.0, QString::fromLatin1( "N" ) );
38 map.insert( 45.0, QString::fromLatin1( "NE" ) );
39 map.insert( 90.0, QString::fromLatin1( "E" ) );
40 map.insert( 135.0, QString::fromLatin1( "SE" ) );
41 map.insert( 180.0, QString::fromLatin1( "S" ) );
42 map.insert( 225.0, QString::fromLatin1( "SW" ) );
43 map.insert( 270.0, QString::fromLatin1( "W" ) );
44 map.insert( 315.0, QString::fromLatin1( "NW" ) );
45
46 #if 0
47 map.insert( 22.5, QString::fromLatin1( "NNE" ) );
48 map.insert( 67.5, QString::fromLatin1( "NEE" ) );
49 map.insert( 112.5, QString::fromLatin1( "SEE" ) );
50 map.insert( 157.5, QString::fromLatin1( "SSE" ) );
51 map.insert( 202.5, QString::fromLatin1( "SSW" ) );
52 map.insert( 247.5, QString::fromLatin1( "SWW" ) );
53 map.insert( 292.5, QString::fromLatin1( "NWW" ) );
54 map.insert( 337.5, QString::fromLatin1( "NNW" ) );
55 #endif
56 }
57
63 QwtCompassScaleDraw::QwtCompassScaleDraw( const QMap< double, QString >& map )
64 {
65 m_data = new PrivateData;
66 m_data->labelMap = map;
67
68 enableComponent( QwtAbstractScaleDraw::Backbone, false );
69 enableComponent( QwtAbstractScaleDraw::Ticks, false );
70 }
71
73 QwtCompassScaleDraw::~QwtCompassScaleDraw()
74 {
75 delete m_data;
76 }
77
90 void QwtCompassScaleDraw::setLabelMap( const QMap< double, QString >& map )
91 {
92 m_data->labelMap = map;
93 }
94
99 QMap< double, QString > QwtCompassScaleDraw::labelMap() const
100 {
101 return m_data->labelMap;
102 }
103
116 QwtText QwtCompassScaleDraw::label( double value ) const
117 {
118 if ( qFuzzyCompare( value + 1.0, 1.0 ) )
119 value = 0.0;
120
121 if ( value < 0.0 )
122 value += 360.0;
123
124 QMap< double, QString >::const_iterator it =
125 m_data->labelMap.constFind( value );
126
127 if ( it != m_data->labelMap.constEnd() )
128 return *it;
129
130 return QwtText();
131 }
132
133 class QwtCompass::PrivateData
134 {
135 public:
136 PrivateData():
137 rose( NULL )
138 {
139 }
140
141 ~PrivateData()
142 {
143 delete rose;
144 }
145
146 QwtCompassRose* rose;
147 };
148
158 QwtCompass::QwtCompass( QWidget* parent )
159 : QwtDial( parent )
160 {
161 m_data = new PrivateData;
162
163 setScaleDraw( new QwtCompassScaleDraw() );
164
165 setOrigin( 270.0 );
166 setWrapping( true );
167
168 setScaleMaxMajor( 36 );
169 setScaleMaxMinor( 10 );
170
171 setScale( 0.0, 360.0 ); // degrees as default
172 setTotalSteps( 360 );
173 }
174
176 QwtCompass::~QwtCompass()
177 {
178 delete m_data;
179 }
180
181
189 void QwtCompass::drawScaleContents( QPainter* painter,
190 const QPointF& center, double radius ) const
191 {
192 QPalette::ColorGroup cg;
193 if ( isEnabled() )
194 cg = hasFocus() ? QPalette::Active : QPalette::Inactive;
195 else
196 cg = QPalette::Disabled;
197
198 double north = origin();
199 if ( isValid() )
200 {
201 if ( mode() == RotateScale )
202 north -= value();
203 }
204
205 const int margin = 4;
206 drawRose( painter, center, radius - margin, 360.0 - north, cg );
207 }
208
218 void QwtCompass::drawRose( QPainter* painter, const QPointF& center,
219 double radius, double north, QPalette::ColorGroup cg ) const
220 {
221 if ( m_data->rose )
222 m_data->rose->draw( painter, center, radius, north, cg );
223 }
224
232 void QwtCompass::setRose( QwtCompassRose* rose )
233 {
234 if ( rose != m_data->rose )
235 {
236 if ( m_data->rose )
237 delete m_data->rose;
238
239 m_data->rose = rose;
240 update();
241 }
242 }
243
248 const QwtCompassRose* QwtCompass::rose() const
249 {
250 return m_data->rose;
251 }
252
257 QwtCompassRose* QwtCompass::rose()
258 {
259 return m_data->rose;
260 }
261
271 void QwtCompass::keyPressEvent( QKeyEvent* kev )
272 {
273 if ( isReadOnly() )
274 return;
275
276 #if 0
277 if ( kev->key() == Key_5 )
278 {
279 invalidate(); // signal ???
280 return;
281 }
282 #endif
283
284 double newValue = value();
285
286 if ( kev->key() >= Qt::Key_1 && kev->key() <= Qt::Key_9 )
287 {
288 if ( mode() != RotateNeedle || kev->key() == Qt::Key_5 )
289 return;
290
291 switch ( kev->key() )
292 {
293 case Qt::Key_6:
294 newValue = 180.0 * 0.0;
295 break;
296 case Qt::Key_3:
297 newValue = 180.0 * 0.25;
298 break;
299 case Qt::Key_2:
300 newValue = 180.0 * 0.5;
301 break;
302 case Qt::Key_1:
303 newValue = 180.0 * 0.75;
304 break;
305 case Qt::Key_4:
306 newValue = 180.0 * 1.0;
307 break;
308 case Qt::Key_7:
309 newValue = 180.0 * 1.25;
310 break;
311 case Qt::Key_8:
312 newValue = 180.0 * 1.5;
313 break;
314 case Qt::Key_9:
315 newValue = 180.0 * 1.75;
316 break;
317 }
318 newValue -= origin();
319 setValue( newValue );
320 }
321 else
322 {
323 QwtDial::keyPressEvent( kev );
324 }
325 }
326
327 #if QWT_MOC_INCLUDE
328 #include "moc_qwt_compass.cpp"
329 #endif
QMap< double, QString >
QwtAbstractScaleDraw::Ticks
@ Ticks
Ticks.
Definition: qwt_abstract_scale_draw.h:45
QwtAbstractScaleDraw::Backbone
@ Backbone
Backbone = the line where the ticks are located.
Definition: qwt_abstract_scale_draw.h:42
QwtAbstractScaleDraw::enableComponent
void enableComponent(ScaleComponent, bool enable=true)
Definition: qwt_abstract_scale_draw.cpp:79
QwtAbstractScale::setScaleMaxMajor
void setScaleMaxMajor(int ticks)
Set the maximum number of major tick intervals.
Definition: qwt_abstract_scale.cpp:189
QwtAbstractScale::setScaleMaxMinor
void setScaleMaxMinor(int ticks)
Set the maximum number of minor tick intervals.
Definition: qwt_abstract_scale.cpp:219
QwtAbstractScale::setScale
void setScale(double lowerBound, double upperBound)
Specify a scale.
Definition: qwt_abstract_scale.cpp:130
QwtAbstractSlider::keyPressEvent
virtual void keyPressEvent(QKeyEvent *) override
Definition: qwt_abstract_slider.cpp:370
QwtAbstractSlider::setTotalSteps
void setTotalSteps(uint)
Set the number of steps.
Definition: qwt_abstract_slider.cpp:474
QwtAbstractSlider::setValue
void setValue(double value)
Definition: qwt_abstract_slider.cpp:570
QwtAbstractSlider::isReadOnly
bool isReadOnly() const
Definition: qwt_abstract_slider.cpp:159
QwtAbstractSlider::isValid
bool isValid() const
Definition: qwt_abstract_slider.cpp:125
QwtAbstractSlider::value
double value() const
Returns the current value.
Definition: qwt_abstract_slider.cpp:587
QwtAbstractSlider::setWrapping
void setWrapping(bool)
Definition: qwt_abstract_slider.cpp:599
QwtCompass::QwtCompass
QwtCompass(QWidget *parent=NULL)
Constructor.
Definition: qwt_compass.cpp:158
QwtCompass::setRose
void setRose(QwtCompassRose *rose)
Definition: qwt_compass.cpp:232
QwtCompass::keyPressEvent
virtual void keyPressEvent(QKeyEvent *) override
Definition: qwt_compass.cpp:271
QwtCompass::drawRose
virtual void drawRose(QPainter *, const QPointF &center, double radius, double north, QPalette::ColorGroup) const
Definition: qwt_compass.cpp:218
QwtCompass::drawScaleContents
virtual void drawScaleContents(QPainter *, const QPointF &center, double radius) const override
Definition: qwt_compass.cpp:189
QwtCompass::~QwtCompass
virtual ~QwtCompass()
Destructor.
Definition: qwt_compass.cpp:176
QwtCompass::rose
const QwtCompassRose * rose() const
Definition: qwt_compass.cpp:248
QwtCompassRose
Abstract base class for a compass rose.
Definition: qwt_compass_rose.h:22
QwtCompassScaleDraw
A special scale draw made for QwtCompass.
Definition: qwt_compass.h:33
QwtCompassScaleDraw::label
virtual QwtText label(double value) const override
Definition: qwt_compass.cpp:116
QwtCompassScaleDraw::setLabelMap
void setLabelMap(const QMap< double, QString > &map)
Set a map, mapping values to labels.
Definition: qwt_compass.cpp:90
QwtCompassScaleDraw::~QwtCompassScaleDraw
virtual ~QwtCompassScaleDraw()
Destructor.
Definition: qwt_compass.cpp:73
QwtCompassScaleDraw::labelMap
QMap< double, QString > labelMap() const
Definition: qwt_compass.cpp:99
QwtCompassScaleDraw::QwtCompassScaleDraw
QwtCompassScaleDraw()
Constructor.
Definition: qwt_compass.cpp:28
QwtDial
QwtDial class provides a rounded range control.
Definition: qwt_dial.h:51
QwtDial::setScaleDraw
void setScaleDraw(QwtRoundScaleDraw *)
Definition: qwt_dial.cpp:574
QwtDial::setOrigin
virtual void setOrigin(double)
Change the origin.
Definition: qwt_dial.cpp:660
QwtDial::origin
double origin() const
Definition: qwt_dial.cpp:674
QwtDial::RotateScale
@ RotateScale
The needle is fixed, the scales are rotating.
Definition: qwt_dial.h:92
QwtDial::RotateNeedle
@ RotateNeedle
The needle is rotating.
Definition: qwt_dial.h:89
QwtDial::mode
Mode mode() const
Definition: qwt_dial.cpp:293
QwtText
A class representing a text.
Definition: qwt_text.h:52