1
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
3
* Copyright (C) 1997 Josef Wilgen
4
* Copyright (C) 2002 Uwe Rathmann
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
*****************************************************************************/
17
#include "qwt_scale_draw.h"
18
#include "qwt_paint_buffer.h"
19
#include "qwt_painter.h"
20
#include "qwt_dial_needle.h"
21
#include "qwt_compass_rose.h"
22
#include "qwt_compass.h"
24
class QwtCompass::PrivateData
38
QMap<double, QString> labelMap;
43
\param parent Parent widget
45
Create a compass widget with a scale, no needle and no rose.
46
The default origin is 270.0 with no valid value. It accepts
47
mouse and keyboard inputs and has no step size. The default mode
48
is QwtDial::RotateNeedle.
50
QwtCompass::QwtCompass(QWidget* parent):
56
#if QT_VERSION < 0x040000
60
\param parent Parent widget
61
\param name Object name
63
Create a compass widget with a scale, no needle and no rose.
64
The default origin is 270.0 with no valid value. It accepts
65
mouse and keyboard inputs and has no step size. The default mode
66
is QwtDial::RotateNeedle.
68
QwtCompass::QwtCompass(QWidget* parent, const char *name):
77
QwtCompass::~QwtCompass()
82
void QwtCompass::initCompass()
84
d_data = new PrivateData;
86
setScaleOptions(ScaleLabel); // Only labels, no backbone, no ticks
92
d_data->labelMap.insert(0.0, QString::fromLatin1("N"));
93
d_data->labelMap.insert(45.0, QString::fromLatin1("NE"));
94
d_data->labelMap.insert(90.0, QString::fromLatin1("E"));
95
d_data->labelMap.insert(135.0, QString::fromLatin1("SE"));
96
d_data->labelMap.insert(180.0, QString::fromLatin1("S"));
97
d_data->labelMap.insert(225.0, QString::fromLatin1("SW"));
98
d_data->labelMap.insert(270.0, QString::fromLatin1("W"));
99
d_data->labelMap.insert(315.0, QString::fromLatin1("NW"));
102
d_data->labelMap.insert(22.5, QString::fromLatin1("NNE"));
103
d_data->labelMap.insert(67.5, QString::fromLatin1("NEE"));
104
d_data->labelMap.insert(112.5, QString::fromLatin1("SEE"));
105
d_data->labelMap.insert(157.5, QString::fromLatin1("SSE"));
106
d_data->labelMap.insert(202.5, QString::fromLatin1("SSW"));
107
d_data->labelMap.insert(247.5, QString::fromLatin1("SWW"));
108
d_data->labelMap.insert(292.5, QString::fromLatin1("NWW"));
109
d_data->labelMap.insert(337.5, QString::fromLatin1("NNW"));
113
//! Draw the contents of the scale
114
void QwtCompass::drawScaleContents(QPainter *painter,
115
const QPoint ¢er, int radius) const
117
QPalette::ColorGroup cg;
119
cg = hasFocus() ? QPalette::Active : QPalette::Inactive;
121
cg = QPalette::Disabled;
123
double north = origin();
126
if ( mode() == RotateScale )
130
const int margin = 4;
131
drawRose(painter, center, radius - margin, 360.0 - north, cg);
135
Draw the compass rose
137
\param painter Painter
138
\param center Center of the compass
139
\param radius of the circle, where to paint the rose
140
\param north Direction pointing north, in degrees counter clockwise
141
\param cg Color group
143
void QwtCompass::drawRose(QPainter *painter, const QPoint ¢er,
144
int radius, double north, QPalette::ColorGroup cg) const
147
d_data->rose->draw(painter, center, radius, north, cg);
151
Set a rose for the compass
152
\param rose Compass rose
153
\warning The rose will be deleted, when a different rose is
154
set or in ~QwtCompass
157
void QwtCompass::setRose(QwtCompassRose *rose)
159
if ( rose != d_data->rose )
173
const QwtCompassRose *QwtCompass::rose() const
182
QwtCompassRose *QwtCompass::rose()
190
Beside the keys described in QwtDial::keyPressEvent numbers
191
from 1-9 (without 5) set the direction according to their
192
position on the num pad.
196
void QwtCompass::keyPressEvent(QKeyEvent *kev)
202
if ( kev->key() == Key_5 )
204
invalidate(); // signal ???
209
double newValue = value();
211
if ( kev->key() >= Qt::Key_1 && kev->key() <= Qt::Key_9 )
213
if ( mode() != RotateNeedle || kev->key() == Qt::Key_5 )
219
newValue = 180.0 * 0.0;
222
newValue = 180.0 * 0.25;
225
newValue = 180.0 * 0.5;
228
newValue = 180.0 * 0.75;
231
newValue = 180.0 * 1.0;
234
newValue = 180.0 * 1.25;
237
newValue = 180.0 * 1.5;
240
newValue = 180.0 * 1.75;
243
newValue -= origin();
248
QwtDial::keyPressEvent(kev);
253
\return map, mapping values to labels
256
const QMap<double, QString> &QwtCompass::labelMap() const
258
return d_data->labelMap;
262
\return map, mapping values to labels
265
QMap<double, QString> &QwtCompass::labelMap()
267
return d_data->labelMap;
271
\brief Set a map, mapping values to labels
272
\param map value to label map
274
The values of the major ticks are found by looking into this
275
map. The default map consists of the labels N, NE, E, SE, S, SW, W, NW.
277
\warning The map will have no effect for values that are no major
278
tick values. Major ticks can be changed by QwtScaleDraw::setScale
280
\sa labelMap(), scaleDraw(), setScale()
282
void QwtCompass::setLabelMap(const QMap<double, QString> &map)
284
d_data->labelMap = map;
288
Map a value to a corresponding label
289
\param value Value that will be mapped
290
\return Label, or QString::null
292
label() looks in a map for a corresponding label for value
293
or return an null text.
294
\sa labelMap(), setLabelMap()
297
QwtText QwtCompass::scaleLabel(double value) const
300
// better solution ???
308
if ( d_data->labelMap.contains(value) )
309
return d_data->labelMap[value];