-rw-r--r-- | noncore/tools/clock/analogclock.cpp | 112 | ||||
-rw-r--r-- | noncore/tools/clock/analogclock.h | 1 |
2 files changed, 59 insertions, 54 deletions
diff --git a/noncore/tools/clock/analogclock.cpp b/noncore/tools/clock/analogclock.cpp index 7f8ca67..c5f0155 100644 --- a/noncore/tools/clock/analogclock.cpp +++ b/noncore/tools/clock/analogclock.cpp | |||
@@ -1,197 +1,203 @@ | |||
1 | /********************************************************************** | 1 | /********************************************************************** |
2 | ** Copyright (C) 2000-2002 Trolltech AS. All rights reserved. | 2 | ** Copyright (C) 2000-2002 Trolltech AS. All rights reserved. |
3 | ** | 3 | ** |
4 | ** This file is part of the Qtopia Environment. | 4 | ** This file is part of the Qtopia Environment. |
5 | ** | 5 | ** |
6 | ** This file may be distributed and/or modified under the terms of the | 6 | ** This file may be distributed and/or modified under the terms of the |
7 | ** GNU General Public License version 2 as published by the Free Software | 7 | ** GNU General Public License version 2 as published by the Free Software |
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | 8 | ** Foundation and appearing in the file LICENSE.GPL included in the |
9 | ** packaging of this file. | 9 | ** packaging of this file. |
10 | ** | 10 | ** |
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | 11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | 12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
13 | ** | 13 | ** |
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | 14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. |
15 | ** | 15 | ** |
16 | ** Contact info@trolltech.com if any conditions of this licensing are | 16 | ** Contact info@trolltech.com if any conditions of this licensing are |
17 | ** not clear to you. | 17 | ** not clear to you. |
18 | ** | 18 | ** |
19 | **********************************************************************/ | 19 | **********************************************************************/ |
20 | 20 | ||
21 | #include "analogclock.h" | 21 | #include "analogclock.h" |
22 | 22 | ||
23 | #include <qlayout.h> | 23 | #include <qlayout.h> |
24 | #include <qpainter.h> | 24 | #include <qpainter.h> |
25 | #include <qtopia/global.h> | 25 | #include <qtopia/global.h> |
26 | 26 | ||
27 | #include <math.h> | 27 | #include <math.h> |
28 | 28 | ||
29 | const double deg2rad = 0.017453292519943295769;// pi/180 | 29 | const double deg2rad = 0.017453292519943295769;// pi/180 |
30 | 30 | ||
31 | AnalogClock::AnalogClock( QWidget *parent, const char *name ) | 31 | AnalogClock::AnalogClock( QWidget *parent, const char *name ) |
32 | : QFrame( parent, name ), clear(false) | 32 | : QFrame( parent, name ), clear(false) |
33 | { | 33 | { |
34 | setMinimumSize(50,50); | 34 | setMinimumSize(50,50); |
35 | } | 35 | } |
36 | 36 | ||
37 | QSizePolicy AnalogClock::sizePolicy() const | 37 | QSizePolicy AnalogClock::sizePolicy() const |
38 | { | 38 | { |
39 | return QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ); | 39 | return QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ); |
40 | } | 40 | } |
41 | 41 | ||
42 | void AnalogClock::drawContents( QPainter *p ) | 42 | void AnalogClock::drawContents( QPainter *p ) |
43 | { | 43 | { |
44 | #if !defined(NO_DEBUG) | ||
45 | static bool first = true; | ||
46 | if ( first ) { | ||
47 | //QTOPIA_PROFILE("first paint event"); | ||
48 | first = false; | ||
49 | } | ||
50 | #endif | ||
44 | 51 | ||
45 | QRect r = contentsRect(); | 52 | QRect r = contentsRect(); |
46 | 53 | ||
47 | if ( r.width() < r.height() ) { | 54 | if ( r.width() < r.height() ) { |
48 | r.setY( (r.height() - r.width())/2 ); | 55 | r.setY( (r.height() - r.width())/2 ); |
49 | r.setHeight( r.width() ); | 56 | r.setHeight( r.width() ); |
50 | } | 57 | } |
51 | 58 | ||
52 | QPoint l1( r.x() + r.width() / 2, r.y() + 2 ); | ||
53 | QPoint l2( r.x() + r.width() / 2, r.y() + 8 ); | ||
54 | QPoint center( r.x() + r.width() / 2, r.y() + r.height() / 2 ); | 59 | QPoint center( r.x() + r.width() / 2, r.y() + r.height() / 2 ); |
55 | 60 | ||
56 | QBrush bgBrush = colorGroup().brush( QColorGroup::Background ); | 61 | const int w_tick = r.width()/300+1; |
57 | 62 | const int w_sec = r.width()/400+1; | |
58 | // if ( clear ){ | 63 | const int w_hour = r.width()/80+1; |
59 | p->fillRect ( r, bgBrush ); | ||
60 | |||
61 | |||
62 | // draw ticks | ||
63 | p->setPen( QPen( colorGroup ( ). color ( QColorGroup::Text ), 1 ) ); | ||
64 | for ( int i = 0; i < 12; i++ ) | ||
65 | p->drawLine( rotate( center, l1, i * 30 ), rotate( center, l2, i * 30 ) ); | ||
66 | // }else { | ||
67 | // p->setBrush( bgBrush ); | ||
68 | // drawPointers ( p, r, colorGroup ( ). color ( QColorGroup::Background ), prevTime, &currTime ); | ||
69 | // } | ||
70 | 64 | ||
71 | QColor col = colorGroup().color( QColorGroup::Text ); | 65 | QPoint l1( r.x() + r.width() / 2, r.y() + 2 ); |
72 | p->setBrush( col ); | 66 | QPoint l2( r.x() + r.width() / 2, r.y() + 8 ); |
73 | drawPointers ( p, r, col, currTime ); | ||
74 | |||
75 | |||
76 | prevTime = currTime; | ||
77 | } | ||
78 | |||
79 | void AnalogClock::drawPointers ( QPainter *p, const QRect &r, const QColor &c, const QTime &t, const QTime *t2 ) | ||
80 | { | ||
81 | QPoint center = r. center ( ); | ||
82 | 67 | ||
68 | QPoint h1( r.x() + r.width() / 2, r.y() + r.height() / 4 ); | ||
69 | QPoint h2( r.x() + r.width() / 2, r.y() + r.height() / 2 ); | ||
83 | 70 | ||
84 | QPoint h1( center. x ( ), r. y ( ) + r. height ( ) / 4 ); | 71 | QPoint m1( r.x() + r.width() / 2, r.y() + r.height() / 9 ); |
85 | QPoint h2( center. x ( ), center. y ( ) ); | 72 | QPoint m2( r.x() + r.width() / 2, r.y() + r.height() / 2 ); |
86 | 73 | ||
87 | QPoint m1( center. x ( ), r.y() + r.height() / 8 ); | 74 | QPoint s1( r.x() + r.width() / 2, r.y() + 8 ); |
88 | QPoint m2( center. x ( ), center. y ( ) ); | 75 | QPoint s2( r.x() + r.width() / 2, r.y() + r.height() / 2 ); |
89 | 76 | ||
90 | QPoint s1( center. x ( ), r. y ( ) + 8 ); | 77 | QColor color( clear ? backgroundColor() : black ); |
91 | QPoint s2( center. x ( ), center. y ( ) ); | 78 | QTime time = clear ? prevTime : currTime; |
92 | 79 | ||
80 | if ( clear && prevTime.secsTo(currTime) > 1 ) { | ||
81 | p->eraseRect( rect() ); | ||
82 | return; | ||
83 | } | ||
93 | 84 | ||
94 | if ( !t2 || ( t. minute ( ) != t2-> minute ( ) || t. hour ( ) != t2-> hour ( ))) { | 85 | if ( !clear ) { |
95 | // draw hour pointer | 86 | // draw ticks |
96 | h1 = rotate( center, h1, 30 * ( t.hour() % 12 ) + t.minute() / 2 ); | 87 | p->setPen( QPen( color, w_tick ) ); |
97 | h2 = rotate( center, h2, 30 * ( t.hour() % 12 ) + t.minute() / 2 ); | 88 | for ( int i = 0; i < 12; i++ ) |
98 | p-> setPen ( QPen ( c, 3 )); | 89 | p->drawLine( rotate( center, l1, i * 30 ), rotate( center, l2, i * 30 ) ); |
99 | drawHand( p, h1, h2 ); | ||
100 | } | 90 | } |
101 | 91 | ||
102 | if ( !t2 || ( t. minute ( ) != t2-> minute ( ))) { | 92 | if ( !clear || prevTime.minute() != currTime.minute() || |
103 | // draw minute pointer | 93 | prevTime.hour() != currTime.hour() ) { |
104 | m1 = rotate( center, m1, t.minute() * 6 ); | 94 | // draw hour pointer |
105 | m2 = rotate( center, m2, t.minute() * 6 ); | 95 | h1 = rotate( center, h1, 30 * ( time.hour() % 12 ) + time.minute() / 2 ); |
106 | p-> setPen ( QPen ( c, 2 )); | 96 | h2 = rotate( center, h2, 30 * ( time.hour() % 12 ) + time.minute() / 2 ); |
107 | drawHand( p, m1, m2 ); | 97 | p->setPen( color ); |
98 | p->setBrush( color ); | ||
99 | drawHand( p, h1, h2 ); | ||
108 | } | 100 | } |
109 | 101 | ||
110 | if ( !t2 || ( t. second ( ) != t2-> second ( ))) { | 102 | if ( !clear || prevTime.minute() != currTime.minute() ) { |
111 | // draw second pointer | 103 | // draw minute pointer |
112 | s1 = rotate( center, s1, t.second() * 6 ); | 104 | m1 = rotate( center, m1, time.minute() * 6 ); |
113 | s2 = rotate( center, s2, t.second() * 6 ); | 105 | m2 = rotate( center, m2, time.minute() * 6 ); |
114 | p-> setPen ( QPen ( c, 1 )); | 106 | p->setPen( color ); |
115 | p-> drawLine ( s1, s2 ); | 107 | p->setBrush( color ); |
108 | drawHand( p, m1, m2 ); | ||
116 | } | 109 | } |
117 | } | ||
118 | 110 | ||
111 | // draw second pointer | ||
112 | s1 = rotate( center, s1, time.second() * 6 ); | ||
113 | s2 = rotate( center, s2, time.second() * 6 ); | ||
114 | p->setPen( QPen( color, w_sec ) ); | ||
115 | p->drawLine( s1, s2 ); | ||
116 | |||
117 | // cap | ||
118 | p->setBrush(color); | ||
119 | p->drawEllipse( center.x()-w_hour/2, center.y()-w_hour/2, w_hour, w_hour ); | ||
120 | |||
121 | if ( !clear ) | ||
122 | prevTime = currTime; | ||
123 | } | ||
119 | 124 | ||
120 | // Dijkstra's bisection algorithm to find the square root as an integer. | 125 | // Dijkstra's bisection algorithm to find the square root as an integer. |
121 | 126 | ||
122 | static uint int_sqrt(uint n) | 127 | static uint int_sqrt(uint n) |
123 | { | 128 | { |
124 | if ( n >= UINT_MAX>>2 ) // n must be in the range 0...UINT_MAX/2-1 | 129 | if ( n >= UINT_MAX>>2 ) // n must be in the range 0...UINT_MAX/2-1 |
125 | return 2*int_sqrt( n/4 ); | 130 | return 2*int_sqrt( n/4 ); |
126 | uint h, p= 0, q= 1, r= n; | 131 | uint h, p= 0, q= 1, r= n; |
127 | while ( q <= n ) | 132 | while ( q <= n ) |
128 | q <<= 2; | 133 | q <<= 2; |
129 | while ( q != 1 ) { | 134 | while ( q != 1 ) { |
130 | q >>= 2; | 135 | q >>= 2; |
131 | h= p + q; | 136 | h= p + q; |
132 | p >>= 1; | 137 | p >>= 1; |
133 | if ( r >= h ) { | 138 | if ( r >= h ) { |
134 | p += q; | 139 | p += q; |
135 | r -= h; | 140 | r -= h; |
136 | } | 141 | } |
137 | } | 142 | } |
138 | return p; | 143 | return p; |
139 | } | 144 | } |
140 | 145 | ||
141 | void AnalogClock::drawHand( QPainter *p, QPoint p1, QPoint p2 ) | 146 | void AnalogClock::drawHand( QPainter *p, QPoint p1, QPoint p2 ) |
142 | { | 147 | { |
143 | int hw = 7; | 148 | int hw = 7; |
144 | if ( contentsRect().height() < 100 ) | 149 | if ( contentsRect().height() < 100 ) |
145 | hw = 5; | 150 | hw = 5; |
146 | 151 | ||
147 | int dx = p2.x() - p1.x(); | 152 | int dx = p2.x() - p1.x(); |
148 | int dy = p2.y() - p1.y(); | 153 | int dy = p2.y() - p1.y(); |
149 | int w = dx*dx+dy*dy; | 154 | int w = dx*dx+dy*dy; |
150 | int ix,iy; | 155 | int ix,iy; |
151 | w = int_sqrt(w*256); | 156 | w = int_sqrt(w*256); |
152 | iy = w ? (hw * dy * 16)/ w : dy ? 0 : hw; | 157 | iy = w ? (hw * dy * 16)/ w : dy ? 0 : hw; |
153 | ix = w ? (hw * dx * 16)/ w : dx ? 0 : hw; | 158 | ix = w ? (hw * dx * 16)/ w : dx ? 0 : hw; |
154 | 159 | ||
155 | // rounding dependent on sign | 160 | // rounding dependent on sign |
156 | int nix, niy; | 161 | int nix, niy; |
157 | if ( ix < 0 ) { | 162 | if ( ix < 0 ) { |
158 | nix = ix/2; | 163 | nix = ix/2; |
159 | ix = (ix-1)/2; | 164 | ix = (ix-1)/2; |
160 | } else { | 165 | } else { |
161 | nix = (ix+1)/2; | 166 | nix = (ix+1)/2; |
162 | ix = ix/2; | 167 | ix = ix/2; |
163 | } | 168 | } |
164 | if ( iy < 0 ) { | 169 | if ( iy < 0 ) { |
165 | niy = iy/2; | 170 | niy = iy/2; |
166 | iy = (iy-1)/2; | 171 | iy = (iy-1)/2; |
167 | } else { | 172 | } else { |
168 | niy = (iy+1)/2; | 173 | niy = (iy+1)/2; |
169 | iy = iy/2; | 174 | iy = iy/2; |
170 | } | 175 | } |
171 | 176 | ||
172 | QPointArray pa(4); | 177 | QPointArray pa(4); |
173 | pa[0] = p1; | 178 | pa[0] = p1; |
174 | pa[1] = QPoint( p2.x()+iy, p2.y()-nix ); | 179 | pa[1] = QPoint( p2.x()+iy, p2.y()-nix ); |
175 | pa[2] = QPoint( p2.x()-niy, p2.y()+ix ); | 180 | pa[2] = QPoint( p2.x()-niy, p2.y()+ix ); |
176 | pa[3] = p1; | 181 | pa[3] = p1; |
177 | 182 | ||
178 | p->drawPolygon( pa ); | 183 | p->drawPolygon( pa ); |
179 | } | 184 | } |
180 | 185 | ||
181 | void AnalogClock::display( const QTime& t ) | 186 | void AnalogClock::display( const QTime& t ) |
182 | { | 187 | { |
183 | currTime = t; | 188 | currTime = t; |
189 | clear = true; | ||
190 | repaint( false ); | ||
184 | clear = false; | 191 | clear = false; |
185 | repaint( false ); | 192 | repaint( false ); |
186 | clear = true; | ||
187 | } | 193 | } |
188 | 194 | ||
189 | QPoint AnalogClock::rotate( QPoint c, QPoint p, int a ) | 195 | QPoint AnalogClock::rotate( QPoint c, QPoint p, int a ) |
190 | { | 196 | { |
191 | double angle = deg2rad * ( - a + 180 ); | 197 | double angle = deg2rad * ( - a + 180 ); |
192 | double nx = c.x() - ( p.x() - c.x() ) * cos( angle ) - | 198 | double nx = c.x() - ( p.x() - c.x() ) * cos( angle ) - |
193 | ( p.y() - c.y() ) * sin( angle ); | 199 | ( p.y() - c.y() ) * sin( angle ); |
194 | double ny = c.y() - ( p.y() - c.y() ) * cos( angle ) + | 200 | double ny = c.y() - ( p.y() - c.y() ) * cos( angle ) + |
195 | ( p.x() - c.x() ) * sin( angle ); | 201 | ( p.x() - c.x() ) * sin( angle ); |
196 | return QPoint( int(nx), int(ny) ); | 202 | return QPoint( int(nx), int(ny) ); |
197 | } | 203 | } |
diff --git a/noncore/tools/clock/analogclock.h b/noncore/tools/clock/analogclock.h index 5f30d68..3aa035e 100644 --- a/noncore/tools/clock/analogclock.h +++ b/noncore/tools/clock/analogclock.h | |||
@@ -1,50 +1,49 @@ | |||
1 | /********************************************************************** | 1 | /********************************************************************** |
2 | ** Copyright (C) 2000-2002 Trolltech AS. All rights reserved. | 2 | ** Copyright (C) 2000-2002 Trolltech AS. All rights reserved. |
3 | ** | 3 | ** |
4 | ** This file is part of the Qtopia Environment. | 4 | ** This file is part of the Qtopia Environment. |
5 | ** | 5 | ** |
6 | ** This file may be distributed and/or modified under the terms of the | 6 | ** This file may be distributed and/or modified under the terms of the |
7 | ** GNU General Public License version 2 as published by the Free Software | 7 | ** GNU General Public License version 2 as published by the Free Software |
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | 8 | ** Foundation and appearing in the file LICENSE.GPL included in the |
9 | ** packaging of this file. | 9 | ** packaging of this file. |
10 | ** | 10 | ** |
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | 11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | 12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
13 | ** | 13 | ** |
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | 14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. |
15 | ** | 15 | ** |
16 | ** Contact info@trolltech.com if any conditions of this licensing are | 16 | ** Contact info@trolltech.com if any conditions of this licensing are |
17 | ** not clear to you. | 17 | ** not clear to you. |
18 | ** | 18 | ** |
19 | **********************************************************************/ | 19 | **********************************************************************/ |
20 | #ifndef ANALOGCLOCK_H | 20 | #ifndef ANALOGCLOCK_H |
21 | #define ANALOGCLOCK_H | 21 | #define ANALOGCLOCK_H |
22 | 22 | ||
23 | #include <qdatetime.h> | 23 | #include <qdatetime.h> |
24 | #include <qframe.h> | 24 | #include <qframe.h> |
25 | 25 | ||
26 | class AnalogClock : public QFrame | 26 | class AnalogClock : public QFrame |
27 | { | 27 | { |
28 | Q_OBJECT | 28 | Q_OBJECT |
29 | public: | 29 | public: |
30 | AnalogClock( QWidget *parent=0, const char *name=0 ); | 30 | AnalogClock( QWidget *parent=0, const char *name=0 ); |
31 | 31 | ||
32 | QSizePolicy sizePolicy() const; | 32 | QSizePolicy sizePolicy() const; |
33 | 33 | ||
34 | void display( const QTime& time ); | 34 | void display( const QTime& time ); |
35 | 35 | ||
36 | protected: | 36 | protected: |
37 | void drawContents( QPainter *p ); | 37 | void drawContents( QPainter *p ); |
38 | void drawHand( QPainter *p, QPoint, QPoint ); | 38 | void drawHand( QPainter *p, QPoint, QPoint ); |
39 | void drawPointers ( QPainter *p, const QRect &r, const QColor &c, const QTime &t, const QTime *t2 = 0); | ||
40 | 39 | ||
41 | private: | 40 | private: |
42 | QPoint rotate( QPoint center, QPoint p, int angle ); | 41 | QPoint rotate( QPoint center, QPoint p, int angle ); |
43 | 42 | ||
44 | QTime currTime; | 43 | QTime currTime; |
45 | QTime prevTime; | 44 | QTime prevTime; |
46 | bool clear; | 45 | bool clear; |
47 | }; | 46 | }; |
48 | 47 | ||
49 | #endif | 48 | #endif |
50 | 49 | ||