author | kergoth <kergoth> | 2002-01-25 22:14:26 (UTC) |
---|---|---|
committer | kergoth <kergoth> | 2002-01-25 22:14:26 (UTC) |
commit | 15318cad33835e4e2dc620d033e43cd930676cdd (patch) (unidiff) | |
tree | c2fa0399a2c47fda8e2cd0092c73a809d17f68eb /noncore/tools/clock/clock.cpp | |
download | opie-15318cad33835e4e2dc620d033e43cd930676cdd.zip opie-15318cad33835e4e2dc620d033e43cd930676cdd.tar.gz opie-15318cad33835e4e2dc620d033e43cd930676cdd.tar.bz2 |
Initial revision
Diffstat (limited to 'noncore/tools/clock/clock.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/tools/clock/clock.cpp | 319 |
1 files changed, 319 insertions, 0 deletions
diff --git a/noncore/tools/clock/clock.cpp b/noncore/tools/clock/clock.cpp new file mode 100644 index 0000000..ef93e11 --- a/dev/null +++ b/noncore/tools/clock/clock.cpp | |||
@@ -0,0 +1,319 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | ||
3 | ** | ||
4 | ** This file is part of Qtopia Environment. | ||
5 | ** | ||
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 | ||
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | ||
9 | ** packaging of this file. | ||
10 | ** | ||
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. | ||
13 | ** | ||
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | ||
15 | ** | ||
16 | ** Contact info@trolltech.com if any conditions of this licensing are | ||
17 | ** not clear to you. | ||
18 | ** | ||
19 | **********************************************************************/ | ||
20 | |||
21 | #include "clock.h" | ||
22 | |||
23 | #include <qpe/qpeapplication.h> | ||
24 | #include <qpe/qcopenvelope_qws.h> | ||
25 | #include <qpe/config.h> | ||
26 | #include <qpe/timestring.h> | ||
27 | |||
28 | #include <qlcdnumber.h> | ||
29 | #include <qlabel.h> | ||
30 | #include <qlayout.h> | ||
31 | #include <qtimer.h> | ||
32 | #include <qpushbutton.h> | ||
33 | #include <qradiobutton.h> | ||
34 | #include <qbuttongroup.h> | ||
35 | #include <qpainter.h> | ||
36 | |||
37 | #include <math.h> | ||
38 | |||
39 | const double deg2rad = 0.017453292519943295769;// pi/180 | ||
40 | const int sw_prec = 2; | ||
41 | |||
42 | static void toggleScreenSaver( bool on ) | ||
43 | { | ||
44 | QCopEnvelope e("QPE/System", "setScreenSaverMode(int)" ); | ||
45 | e << (on ? QPEApplication::Enable: QPEApplication::DisableSuspend ); | ||
46 | } | ||
47 | |||
48 | Clock::Clock( QWidget * parent, const char * name, WFlags f ) | ||
49 | : QVBox( parent, name , f ) | ||
50 | { | ||
51 | setSpacing( 4 ); | ||
52 | setMargin( 1 ); | ||
53 | |||
54 | Config config( "qpe" ); | ||
55 | config.setGroup("Time"); | ||
56 | ampm = config.readBoolEntry( "AMPM", TRUE ); | ||
57 | |||
58 | aclock = new AnalogClock( this ); | ||
59 | aclock->display( QTime::currentTime() ); | ||
60 | aclock->setLineWidth( 2 ); | ||
61 | |||
62 | QHBox *hb = new QHBox( this ); | ||
63 | hb->setMargin( 0 ); | ||
64 | QWidget *space = new QWidget( hb ); | ||
65 | lcd = new QLCDNumber( hb ); | ||
66 | lcd->setSegmentStyle( QLCDNumber::Flat ); | ||
67 | lcd->setFrameStyle( QFrame::NoFrame ); | ||
68 | lcd->setSizePolicy( QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum ) ); | ||
69 | lcd->setFixedHeight( 23 ); | ||
70 | |||
71 | ampmLabel = new QLabel( "PM", hb ); | ||
72 | ampmLabel->setFont( QFont( "Helvetica", 14, QFont::Bold ) ); | ||
73 | ampmLabel->setSizePolicy( QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Preferred ) ); | ||
74 | ampmLabel->setAlignment( AlignLeft | AlignBottom ); | ||
75 | space = new QWidget( hb ); | ||
76 | |||
77 | date = new QLabel( this ); | ||
78 | date->setAlignment( AlignHCenter | AlignVCenter ); | ||
79 | date->setFont( QFont( "Helvetica", 14, QFont::Bold ) ); | ||
80 | date->setText( TimeString::longDateString( QDate::currentDate() ) ); | ||
81 | |||
82 | QWidget *controls = new QWidget( this ); | ||
83 | QGridLayout *gl = new QGridLayout( controls, 2, 2, 6, 4 ); | ||
84 | |||
85 | QButtonGroup *grp = new QButtonGroup( controls ); | ||
86 | grp->setRadioButtonExclusive( true ); | ||
87 | grp->hide(); | ||
88 | |||
89 | clockRB = new QRadioButton ( tr( "Clock" ), controls ); | ||
90 | gl->addWidget( clockRB, 0, 0 ); | ||
91 | grp->insert( clockRB ); | ||
92 | |||
93 | swatchRB = new QRadioButton ( tr( "Stopwatch" ), controls ); | ||
94 | gl->addWidget( swatchRB, 1, 0 ); | ||
95 | grp->insert( swatchRB ); | ||
96 | |||
97 | connect( grp, SIGNAL(clicked(int)), this, SLOT(modeSelect(int)) ); | ||
98 | grp->setButton( 0 ); | ||
99 | |||
100 | set = new QPushButton ( controls ); | ||
101 | gl->addWidget( set, 0, 1 ); | ||
102 | set->setText( tr( "Start" ) ); | ||
103 | set->setEnabled( FALSE ); | ||
104 | grp->insert( set ); | ||
105 | |||
106 | reset = new QPushButton ( controls ); | ||
107 | gl->addWidget( reset, 1, 1 ); | ||
108 | reset->setText( tr( "Reset" ) ); | ||
109 | reset->setEnabled( FALSE ); | ||
110 | grp->insert( reset ); | ||
111 | |||
112 | connect( set, SIGNAL( pressed() ), SLOT( slotSet() ) ); | ||
113 | connect( reset, SIGNAL( clicked() ), SLOT( slotReset() ) ); | ||
114 | |||
115 | t = new QTimer( this ); | ||
116 | connect( t, SIGNAL( timeout() ), SLOT( updateClock() ) ); | ||
117 | t->start( 1000 ); | ||
118 | |||
119 | connect( qApp, SIGNAL( timeChanged() ), SLOT( updateClock() ) ); | ||
120 | |||
121 | swatch_running = FALSE; | ||
122 | swatch_totalms = 0; | ||
123 | |||
124 | connect( qApp, SIGNAL(clockChanged(bool)), this, SLOT(changeClock(bool)) ); | ||
125 | |||
126 | QTimer::singleShot( 0, this, SLOT(updateClock()) ); | ||
127 | modeSelect(0); | ||
128 | } | ||
129 | |||
130 | Clock::~Clock() | ||
131 | { | ||
132 | toggleScreenSaver( true ); | ||
133 | } | ||
134 | |||
135 | void Clock::updateClock() | ||
136 | { | ||
137 | if ( clockRB->isChecked() ) { | ||
138 | QTime tm = QDateTime::currentDateTime().time(); | ||
139 | QString s; | ||
140 | if ( ampm ) { | ||
141 | int hour = tm.hour(); | ||
142 | if (hour == 0) | ||
143 | hour = 12; | ||
144 | if (hour > 12) | ||
145 | hour -= 12; | ||
146 | s.sprintf( "%2d%c%02d", hour, ':', tm.minute() ); | ||
147 | ampmLabel->setText( (tm.hour() >= 12) ? "PM" : "AM" ); | ||
148 | ampmLabel->show(); | ||
149 | } else { | ||
150 | s.sprintf( "%2d%c%02d", tm.hour(), ':', tm.minute() ); | ||
151 | ampmLabel->hide(); | ||
152 | } | ||
153 | lcd->display( s ); | ||
154 | lcd->repaint( FALSE ); | ||
155 | aclock->display( QTime::currentTime() ); | ||
156 | date->setText( TimeString::longDateString( QDate::currentDate() ) ); | ||
157 | } else { | ||
158 | QTime swatch_time; | ||
159 | QString lcdtext; | ||
160 | int totalms = swatch_totalms; | ||
161 | if ( swatch_running ) | ||
162 | totalms += swatch_start.elapsed(); | ||
163 | swatch_time = QTime(0,0,0).addMSecs(totalms); | ||
164 | QString d = swatch_running ? QString(" ") | ||
165 | : QString::number(totalms%1000+1000); | ||
166 | lcdtext = swatch_time.toString() + "." + d.right(3).left(sw_prec); | ||
167 | lcd->display( lcdtext ); | ||
168 | lcd->repaint( FALSE ); | ||
169 | aclock->display( swatch_time ); | ||
170 | date->setText( TimeString::longDateString( QDate::currentDate() ) ); | ||
171 | } | ||
172 | } | ||
173 | |||
174 | void Clock::changeClock( bool a ) | ||
175 | { | ||
176 | ampm = a; | ||
177 | updateClock(); | ||
178 | } | ||
179 | |||
180 | void Clock::clearClock( void ) | ||
181 | { | ||
182 | lcd->display( QTime( 0,0,0 ).toString() ); | ||
183 | aclock->display( QTime( 0,0,0 ) ); | ||
184 | } | ||
185 | |||
186 | void Clock::slotSet() | ||
187 | { | ||
188 | if ( t->isActive() ) { | ||
189 | swatch_totalms += swatch_start.elapsed(); | ||
190 | set->setText( tr( "Start" ) ); | ||
191 | t->stop(); | ||
192 | swatch_running = FALSE; | ||
193 | toggleScreenSaver( TRUE ); | ||
194 | updateClock(); | ||
195 | } else { | ||
196 | swatch_start.start(); | ||
197 | set->setText( tr( "Stop" ) ); | ||
198 | t->start( 1000 ); | ||
199 | swatch_running = TRUE; | ||
200 | // disable screensaver while stop watch is running | ||
201 | toggleScreenSaver( FALSE ); | ||
202 | } | ||
203 | } | ||
204 | |||
205 | void Clock::slotReset() | ||
206 | { | ||
207 | t->stop(); | ||
208 | swatch_start.start(); | ||
209 | swatch_totalms = 0; | ||
210 | |||
211 | if (swatch_running ) | ||
212 | t->start(1000); | ||
213 | |||
214 | updateClock(); | ||
215 | } | ||
216 | |||
217 | void Clock::modeSelect( int m ) | ||
218 | { | ||
219 | if ( m ) { | ||
220 | lcd->setNumDigits( 8+1+sw_prec ); | ||
221 | lcd->setMinimumWidth( lcd->sizeHint().width() ); | ||
222 | set->setEnabled( TRUE ); | ||
223 | reset->setEnabled( TRUE ); | ||
224 | ampmLabel->hide(); | ||
225 | |||
226 | if ( !swatch_running ) | ||
227 | t->stop(); | ||
228 | } else { | ||
229 | lcd->setNumDigits( 5 ); | ||
230 | lcd->setMinimumWidth( lcd->sizeHint().width() ); | ||
231 | set->setEnabled( FALSE ); | ||
232 | reset->setEnabled( FALSE ); | ||
233 | t->start(1000); | ||
234 | } | ||
235 | updateClock(); | ||
236 | } | ||
237 | |||
238 | QSizePolicy AnalogClock::sizePolicy() const | ||
239 | { | ||
240 | return QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ); | ||
241 | } | ||
242 | |||
243 | void AnalogClock::drawContents( QPainter *p ) | ||
244 | { | ||
245 | QRect r = contentsRect(); | ||
246 | QPoint center( r.x() + r.width() / 2, r.y() + r.height() / 2 ); | ||
247 | |||
248 | QPoint l1( r.x() + r.width() / 2, r.y() + 2 ); | ||
249 | QPoint l2( r.x() + r.width() / 2, r.y() + 8 ); | ||
250 | |||
251 | QPoint h1( r.x() + r.width() / 2, r.y() + r.height() / 4 ); | ||
252 | QPoint h2( r.x() + r.width() / 2, r.y() + r.height() / 2 ); | ||
253 | |||
254 | QPoint m1( r.x() + r.width() / 2, r.y() + r.height() / 8 ); | ||
255 | QPoint m2( r.x() + r.width() / 2, r.y() + r.height() / 2 ); | ||
256 | |||
257 | QPoint s1( r.x() + r.width() / 2, r.y() + 8 ); | ||
258 | QPoint s2( r.x() + r.width() / 2, r.y() + r.height() / 2 ); | ||
259 | |||
260 | QColor color( clear ? backgroundColor() : black ); | ||
261 | QTime time = clear ? prevTime : currTime; | ||
262 | |||
263 | if ( clear && prevTime.secsTo(currTime) > 1 ) { | ||
264 | p->eraseRect( rect() ); | ||
265 | return; | ||
266 | } | ||
267 | |||
268 | if ( !clear ) { | ||
269 | // draw ticks | ||
270 | p->setPen( QPen( color, 1 ) ); | ||
271 | for ( int i = 0; i < 12; i++ ) | ||
272 | p->drawLine( rotate( center, l1, i * 30 ), rotate( center, l2, i * 30 ) ); | ||
273 | } | ||
274 | |||
275 | if ( !clear || prevTime.minute() != currTime.minute() || | ||
276 | prevTime.hour() != currTime.hour() ) { | ||
277 | // draw hour pointer | ||
278 | h1 = rotate( center, h1, 30 * ( time.hour() % 12 ) + time.minute() / 2 ); | ||
279 | h2 = rotate( center, h2, 30 * ( time.hour() % 12 ) + time.minute() / 2 ); | ||
280 | p->setPen( QPen( color, 3 ) ); | ||
281 | p->drawLine( h1, h2 ); | ||
282 | } | ||
283 | |||
284 | if ( !clear || prevTime.minute() != currTime.minute() ) { | ||
285 | // draw minute pointer | ||
286 | m1 = rotate( center, m1, time.minute() * 6 ); | ||
287 | m2 = rotate( center, m2, time.minute() * 6 ); | ||
288 | p->setPen( QPen( color, 2 ) ); | ||
289 | p->drawLine( m1, m2 ); | ||
290 | } | ||
291 | |||
292 | // draw second pointer | ||
293 | s1 = rotate( center, s1, time.second() * 6 ); | ||
294 | s2 = rotate( center, s2, time.second() * 6 ); | ||
295 | p->setPen( QPen( color, 1 ) ); | ||
296 | p->drawLine( s1, s2 ); | ||
297 | |||
298 | if ( !clear ) | ||
299 | prevTime = currTime; | ||
300 | } | ||
301 | |||
302 | void AnalogClock::display( const QTime& t ) | ||
303 | { | ||
304 | currTime = t; | ||
305 | clear = true; | ||
306 | repaint( false ); | ||
307 | clear = false; | ||
308 | repaint( false ); | ||
309 | } | ||
310 | |||
311 | QPoint AnalogClock::rotate( QPoint c, QPoint p, int a ) | ||
312 | { | ||
313 | double angle = deg2rad * ( - a + 180 ); | ||
314 | double nx = c.x() - ( p.x() - c.x() ) * cos( angle ) - | ||
315 | ( p.y() - c.y() ) * sin( angle ); | ||
316 | double ny = c.y() - ( p.y() - c.y() ) * cos( angle ) + | ||
317 | ( p.x() - c.x() ) * sin( angle ); | ||
318 | return QPoint( nx, ny ); | ||
319 | } | ||