Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "qwt_analog_clock.h"
00011 #include "qwt_round_scale_draw.h"
00012 #include <qmath.h>
00013 #include <qlocale.h>
00014
00015 class QwtAnalogClockScaleDraw: public QwtRoundScaleDraw
00016 {
00017 public:
00018 QwtAnalogClockScaleDraw()
00019 {
00020 setSpacing( 8 );
00021
00022 enableComponent( QwtAbstractScaleDraw::Backbone, false );
00023
00024 setTickLength( QwtScaleDiv::MinorTick, 2 );
00025 setTickLength( QwtScaleDiv::MediumTick, 4 );
00026 setTickLength( QwtScaleDiv::MajorTick, 8 );
00027
00028 setPenWidth( 1 );
00029 }
00030
00031 virtual QwtText label( double value ) const
00032 {
00033 if ( qFuzzyCompare( value + 1.0, 1.0 ) )
00034 value = 60.0 * 60.0 * 12.0;
00035
00036 return QLocale().toString( qRound( value / ( 60.0 * 60.0 ) ) );
00037 }
00038 };
00039
00044 QwtAnalogClock::QwtAnalogClock( QWidget *parent ):
00045 QwtDial( parent )
00046 {
00047 setWrapping( true );
00048 setReadOnly( true );
00049
00050 setOrigin( 270.0 );
00051 setScaleDraw( new QwtAnalogClockScaleDraw() );
00052
00053 setTotalSteps( 60 );
00054
00055 const int secondsPerHour = 60.0 * 60.0;
00056
00057 QList<double> majorTicks;
00058 QList<double> minorTicks;
00059
00060 for ( int i = 0; i < 12; i++ )
00061 {
00062 majorTicks += i * secondsPerHour;
00063
00064 for ( int j = 1; j < 5; j++ )
00065 minorTicks += i * secondsPerHour + j * secondsPerHour / 5.0;
00066 }
00067
00068 QwtScaleDiv scaleDiv;
00069 scaleDiv.setInterval( 0.0, 12.0 * secondsPerHour );
00070 scaleDiv.setTicks( QwtScaleDiv::MajorTick, majorTicks );
00071 scaleDiv.setTicks( QwtScaleDiv::MinorTick, minorTicks );
00072 setScale( scaleDiv );
00073
00074 QColor knobColor = palette().color( QPalette::Active, QPalette::Text );
00075 knobColor = knobColor.dark( 120 );
00076
00077 QColor handColor;
00078 int width;
00079
00080 for ( int i = 0; i < NHands; i++ )
00081 {
00082 if ( i == SecondHand )
00083 {
00084 width = 2;
00085 handColor = knobColor.dark( 120 );
00086 }
00087 else
00088 {
00089 width = 8;
00090 handColor = knobColor;
00091 }
00092
00093 QwtDialSimpleNeedle *hand = new QwtDialSimpleNeedle(
00094 QwtDialSimpleNeedle::Arrow, true, handColor, knobColor );
00095 hand->setWidth( width );
00096
00097 d_hand[i] = NULL;
00098 setHand( static_cast<Hand>( i ), hand );
00099 }
00100 }
00101
00103 QwtAnalogClock::~QwtAnalogClock()
00104 {
00105 for ( int i = 0; i < NHands; i++ )
00106 delete d_hand[i];
00107 }
00108
00113 void QwtAnalogClock::setNeedle( QwtDialNeedle * )
00114 {
00115
00116 return;
00117 }
00118
00125 void QwtAnalogClock::setHand( Hand hand, QwtDialNeedle *needle )
00126 {
00127 if ( hand >= 0 && hand < NHands )
00128 {
00129 delete d_hand[hand];
00130 d_hand[hand] = needle;
00131 }
00132 }
00133
00139 QwtDialNeedle *QwtAnalogClock::hand( Hand hd )
00140 {
00141 if ( hd < 0 || hd >= NHands )
00142 return NULL;
00143
00144 return d_hand[hd];
00145 }
00146
00152 const QwtDialNeedle *QwtAnalogClock::hand( Hand hd ) const
00153 {
00154 return const_cast<QwtAnalogClock *>( this )->hand( hd );
00155 }
00156
00160 void QwtAnalogClock::setCurrentTime()
00161 {
00162 setTime( QTime::currentTime() );
00163 }
00164
00169 void QwtAnalogClock::setTime( const QTime &time )
00170 {
00171 if ( time.isValid() )
00172 {
00173 setValue( ( time.hour() % 12 ) * 60.0 * 60.0
00174 + time.minute() * 60.0 + time.second() );
00175 }
00176 else
00177 setValid( false );
00178 }
00179
00195 void QwtAnalogClock::drawNeedle( QPainter *painter, const QPointF ¢er,
00196 double radius, double dir, QPalette::ColorGroup colorGroup ) const
00197 {
00198 Q_UNUSED( dir );
00199
00200 if ( isValid() )
00201 {
00202 const double hours = value() / ( 60.0 * 60.0 );
00203 const double minutes =
00204 ( value() - qFloor(hours) * 60.0 * 60.0 ) / 60.0;
00205 const double seconds = value() - qFloor(hours) * 60.0 * 60.0
00206 - qFloor(minutes) * 60.0;
00207
00208 double angle[NHands];
00209 angle[HourHand] = 360.0 * hours / 12.0;
00210 angle[MinuteHand] = 360.0 * minutes / 60.0;
00211 angle[SecondHand] = 360.0 * seconds / 60.0;
00212
00213 for ( int hand = 0; hand < NHands; hand++ )
00214 {
00215 const double d = 360.0 - angle[hand] - origin();
00216 drawHand( painter, static_cast<Hand>( hand ),
00217 center, radius, d, colorGroup );
00218 }
00219 }
00220 }
00221
00232 void QwtAnalogClock::drawHand( QPainter *painter, Hand hd,
00233 const QPointF ¢er, double radius, double direction,
00234 QPalette::ColorGroup cg ) const
00235 {
00236 const QwtDialNeedle *needle = hand( hd );
00237 if ( needle )
00238 {
00239 if ( hd == HourHand )
00240 radius = qRound( 0.8 * radius );
00241
00242 needle->draw( painter, center, radius, direction, cg );
00243 }
00244 }