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 | #include "qwt_round_scale_draw.h"
|
---|
12 | #include <qmath.h>
|
---|
13 | #include <qlocale.h>
|
---|
14 |
|
---|
15 | class QwtAnalogClockScaleDraw: public QwtRoundScaleDraw
|
---|
16 | {
|
---|
17 | public:
|
---|
18 | QwtAnalogClockScaleDraw()
|
---|
19 | {
|
---|
20 | setSpacing( 8 );
|
---|
21 |
|
---|
22 | enableComponent( QwtAbstractScaleDraw::Backbone, false );
|
---|
23 |
|
---|
24 | setTickLength( QwtScaleDiv::MinorTick, 2 );
|
---|
25 | setTickLength( QwtScaleDiv::MediumTick, 4 );
|
---|
26 | setTickLength( QwtScaleDiv::MajorTick, 8 );
|
---|
27 |
|
---|
28 | setPenWidth( 1 );
|
---|
29 | }
|
---|
30 |
|
---|
31 | virtual QwtText label( double value ) const
|
---|
32 | {
|
---|
33 | if ( qFuzzyCompare( value + 1.0, 1.0 ) )
|
---|
34 | value = 60.0 * 60.0 * 12.0;
|
---|
35 |
|
---|
36 | return QLocale().toString( qRound( value / ( 60.0 * 60.0 ) ) );
|
---|
37 | }
|
---|
38 | };
|
---|
39 |
|
---|
40 | /*!
|
---|
41 | Constructor
|
---|
42 | \param parent Parent widget
|
---|
43 | */
|
---|
44 | QwtAnalogClock::QwtAnalogClock( QWidget *parent ):
|
---|
45 | QwtDial( parent )
|
---|
46 | {
|
---|
47 | setWrapping( true );
|
---|
48 | setReadOnly( true );
|
---|
49 |
|
---|
50 | setOrigin( 270.0 );
|
---|
51 | setScaleDraw( new QwtAnalogClockScaleDraw() );
|
---|
52 |
|
---|
53 | setTotalSteps( 60 );
|
---|
54 |
|
---|
55 | const int secondsPerHour = 60.0 * 60.0;
|
---|
56 |
|
---|
57 | QList<double> majorTicks;
|
---|
58 | QList<double> minorTicks;
|
---|
59 |
|
---|
60 | for ( int i = 0; i < 12; i++ )
|
---|
61 | {
|
---|
62 | majorTicks += i * secondsPerHour;
|
---|
63 |
|
---|
64 | for ( int j = 1; j < 5; j++ )
|
---|
65 | minorTicks += i * secondsPerHour + j * secondsPerHour / 5.0;
|
---|
66 | }
|
---|
67 |
|
---|
68 | QwtScaleDiv scaleDiv;
|
---|
69 | scaleDiv.setInterval( 0.0, 12.0 * secondsPerHour );
|
---|
70 | scaleDiv.setTicks( QwtScaleDiv::MajorTick, majorTicks );
|
---|
71 | scaleDiv.setTicks( QwtScaleDiv::MinorTick, minorTicks );
|
---|
72 | setScale( scaleDiv );
|
---|
73 |
|
---|
74 | QColor knobColor = palette().color( QPalette::Active, QPalette::Text );
|
---|
75 | knobColor = knobColor.darker( 120 );
|
---|
76 |
|
---|
77 | QColor handColor;
|
---|
78 | int width;
|
---|
79 |
|
---|
80 | for ( int i = 0; i < NHands; i++ )
|
---|
81 | {
|
---|
82 | if ( i == SecondHand )
|
---|
83 | {
|
---|
84 | width = 2;
|
---|
85 | handColor = knobColor.darker( 120 );
|
---|
86 | }
|
---|
87 | else
|
---|
88 | {
|
---|
89 | width = 8;
|
---|
90 | handColor = knobColor;
|
---|
91 | }
|
---|
92 |
|
---|
93 | QwtDialSimpleNeedle *hand = new QwtDialSimpleNeedle(
|
---|
94 | QwtDialSimpleNeedle::Arrow, true, handColor, knobColor );
|
---|
95 | hand->setWidth( width );
|
---|
96 |
|
---|
97 | d_hand[i] = NULL;
|
---|
98 | setHand( static_cast<Hand>( i ), hand );
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | //! Destructor
|
---|
103 | QwtAnalogClock::~QwtAnalogClock()
|
---|
104 | {
|
---|
105 | for ( int i = 0; i < NHands; i++ )
|
---|
106 | delete d_hand[i];
|
---|
107 | }
|
---|
108 |
|
---|
109 | /*!
|
---|
110 | Nop method, use setHand() instead
|
---|
111 | \sa setHand()
|
---|
112 | */
|
---|
113 | void QwtAnalogClock::setNeedle( QwtDialNeedle * )
|
---|
114 | {
|
---|
115 | // no op
|
---|
116 | return;
|
---|
117 | }
|
---|
118 |
|
---|
119 | /*!
|
---|
120 | Set a clock hand
|
---|
121 | \param hand Specifies the type of hand
|
---|
122 | \param needle Hand
|
---|
123 | \sa hand()
|
---|
124 | */
|
---|
125 | void QwtAnalogClock::setHand( Hand hand, QwtDialNeedle *needle )
|
---|
126 | {
|
---|
127 | if ( hand >= 0 && hand < NHands )
|
---|
128 | {
|
---|
129 | delete d_hand[hand];
|
---|
130 | d_hand[hand] = needle;
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | /*!
|
---|
135 | \return Clock hand
|
---|
136 | \param hd Specifies the type of hand
|
---|
137 | \sa setHand()
|
---|
138 | */
|
---|
139 | QwtDialNeedle *QwtAnalogClock::hand( Hand hd )
|
---|
140 | {
|
---|
141 | if ( hd < 0 || hd >= NHands )
|
---|
142 | return NULL;
|
---|
143 |
|
---|
144 | return d_hand[hd];
|
---|
145 | }
|
---|
146 |
|
---|
147 | /*!
|
---|
148 | \return Clock hand
|
---|
149 | \param hd Specifies the type of hand
|
---|
150 | \sa setHand()
|
---|
151 | */
|
---|
152 | const QwtDialNeedle *QwtAnalogClock::hand( Hand hd ) const
|
---|
153 | {
|
---|
154 | return const_cast<QwtAnalogClock *>( this )->hand( hd );
|
---|
155 | }
|
---|
156 |
|
---|
157 | /*!
|
---|
158 | \brief Set the current time
|
---|
159 | */
|
---|
160 | void QwtAnalogClock::setCurrentTime()
|
---|
161 | {
|
---|
162 | setTime( QTime::currentTime() );
|
---|
163 | }
|
---|
164 |
|
---|
165 | /*!
|
---|
166 | Set a time
|
---|
167 | \param time Time to display
|
---|
168 | */
|
---|
169 | void QwtAnalogClock::setTime( const QTime &time )
|
---|
170 | {
|
---|
171 | if ( time.isValid() )
|
---|
172 | {
|
---|
173 | setValue( ( time.hour() % 12 ) * 60.0 * 60.0
|
---|
174 | + time.minute() * 60.0 + time.second() );
|
---|
175 | }
|
---|
176 | else
|
---|
177 | setValid( false );
|
---|
178 | }
|
---|
179 |
|
---|
180 | /*!
|
---|
181 | \brief Draw the needle
|
---|
182 |
|
---|
183 | A clock has no single needle but three hands instead. drawNeedle()
|
---|
184 | translates value() into directions for the hands and calls
|
---|
185 | drawHand().
|
---|
186 |
|
---|
187 | \param painter Painter
|
---|
188 | \param center Center of the clock
|
---|
189 | \param radius Maximum length for the hands
|
---|
190 | \param dir Dummy, not used.
|
---|
191 | \param colorGroup ColorGroup
|
---|
192 |
|
---|
193 | \sa drawHand()
|
---|
194 | */
|
---|
195 | void QwtAnalogClock::drawNeedle( QPainter *painter, const QPointF ¢er,
|
---|
196 | double radius, double dir, QPalette::ColorGroup colorGroup ) const
|
---|
197 | {
|
---|
198 | Q_UNUSED( dir );
|
---|
199 |
|
---|
200 | if ( isValid() )
|
---|
201 | {
|
---|
202 | const double hours = value() / ( 60.0 * 60.0 );
|
---|
203 | const double minutes =
|
---|
204 | ( value() - qFloor(hours) * 60.0 * 60.0 ) / 60.0;
|
---|
205 | const double seconds = value() - qFloor(hours) * 60.0 * 60.0
|
---|
206 | - qFloor(minutes) * 60.0;
|
---|
207 |
|
---|
208 | double angle[NHands];
|
---|
209 | angle[HourHand] = 360.0 * hours / 12.0;
|
---|
210 | angle[MinuteHand] = 360.0 * minutes / 60.0;
|
---|
211 | angle[SecondHand] = 360.0 * seconds / 60.0;
|
---|
212 |
|
---|
213 | for ( int hand = 0; hand < NHands; hand++ )
|
---|
214 | {
|
---|
215 | const double d = 360.0 - angle[hand] - origin();
|
---|
216 | drawHand( painter, static_cast<Hand>( hand ),
|
---|
217 | center, radius, d, colorGroup );
|
---|
218 | }
|
---|
219 | }
|
---|
220 | }
|
---|
221 |
|
---|
222 | /*!
|
---|
223 | Draw a clock hand
|
---|
224 |
|
---|
225 | \param painter Painter
|
---|
226 | \param hd Specify the type of hand
|
---|
227 | \param center Center of the clock
|
---|
228 | \param radius Maximum length for the hands
|
---|
229 | \param direction Direction of the hand in degrees, counter clockwise
|
---|
230 | \param cg ColorGroup
|
---|
231 | */
|
---|
232 | void QwtAnalogClock::drawHand( QPainter *painter, Hand hd,
|
---|
233 | const QPointF ¢er, double radius, double direction,
|
---|
234 | QPalette::ColorGroup cg ) const
|
---|
235 | {
|
---|
236 | const QwtDialNeedle *needle = hand( hd );
|
---|
237 | if ( needle )
|
---|
238 | {
|
---|
239 | if ( hd == HourHand )
|
---|
240 | radius = qRound( 0.8 * radius );
|
---|
241 |
|
---|
242 | needle->draw( painter, center, radius, direction, cg );
|
---|
243 | }
|
---|
244 | }
|
---|