~l3on/ubuntu/oneiric/qwt/fix-921430

« back to all changes in this revision

Viewing changes to qwt-5.0.1/src/qwt_compass.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Fathi Boudra
  • Date: 2007-10-05 15:20:41 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20071005152041-qmybqh4fj9jejyo2
Tags: 5.0.2-2
* Handle nostrip build option. (Closes: #437877)
* Build libqwt5-doc package in binary-indep target. (Closes: #443110)

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
// vim: expandtab
11
 
 
12
 
#include <math.h>
13
 
#include <qpainter.h>
14
 
#include <qpixmap.h>
15
 
#include <qevent.h>
16
 
#include "qwt_math.h"
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"
23
 
 
24
 
class QwtCompass::PrivateData
25
 
{
26
 
public:
27
 
    PrivateData():
28
 
        rose(NULL)
29
 
    {
30
 
    }
31
 
 
32
 
    ~PrivateData()
33
 
    {
34
 
        delete rose;
35
 
    }
36
 
 
37
 
    QwtCompassRose *rose;
38
 
    QMap<double, QString> labelMap;
39
 
};
40
 
 
41
 
/*!
42
 
  \brief Constructor
43
 
  \param parent Parent widget
44
 
 
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.
49
 
*/  
50
 
QwtCompass::QwtCompass(QWidget* parent):
51
 
    QwtDial(parent)
52
 
{
53
 
    initCompass();
54
 
}
55
 
 
56
 
#if QT_VERSION < 0x040000
57
 
 
58
 
/*!
59
 
  \brief Constructor
60
 
  \param parent Parent widget
61
 
  \param name Object name
62
 
 
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.
67
 
*/  
68
 
QwtCompass::QwtCompass(QWidget* parent, const char *name):
69
 
    QwtDial(parent, name)
70
 
{
71
 
    initCompass();
72
 
}
73
 
 
74
 
#endif
75
 
 
76
 
//!  Destructor
77
 
QwtCompass::~QwtCompass() 
78
 
{
79
 
    delete d_data;
80
 
}
81
 
 
82
 
void QwtCompass::initCompass()
83
 
{
84
 
    d_data = new PrivateData;
85
 
 
86
 
    setScaleOptions(ScaleLabel); // Only labels, no backbone, no ticks
87
 
 
88
 
    setOrigin(270.0);
89
 
    setWrapping(true);
90
 
 
91
 
 
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"));
100
 
 
101
 
#if 0
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"));
110
 
#endif
111
 
}
112
 
 
113
 
//! Draw the contents of the scale
114
 
void QwtCompass::drawScaleContents(QPainter *painter, 
115
 
        const QPoint &center, int radius) const
116
 
{
117
 
    QPalette::ColorGroup cg;
118
 
    if ( isEnabled() )
119
 
        cg = hasFocus() ? QPalette::Active : QPalette::Inactive;
120
 
    else
121
 
        cg = QPalette::Disabled;
122
 
 
123
 
    double north = origin();
124
 
    if ( isValid() )
125
 
    {
126
 
        if ( mode() == RotateScale )
127
 
            north -= value(); 
128
 
    }
129
 
 
130
 
    const int margin = 4;
131
 
    drawRose(painter, center, radius - margin, 360.0 - north,  cg);
132
 
}
133
 
 
134
 
/*!
135
 
  Draw the compass rose
136
 
  
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
142
 
*/
143
 
void QwtCompass::drawRose(QPainter *painter, const QPoint &center,
144
 
    int radius, double north, QPalette::ColorGroup cg) const
145
 
{
146
 
    if ( d_data->rose )
147
 
        d_data->rose->draw(painter, center, radius, north,  cg);
148
 
}
149
 
 
150
 
/*!
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
155
 
  \sa rose()
156
 
*/
157
 
void QwtCompass::setRose(QwtCompassRose *rose)
158
 
{
159
 
    if ( rose != d_data->rose )
160
 
    {
161
 
        if ( d_data->rose )
162
 
            delete d_data->rose;
163
 
 
164
 
        d_data->rose = rose;
165
 
        update();
166
 
    }
167
 
}
168
 
 
169
 
/*! 
170
 
  \return rose
171
 
  \sa setRose()
172
 
*/
173
 
const QwtCompassRose *QwtCompass::rose() const 
174
 
175
 
    return d_data->rose; 
176
 
}
177
 
 
178
 
/*! 
179
 
  \return rose
180
 
  \sa setRose()
181
 
*/
182
 
QwtCompassRose *QwtCompass::rose() 
183
 
184
 
    return d_data->rose; 
185
 
}
186
 
 
187
 
