qwt_analog_clock.cpp
Go to the documentation of this file.
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 
29 
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 );
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 
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 
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 
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
QwtRoundScaleDraw
A class for drawing round scales.
Definition: qwt_round_scale_draw.h:31
QWT_FINAL
#define QWT_FINAL
Definition: qwt_global.h:57
QwtDial::needle
const QwtDialNeedle * needle() const
Definition: qwt_dial.cpp:538
QwtAbstractSlider::setWrapping
void setWrapping(bool)
Definition: qwt_abstract_slider.cpp:599
QwtDialNeedle
Base class for needles that can be used in a QwtDial.
Definition: qwt_dial_needle.h:27
QwtDial
QwtDial class provides a rounded range control.
Definition: qwt_dial.h:50
QwtAnalogClock::HourHand
@ HourHand
Needle displaying the hours.
Definition: qwt_analog_clock.h:61
QwtAnalogClock::drawNeedle
virtual void drawNeedle(QPainter *, const QPointF &, double radius, double direction, QPalette::ColorGroup) const QWT_OVERRIDE
Draw the needle.
Definition: qwt_analog_clock.cpp:202
QwtAbstractScaleDraw::setSpacing
void setSpacing(double)
Set the spacing between tick and labels.
Definition: qwt_abstract_scale_draw.cpp:254
qwt_analog_clock.h
QwtScaleDiv::MediumTick
@ MediumTick
Medium ticks.
Definition: qwt_scale_div.h:46
QwtAbstractSlider::setReadOnly
void setReadOnly(bool)
Definition: qwt_abstract_slider.cpp:141
QwtDialSimpleNeedle::Arrow
@ Arrow
Arrow.
Definition: qwt_dial_needle.h:88
QwtAnalogClock::~QwtAnalogClock
virtual ~QwtAnalogClock()
Destructor.
Definition: qwt_analog_clock.cpp:110
QwtAnalogClock::m_hand
QwtDialNeedle * m_hand[NHands]
Definition: qwt_analog_clock.h:90
QwtAbstractSlider::setValid
void setValid(bool)
Definition: qwt_abstract_slider.cpp:113
QwtAbstractSlider::setValue
void setValue(double value)
Definition: qwt_abstract_slider.cpp:570
QwtAnalogClock::setHand
void setHand(Hand, QwtDialNeedle *)
Definition: qwt_analog_clock.cpp:132
QList< double >
qwt_math.h
QwtScaleDiv::setInterval
void setInterval(double lowerBound, double upperBound)
Definition: qwt_scale_div.cpp:91
QwtAnalogClock::Hand
Hand
Definition: qwt_analog_clock.h:52
QwtAnalogClock::hand
const QwtDialNeedle * hand(Hand) const
Definition: qwt_analog_clock.cpp:159
QwtAnalogClock::SecondHand
@ SecondHand
Needle displaying the seconds.
Definition: qwt_analog_clock.h:55
QwtDialNeedle::draw
virtual void draw(QPainter *, const QPointF &center, double length, double direction, QPalette::ColorGroup=QPalette::Active) const
Definition: qwt_dial_needle.cpp:219
QwtScaleDiv::MajorTick
@ MajorTick
Major ticks.
Definition: qwt_scale_div.h:49
QwtAbstractScaleDraw::Backbone
@ Backbone
Backbone = the line where the ticks are located.
Definition: qwt_abstract_scale_draw.h:42
QwtDial::setScaleDraw
void setScaleDraw(QwtRoundScaleDraw *)
Definition: qwt_dial.cpp:574
QwtAnalogClock::setTime
void setTime(const QTime &)
Definition: qwt_analog_clock.cpp:176
QwtDial::setOrigin
virtual void setOrigin(double)
Change the origin.
Definition: qwt_dial.cpp:660
QwtAbstractScaleDraw::enableComponent
void enableComponent(ScaleComponent, bool enable=true)
Definition: qwt_abstract_scale_draw.cpp:79
QwtText
A class representing a text.
Definition: qwt_text.h:51
QwtDialSimpleNeedle
A needle for dial widgets.
Definition: qwt_dial_needle.h:81
QwtAnalogClock::setCurrentTime
void setCurrentTime()
Set the current time.
Definition: qwt_analog_clock.cpp:167
QwtAbstractScale::scaleDiv
const QwtScaleDiv & scaleDiv() const
Definition: qwt_abstract_scale.cpp:349
QwtAbstractScaleDraw::setTickLength
void setTickLength(QwtScaleDiv::TickType, double length)
Definition: qwt_abstract_scale_draw.cpp:315
QwtAbstractSlider::setTotalSteps
void setTotalSteps(uint)
Set the number of steps.
Definition: qwt_abstract_slider.cpp:474
QwtAnalogClock::MinuteHand
@ MinuteHand
Needle displaying the minutes.
Definition: qwt_analog_clock.h:58
mqtt_test.time
float time
Definition: mqtt_test.py:17
QwtAbstractScaleDraw::label
virtual QwtText label(double) const
Convert a value into its representing label.
Definition: qwt_abstract_scale_draw.cpp:375
d
d
QwtAbstractSlider::isValid
bool isValid() const
Definition: qwt_abstract_slider.cpp:125
QwtAbstractSlider::value
double value
Returns the current value.
Definition: qwt_abstract_slider.h:43
QwtAnalogClock::NHands
@ NHands
Number of needles.
Definition: qwt_analog_clock.h:64
nlohmann::detail::parse_event_t::value
@ value
the parser finished reading a JSON value
qwt_dial_needle.h
QwtAnalogClock::setNeedle
void setNeedle(QwtDialNeedle *)
Definition: qwt_analog_clock.cpp:120
QwtAbstractScaleDraw::setPenWidthF
void setPenWidthF(qreal width)
Specify the width of the scale pen.
Definition: qwt_abstract_scale_draw.cpp:144
QwtAbstractScale::setScale
void setScale(double lowerBound, double upperBound)
Specify a scale.
Definition: qwt_abstract_scale.cpp:130
QwtScaleDiv::setTicks
void setTicks(int tickType, const QList< double > &)
Definition: qwt_scale_div.cpp:297
qwt_round_scale_draw.h
QWT_OVERRIDE
#define QWT_OVERRIDE
Definition: qwt_global.h:53
QwtAnalogClock::QwtAnalogClock
QwtAnalogClock(QWidget *parent=NULL)
Definition: qwt_analog_clock.cpp:51
QwtScaleDiv::MinorTick
@ MinorTick
Minor ticks.
Definition: qwt_scale_div.h:43
QwtScaleDiv
A class representing a scale division.
Definition: qwt_scale_div.h:33
qwt_text.h
QwtAnalogClock::drawHand
virtual void drawHand(QPainter *, Hand, const QPointF &, double radius, double direction, QPalette::ColorGroup) const
Definition: qwt_analog_clock.cpp:239
QwtDial::origin
double origin
Definition: qwt_dial.h:59
QwtAnalogClock
An analog clock.
Definition: qwt_analog_clock.h:43


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:45