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 */
QwtAnalogClock(QWidget * parent)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 */
QwtAnalogClock(QWidget * parent,const char * name)28 QwtAnalogClock::QwtAnalogClock(QWidget* parent, const char *name):
29 QwtDial(parent, name)
30 {
31 initClock();
32 }
33 #endif
34
initClock()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.darker(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.darker(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
~QwtAnalogClock()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 setHand()
91 */
setNeedle(QwtDialNeedle *)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 hand()
103 */
setHand(Hand hand,QwtDialNeedle * needle)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 setHand()
117 */
hand(Hand hd)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 setHand()
130 */
hand(Hand hd) const131 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 */
setCurrentTime()142 void QwtAnalogClock::setCurrentTime()
143 {
144 setTime(QTime::currentTime());
145 }
146
147 /*!
148 Set a time
149 \param time Time to display
150 */
setTime(const QTime & time)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 */
scaleLabel(double value) const168 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 drawHand()
190 */
drawNeedle(QPainter * painter,const QPoint & center,int radius,double,QPalette::ColorGroup cg) const191 void QwtAnalogClock::drawNeedle(QPainter *painter, const QPoint ¢er,
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 double angle[NHands];
202 angle[HourHand] = 360.0 * hours / 12.0;
203 angle[MinuteHand] = 360.0 * minutes / 60.0;
204 angle[SecondHand] = 360.0 * seconds / 60.0;
205
206 for ( int hand = 0; hand < NHands; hand++ )
207 {
208 double d = angle[hand];
209 if ( direction() == Clockwise )
210 d = 360.0 - d;
211
212 d -= origin();
213
214 drawHand(painter, (Hand)hand, center, radius, d, cg);
215 }
216 }
217 }
218
219 /*!
220 Draw a clock hand
221
222 \param painter Painter
223 \param hd Specify the type of hand
224 \param center Center of the clock
225 \param radius Maximum length for the hands
226 \param direction Direction of the hand in degrees, counter clockwise
227 \param cg ColorGroup
228 */
drawHand(QPainter * painter,Hand hd,const QPoint & center,int radius,double direction,QPalette::ColorGroup cg) const229 void QwtAnalogClock::drawHand(QPainter *painter, Hand hd,
230 const QPoint ¢er, int radius, double direction,
231 QPalette::ColorGroup cg) const
232 {
233 const QwtDialNeedle *needle = hand(hd);
234 if ( needle )
235 {
236 if ( hd == HourHand )
237 radius = qRound(0.8 * radius);
238
239 needle->draw(painter, center, radius, direction, cg);
240 }
241 }
242