Qwt User's Guide: qwt_analog_clock.cpp Source File

Qwt User's Guide 6.2.0

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
19 namespace
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
51 QwtAnalogClock::QwtAnalogClock( QWidget* parent )
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
75 QwtScaleDiv scaleDiv;
76 scaleDiv.setInterval( 0.0, 12.0 * secondsPerHour );
77 scaleDiv.setTicks( QwtScaleDiv::MajorTick, majorTicks );
78 scaleDiv.setTicks( QwtScaleDiv::MinorTick, minorTicks );
79 setScale( scaleDiv );
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
100 QwtDialSimpleNeedle* hand = new QwtDialSimpleNeedle(
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
110 QwtAnalogClock::~QwtAnalogClock()
111 {
112 for ( int i = 0; i < NHands; i++ )
113 delete m_hand[i];
114 }
115
120 void QwtAnalogClock::setNeedle( QwtDialNeedle* )
121 {
122 // no op
123 return;
124 }
125
132 void QwtAnalogClock::setHand( Hand hand, QwtDialNeedle* needle )
133 {
134 if ( hand >= 0 && hand < NHands )
135 {
136 delete m_hand[hand];
137 m_hand[hand] = needle;
138 }
139 }
140
146 QwtDialNeedle* QwtAnalogClock::hand( Hand hd )
147 {
148 if ( hd < 0 || hd >= NHands )
149 return NULL;
150
151 return m_hand[hd];
152 }
153
159 const QwtDialNeedle* QwtAnalogClock::hand( Hand hd ) const
160 {
161 return const_cast< QwtAnalogClock* >( this )->hand( hd );
162 }
163
167 void QwtAnalogClock::setCurrentTime()
168 {
169 setTime( QTime::currentTime() );
170 }
171
176 void 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
202 void 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
239 void 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 #if QWT_MOC_INCLUDE
254 #include "moc_qwt_analog_clock.cpp"
255 #endif
QList< double >
QwtAbstractScaleDraw::setSpacing
void setSpacing(double)
Set the spacing between tick and labels.
Definition: qwt_abstract_scale_draw.cpp:254
QwtAbstractScaleDraw::Backbone
@ Backbone
Backbone = the line where the ticks are located.
Definition: qwt_abstract_scale_draw.h:42
QwtAbstractScaleDraw::setPenWidthF
void setPenWidthF(qreal width)
Specify the width of the scale pen.
Definition: qwt_abstract_scale_draw.cpp:144
QwtAbstractScaleDraw::setTickLength
void setTickLength(QwtScaleDiv::TickType, double length)
Definition: qwt_abstract_scale_draw.cpp:315
QwtAbstractScaleDraw::label
virtual QwtText label(double) const
Convert a value into its representing label.
Definition: qwt_abstract_scale_draw.cpp:375
QwtAbstractScaleDraw::enableComponent
void enableComponent(ScaleComponent, bool enable=true)
Definition: qwt_abstract_scale_draw.cpp:79
QwtAbstractScale::scaleDiv
const QwtScaleDiv & scaleDiv() const
Definition: qwt_abstract_scale.cpp:349
QwtAbstractScale::setScale
void setScale(double lowerBound, double upperBound)
Specify a scale.
Definition: qwt_abstract_scale.cpp:130
QwtAbstractSlider::setTotalSteps
void setTotalSteps(uint)
Set the number of steps.
Definition: qwt_abstract_slider.cpp:474
QwtAbstractSlider::setValid
void setValid(bool)
Definition: qwt_abstract_slider.cpp:113
QwtAbstractSlider::setValue
void setValue(double value)
Definition: qwt_abstract_slider.cpp:570
QwtAbstractSlider::setReadOnly
void setReadOnly(bool)
Definition: qwt_abstract_slider.cpp:141
QwtAbstractSlider::isValid
bool isValid() const
Definition: qwt_abstract_slider.cpp:125
QwtAbstractSlider::value
double value() const
Returns the current value.
Definition: qwt_abstract_slider.cpp:587
QwtAbstractSlider::setWrapping
void setWrapping(bool)
Definition: qwt_abstract_slider.cpp:599
QwtAnalogClock
An analog clock.
Definition: qwt_analog_clock.h:44
QwtAnalogClock::setCurrentTime
void setCurrentTime()
Set the current time.
Definition: qwt_analog_clock.cpp:167
QwtAnalogClock::~QwtAnalogClock
virtual ~QwtAnalogClock()
Destructor.
Definition: qwt_analog_clock.cpp:110
QwtAnalogClock::hand
const QwtDialNeedle * hand(Hand) const
Definition: qwt_analog_clock.cpp:159
QwtAnalogClock::setHand
void setHand(Hand, QwtDialNeedle *)
Definition: qwt_analog_clock.cpp:132
QwtAnalogClock::drawHand
virtual void drawHand(QPainter *, Hand, const QPointF &, double radius, double direction, QPalette::ColorGroup) const
Definition: qwt_analog_clock.cpp:239
QwtAnalogClock::setTime
void setTime(const QTime &)
Definition: qwt_analog_clock.cpp:176
QwtAnalogClock::Hand
Hand
Definition: qwt_analog_clock.h:53
QwtAnalogClock::NHands
@ NHands
Number of needles.
Definition: qwt_analog_clock.h:64
QwtAnalogClock::MinuteHand
@ MinuteHand
Needle displaying the minutes.
Definition: qwt_analog_clock.h:58
QwtAnalogClock::HourHand
@ HourHand
Needle displaying the hours.
Definition: qwt_analog_clock.h:61
QwtAnalogClock::SecondHand
@ SecondHand
Needle displaying the seconds.
Definition: qwt_analog_clock.h:55
QwtAnalogClock::drawNeedle
virtual void drawNeedle(QPainter *, const QPointF &, double radius, double direction, QPalette::ColorGroup) const override
Draw the needle.
Definition: qwt_analog_clock.cpp:202
QwtAnalogClock::QwtAnalogClock
QwtAnalogClock(QWidget *parent=NULL)
Definition: qwt_analog_clock.cpp:51
QwtDial
QwtDial class provides a rounded range control.
Definition: qwt_dial.h:51
QwtDial::setScaleDraw
void setScaleDraw(QwtRoundScaleDraw *)
Definition: qwt_dial.cpp:574
QwtDial::setOrigin
virtual void setOrigin(double)
Change the origin.
Definition: qwt_dial.cpp:660
QwtDial::origin
double origin() const
Definition: qwt_dial.cpp:674
QwtDial::needle
const QwtDialNeedle * needle() const
Definition: qwt_dial.cpp:538
QwtDialNeedle
Base class for needles that can be used in a QwtDial.
Definition: qwt_dial_needle.h:28
QwtDialNeedle::draw
virtual void draw(QPainter *, const QPointF &center, double length, double direction, QPalette::ColorGroup=QPalette::Active) const
Definition: qwt_dial_needle.cpp:219
QwtDialSimpleNeedle
A needle for dial widgets.
Definition: qwt_dial_needle.h:82
QwtDialSimpleNeedle::Arrow
@ Arrow
Arrow.
Definition: qwt_dial_needle.h:88
QwtRoundScaleDraw
A class for drawing round scales.
Definition: qwt_round_scale_draw.h:32
QwtScaleDiv
A class representing a scale division.
Definition: qwt_scale_div.h:34
QwtScaleDiv::setInterval
void setInterval(double lowerBound, double upperBound)
Definition: qwt_scale_div.cpp:91
QwtScaleDiv::MediumTick
@ MediumTick
Medium ticks.
Definition: qwt_scale_div.h:46
QwtScaleDiv::MinorTick
@ MinorTick
Minor ticks.
Definition: qwt_scale_div.h:43
QwtScaleDiv::MajorTick
@ MajorTick
Major ticks.
Definition: qwt_scale_div.h:49
QwtScaleDiv::setTicks
void setTicks(int tickType, const QList< double > &)
Definition: qwt_scale_div.cpp:297
QwtText
A class representing a text.
Definition: qwt_text.h:52