qwt_analog_clock.cpp

00001 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
00002  * Qwt Widget Library
00003  * Copyright (C) 1997   Josef Wilgen
00004  * Copyright (C) 2002   Uwe Rathmann
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the Qwt License, Version 1.0
00008  *****************************************************************************/
00009 
00010 #include "qwt_analog_clock.h"
00011 
00016 QwtAnalogClock::QwtAnalogClock(QWidget *parent):
00017     QwtDial(parent)
00018 {
00019     initClock();
00020 }
00021 
00022 #if QT_VERSION < 0x040000
00023 
00028 QwtAnalogClock::QwtAnalogClock(QWidget* parent, const char *name):
00029     QwtDial(parent, name)
00030 {
00031     initClock();
00032 }
00033 #endif
00034 
00035 void QwtAnalogClock::initClock()
00036 {
00037     setWrapping(true);
00038     setReadOnly(true);
00039 
00040     setOrigin(270.0);
00041     setRange(0.0, 60.0 * 60.0 * 12.0); // seconds
00042     setScale(-1, 5, 60.0 * 60.0);
00043 
00044     setScaleOptions(ScaleTicks | ScaleLabel);
00045     setScaleTicks(1, 0, 8);
00046     scaleDraw()->setSpacing(8);
00047 
00048     QColor knobColor =
00049 #if QT_VERSION < 0x040000
00050         palette().color(QPalette::Active, QColorGroup::Text);
00051 #else
00052         palette().color(QPalette::Active, QPalette::Text);
00053 #endif
00054     knobColor = knobColor.dark(120);
00055 
00056     QColor handColor;
00057     int width;
00058 
00059     for ( int i = 0; i < NHands; i++ )
00060     {
00061         if ( i == SecondHand )
00062         {
00063             width = 2;
00064             handColor = knobColor.dark(120);
00065         }
00066         else
00067         {
00068             width = 8;
00069             handColor = knobColor;
00070         }
00071 
00072         QwtDialSimpleNeedle *hand = new QwtDialSimpleNeedle(
00073             QwtDialSimpleNeedle::Arrow, true, handColor, knobColor);
00074         hand->setWidth(width);
00075 
00076         d_hand[i] = NULL;
00077         setHand((Hand)i, hand);
00078     }
00079 }
00080 
00082 QwtAnalogClock::~QwtAnalogClock()
00083 {
00084     for ( int i = 0; i < NHands; i++ )
00085         delete d_hand[i];
00086 }
00087 
00092 void QwtAnalogClock::setNeedle(QwtDialNeedle *)
00093 {
00094     // no op
00095     return;
00096 }
00097 
00104 void QwtAnalogClock::setHand(Hand hand, QwtDialNeedle *needle)
00105 {
00106     if ( hand >= 0 || hand < NHands )
00107     {
00108         delete d_hand[hand];
00109         d_hand[hand] = needle;
00110     }
00111 }
00112 
00118 QwtDialNeedle *QwtAnalogClock::hand(Hand hd)
00119 {
00120     if ( hd < 0 || hd >= NHands )
00121         return NULL;
00122 
00123     return d_hand[hd];
00124 }
00125 
00131 const QwtDialNeedle *QwtAnalogClock::hand(Hand hd) const
00132 {
00133     return ((QwtAnalogClock *)this)->hand(hd);
00134 }
00135 
00142 void QwtAnalogClock::setCurrentTime()
00143 { 
00144     setTime(QTime::currentTime()); 
00145 }
00146 
00151 void QwtAnalogClock::setTime(const QTime &time)
00152 {
00153     if ( time.isValid() )
00154     {
00155         setValue((time.hour() % 12) * 60.0 * 60.0 
00156             + time.minute() * 60.0 + time.second());
00157     }
00158     else
00159         setValid(false);
00160 }
00161 
00168 QwtText QwtAnalogClock::scaleLabel(double value) const
00169 {
00170     if ( value == 0.0 )
00171         value = 60.0 * 60.0 * 12.0;
00172 
00173     return QString::number(int(value / (60.0 * 60.0)));
00174 }
00175 
00191 void QwtAnalogClock::drawNeedle(QPainter *painter, const QPoint &center,
00192         int radius, double, QPalette::ColorGroup cg) const
00193 {
00194     if ( isValid() )
00195     {
00196         const double hours = value() / (60.0 * 60.0);
00197         const double minutes = (value() - (int)hours * 60.0 * 60.0) / 60.0;
00198         const double seconds = value() - (int)hours * 60.0 * 60.0 
00199             - (int)minutes * 60.0;
00200 
00201         drawHand(painter, HourHand, center, radius,
00202             360.0 - (origin() + 360.0 * hours / 12.0), cg);
00203         drawHand(painter, MinuteHand, center, radius,
00204             360.0 - (origin() + 360.0 * minutes / 60.0), cg);
00205         drawHand(painter, SecondHand, center, radius,
00206             360.0 - (origin() + 360.0 * seconds / 60.0), cg);
00207     }
00208 }
00209 
00220 void QwtAnalogClock::drawHand(QPainter *painter, Hand hd,
00221     const QPoint &center, int radius, double direction, 
00222     QPalette::ColorGroup cg) const
00223 {
00224     const QwtDialNeedle *needle = hand(hd);
00225     if ( needle )
00226     {
00227         if ( hd == HourHand )
00228             radius = qRound(0.8 * radius);
00229 
00230         needle->draw(painter, center, radius, direction, cg);
00231     }
00232 }

Generated on Mon Feb 26 21:22:36 2007 for Qwt User's Guide by  doxygen 1.4.6