~ubuntu-branches/ubuntu/oneiric/qwt/oneiric-proposed

« back to all changes in this revision

Viewing changes to qwt-5.0.1/src/qwt_analog_clock.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
 
#include "qwt_analog_clock.h"
11
 
 
12
 
/*!
13
 
  Constructor
14
 
  \param parent Parent widget
15
 
*/
16
 
QwtAnalogClock::QwtAnalogClock(QWidget *parent):
17
 
    QwtDial(parent)
18
 
{
19
 
    initClock();
20
 
}
21
 
 
22
 
#if QT_VERSION < 0x040000
23
 
/*!
24
 
  Constructor
25
 
  \param parent Parent widget
26
 
  \param name Object name
27
 
*/
28
 
QwtAnalogClock::QwtAnalogClock(QWidget* parent, const char *name):
29
 
    QwtDial(parent, name)
30
 
{
31
 
    initClock();
32
 
}
33
 
#endif
34
 
 
35
 
void QwtAnalogClock::initClock()
36
 
{
37
 
    setWrapping(true);
38
 
    setReadOnly(true);
39
 
 
40
 
    setOrigin(270.0);
41
 
    setRange(0.0, 60.0 * 60.0 * 12.0); // seconds
42
 
    setScale(-1, 5, 60.0 * 60.0);
43
 
 
44
 
    setScaleOptions(ScaleTicks | ScaleLabel);
45
 
    setScaleTicks(1, 0, 8);
46
 
    scaleDraw()->setSpacing(8);
47
 
 
48
 
    QColor knobColor =
49
 
#if QT_VERSION < 0x040000
50
 
        palette().color(QPalette::Active, QColorGroup::Text);
51
 
#else
52
 
        palette().color(QPalette::Active, QPalette::Text);
53
 
#endif
54
 
    knobColor = knobColor.dark(120);
55
 
 
56
 
    QColor handColor;
57
 
    int width;
58
 
 
59
 
    for ( int i = 0; i < NHands; i++ )
60
 
    {
61
 
        if ( i == SecondHand )
62
 
        {
63
 
            width = 2;
64
 
            handColor = knobColor.dark(120);
65
 
        }
66
 
        else
67
 
        {
68
 
            width = 8;
69
 
            handColor = knobColor;
70
 
        }
71
 
 
72
 
        QwtDialSimpleNeedle *hand = new QwtDialSimpleNeedle(
73
 
            QwtDialSimpleNeedle::Arrow, true, handColor, knobColor);
74
 
        hand->setWidth(width);
75
 
 
76
 
        d_hand[i] = NULL;
77
 
        setHand((Hand)i, hand);
78
 
    }
79
 
}
80
 
 
81
 
//! Destructor
82
 
QwtAnalogClock::~QwtAnalogClock()
83
 
{
84
 
    for ( int i = 0; i < NHands; i++ )
85
 
        delete d_hand[i];
86
 
}
87
 
 
88
 
/*! 
89
 
  Nop method, use setHand instead
90
 
  \sa QwtAnalogClock::setHand
91
 
*/
92
 
void QwtAnalogClock::setNeedle(QwtDialNeedle *)
93
 
{
94
 
    // no op
95
 
    return;
96
 
}
97
 
 
98
 
/*!
99
 
   Set a clockhand
100
 
   \param hand Specifies the type of hand
101
 
   \param needle Hand
102
 
   \sa QwtAnalogClock::hand()
103
 
*/
104
 
void QwtAnalogClock::setHand(Hand hand, QwtDialNeedle *needle)
105
 
{
106
 
    if ( hand >= 0 || hand < NHands )
107
 
    {
108
 
        delete d_hand[hand];
109
 
        d_hand[hand] = needle;
110
 
    }
111
 
}
112
 
 
113
 
/*!
114
 
  \return Clock hand
115
 
  \param hd Specifies the type of hand
116
 
  \sa QwtAnalogClock::setHand
117
 
*/
118
 
QwtDialNeedle *QwtAnalogClock::hand(Hand hd)
119
 
{
120
 
    if ( hd < 0 || hd >= NHands )
121
 
        return NULL;
122
 
 
123
 
    return d_hand[hd];
124
 
}
125
 
 
126
 
