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 QString QwtAnalogClock::scaleLabel(double value) const
00150 {
00151     if ( value == 0.0 )
00152         value = 60.0 * 60.0 * 12.0;
00153 
00154     QString label;
00155     label.sprintf("%d", int(value / (60.0 * 60.0)));
00156 
00157     return label;
00158 }
00159 
00175 void QwtAnalogClock::drawNeedle(QPainter *painter, const QPoint &center,
00176         int radius, double, QPalette::ColorGroup cg) const
00177 {
00178     if ( isValid() )
00179     {
00180         const double hours = value() / (60.0 * 60.0);
00181         const double minutes = (value() - (int)hours * 60.0 * 60.0) / 60.0;
00182         const double seconds = value() - (int)hours * 60.0 * 60.0 
00183             - (int)minutes * 60.0;
00184 
00185         drawHand(painter, HourHand, center, radius,
00186             360.0 - (origin() + 360.0 * hours / 12.0), cg);
00187         drawHand(painter, MinuteHand, center, radius,
00188             360.0 - (origin() + 360.0 * minutes / 60.0), cg);
00189         drawHand(painter, SecondHand, center, radius,
00190             360.0 - (origin() + 360.0 * seconds / 60.0), cg);
00191     }
00192 }
00193 
00204 void QwtAnalogClock::drawHand(QPainter *painter, Hand hd,
00205     const QPoint &center, int radius, double direction, 
00206     QPalette::ColorGroup cg) const
00207 {
00208     const QwtDialNeedle *needle = hand(hd);
00209     if ( needle )
00210     {
00211         if ( hd == HourHand )
00212             radius = qRound(0.8 * radius);
00213 
00214         needle->draw(painter, center, radius, direction, cg);
00215     }
00216 }

Generated on Wed Aug 31 23:02:29 2005 for Qwt User's Guide by  doxygen 1.4.1