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_round_scale_draw.h"
12 #include <qmath.h>
13 #include <qlocale.h>
14 
16 {
17 public:
19  {
20  setSpacing( 8 );
21 
23 
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 
44 QwtAnalogClock::QwtAnalogClock( QWidget *parent ):
45  QwtDial( parent )
46 {
47  setWrapping( true );
48  setReadOnly( true );
49 
50  setOrigin( 270.0 );
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 
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.dark( 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.dark( 120 );
86  }
87  else
88  {
89  width = 8;
90  handColor = knobColor;
91  }
92 
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 
104 {
105  for ( int i = 0; i < NHands; i++ )
106  delete d_hand[i];
107 }
108 
114 {
115  // no op
116  return;
117 }
118 
126 {
127  if ( hand >= 0 && hand < NHands )
128  {
129  delete d_hand[hand];
130  d_hand[hand] = needle;
131  }
132 }
133 
140 {
141  if ( hd < 0 || hd >= NHands )
142  return NULL;
143 
144  return d_hand[hd];
145 }
146 
153 {
154  return const_cast<QwtAnalogClock *>( this )->hand( hd );
155 }
156 
161 {
162  setTime( QTime::currentTime() );
163 }
164 
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 
195 void QwtAnalogClock::drawNeedle( QPainter *painter, const QPointF &center,
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 
232 void QwtAnalogClock::drawHand( QPainter *painter, Hand hd,
233  const QPointF &center, 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 }
d
void setNeedle(QwtDialNeedle *)
virtual ~QwtAnalogClock()
Destructor.
void setScale(double lowerBound, double upperBound)
Specify a scale.
void setHand(Hand, QwtDialNeedle *)
Needle displaying the minutes.
QwtDialNeedle * d_hand[NHands]
void setValue(double val)
void setTicks(int tickType, const QList< double > &)
An analog clock.
virtual void setOrigin(double)
Change the origin.
Definition: qwt_dial.cpp:662
void setSpacing(double margin)
Set the spacing between tick and labels.
A class representing a scale division.
Definition: qwt_scale_div.h:36
void setCurrentTime()
Set the current time.
QwtAnalogClock(QWidget *parent=NULL)
virtual void drawNeedle(QPainter *, const QPointF &, double radius, double direction, QPalette::ColorGroup) const
Draw the needle.
TFSIMD_FORCE_INLINE tfScalar angle(const Quaternion &q1, const Quaternion &q2)
QwtDial class provides a rounded range control.
Definition: qwt_dial.h:49
void setInterval(double lowerBound, double upperBound)
std::chrono::duration< std::int_fast64_t > seconds
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:540
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.
A needle for dial widgets.
void setTickLength(QwtScaleDiv::TickType, double length)
void enableComponent(ScaleComponent, bool enable=true)
A class for drawing round scales.
void setWidth(double width)
virtual QwtText label(double value) const
Convert a value into its representing label.
void setPenWidth(int width)
Specify the width of the scale pen.
void setScaleDraw(QwtRoundScaleDraw *)
Definition: qwt_dial.cpp:576
virtual void draw(QPainter *painter, const QPointF &center, double length, double direction, QPalette::ColorGroup=QPalette::Active) const
const QwtDialNeedle * hand(Hand) const
int i
virtual void drawHand(QPainter *, Hand, const QPointF &, double radius, double direction, QPalette::ColorGroup) const
Needle displaying the seconds.
void setTime(const QTime &)


plotjuggler
Author(s): Davide Faconti
autogenerated on Sat Jul 6 2019 03:44:17