/*!
127
 
  \return Clock hand
128
 
  \param hd Specifies the type of hand
129
 
  \sa QwtAnalogClock::setHand
130
 
*/
131
 
const QwtDialNeedle *QwtAnalogClock::hand(Hand hd) const
132
 
{
133
 
    return ((QwtAnalogClock *)this)->hand(hd);
134
 
}
135
 
 
136
 
/*!
137
 
  \brief Set the current time
138
 
 
139
 
  This is the same as QwtAnalogClock::setTime(), but Qt < 3.0
140
 
  can't handle default parameters for slots.
141
 
*/
142
 
void QwtAnalogClock::setCurrentTime()
143
 
144
 
    setTime(QTime::currentTime()); 
145
 
}
146
 
 
147
 
/*!
148
 
  Set a time 
149
 
  \param time Time to display
150
 
*/
151
 
void QwtAnalogClock::setTime(const QTime &time)
152
 
{
153
 
    if ( time.isValid() )
154
 
    {
155
 
        setValue((time.hour() % 12) * 60.0 * 60.0 
156
 
            + time.minute() * 60.0 + time.second());
157
 
    }
158
 
    else
159
 
        setValid(false);
160
 
}
161
 
 
162
 
/*! 
163
 
  Find the scale label for a given value
164
 
 
165
 
  \param value Value
166
 
  \return Label
167
 
*/
168
 
QwtText QwtAnalogClock::scaleLabel(double value) const
169
 
{
170
 
    if ( value == 0.0 )
171
 
        value = 60.0 * 60.0 * 12.0;
172
 
 
173
 
    return QString::number(int(value / (60.0 * 60.0)));
174
 
}
175
 
 
176
 
/*!
177
 
  \brief Draw the needle
178
 
 
179
 
  A clock has no single needle but three hands instead. drawNeedle
180
 
  translates value() into directions for the hands and calls
181
 
  drawHand().
182
 
 
183
 
  \param painter Painter
184
 
  \param center Center of the clock
185
 
  \param radius Maximum length for the hands
186
 
  \param direction Dummy, not used.
187
 
  \param cg ColorGroup
188
 
 
189
 
  \sa QwtAnalogClock::drawHand()
190
 
*/
191
 
void QwtAnalogClock::drawNeedle(QPainter *painter, const QPoint &center,
192
 
        int radius, double, QPalette::ColorGroup cg) const
193
 
{
194
 
    if ( isValid() )
195
 
    {
196
 
        const double hours = value() / (60.0 * 60.0);
197
 
        const double minutes = (value() - (int)hours * 60.0 * 60.0) / 60.0;
198
 
        const double seconds = value() - (int)hours * 60.0 * 60.0 
199
 
            - (int)minutes * 60.0;
200
 
 
201
 
        drawHand(painter, HourHand, center, radius,
202
 
            360.0 - (origin() + 360.0 * hours / 12.0), cg);
203
 
        drawHand(painter, MinuteHand, center, radius,
204
 
            360.0 - (origin() + 360.0 * minutes / 60.0), cg);
205
 
        drawHand(painter, SecondHand, center, radius,
206
 
            360.0 - (origin() + 360.0 * seconds / 60.0), cg);
207
 
    }
208
 
}
209
 
 
210
 
/*!
211
 
  Draw a clock hand
212
 
 
213
 
  \param painter Painter
214
 
  \param hd Specify the type of hand
215
 
  \param center Center of the clock
216
 
  \param radius Maximum length for the hands
217
 
  \param direction Direction of the hand in degrees, counter clockwise
218
 
  \param cg ColorGroup
219
 
*/
220
 
void QwtAnalogClock::drawHand(QPainter *painter, Hand hd,
221
 
    const QPoint &center, int radius, double direction, 
222
 
    QPalette::ColorGroup cg) const
223
 
{
224
 
    const QwtDialNeedle *needle = hand(hd);
225
 
    if ( needle )
226
 
    {
227
 
        if ( hd == HourHand )
228
 
            radius = qRound(0.8 * radius);
229
 
 
230
 
        needle->draw(painter, center, radius, direction, cg);
231
 
    }
232
 
}