Qwt User's Guide 6.3.0
Loading...
Searching...
No Matches
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
17class QwtCompassScaleDraw::PrivateData
18{
19 public:
21};
22
29{
30 m_data = new PrivateData;
31
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
64{
65 m_data = new PrivateData;
66 m_data->labelMap = map;
67
70}
71
77
91{
92 m_data->labelMap = map;
93}
94
100{
101 return m_data->labelMap;
102}
103
117{
118 if ( qFuzzyCompare( value + 1.0, 1.0 ) )
119 value = 0.0;
120
121 if ( value < 0.0 )
122 value += 360.0;
123
125 m_data->labelMap.constFind( value );
126
127 if ( it != m_data->labelMap.constEnd() )
128 return *it;
129
130 return QwtText();
131}
132
133class 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
158QwtCompass::QwtCompass( QWidget* parent )
159 : QwtDial( parent )
160{
161 m_data = new PrivateData;
162
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
177{
178 delete m_data;
179}
180
181
189void 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
218void 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
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
249{
250 return m_data->rose;
251}
252
258{
259 return m_data->rose;
260}
261
271void 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 {
324 }
325}
326
327#include "moc_qwt_compass.cpp"
@ Backbone
Backbone = the line where the ticks are located.
void enableComponent(ScaleComponent, bool enable=true)
void setScaleMaxMajor(int ticks)
Set the maximum number of major tick intervals.
void setScaleMaxMinor(int ticks)
Set the maximum number of minor tick intervals.
void setScale(double lowerBound, double upperBound)
Specify a scale.
virtual void keyPressEvent(QKeyEvent *) override
void setTotalSteps(uint)
Set the number of steps.
void setValue(double value)
double value() const
Returns the current value.
QwtCompass(QWidget *parent=NULL)
Constructor.
void setRose(QwtCompassRose *rose)
virtual void keyPressEvent(QKeyEvent *) override
virtual void drawRose(QPainter *, const QPointF &center, double radius, double north, QPalette::ColorGroup) const
virtual void drawScaleContents(QPainter *, const QPointF &center, double radius) const override
virtual ~QwtCompass()
Destructor.
const QwtCompassRose * rose() const
Abstract base class for a compass rose.
virtual void draw(QPainter *painter, const QPointF &center, double radius, double north, QPalette::ColorGroup colorGroup=QPalette::Active) const =0
A special scale draw made for QwtCompass.
Definition qwt_compass.h:33
virtual QwtText label(double value) const override
void setLabelMap(const QMap< double, QString > &map)
Set a map, mapping values to labels.
virtual ~QwtCompassScaleDraw()
Destructor.
QMap< double, QString > labelMap() const
QwtCompassScaleDraw()
Constructor.
QwtDial class provides a rounded range control.
Definition qwt_dial.h:51
void setScaleDraw(QwtRoundScaleDraw *)
Definition qwt_dial.cpp:574
virtual void setOrigin(double)
Change the origin.
Definition qwt_dial.cpp:660
double origin() const
Definition qwt_dial.cpp:674
@ RotateScale
The needle is fixed, the scales are rotating.
Definition qwt_dial.h:92
@ RotateNeedle
The needle is rotating.
Definition qwt_dial.h:89
Mode mode() const
Definition qwt_dial.cpp:293
A class representing a text.
Definition qwt_text.h:52