qwt_analog_clock.cpp
/******************************************************************************
* Qwt Widget Library
* Copyright (C) 1997 Josef Wilgen
* Copyright (C) 2002 Uwe Rathmann
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the Qwt License, Version 1.0
*****************************************************************************/
#include "qwt_analog_clock.h"
#include "qwt_dial_needle.h"
#include "qwt_round_scale_draw.h"
#include "qwt_text.h"
#include "qwt_math.h"
#include <qlocale.h>
#include <qdatetime.h>
namespace
{
class QwtAnalogClockScaleDraw QWT_FINAL : public QwtRoundScaleDraw
{
public:
QwtAnalogClockScaleDraw()
{
setSpacing( 8 );
enableComponent( QwtAbstractScaleDraw::Backbone, false );
setTickLength( QwtScaleDiv::MinorTick, 2 );
setTickLength( QwtScaleDiv::MediumTick, 4 );
setTickLength( QwtScaleDiv::MajorTick, 8 );
setPenWidthF( 1.0 );
}
virtual QwtText label( double value ) const QWT_OVERRIDE
{
if ( qFuzzyCompare( value + 1.0, 1.0 ) )
value = 60.0 * 60.0 * 12.0;
return QLocale().toString( qRound( value / ( 60.0 * 60.0 ) ) );
}
};
}
QwtAnalogClock::QwtAnalogClock( QWidget* parent )
: QwtDial( parent )
{
setWrapping( true );
setReadOnly( true );
setOrigin( 270.0 );
setScaleDraw( new QwtAnalogClockScaleDraw() );
setTotalSteps( 60 );
const int secondsPerHour = 60.0 * 60.0;
QList< double > majorTicks;
QList< double > minorTicks;
for ( int i = 0; i < 12; i++ )
{
majorTicks += i * secondsPerHour;
for ( int j = 1; j < 5; j++ )
minorTicks += i * secondsPerHour + j * secondsPerHour / 5.0;
}
QwtScaleDiv scaleDiv;
scaleDiv.setInterval( 0.0, 12.0 * secondsPerHour );
scaleDiv.setTicks( QwtScaleDiv::MajorTick, majorTicks );
scaleDiv.setTicks( QwtScaleDiv::MinorTick, minorTicks );
setScale( scaleDiv );
QColor knobColor = palette().color( QPalette::Active, QPalette::Text );
knobColor = knobColor.darker( 120 );
QColor handColor;
int width;
for ( int i = 0; i < NHands; i++ )
{
if ( i == SecondHand )
{
width = 2;
handColor = knobColor.darker( 120 );
}
else
{
width = 8;
handColor = knobColor;
}
QwtDialSimpleNeedle* hand = new QwtDialSimpleNeedle(
QwtDialSimpleNeedle::Arrow, true, handColor, knobColor );
hand->setWidth( width );
m_hand[i] = NULL;
setHand( static_cast< Hand >( i ), hand );
}
}
QwtAnalogClock::~QwtAnalogClock()
{
for ( int i = 0; i < NHands; i++ )
delete m_hand[i];
}
void QwtAnalogClock::setNeedle( QwtDialNeedle* )
{
// no op
return;
}
void QwtAnalogClock::setHand( Hand hand, QwtDialNeedle* needle )
{
if ( hand >= 0 && hand < NHands )
{
delete m_hand[hand];
m_hand[hand] = needle;
}
}
QwtDialNeedle* QwtAnalogClock::hand( Hand hd )
{
if ( hd < 0 || hd >= NHands )
return NULL;
return m_hand[hd];
}
const QwtDialNeedle* QwtAnalogClock::hand( Hand hd ) const
{
return const_cast< QwtAnalogClock* >( this )->hand( hd );
}
void QwtAnalogClock::setCurrentTime()
{
setTime( QTime::currentTime() );
}
void QwtAnalogClock::setTime( const QTime& time )
{
if ( time.isValid() )
{
setValue( ( time.hour() % 12 ) * 60.0 * 60.0
+ time.minute() * 60.0 + time.second() );
}
else
setValid( false );
}
void QwtAnalogClock::drawNeedle( QPainter* painter, const QPointF& center,
double radius, double direction, QPalette::ColorGroup colorGroup ) const
{
Q_UNUSED( direction );
if ( isValid() )
{
const double hours = value() / ( 60.0 * 60.0 );
const double minutes =
( value() - std::floor(hours) * 60.0 * 60.0 ) / 60.0;
const double seconds = value() - std::floor(hours) * 60.0 * 60.0
- std::floor(minutes) * 60.0;
double angle[NHands];
angle[HourHand] = 360.0 * hours / 12.0;
angle[MinuteHand] = 360.0 * minutes / 60.0;
angle[SecondHand] = 360.0 * seconds / 60.0;
for ( int hand = 0; hand < NHands; hand++ )
{
const double d = 360.0 - angle[hand] - origin();
drawHand( painter, static_cast< Hand >( hand ),
center, radius, d, colorGroup );
}
}
}
void QwtAnalogClock::drawHand( QPainter* painter, Hand hd,
const QPointF& center, double radius, double direction,
QPalette::ColorGroup cg ) const
{
const QwtDialNeedle* needle = hand( hd );
if ( needle )
{
if ( hd == HourHand )
radius = qRound( 0.8 * radius );
needle->draw( painter, center, radius, direction, cg );
}
}
#if QWT_MOC_INCLUDE
#include "moc_qwt_analog_clock.cpp"
#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