-rw-r--r-- | noncore/tools/clock/clock.cpp | 157 |
1 files changed, 157 insertions, 0 deletions
diff --git a/noncore/tools/clock/clock.cpp b/noncore/tools/clock/clock.cpp index ef93e11..d5d77bc 100644 --- a/noncore/tools/clock/clock.cpp +++ b/noncore/tools/clock/clock.cpp | |||
@@ -10,60 +10,72 @@ | |||
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 "clock.h" | 21 | #include "clock.h" |
22 | #include "setAlarm.h" | ||
22 | 23 | ||
23 | #include <qpe/qpeapplication.h> | 24 | #include <qpe/qpeapplication.h> |
24 | #include <qpe/qcopenvelope_qws.h> | 25 | #include <qpe/qcopenvelope_qws.h> |
25 | #include <qpe/config.h> | 26 | #include <qpe/config.h> |
26 | #include <qpe/timestring.h> | 27 | #include <qpe/timestring.h> |
28 | #include <qpe/alarmserver.h> | ||
29 | #include <qpe/sound.h> | ||
30 | #include <qpe/resource.h> | ||
31 | #include <qsound.h> | ||
32 | #include <qtimer.h> | ||
27 | 33 | ||
28 | #include <qlcdnumber.h> | 34 | #include <qlcdnumber.h> |
35 | #include <qslider.h> | ||
29 | #include <qlabel.h> | 36 | #include <qlabel.h> |
30 | #include <qlayout.h> | 37 | #include <qlayout.h> |
31 | #include <qtimer.h> | 38 | #include <qtimer.h> |
32 | #include <qpushbutton.h> | 39 | #include <qpushbutton.h> |
33 | #include <qradiobutton.h> | 40 | #include <qradiobutton.h> |
34 | #include <qbuttongroup.h> | 41 | #include <qbuttongroup.h> |
35 | #include <qpainter.h> | 42 | #include <qpainter.h> |
43 | #include <qmessagebox.h> | ||
44 | #include <qdatetime.h> | ||
36 | 45 | ||
37 | #include <math.h> | 46 | #include <math.h> |
38 | 47 | ||
39 | const double deg2rad = 0.017453292519943295769;// pi/180 | 48 | const double deg2rad = 0.017453292519943295769;// pi/180 |
40 | const int sw_prec = 2; | 49 | const int sw_prec = 2; |
41 | 50 | ||
42 | static void toggleScreenSaver( bool on ) | 51 | static void toggleScreenSaver( bool on ) |
43 | { | 52 | { |
44 | QCopEnvelope e("QPE/System", "setScreenSaverMode(int)" ); | 53 | QCopEnvelope e("QPE/System", "setScreenSaverMode(int)" ); |
45 | e << (on ? QPEApplication::Enable: QPEApplication::DisableSuspend ); | 54 | e << (on ? QPEApplication::Enable: QPEApplication::DisableSuspend ); |
46 | } | 55 | } |
47 | 56 | ||
48 | Clock::Clock( QWidget * parent, const char * name, WFlags f ) | 57 | Clock::Clock( QWidget * parent, const char * name, WFlags f ) |
49 | : QVBox( parent, name , f ) | 58 | : QVBox( parent, name , f ) |
50 | { | 59 | { |
51 | setSpacing( 4 ); | 60 | setSpacing( 4 ); |
52 | setMargin( 1 ); | 61 | setMargin( 1 ); |
53 | 62 | ||
54 | Config config( "qpe" ); | 63 | Config config( "qpe" ); |
55 | config.setGroup("Time"); | 64 | config.setGroup("Time"); |
56 | ampm = config.readBoolEntry( "AMPM", TRUE ); | 65 | ampm = config.readBoolEntry( "AMPM", TRUE ); |
57 | 66 | ||
67 | snoozeBtn = new QPushButton ( this); | ||
68 | snoozeBtn->setText( tr( "Snooze" ) ); | ||
69 | |||
58 | aclock = new AnalogClock( this ); | 70 | aclock = new AnalogClock( this ); |
59 | aclock->display( QTime::currentTime() ); | 71 | aclock->display( QTime::currentTime() ); |
60 | aclock->setLineWidth( 2 ); | 72 | aclock->setLineWidth( 2 ); |
61 | 73 | ||
62 | QHBox *hb = new QHBox( this ); | 74 | QHBox *hb = new QHBox( this ); |
63 | hb->setMargin( 0 ); | 75 | hb->setMargin( 0 ); |
64 | QWidget *space = new QWidget( hb ); | 76 | QWidget *space = new QWidget( hb ); |
65 | lcd = new QLCDNumber( hb ); | 77 | lcd = new QLCDNumber( hb ); |
66 | lcd->setSegmentStyle( QLCDNumber::Flat ); | 78 | lcd->setSegmentStyle( QLCDNumber::Flat ); |
67 | lcd->setFrameStyle( QFrame::NoFrame ); | 79 | lcd->setFrameStyle( QFrame::NoFrame ); |
68 | lcd->setSizePolicy( QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum ) ); | 80 | lcd->setSizePolicy( QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum ) ); |
69 | lcd->setFixedHeight( 23 ); | 81 | lcd->setFixedHeight( 23 ); |
@@ -89,49 +101,82 @@ Clock::Clock( QWidget * parent, const char * name, WFlags f ) | |||
89 | clockRB = new QRadioButton ( tr( "Clock" ), controls ); | 101 | clockRB = new QRadioButton ( tr( "Clock" ), controls ); |
90 | gl->addWidget( clockRB, 0, 0 ); | 102 | gl->addWidget( clockRB, 0, 0 ); |
91 | grp->insert( clockRB ); | 103 | grp->insert( clockRB ); |
92 | 104 | ||
93 | swatchRB = new QRadioButton ( tr( "Stopwatch" ), controls ); | 105 | swatchRB = new QRadioButton ( tr( "Stopwatch" ), controls ); |
94 | gl->addWidget( swatchRB, 1, 0 ); | 106 | gl->addWidget( swatchRB, 1, 0 ); |
95 | grp->insert( swatchRB ); | 107 | grp->insert( swatchRB ); |
96 | 108 | ||
97 | connect( grp, SIGNAL(clicked(int)), this, SLOT(modeSelect(int)) ); | 109 | connect( grp, SIGNAL(clicked(int)), this, SLOT(modeSelect(int)) ); |
98 | grp->setButton( 0 ); | 110 | grp->setButton( 0 ); |
99 | 111 | ||
100 | set = new QPushButton ( controls ); | 112 | set = new QPushButton ( controls ); |
113 | set->setMaximumSize(50,30); | ||
101 | gl->addWidget( set, 0, 1 ); | 114 | gl->addWidget( set, 0, 1 ); |
102 | set->setText( tr( "Start" ) ); | 115 | set->setText( tr( "Start" ) ); |
103 | set->setEnabled( FALSE ); | 116 | set->setEnabled( FALSE ); |
104 | grp->insert( set ); | 117 | grp->insert( set ); |
105 | 118 | ||
106 | reset = new QPushButton ( controls ); | 119 | reset = new QPushButton ( controls ); |
107 | gl->addWidget( reset, 1, 1 ); | 120 | gl->addWidget( reset, 1, 1 ); |
108 | reset->setText( tr( "Reset" ) ); | 121 | reset->setText( tr( "Reset" ) ); |
109 | reset->setEnabled( FALSE ); | 122 | reset->setEnabled( FALSE ); |
110 | grp->insert( reset ); | 123 | grp->insert( reset ); |
111 | 124 | ||
125 | alarmOffBtn = new QPushButton ( controls ); | ||
126 | alarmOffBtn->setMaximumSize(60,30); | ||
127 | gl->addWidget( alarmOffBtn, 0, 2 ); | ||
128 | |||
129 | alarmBtn = new QPushButton ( controls ); | ||
130 | alarmBtn->setMaximumSize(60,30); | ||
131 | gl->addWidget( alarmBtn, 1, 2 ); | ||
132 | alarmBtn->setText( tr( "Set Alarm" ) ); | ||
133 | |||
112 | connect( set, SIGNAL( pressed() ), SLOT( slotSet() ) ); | 134 | connect( set, SIGNAL( pressed() ), SLOT( slotSet() ) ); |
113 | connect( reset, SIGNAL( clicked() ), SLOT( slotReset() ) ); | 135 | connect( reset, SIGNAL( clicked() ), SLOT( slotReset() ) ); |
114 | 136 | ||
137 | connect( alarmBtn, SIGNAL( clicked() ), SLOT( slotSetAlarm() ) ); | ||
138 | connect( snoozeBtn, SIGNAL( clicked() ), SLOT( slotSnooze() ) ); | ||
139 | connect( alarmOffBtn, SIGNAL( clicked() ), SLOT( slotToggleAlarm() ) ); | ||
140 | |||
141 | connect( qApp, SIGNAL(appMessage(const QCString&, const QByteArray&)), | ||
142 | this, SLOT(appMessage(const QCString&, const QByteArray&)) ); | ||
143 | |||
115 | t = new QTimer( this ); | 144 | t = new QTimer( this ); |
116 | connect( t, SIGNAL( timeout() ), SLOT( updateClock() ) ); | 145 | connect( t, SIGNAL( timeout() ), SLOT( updateClock() ) ); |
117 | t->start( 1000 ); | 146 | t->start( 1000 ); |
118 | 147 | ||
119 | connect( qApp, SIGNAL( timeChanged() ), SLOT( updateClock() ) ); | 148 | connect( qApp, SIGNAL( timeChanged() ), SLOT( updateClock() ) ); |
120 | 149 | ||
121 | swatch_running = FALSE; | 150 | swatch_running = FALSE; |
122 | swatch_totalms = 0; | 151 | swatch_totalms = 0; |
123 | 152 | ||
124 | connect( qApp, SIGNAL(clockChanged(bool)), this, SLOT(changeClock(bool)) ); | 153 | connect( qApp, SIGNAL(clockChanged(bool)), this, SLOT(changeClock(bool)) ); |
125 | 154 | ||
155 | QString tmp = config.readEntry("clockAlarmHour", ""); | ||
156 | bool ok; | ||
157 | hour = tmp.toInt(&ok,10); | ||
158 | tmp = config.readEntry("clockAlarmMinute",""); | ||
159 | minute = tmp.toInt(&ok,10); | ||
160 | |||
161 | if( config.readEntry("clockAlarmSet","FALSE") == "TRUE") { | ||
162 | alarmOffBtn->setText( tr( "Alarm On" ) ); | ||
163 | alarmBool=TRUE; | ||
164 | snoozeBtn->show(); | ||
165 | } else { | ||
166 | alarmOffBtn->setText( tr( "Alarm Off" ) ); | ||
167 | alarmBool=FALSE; | ||
168 | snoozeBtn->hide(); | ||
169 | } | ||
170 | |||
126 | QTimer::singleShot( 0, this, SLOT(updateClock()) ); | 171 | QTimer::singleShot( 0, this, SLOT(updateClock()) ); |
127 | modeSelect(0); | 172 | modeSelect(0); |
128 | } | 173 | } |
129 | 174 | ||
130 | Clock::~Clock() | 175 | Clock::~Clock() |
131 | { | 176 | { |
132 | toggleScreenSaver( true ); | 177 | toggleScreenSaver( true ); |
133 | } | 178 | } |
134 | 179 | ||
135 | void Clock::updateClock() | 180 | void Clock::updateClock() |
136 | { | 181 | { |
137 | if ( clockRB->isChecked() ) { | 182 | if ( clockRB->isChecked() ) { |
@@ -226,24 +271,136 @@ void Clock::modeSelect( int m ) | |||
226 | if ( !swatch_running ) | 271 | if ( !swatch_running ) |
227 | t->stop(); | 272 | t->stop(); |
228 | } else { | 273 | } else { |
229 | lcd->setNumDigits( 5 ); | 274 | lcd->setNumDigits( 5 ); |
230 | lcd->setMinimumWidth( lcd->sizeHint().width() ); | 275 | lcd->setMinimumWidth( lcd->sizeHint().width() ); |
231 | set->setEnabled( FALSE ); | 276 | set->setEnabled( FALSE ); |
232 | reset->setEnabled( FALSE ); | 277 | reset->setEnabled( FALSE ); |
233 | t->start(1000); | 278 | t->start(1000); |
234 | } | 279 | } |
235 | updateClock(); | 280 | updateClock(); |
236 | } | 281 | } |
237 | 282 | ||
283 | //this sets the alarm time | ||
284 | void Clock::slotSetAlarm() | ||
285 | { | ||
286 | if( !snoozeBtn->isHidden()) | ||
287 | slotToggleAlarm(); | ||
288 | Set_Alarm *setAlarmDlg; | ||
289 | setAlarmDlg = new Set_Alarm(this,"SetAlarm", TRUE); | ||
290 | int result = setAlarmDlg->exec(); | ||
291 | if(result == 1) { | ||
292 | Config config( "qpe" ); | ||
293 | config.setGroup("Time"); | ||
294 | QString tmp; | ||
295 | hour = setAlarmDlg->Hour_Slider->value(); | ||
296 | minute = setAlarmDlg->Minute_Slider->value(); | ||
297 | snoozeTime=setAlarmDlg->SnoozeSlider->value(); | ||
298 | if(ampm) { | ||
299 | if(setAlarmDlg->Pm_RadioButton->isChecked() && hour < 12 ) | ||
300 | hour+=12; | ||
301 | } | ||
302 | config.writeEntry("clockAlarmHour", tmp.setNum( hour ),10); | ||
303 | config.writeEntry("clockAlarmMinute",tmp.setNum( minute ),10); | ||
304 | config.writeEntry("clockAlarmSnooze",tmp.setNum( snoozeTime ),10); | ||
305 | config.write(); | ||
306 | } | ||
307 | } | ||
308 | |||
309 | void Clock::slotSnooze() | ||
310 | { | ||
311 | bSound=FALSE; | ||
312 | int warn; | ||
313 | QTime t = QTime::currentTime(); | ||
314 | QDateTime whenl( when.date(), t.addSecs( snoozeTime*60)); | ||
315 | when=whenl; | ||
316 | AlarmServer::addAlarm( when, | ||
317 | "QPE/Application/clock", | ||
318 | "alarm(QDateTime,int)", warn ); | ||
319 | |||
320 | } | ||
321 | |||
322 | //toggles alarm on/off | ||
323 | void Clock::slotToggleAlarm() | ||
324 | { | ||
325 | Config config( "qpe" ); | ||
326 | config.setGroup("Time"); | ||
327 | if(alarmBool) { | ||
328 | config.writeEntry("clockAlarmSet","FALSE"); | ||
329 | alarmOffBtn->setText( tr( "Alarm Off" ) ); | ||
330 | snoozeBtn->hide(); | ||
331 | alarmBool=FALSE; | ||
332 | alarmOff(); | ||
333 | } else { | ||
334 | config.writeEntry("clockAlarmSet","TRUE"); | ||
335 | alarmOffBtn->setText( tr( "Alarm On" ) ); | ||
336 | snoozeBtn->show(); | ||
337 | alarmBool=TRUE; | ||
338 | alarmOn(); | ||
339 | } | ||
340 | config.write(); | ||
341 | } | ||
342 | |||
343 | void Clock::alarmOn() | ||
344 | { | ||
345 | QDate d = QDate::currentDate(); | ||
346 | QTime tm((int)hour,(int)minute,0); | ||
347 | qDebug("Time set "+tm.toString()); | ||
348 | QTime t = QTime::currentTime(); | ||
349 | if( t > tm) | ||
350 | d = d.addDays(1); | ||
351 | int warn; | ||
352 | QDateTime whenl(d,tm); | ||
353 | when=whenl; | ||
354 | AlarmServer::addAlarm( when, | ||
355 | "QPE/Application/clock", | ||
356 | "alarm(QDateTime,int)", warn ); | ||
357 | QMessageBox::message("Note","Alarm is set for:\n"+ whenl.toString()); | ||
358 | } | ||
359 | |||
360 | void Clock::alarmOff() | ||
361 | { | ||
362 | int warn; | ||
363 | bSound=FALSE; | ||
364 | AlarmServer::deleteAlarm( when, | ||
365 | "QPE/Application/clock", | ||
366 | "alarm(QDateTime,int)", warn ); | ||
367 | qDebug("Alarm Off "+ when.toString()); | ||
368 | |||
369 | } | ||
370 | |||
371 | void Clock::appMessage(const QCString& msg, const QByteArray& data) | ||
372 | { | ||
373 | int stopTimer = 0; | ||
374 | int timerStay = 5000; | ||
375 | bSound=TRUE; | ||
376 | if ( msg == "alarm(QDateTime,int)" ) { | ||
377 | Sound::soundAlarm(); | ||
378 | stopTimer = startTimer( timerStay); | ||
379 | } | ||
380 | } | ||
381 | |||
382 | void Clock::timerEvent( QTimerEvent *e ) | ||
383 | { | ||
384 | static int stop = 0; | ||
385 | if ( stop < 10 && bSound) { | ||
386 | Sound::soundAlarm(); | ||
387 | stop++; | ||
388 | } else { | ||
389 | stop = 0; | ||
390 | killTimer( e->timerId() ); | ||
391 | } | ||
392 | } | ||
393 | |||
394 | |||
238 | QSizePolicy AnalogClock::sizePolicy() const | 395 | QSizePolicy AnalogClock::sizePolicy() const |
239 | { | 396 | { |
240 | return QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ); | 397 | return QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ); |
241 | } | 398 | } |
242 | 399 | ||
243 | void AnalogClock::drawContents( QPainter *p ) | 400 | void AnalogClock::drawContents( QPainter *p ) |
244 | { | 401 | { |
245 | QRect r = contentsRect(); | 402 | QRect r = contentsRect(); |
246 | QPoint center( r.x() + r.width() / 2, r.y() + r.height() / 2 ); | 403 | QPoint center( r.x() + r.width() / 2, r.y() + r.height() / 2 ); |
247 | 404 | ||
248 | QPoint l1( r.x() + r.width() / 2, r.y() + 2 ); | 405 | QPoint l1( r.x() + r.width() / 2, r.y() + 2 ); |
249 | QPoint l2( r.x() + r.width() / 2, r.y() + 8 ); | 406 | QPoint l2( r.x() + r.width() / 2, r.y() + 8 ); |