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

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     setWrapping(true);
00020     setReadOnly(true);
00021 
00022     setOrigin(270.0);
00023     setRange(0.0, 60.0 * 60.0 * 12.0); // seconds
00024     setScale(-1, 5, 60.0 * 60.0);
00025 
00026     setScaleOptions(ScaleTicks | ScaleLabel);
00027     setScaleTicks(1, 0, 8);
00028 
00029     QColor knobColor =
00030 #if QT_VERSION < 0x040000
00031         palette().color(QPalette::Active, QColorGroup::Text);
00032 #else
00033         palette().color(QPalette::Active, QPalette::Text);
00034 #endif
00035     knobColor = knobColor.dark(120);
00036 
00037     QColor handColor;
00038     int width;
00039 
00040     for ( int i = 0; i < NHands; i++ )
00041     {
00042         if ( i == SecondHand )
00043         {
00044             width = 2;
00045             handColor = knobColor.dark(120);
00046         }
00047         else
00048         {
00049             width = 8;
00050             handColor = knobColor;
00051         }
00052 
00053         QwtDialSimpleNeedle *hand = new QwtDialSimpleNeedle(
00054             QwtDialSimpleNeedle::Arrow, true, handColor, knobColor);
00055         hand->setWidth(width);
00056 
00057         d_hand[i] = NULL;
00058         setHand((Hand)i, hand);
00059     }
00060 }
00061 
00063 QwtAnalogClock::~QwtAnalogClock()
00064 {
00065     for ( int i = 0; i < NHands; i++ )
00066         delete d_hand[i];
00067 }
00068 
00073 void QwtAnalogClock::setNeedle(QwtDialNeedle *)
00074 {
00075     // no op
00076     return;
00077 }
00078 
00085 void QwtAnalogClock::setHand(Hand hand, QwtDialNeedle *needle)
00086 {
00087     if ( hand >= 0 || hand < NHands )
00088     {
00089         delete d_hand[hand];
00090         d_hand[hand] = needle;
00091     }
00092 }
00093 
00099 QwtDialNeedle *QwtAnalogClock::hand(Hand hd)
00100 {
00101     if ( hd < 0 || hd >= NHands )
00102         return NULL;
00103 
00104     return d_hand[hd];
00105 }
00106 
00112 const QwtDialNeedle *QwtAnalogClock::hand(Hand hd) const
00113 {
00114     return ((QwtAnalogClock *)this)->hand(hd);
00115 }
00116 
00123 void QwtAnalogClock::setCurrentTime()
00124 { 
00125     setTime(QTime::currentTime()); 
00126 }
00127 
00132 void QwtAnalogClock::setTime(const QTime &time)
00133 {
00134     if ( time.isValid() )
00135     {
00136         setValue((time.hour() % 12) * 60.0 * 60.0 
00137             + time.minute() * 60.0 + time.second());
00138     }
00139     else
00140         setValid(false);
00141 }
00142 
00149 QwtText QwtAnalogClock::scaleLabel(double value) const
00150 {
00151     if ( value == 0.0 )
00152         value = 60.0 * 60.0 * 12.0;
00153 
00154     return QString::number(int(value / (60.0 * 60.0)));
00155 }
00156 
00172 void QwtAnalogClock::drawNeedle(QPainter *painter, const QPoint &center,
00173         int radius, double, QPalette::ColorGroup cg) const
00174 {
00175     if ( isValid() )
00176     {
00177         const double hours = value() / (60.0 * 60.0);
00178         const double minutes = (value() - (int)hours * 60.0 * 60.0) / 60.0;
00179         const double seconds = value() - (int)hours * 60.0 * 60.0 
00180             - (int)minutes * 60.0;
00181 
00182         drawHand(painter, HourHand, center, radius,
00183             360.0 - (origin() + 360.0 * hours / 12.0), cg);
00184         drawHand(painter, MinuteHand, center, radius,
00185             360.0 - (origin() + 360.0 * minutes / 60.0), cg);
00186         drawHand(painter, SecondHand, center, radius,
00187             360.0 - (origin() + 360.0 * seconds / 60.0), cg);
00188     }
00189 }
00190 
00201 void QwtAnalogClock::drawHand(QPainter *painter, Hand hd,
00202     const QPoint &center, int radius, double direction, 
00203     QPalette::ColorGroup cg) const
00204 {
00205     const QwtDialNeedle *needle = hand(hd);
00206     if ( needle )
00207     {
00208         if ( hd == HourHand )
00209             radius = qRound(0.8 * radius);
00210 
00211         needle->draw(painter, center, radius, direction, cg);
00212     }
00213 }

Generated on Mon Jan 30 22:16:25 2006 for Qwt User's Guide by  doxygen 1.4.4