qwt_analog_clock.cpp
Go to the documentation of this file.
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_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 
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 
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 
101  QwtDialSimpleNeedle::Arrow, true, handColor, knobColor );
102  hand->setWidth( width );
103 
104  d_hand[i] = NULL;
105  setHand( static_cast<Hand>( i ), hand );
106  }
107 }
108 
111 {
112  for ( int i = 0; i < NHands; i++ )
113  delete d_hand[i];
114 }
115 
121 {
122  // no op
123  return;
124 }
125 
133 {
134  if ( hand >= 0 && hand < NHands )
135  {
136  delete d_hand[hand];
137  d_hand[hand] = needle;
138  }
139 }
140 
147 {
148  if ( hd < 0 || hd >= NHands )
149  return NULL;
150 
151  return d_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
void setNeedle(QwtDialNeedle *)
virtual ~QwtAnalogClock()
Destructor.
void setScale(double lowerBound, double upperBound)
Specify a scale.
virtual void draw(QPainter *, const QPointF &center, double length, double direction, QPalette::ColorGroup=QPalette::Active) const
enum MQTTPropertyCodes value
void setHand(Hand, QwtDialNeedle *)
Needle displaying the minutes.
QwtDialNeedle * d_hand[NHands]
MQTTClient d
Definition: test10.c:1656
void setTicks(int tickType, const QList< double > &)
void setValue(double value)
An analog clock.
virtual void setOrigin(double)
Change the origin.
Definition: qwt_dial.cpp:660
A class representing a scale division.
Definition: qwt_scale_div.h:33
void setCurrentTime()
Set the current time.
QwtAnalogClock(QWidget *parent=NULL)
QwtDial class provides a rounded range control.
Definition: qwt_dial.h:50
void setInterval(double lowerBound, double upperBound)
double value() const
Base class for needles that can be used in a QwtDial.
double origin() const
const QwtScaleDiv & scaleDiv() const
void setTotalSteps(uint)
Set the number of steps.
const QwtDialNeedle * needle() const
Definition: qwt_dial.cpp:538
A class representing a text.
Definition: qwt_text.h:51
Number of needles.
Needle displaying the hours.
Backbone = the line where the ticks are located.
virtual QwtText label(double) const
Convert a value into its representing label.
A needle for dial widgets.
void setTickLength(QwtScaleDiv::TickType, double length)
#define QWT_FINAL
Definition: qwt_global.h:57
void enableComponent(ScaleComponent, bool enable=true)
A class for drawing round scales.
void setWidth(double width)
float time
Definition: mqtt_test.py:17
void setScaleDraw(QwtRoundScaleDraw *)
Definition: qwt_dial.cpp:574
virtual void drawNeedle(QPainter *, const QPointF &, double radius, double direction, QPalette::ColorGroup) const QWT_OVERRIDE
Draw the needle.
const QwtDialNeedle * hand(Hand) const
void setPenWidthF(qreal width)
Specify the width of the scale pen.
virtual void drawHand(QPainter *, Hand, const QPointF &, double radius, double direction, QPalette::ColorGroup) const
void setSpacing(double)
Set the spacing between tick and labels.
Needle displaying the seconds.
#define QWT_OVERRIDE
Definition: qwt_global.h:53
void setTime(const QTime &)


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 03:48:10