Diffstat (limited to 'noncore/tools/clock/analogclock.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/tools/clock/analogclock.cpp | 112 |
1 files changed, 53 insertions, 59 deletions
diff --git a/noncore/tools/clock/analogclock.cpp b/noncore/tools/clock/analogclock.cpp index c5f0155..7f8ca67 100644 --- a/noncore/tools/clock/analogclock.cpp +++ b/noncore/tools/clock/analogclock.cpp | |||
@@ -20,129 +20,124 @@ | |||
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 | ||
51 | 44 | ||
52 | QRect r = contentsRect(); | 45 | QRect r = contentsRect(); |
53 | 46 | ||
54 | if ( r.width() < r.height() ) { | 47 | if ( r.width() < r.height() ) { |
55 | r.setY( (r.height() - r.width())/2 ); | 48 | r.setY( (r.height() - r.width())/2 ); |
56 | r.setHeight( r.width() ); | 49 | r.setHeight( r.width() ); |
57 | } | 50 | } |
58 | 51 | ||
52 | QPoint l1( r.x() + r.width() / 2, r.y() + 2 ); | ||
53 | QPoint l2( r.x() + r.width() / 2, r.y() + 8 ); | ||
59 | QPoint center( r.x() + r.width() / 2, r.y() + r.height() / 2 ); | 54 | QPoint center( r.x() + r.width() / 2, r.y() + r.height() / 2 ); |
60 | 55 | ||
61 | const int w_tick = r.width()/300+1; | 56 | QBrush bgBrush = colorGroup().brush( QColorGroup::Background ); |
62 | const int w_sec = r.width()/400+1; | ||
63 | const int w_hour = r.width()/80+1; | ||
64 | 57 | ||
65 | QPoint l1( r.x() + r.width() / 2, r.y() + 2 ); | 58 | // if ( clear ){ |
66 | QPoint l2( r.x() + r.width() / 2, r.y() + 8 ); | 59 | p->fillRect ( r, bgBrush ); |
67 | 60 | ||
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 ); | ||
70 | 61 | ||
71 | QPoint m1( r.x() + r.width() / 2, r.y() + r.height() / 9 ); | 62 | // draw ticks |
72 | QPoint m2( r.x() + r.width() / 2, r.y() + r.height() / 2 ); | 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 | // } | ||
73 | 70 | ||
74 | QPoint s1( r.x() + r.width() / 2, r.y() + 8 ); | 71 | QColor col = colorGroup().color( QColorGroup::Text ); |
75 | QPoint s2( r.x() + r.width() / 2, r.y() + r.height() / 2 ); | 72 | p->setBrush( col ); |
73 | drawPointers ( p, r, col, currTime ); | ||
76 | 74 | ||
77 | QColor color( clear ? backgroundColor() : black ); | ||
78 | QTime time = clear ? prevTime : currTime; | ||
79 | 75 | ||
80 | if ( clear && prevTime.secsTo(currTime) > 1 ) { | 76 | prevTime = currTime; |
81 | p->eraseRect( rect() ); | 77 | } |
82 | return; | ||
83 | } | ||
84 | 78 | ||
85 | if ( !clear ) { | 79 | void AnalogClock::drawPointers ( QPainter *p, const QRect &r, const QColor &c, const QTime &t, const QTime *t2 ) |
86 | // draw ticks | 80 | { |
87 | p->setPen( QPen( color, w_tick ) ); | 81 | QPoint center = r. center ( ); |
88 | for ( int i = 0; i < 12; i++ ) | ||
89 | p->drawLine( rotate( center, l1, i * 30 ), rotate( center, l2, i * 30 ) ); | ||
90 | } | ||
91 | 82 | ||
92 | if ( !clear || prevTime.minute() != currTime.minute() || | ||
93 | prevTime.hour() != currTime.hour() ) { | ||
94 | // draw hour pointer | ||
95 | h1 = rotate( center, h1, 30 * ( time.hour() % 12 ) + time.minute() / 2 ); | ||
96 | h2 = rotate( center, h2, 30 * ( time.hour() % 12 ) + time.minute() / 2 ); | ||
97 | p->setPen( color ); | ||
98 | p->setBrush( color ); | ||
99 | drawHand( p, h1, h2 ); | ||
100 | } | ||
101 | 83 | ||
102 | if ( !clear || prevTime.minute() != currTime.minute() ) { | 84 | QPoint h1( center. x ( ), r. y ( ) + r. height ( ) / 4 ); |
103 | // draw minute pointer | 85 | QPoint h2( center. x ( ), center. y ( ) ); |
104 | m1 = rotate( center, m1, time.minute() * 6 ); | ||
105 | m2 = rotate( center, m2, time.minute() * 6 ); | ||
106 | p->setPen( color ); | ||
107 | p->setBrush( color ); | ||
108 | drawHand( p, m1, m2 ); | ||
109 | } | ||
110 | 86 | ||
111 | // draw second pointer | 87 | QPoint m1( center. x ( ), r.y() + r.height() / 8 ); |
112 | s1 = rotate( center, s1, time.second() * 6 ); | 88 | QPoint m2( center. x ( ), center. y ( ) ); |
113 | s2 = rotate( center, s2, time.second() * 6 ); | ||
114 | p->setPen( QPen( color, w_sec ) ); | ||
115 | p->drawLine( s1, s2 ); | ||
116 | 89 | ||
117 | // cap | 90 | QPoint s1( center. x ( ), r. y ( ) + 8 ); |
118 | p->setBrush(color); | 91 | QPoint s2( center. x ( ), center. y ( ) ); |
119 | p->drawEllipse( center.x()-w_hour/2, center.y()-w_hour/2, w_hour, w_hour ); | ||
120 | 92 | ||
121 | if ( !clear ) | 93 | |
122 | prevTime = currTime; | 94 | if ( !t2 || ( t. minute ( ) != t2-> minute ( ) || t. hour ( ) != t2-> hour ( ))) { |
95 | // draw hour pointer | ||
96 | h1 = rotate( center, h1, 30 * ( t.hour() % 12 ) + t.minute() / 2 ); | ||
97 | h2 = rotate( center, h2, 30 * ( t.hour() % 12 ) + t.minute() / 2 ); | ||
98 | p-> setPen ( QPen ( c, 3 )); | ||
99 | drawHand( p, h1, h2 ); | ||
100 | } | ||
101 | |||
102 | if ( !t2 || ( t. minute ( ) != t2-> minute ( ))) { | ||
103 | // draw minute pointer | ||
104 | m1 = rotate( center, m1, t.minute() * 6 ); | ||
105 | m2 = rotate( center, m2, t.minute() * 6 ); | ||
106 | p-> setPen ( QPen ( c, 2 )); | ||
107 | drawHand( p, m1, m2 ); | ||
108 | } | ||
109 | |||
110 | if ( !t2 || ( t. second ( ) != t2-> second ( ))) { | ||
111 | // draw second pointer | ||
112 | s1 = rotate( center, s1, t.second() * 6 ); | ||
113 | s2 = rotate( center, s2, t.second() * 6 ); | ||
114 | p-> setPen ( QPen ( c, 1 )); | ||
115 | p-> drawLine ( s1, s2 ); | ||
116 | } | ||
123 | } | 117 | } |
124 | 118 | ||
119 | |||
125 | // Dijkstra's bisection algorithm to find the square root as an integer. | 120 | // Dijkstra's bisection algorithm to find the square root as an integer. |
126 | 121 | ||
127 | static uint int_sqrt(uint n) | 122 | static uint int_sqrt(uint n) |
128 | { | 123 | { |
129 | if ( n >= UINT_MAX>>2 ) // n must be in the range 0...UINT_MAX/2-1 | 124 | if ( n >= UINT_MAX>>2 ) // n must be in the range 0...UINT_MAX/2-1 |
130 | return 2*int_sqrt( n/4 ); | 125 | return 2*int_sqrt( n/4 ); |
131 | uint h, p= 0, q= 1, r= n; | 126 | uint h, p= 0, q= 1, r= n; |
132 | while ( q <= n ) | 127 | while ( q <= n ) |
133 | q <<= 2; | 128 | q <<= 2; |
134 | while ( q != 1 ) { | 129 | while ( q != 1 ) { |
135 | q >>= 2; | 130 | q >>= 2; |
136 | h= p + q; | 131 | h= p + q; |
137 | p >>= 1; | 132 | p >>= 1; |
138 | if ( r >= h ) { | 133 | if ( r >= h ) { |
139 | p += q; | 134 | p += q; |
140 | r -= h; | 135 | r -= h; |
141 | } | 136 | } |
142 | } | 137 | } |
143 | return p; | 138 | return p; |
144 | } | 139 | } |
145 | 140 | ||
146 | void AnalogClock::drawHand( QPainter *p, QPoint p1, QPoint p2 ) | 141 | void AnalogClock::drawHand( QPainter *p, QPoint p1, QPoint p2 ) |
147 | { | 142 | { |
148 | int hw = 7; | 143 | int hw = 7; |
@@ -165,39 +160,38 @@ void AnalogClock::drawHand( QPainter *p, QPoint p1, QPoint p2 ) | |||
165 | } else { | 160 | } else { |
166 | nix = (ix+1)/2; | 161 | nix = (ix+1)/2; |
167 | ix = ix/2; | 162 | ix = ix/2; |
168 | } | 163 | } |
169 | if ( iy < 0 ) { | 164 | if ( iy < 0 ) { |
170 | niy = iy/2; | 165 | niy = iy/2; |
171 | iy = (iy-1)/2; | 166 | iy = (iy-1)/2; |
172 | } else { | 167 | } else { |
173 | niy = (iy+1)/2; | 168 | niy = (iy+1)/2; |
174 | iy = iy/2; | 169 | iy = iy/2; |
175 | } | 170 | } |
176 | 171 | ||
177 | QPointArray pa(4); | 172 | QPointArray pa(4); |
178 | pa[0] = p1; | 173 | pa[0] = p1; |
179 | pa[1] = QPoint( p2.x()+iy, p2.y()-nix ); | 174 | pa[1] = QPoint( p2.x()+iy, p2.y()-nix ); |
180 | pa[2] = QPoint( p2.x()-niy, p2.y()+ix ); | 175 | pa[2] = QPoint( p2.x()-niy, p2.y()+ix ); |
181 | pa[3] = p1; | 176 | pa[3] = p1; |
182 | 177 | ||
183 | p->drawPolygon( pa ); | 178 | p->drawPolygon( pa ); |
184 | } | 179 | } |
185 | 180 | ||
186 | void AnalogClock::display( const QTime& t ) | 181 | void AnalogClock::display( const QTime& t ) |
187 | { | 182 | { |
188 | currTime = t; | 183 | currTime = t; |
189 | clear = true; | ||
190 | repaint( false ); | ||
191 | clear = false; | 184 | clear = false; |
192 | repaint( false ); | 185 | repaint( false ); |
186 | clear = true; | ||
193 | } | 187 | } |
194 | 188 | ||
195 | QPoint AnalogClock::rotate( QPoint c, QPoint p, int a ) | 189 | QPoint AnalogClock::rotate( QPoint c, QPoint p, int a ) |
196 | { | 190 | { |
197 | double angle = deg2rad * ( - a + 180 ); | 191 | double angle = deg2rad * ( - a + 180 ); |
198 | double nx = c.x() - ( p.x() - c.x() ) * cos( angle ) - | 192 | double nx = c.x() - ( p.x() - c.x() ) * cos( angle ) - |
199 | ( p.y() - c.y() ) * sin( angle ); | 193 | ( p.y() - c.y() ) * sin( angle ); |
200 | double ny = c.y() - ( p.y() - c.y() ) * cos( angle ) + | 194 | double ny = c.y() - ( p.y() - c.y() ) * cos( angle ) + |
201 | ( p.x() - c.x() ) * sin( angle ); | 195 | ( p.x() - c.x() ) * sin( angle ); |
202 | return QPoint( int(nx), int(ny) ); | 196 | return QPoint( int(nx), int(ny) ); |
203 | } | 197 | } |