Qwt User's Guide 6.3.0
Loading...
Searching...
No Matches
qwt_analog_clock.cpp
1/******************************************************************************
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_dial_needle.h"
12#include "qwt_round_scale_draw.h"
13#include "qwt_text.h"
14#include "qwt_math.h"
15
16#include <qlocale.h>
17#include <qdatetime.h>
18
19namespace
20{
21 class QwtAnalogClockScaleDraw QWT_FINAL : public QwtRoundScaleDraw
22 {
23 public:
24 QwtAnalogClockScaleDraw()
25 {
26 setSpacing( 8 );
27
28 enableComponent( QwtAbstractScaleDraw::Backbone, false );
29
30 setTickLength( QwtScaleDiv::MinorTick, 2 );
31 setTickLength( QwtScaleDiv::MediumTick, 4 );
32 setTickLength( QwtScaleDiv::MajorTick, 8 );
33
34 setPenWidthF( 1.0 );
35 }
36
37 virtual QwtText label( double value ) const QWT_OVERRIDE
38 {
39 if ( qFuzzyCompare( value + 1.0, 1.0 ) )
40 value = 60.0 * 60.0 * 12.0;
41
42 return QLocale().toString( qRound( value / ( 60.0 * 60.0 ) ) );
43 }
44 };
45}
46
52 : QwtDial( parent )
53{
54 setWrapping( true );
55 setReadOnly( true );
56
57 setOrigin( 270.0 );
58 setScaleDraw( new QwtAnalogClockScaleDraw() );
59
60 setTotalSteps( 60 );
61
62 const int secondsPerHour = 60.0 * 60.0;
63
64 QList< double > majorTicks;
65 QList< double > minorTicks;
66
67 for ( int i = 0; i < 12; i++ )
68 {
69 majorTicks += i * secondsPerHour;
70
71 for ( int j = 1; j < 5; j++ )
72 minorTicks += i * secondsPerHour + j * secondsPerHour / 5.0;
73 }
74
76 scaleDiv.setInterval( 0.0, 12.0 * secondsPerHour );
80
81 QColor knobColor = palette().color( QPalette::Active, QPalette::Text );
82 knobColor = knobColor.darker( 120 );
83
84 QColor handColor;
85 int width;
86
87 for ( int i = 0; i < NHands; i++ )
88 {
89 if ( i == SecondHand )
90 {
91 width = 2;
92 handColor = knobColor.darker( 120 );
93 }
94 else
95 {
96 width = 8;
97 handColor = knobColor;
98 }
99
101 QwtDialSimpleNeedle::Arrow, true, handColor, knobColor );
102 hand->setWidth( width );
103
104 m_hand[i] = NULL;
105 setHand( static_cast< Hand >( i ), hand );
106 }
107}
108
111{
112 for ( int i = 0; i < NHands; i++ )
113 delete m_hand[i];
114}
115
120void QwtAnalogClock::setNeedle( QwtDialNeedle* )
121{
122 // no op
123 return;
124}
125
133{
134 if ( hand >= 0 && hand < NHands )
135 {
136 delete m_hand[hand];
137 m_hand[hand] = needle;
138 }
139}
140
147{
148 if ( hd < 0 || hd >= NHands )
149 return NULL;
150
151 return m_hand[hd];
152}
153
160{
161 return const_cast< QwtAnalogClock* >( this )->hand( hd );
162}
163
168{
169 setTime( QTime::currentTime() );
170}
171
176void QwtAnalogClock::setTime( const QTime& time )
177{
178 if ( time.isValid() )
179 {
180 setValue( ( time.hour() % 12 ) * 60.0 * 60.0
181 + time.minute() * 60.0 + time.second() );
182 }
183 else
184 setValid( false );
185}
186
202void QwtAnalogClock::drawNeedle( QPainter* painter, const QPointF& center,
203 double radius, double direction, QPalette::ColorGroup colorGroup ) const
204{
205 Q_UNUSED( direction );
206
207 if ( isValid() )
208 {
209 const double hours = value() / ( 60.0 * 60.0 );
210 const double minutes =
211 ( value() - std::floor(hours) * 60.0 * 60.0 ) / 60.0;
212 const double seconds = value() - std::floor(hours) * 60.0 * 60.0
213 - std::floor(minutes) * 60.0;
214
215 double angle[NHands];
216 angle[HourHand] = 360.0 * hours / 12.0;
217 angle[MinuteHand] = 360.0 * minutes / 60.0;
218 angle[SecondHand] = 360.0 * seconds / 60.0;
219
220 for ( int hand = 0; hand < NHands; hand++ )
221 {
222 const double d = 360.0 - angle[hand] - origin();
223 drawHand( painter, static_cast< Hand >( hand ),
224 center, radius, d, colorGroup );
225 }
226 }
227}
228
239void QwtAnalogClock::drawHand( QPainter* painter, Hand hd,
240 const QPointF& center, double radius, double direction,
241 QPalette::ColorGroup cg ) const
242{
243 const QwtDialNeedle* needle = hand( hd );
244 if ( needle )
245 {
246 if ( hd == HourHand )
247 radius = qRound( 0.8 * radius );
248
249 needle->draw( painter, center, radius, direction, cg );
250 }
251}
252
253#include "moc_qwt_analog_clock.cpp"
@ Backbone
Backbone = the line where the ticks are located.
const QwtScaleDiv & scaleDiv() const
void setScale(double lowerBound, double upperBound)
Specify a scale.
void setTotalSteps(uint)
Set the number of steps.
void setValue(double value)
double value() const
Returns the current value.
An analog clock.
void setCurrentTime()
Set the current time.
virtual ~QwtAnalogClock()
Destructor.
const QwtDialNeedle * hand(Hand) const
void setHand(Hand, QwtDialNeedle *)
virtual void drawHand(QPainter *, Hand, const QPointF &, double radius, double direction, QPalette::ColorGroup) const
void setTime(const QTime &)
@ NHands
Number of needles.
@ MinuteHand
Needle displaying the minutes.
@ HourHand
Needle displaying the hours.
@ SecondHand
Needle displaying the seconds.
virtual void drawNeedle(QPainter *, const QPointF &, double radius, double direction, QPalette::ColorGroup) const override
Draw the needle.
QwtAnalogClock(QWidget *parent=NULL)
QwtDial class provides a rounded range control.
Definition qwt_dial.h:51
void setScaleDraw(QwtRoundScaleDraw *)
Definition qwt_dial.cpp:574
virtual void setOrigin(double)
Change the origin.
Definition qwt_dial.cpp:660
double origin() const
Definition qwt_dial.cpp:674
const QwtDialNeedle * needle() const
Definition qwt_dial.cpp:538
Base class for needles that can be used in a QwtDial.
virtual void draw(QPainter *, const QPointF &center, double length, double direction, QPalette::ColorGroup=QPalette::Active) const
A needle for dial widgets.
A class for drawing round scales.
A class representing a scale division.
void setInterval(double lowerBound, double upperBound)
@ MediumTick
Medium ticks.
@ MinorTick
Minor ticks.
@ MajorTick
Major ticks.
void setTicks(int tickType, const QList< double > &)
A class representing a text.
Definition qwt_text.h:52