Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

qwt_analog_clock.cpp

00001 #include "qwt_analog_clock.h"
00002 
00008 QwtAnalogClock::QwtAnalogClock(QWidget *parent, const char* name):
00009     QwtDial(parent, name)
00010 {
00011     setWrapping(TRUE);
00012     setReadOnly(TRUE);
00013 
00014     setOrigin(270.0);
00015     setRange(0.0, 60.0 * 60.0 * 12.0); // seconds
00016     setScale(-1, 5, 60.0 * 60.0);
00017 
00018     setScaleOptions(ScaleTicks | ScaleLabel);
00019     setScaleTicks(1, 0, 8);
00020 
00021     const QColor knobColor = 
00022         palette().color(QPalette::Active, QColorGroup::Text).dark(120);
00023 
00024     QColor handColor;
00025     int width;
00026 
00027     for ( int i = 0; i < NHands; i++ )
00028     {
00029         if ( i == SecondHand )
00030         {
00031             width = 2;
00032             handColor = knobColor.dark(120);
00033         }
00034         else
00035         {
00036             width = 8;
00037             handColor = knobColor;
00038         }
00039 
00040         QwtDialSimpleNeedle *hand = new QwtDialSimpleNeedle(
00041             QwtDialSimpleNeedle::Arrow, TRUE, handColor, knobColor);
00042         hand->setWidth(width);
00043 
00044         d_hand[i] = NULL;
00045         setHand((Hand)i, hand);
00046     }
00047 }
00048 
00050 QwtAnalogClock::~QwtAnalogClock()
00051 {
00052     for ( int i = 0; i < NHands; i++ )
00053         delete d_hand[i];
00054 }
00055 
00060 void QwtAnalogClock::setNeedle(QwtDialNeedle *)
00061 {
00062     // no op
00063     return;
00064 }
00065 
00072 void QwtAnalogClock::setHand(Hand hand, QwtDialNeedle *needle)
00073 {
00074     if ( hand >= 0 || hand < NHands )
00075     {
00076         delete d_hand[hand];
00077         d_hand[hand] = needle;
00078     }
00079 }
00080 
00086 QwtDialNeedle *QwtAnalogClock::hand(Hand hd)
00087 {
00088     if ( hd < 0 || hd >= NHands )
00089         return NULL;
00090 
00091     return d_hand[hd];
00092 }
00093 
00099 const QwtDialNeedle *QwtAnalogClock::hand(Hand hd) const
00100 {
00101     return ((QwtAnalogClock *)this)->hand(hd);
00102 }
00103 
00110 void QwtAnalogClock::setCurrentTime()
00111 { 
00112     setTime(QTime::currentTime()); 
00113 }
00114 
00119 void QwtAnalogClock::setTime(const QTime &time)
00120 {
00121     if ( time.isValid() )
00122     {
00123         setValue((time.hour() % 12) * 60.0 * 60.0 
00124             + time.minute() * 60.0 + time.second());
00125     }
00126     else
00127         setValid(FALSE);
00128 }
00129 
00136 QString QwtAnalogClock::scaleLabel(double value) const
00137 {
00138     if ( value == 0.0 )
00139         value = 60.0 * 60.0 * 12.0;
00140 
00141     QString label;
00142     label.sprintf("%d", int(value / (60.0 * 60.0)));
00143 
00144     return label;
00145 }
00146 
00162 void QwtAnalogClock::drawNeedle(QPainter *painter, const QPoint &center,
00163         int radius, double, QPalette::ColorGroup cg) const
00164 {
00165     if ( isValid() )
00166     {
00167         const double hours = value() / (60.0 * 60.0);
00168         const double minutes = (value() - (int)hours * 60.0 * 60.0) / 60.0;
00169         const double seconds = value() - (int)hours * 60.0 * 60.0 
00170             - (int)minutes * 60.0;
00171 
00172         drawHand(painter, HourHand, center, radius,
00173             360.0 - (origin() + 360.0 * hours / 12.0), cg);
00174         drawHand(painter, MinuteHand, center, radius,
00175             360.0 - (origin() + 360.0 * minutes / 60.0), cg);
00176         drawHand(painter, SecondHand, center, radius,
00177             360.0 - (origin() + 360.0 * seconds / 60.0), cg);
00178     }
00179 }
00180 
00191 void QwtAnalogClock::drawHand(QPainter *painter, Hand hd,
00192     const QPoint &center, int radius, double direction, 
00193     QPalette::ColorGroup cg) const
00194 {
00195     const QwtDialNeedle *needle = hand(hd);
00196     if ( needle )
00197     {
00198         if ( hd == HourHand )
00199             radius = qRound(0.8 * radius);
00200 
00201         needle->draw(painter, center, radius, direction, cg);
00202     }
00203 }

Generated on Sun Nov 21 11:12:42 2004 for Qwt User's Guide by doxygen 1.3.5