qwt_compass.cpp
Go to the documentation of this file.
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_compass.h"
11 #include "qwt_compass_rose.h"
12 #include "qwt_math.h"
13 #include "qwt_scale_draw.h"
14 #include "qwt_painter.h"
15 #include "qwt_dial_needle.h"
16 #include <qpainter.h>
17 #include <qpixmap.h>
18 #include <qevent.h>
19 
26 {
29 
30  d_labelMap.insert( 0.0, QString::fromLatin1( "N" ) );
31  d_labelMap.insert( 45.0, QString::fromLatin1( "NE" ) );
32  d_labelMap.insert( 90.0, QString::fromLatin1( "E" ) );
33  d_labelMap.insert( 135.0, QString::fromLatin1( "SE" ) );
34  d_labelMap.insert( 180.0, QString::fromLatin1( "S" ) );
35  d_labelMap.insert( 225.0, QString::fromLatin1( "SW" ) );
36  d_labelMap.insert( 270.0, QString::fromLatin1( "W" ) );
37  d_labelMap.insert( 315.0, QString::fromLatin1( "NW" ) );
38 
39 #if 0
40  d_labelMap.insert( 22.5, QString::fromLatin1( "NNE" ) );
41  d_labelMap.insert( 67.5, QString::fromLatin1( "NEE" ) );
42  d_labelMap.insert( 112.5, QString::fromLatin1( "SEE" ) );
43  d_labelMap.insert( 157.5, QString::fromLatin1( "SSE" ) );
44  d_labelMap.insert( 202.5, QString::fromLatin1( "SSW" ) );
45  d_labelMap.insert( 247.5, QString::fromLatin1( "SWW" ) );
46  d_labelMap.insert( 292.5, QString::fromLatin1( "NWW" ) );
47  d_labelMap.insert( 337.5, QString::fromLatin1( "NNW" ) );
48 #endif
49 }
50 
56 QwtCompassScaleDraw::QwtCompassScaleDraw( const QMap<double, QString> &map ):
57  d_labelMap( map )
58 {
61 }
62 
75 void QwtCompassScaleDraw::setLabelMap( const QMap<double, QString> &map )
76 {
77  d_labelMap = map;
78 }
79 
80 
85 QMap<double, QString> QwtCompassScaleDraw::labelMap() const
86 {
87  return d_labelMap;
88 }
89 
102 QwtText QwtCompassScaleDraw::label( double value ) const
103 {
104  if ( qFuzzyCompare( value + 1.0, 1.0 ) )
105  value = 0.0;
106 
107  if ( value < 0.0 )
108  value += 360.0;
109 
110  if ( d_labelMap.contains( value ) )
111  return d_labelMap[value];
112 
113  return QwtText();
114 }
115 
117 {
118 public:
120  rose( NULL )
121  {
122  }
123 
125  {
126  delete rose;
127  }
128 
130 };
131 
141 QwtCompass::QwtCompass( QWidget* parent ):
142  QwtDial( parent )
143 {
144  d_data = new PrivateData;
145 
147 
148  setOrigin( 270.0 );
149  setWrapping( true );
150 
151  setScaleMaxMajor( 36 );
152  setScaleMaxMinor( 10 );
153 
154  setScale( 0.0, 360.0 ); // degrees as default
155  setTotalSteps( 360 );
156 }
157 
160 {
161  delete d_data;
162 }
163 
164 
172 void QwtCompass::drawScaleContents( QPainter *painter,
173  const QPointF &center, double radius ) const
174 {
175  QPalette::ColorGroup cg;
176  if ( isEnabled() )
177  cg = hasFocus() ? QPalette::Active : QPalette::Inactive;
178  else
179  cg = QPalette::Disabled;
180 
181  double north = origin();
182  if ( isValid() )
183  {
184  if ( mode() == RotateScale )
185  north -= value();
186  }
187 
188  const int margin = 4;
189  drawRose( painter, center, radius - margin, 360.0 - north, cg );
190 }
191 
201 void QwtCompass::drawRose( QPainter *painter, const QPointF &center,
202  double radius, double north, QPalette::ColorGroup cg ) const
203 {
204  if ( d_data->rose )
205  d_data->rose->draw( painter, center, radius, north, cg );
206 }
207 
216 {
217  if ( rose != d_data->rose )
218  {
219  if ( d_data->rose )
220  delete d_data->rose;
221 
222  d_data->rose = rose;
223  update();
224  }
225 }
226 
232 {
233  return d_data->rose;
234 }
235 
241 {
242  return d_data->rose;
243 }
244 
254 void QwtCompass::keyPressEvent( QKeyEvent *kev )
255 {
256  if ( isReadOnly() )
257  return;
258 
259 #if 0
260  if ( kev->key() == Key_5 )
261  {
262  invalidate(); // signal ???
263  return;
264  }
265 #endif
266 
267  double newValue = value();
268 
269  if ( kev->key() >= Qt::Key_1 && kev->key() <= Qt::Key_9 )
270  {
271  if ( mode() != RotateNeedle || kev->key() == Qt::Key_5 )
272  return;
273 
274  switch ( kev->key() )
275  {
276  case Qt::Key_6:
277  newValue = 180.0 * 0.0;
278  break;
279  case Qt::Key_3:
280  newValue = 180.0 * 0.25;
281  break;
282  case Qt::Key_2:
283  newValue = 180.0 * 0.5;
284  break;
285  case Qt::Key_1:
286  newValue = 180.0 * 0.75;
287  break;
288  case Qt::Key_4:
289  newValue = 180.0 * 1.0;
290  break;
291  case Qt::Key_7:
292  newValue = 180.0 * 1.25;
293  break;
294  case Qt::Key_8:
295  newValue = 180.0 * 1.5;
296  break;
297  case Qt::Key_9:
298  newValue = 180.0 * 1.75;
299  break;
300  }
301  newValue -= origin();
302  setValue( newValue );
303  }
304  else
305  {
306  QwtDial::keyPressEvent( kev );
307  }
308 }
void setScale(double lowerBound, double upperBound)
Specify a scale.
virtual ~QwtCompass()
Destructor.
The needle is fixed, the scales are rotating.
Definition: qwt_dial.h:91
The needle is rotating.
Definition: qwt_dial.h:88
QwtCompass(QWidget *parent=NULL)
Constructor.
void setRose(QwtCompassRose *rose)
virtual void draw(QPainter *painter, const QPointF &center, double radius, double north, QPalette::ColorGroup colorGroup=QPalette::Active) const =0
void setValue(double val)
virtual void setOrigin(double)
Change the origin.
Definition: qwt_dial.cpp:662
void setScaleMaxMajor(int ticks)
Set the maximum number of major tick intervals.
QwtDial class provides a rounded range control.
Definition: qwt_dial.h:49
void update(const std::string &key, const XmlRpc::XmlRpcValue &v)
virtual void keyPressEvent(QKeyEvent *)
double value() const
virtual void drawRose(QPainter *, const QPointF &center, double radius, double north, QPalette::ColorGroup) const
double origin() const
QwtCompassRose * rose
void setTotalSteps(uint)
Set the number of steps.
A class representing a text.
Definition: qwt_text.h:51
Abstract base class for a compass rose.
const QwtCompassRose * rose() const
Backbone = the line where the ticks are located.
void setScaleMaxMinor(int ticks)
Set the maximum number of minor tick intervals.
A special scale draw made for QwtCompass.
Definition: qwt_compass.h:31
void enableComponent(ScaleComponent, bool enable=true)
virtual void drawScaleContents(QPainter *, const QPointF &center, double radius) const
QwtCompassScaleDraw()
Constructor.
Definition: qwt_compass.cpp:25
void setScaleDraw(QwtRoundScaleDraw *)
Definition: qwt_dial.cpp:576
Mode mode() const
virtual void keyPressEvent(QKeyEvent *)
QMap< double, QString > d_labelMap
Definition: qwt_compass.h:43
PrivateData * d_data
Definition: qwt_compass.h:79
void setLabelMap(const QMap< double, QString > &map)
Set a map, mapping values to labels.
Definition: qwt_compass.cpp:75
virtual QwtText label(double value) const
QMap< double, QString > labelMap() const
Definition: qwt_compass.cpp:85


plotjuggler
Author(s): Davide Faconti
autogenerated on Sat Jul 6 2019 03:44:17