/*! 
188
 
  Handles key events
189
 
 
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.
193
 
 
194
 
  \sa isReadOnly()
195
 
*/
196
 
void QwtCompass::keyPressEvent(QKeyEvent *kev) 
197
 
{
198
 
    if (isReadOnly()) 
199
 
        return;
200
 
 
201
 
#if 0
202
 
    if ( kev->key() == Key_5 )
203
 
    {
204
 
        invalidate(); // signal ???
205
 
        return;
206
 
    }
207
 
#endif
208
 
 
209
 
    double newValue = value();
210
 
 
211
 
    if ( kev->key() >= Qt::Key_1 && kev->key() <= Qt::Key_9 )
212
 
    {
213
 
        if ( mode() != RotateNeedle || kev->key() == Qt::Key_5 )
214
 
            return;
215
 
 
216
 
        switch (kev->key()) 
217
 
        {
218
 
            case Qt::Key_6: 
219
 
                newValue = 180.0 * 0.0;
220
 
                break;
221
 
            case Qt::Key_3: 
222
 
                newValue = 180.0 * 0.25;
223
 
                break;
224
 
            case Qt::Key_2: 
225
 
                newValue = 180.0 * 0.5;
226
 
                break;
227
 
            case Qt::Key_1: 
228
 
                newValue = 180.0 * 0.75;
229
 
                break;
230
 
            case Qt::Key_4: 
231
 
                newValue = 180.0 * 1.0;
232
 
                break;
233
 
            case Qt::Key_7: 
234
 
                newValue = 180.0 * 1.25;
235
 
                break;
236
 
            case Qt::Key_8: 
237
 
                newValue = 180.0 * 1.5;
238
 
                break;
239
 
            case Qt::Key_9: 
240
 
                newValue = 180.0 * 1.75;
241
 
                break;
242
 
        }
243
 
        newValue -= origin();
244
 
        setValue(newValue);
245
 
    }
246
 
    else
247
 
    {
248
 
        QwtDial::keyPressEvent(kev);
249
 
    }
250
 
}
251
 
 
252
 
/*!
253
 
  \return map, mapping values to labels
254
 
  \sa setLabelMap()
255
 
*/
256
 
const QMap<double, QString> &QwtCompass::labelMap() const 
257
 
258
 
    return d_data->labelMap; 
259
 
}
260
 
 
261
 
/*!
262
 
  \return map, mapping values to labels
263
 
  \sa setLabelMap()
264
 
*/
265
 
QMap<double, QString> &QwtCompass::labelMap() 
266
 
267
 
    return d_data->labelMap; 
268
 
}
269
 
 
270
 
/*!
271
 
  \brief Set a map, mapping values to labels
272
 
  \param map value to label map
273
 
 
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.
276
 
 
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
279
 
 
280
 
  \sa labelMap(), scaleDraw(), setScale()
281
 
*/
282
 
void QwtCompass::setLabelMap(const QMap<double, QString> &map) 
283
 
284
 
    d_data->labelMap = map; 
285
 
}
286
 
 
287
 
/*!
288
 
  Map a value to a corresponding label
289
 
  \param value Value that will be mapped
290
 
  \return Label, or QString::null
291
 
 
292
 
  label() looks in a map for a corresponding label for value
293
 
  or return an null text.
294
 
  \sa labelMap(), setLabelMap()
295
 
*/
296
 
 
297
 
QwtText QwtCompass::scaleLabel(double value) const
298
 
{
299
 
#if 0
300
 
    // better solution ???
301
 
    if ( value == -0 )
302
 
        value = 0.0;
303
 
#endif
304
 
 
305
 
    if ( value < 0.0 )
306
 
        value += 360.0;
307
 
 
308
 
    if ( d_data->labelMap.contains(value) )
309
 
        return d_data->labelMap[value];
310
 
 
311
 
    return QwtText();
312
 
}