-rw-r--r-- | noncore/tools/clock/clock.cpp | 1012 |
1 files changed, 577 insertions, 435 deletions
diff --git a/noncore/tools/clock/clock.cpp b/noncore/tools/clock/clock.cpp index 485354b..ad422a6 100644 --- a/noncore/tools/clock/clock.cpp +++ b/noncore/tools/clock/clock.cpp | |||
@@ -1,10 +1,10 @@ | |||
1 | /********************************************************************** | 1 | /********************************************************************** |
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | 2 | ** Copyright (C) 2000-2002 Trolltech AS. All rights reserved. |
3 | ** | 3 | ** |
4 | ** This file is part of 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 | ** |
@@ -15,650 +15,792 @@ | |||
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 | // changes added and Copyright (C) by L. J. Potter <ljp@llornkcor.com> 2002 | 20 | // changes added and Copyright (C) by L. J. Potter <ljp@llornkcor.com> 2002 |
21 | // changes added and Copyright (C) by Holger Freyther 2004 | ||
21 | 22 | ||
22 | #include "clock.h" | 23 | #include "clock.h" |
23 | #include "setAlarm.h" | 24 | |
25 | #include "analogclock.h" | ||
26 | |||
27 | #include <qtabwidget.h> | ||
28 | |||
29 | #include <opie2/ofiledialog.h> | ||
24 | 30 | ||
25 | #include <qpe/qpeapplication.h> | 31 | #include <qpe/qpeapplication.h> |
26 | #include <qpe/qcopenvelope_qws.h> | 32 | #include <qpe/qcopenvelope_qws.h> |
27 | #include <qpe/config.h> | 33 | #include <qpe/config.h> |
28 | #include <qpe/timestring.h> | 34 | #include <qpe/timestring.h> |
29 | #include <qpe/alarmserver.h> | 35 | #include <qpe/alarmserver.h> |
30 | #include <qpe/sound.h> | 36 | #include <qpe/sound.h> |
31 | #include <qpe/resource.h> | 37 | #include <qpe/resource.h> |
32 | #include <qsound.h> | 38 | #include <qsound.h> |
33 | #include <qtimer.h> | 39 | #include <qtimer.h> |
34 | 40 | ||
35 | #include <opie2/oclickablelabel.h> | 41 | |
36 | 42 | ||
37 | #include <qlcdnumber.h> | 43 | #include <qlcdnumber.h> |
38 | #include <qslider.h> | 44 | #include <qslider.h> |
39 | #include <qlabel.h> | 45 | #include <qlabel.h> |
40 | #include <qlayout.h> | ||
41 | #include <qtimer.h> | 46 | #include <qtimer.h> |
42 | #include <qpushbutton.h> | 47 | #include <qpushbutton.h> |
43 | #include <qradiobutton.h> | 48 | #include <qtoolbutton.h> |
44 | #include <qbuttongroup.h> | ||
45 | #include <qpainter.h> | 49 | #include <qpainter.h> |
46 | #include <qmessagebox.h> | 50 | #include <qmessagebox.h> |
47 | #include <qdatetime.h> | 51 | #include <qdatetime.h> |
52 | #include <qspinbox.h> | ||
53 | #include <qcombobox.h> | ||
54 | #include <qcheckbox.h> | ||
55 | #include <qgroupbox.h> | ||
56 | #include <qlayout.h> | ||
57 | #include <qhbox.h> | ||
58 | #include <qlineedit.h> | ||
59 | |||
60 | static const int sw_prec = 2; | ||
61 | static const int magic_daily = 2292922; | ||
62 | static const int magic_countdown = 2292923; | ||
63 | static const int magic_snooze = 2292924; | ||
64 | static const int magic_playmp = 2292925; | ||
48 | 65 | ||
49 | #include <math.h> | 66 | #include <math.h> |
50 | #include <unistd.h> | 67 | #include <unistd.h> |
51 | #include <sys/types.h> | 68 | #include <sys/types.h> |
52 | 69 | ||
53 | #include <pthread.h> | 70 | #include <pthread.h> |
54 | 71 | ||
55 | 72 | ||
56 | const double deg2rad = 0.017453292519943295769; // pi/180 | 73 | static void toggleScreenSaver( bool on ) |
57 | const int sw_prec = 2; | 74 | { |
75 | QCopEnvelope e( "QPE/System", "setScreenSaverMode(int)" ); | ||
76 | e << ( on ? QPEApplication::Enable : QPEApplication::DisableSuspend ); | ||
77 | } | ||
58 | 78 | ||
59 | using namespace Opie::Ui; | 79 | static void startPlayer() |
60 | void startPlayer() | ||
61 | { | 80 | { |
62 | Config config( "qpe" ); | 81 | Config config( "qpe" ); |
63 | config.setGroup( "Time" ); | 82 | config.setGroup( "Time" ); |
64 | sleep(15); | 83 | sleep(15); |
65 | QCopEnvelope e( "QPE/Application/opieplayer", "setDocument(QString)" ); | 84 | QCopEnvelope e( "QPE/Application/opieplayer", "setDocument(QString)" ); |
66 | e << config.readEntry( "mp3File", "" ); | 85 | e << config.readEntry( "mp3File", "" ); |
67 | } | 86 | } |
68 | 87 | ||
69 | 88 | class MySpinBox : public QSpinBox | |
70 | static void toggleScreenSaver( bool on ) | ||
71 | { | 89 | { |
72 | QCopEnvelope e( "QPE/System", "setScreenSaverMode(int)" ); | 90 | public: |
73 | e << ( on ? QPEApplication::Enable : QPEApplication::DisableSuspend ); | 91 | QLineEdit *lineEdit() const { |
92 | return editor(); | ||
74 | } | 93 | } |
94 | }; | ||
75 | 95 | ||
76 | Clock::Clock( QWidget * parent, const char * name, WFlags f ) | 96 | // |
77 | : QVBox( parent, name , f ) | 97 | // |
98 | // | ||
99 | AlarmDlg::AlarmDlg(QWidget *parent, const char *name, bool modal, | ||
100 | const QString &txt) : | ||
101 | AlarmDlgBase(parent, name, modal) | ||
78 | { | 102 | { |
79 | setSpacing( 4 ); | 103 | setCaption( tr("Clock") ); |
80 | setMargin( 1 ); | 104 | pixmap->setPixmap( Resource::loadPixmap("alarmbell") ); |
81 | 105 | alarmDlgLabel->setText(txt); | |
82 | |||
83 | snoozeBtn = new QPushButton ( this ); | ||
84 | snoozeBtn->setText( tr( "Snooze" ) ); | ||
85 | |||
86 | aclock = new AnalogClock( this ); | ||
87 | aclock->display( QTime::currentTime() ); | ||
88 | aclock->setLineWidth( 2 ); | ||
89 | |||
90 | QHBox *hb = new QHBox( this ); | ||
91 | hb->setMargin( 0 ); | ||
92 | QWidget *space = new QWidget( hb ); | ||
93 | lcd = new QLCDNumber( hb ); | ||
94 | lcd->setSegmentStyle( QLCDNumber::Flat ); | ||
95 | lcd->setFrameStyle( QFrame::NoFrame ); | ||
96 | lcd->setSizePolicy( QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum ) ); | ||
97 | lcd->setFixedHeight( 23 ); | ||
98 | 106 | ||
99 | ampmLabel = new QLabel( tr( "PM" ), hb ); | 107 | connect(snoozeTime, SIGNAL(valueChanged(int)), this, |
100 | ampmLabel->setFont( QFont( "Helvetica", 14, QFont::Bold ) ); | 108 | SLOT(changePrompt(int))); |
101 | ampmLabel->setSizePolicy( QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Preferred ) ); | 109 | connect(cmdOk, SIGNAL(clicked()), this, SLOT(checkSnooze())); |
102 | ampmLabel->setAlignment( AlignLeft | AlignBottom ); | 110 | } |
103 | space = new QWidget( hb ); | ||
104 | 111 | ||
105 | date = new QLabel( this ); | 112 | // |
106 | date->setAlignment( AlignHCenter | AlignVCenter ); | 113 | // |
107 | date->setFont( QFont( "Helvetica", 14, QFont::Bold ) ); | 114 | // |
108 | date->setText( TimeString::longDateString( QDate::currentDate() ) ); | 115 | void |
116 | AlarmDlg::setText(const QString &txt) | ||
117 | { | ||
118 | alarmDlgLabel->setText(txt); | ||
119 | } | ||
109 | 120 | ||
110 | QWidget *controls = new QWidget( this ); | 121 | // |
111 | QGridLayout *gl = new QGridLayout( controls, 3, 2, 6, 4 ); | 122 | // |
123 | // | ||
124 | void | ||
125 | AlarmDlg::checkSnooze(void) | ||
126 | { | ||
127 | // | ||
128 | // Ensure we have only one snooze alarm. | ||
129 | // | ||
130 | AlarmServer::deleteAlarm(QDateTime(), "QPE/Application/clock", | ||
131 | "alarm(QDateTime,int)", magic_snooze); | ||
112 | 132 | ||
113 | QButtonGroup *grp = new QButtonGroup( controls ); | 133 | if (snoozeTime->value() > 0) { |
114 | grp->setRadioButtonExclusive( true ); | 134 | QDateTime wake = QDateTime::currentDateTime(); |
115 | grp->hide(); | 135 | wake = wake.addSecs(snoozeTime->value() * 60); // snoozeTime in minutes |
116 | 136 | ||
117 | clockRB = new QRadioButton ( tr( "Clock" ), controls ); | 137 | AlarmServer::addAlarm(wake, "QPE/Application/clock", |
118 | gl->addWidget( clockRB, 0, 0 ); | 138 | "alarm(QDateTime,int)", magic_snooze); |
119 | grp->insert( clockRB ); | 139 | } |
140 | accept(); | ||
141 | } | ||
120 | 142 | ||
121 | swatchRB = new QRadioButton ( tr( "Stopwatch" ), controls ); | ||
122 | gl->addWidget( swatchRB, 1, 0 ); | ||
123 | grp->insert( swatchRB ); | ||
124 | 143 | ||
125 | connect( grp, SIGNAL( clicked(int) ), this, SLOT( modeSelect(int) ) ); | ||
126 | grp->setButton( 0 ); | ||
127 | 144 | ||
128 | set = new QPushButton ( controls ); | 145 | void |
129 | set->setMaximumSize( 50, 30 ); | 146 | AlarmDlg::changePrompt(int mins) |
130 | gl->addWidget( set , 0, 1 ); | 147 | { |
131 | set->setText( tr( "Start" ) ); | 148 | cmdOk->setText(mins > 0 ? tr("Snooze") : tr("Close") ); |
132 | set->setEnabled( FALSE ); | 149 | } |
133 | grp->insert( set ); | ||
134 | 150 | ||
135 | reset = new QPushButton ( controls ); | ||
136 | gl->addWidget( reset, 1, 1 ); | ||
137 | reset->setText( tr( "Reset" ) ); | ||
138 | reset->setEnabled( FALSE ); | ||
139 | grp->insert( reset ); | ||
140 | 151 | ||
141 | alarmOffBtn = new QPushButton ( controls ); | ||
142 | gl->addWidget( alarmOffBtn, 0, 2 ); | ||
143 | 152 | ||
144 | alarmBtn = new QPushButton ( controls ); | 153 | Clock::Clock( QWidget * parent, const char *, WFlags f ) |
145 | gl->addWidget( alarmBtn, 1, 2 ); | 154 | : ClockBase( parent, "clock", f ), swatch_splitms(99), init(FALSE) // No tr |
155 | { | ||
156 | alarmDlg = 0; | ||
157 | swLayout = 0; | ||
158 | dayBtn = new QToolButton * [7]; | ||
146 | 159 | ||
147 | alarmBtn->setText( tr( "Set Alarm" ) ); | 160 | Config config( "qpe" ); |
161 | config.setGroup("Time"); | ||
162 | ampm = config.readBoolEntry( "AMPM", TRUE ); | ||
163 | onMonday = config.readBoolEntry( "MONDAY" ); | ||
148 | 164 | ||
149 | OClickableLabel *click = new Opie::Ui::OClickableLabel( controls, "label" ); | 165 | connect( tabs, SIGNAL(currentChanged(QWidget*)), |
150 | click->setText( tr( "Set date and time." ) ); | 166 | this, SLOT(tabChanged(QWidget*)) ); |
151 | gl->addMultiCellWidget( click, 3, 3, 0, 2, AlignHCenter ); | ||
152 | connect( click, SIGNAL( clicked() ), this, SLOT( slotAdjustTime() ) ); | ||
153 | 167 | ||
154 | connect( set , SIGNAL( pressed() ), SLOT( slotSet() ) ); | 168 | analogStopwatch = new AnalogClock( swFrame ); |
155 | connect( reset, SIGNAL( clicked() ), SLOT( slotReset() ) ); | 169 | stopwatchLcd = new QLCDNumber( swFrame ); |
170 | stopwatchLcd->setFrameStyle( QFrame::NoFrame ); | ||
171 | stopwatchLcd->setSegmentStyle( QLCDNumber::Flat ); | ||
172 | stopwatchLcd->setSizePolicy( QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred) ); | ||
156 | 173 | ||
157 | connect( alarmBtn, SIGNAL( clicked() ), SLOT( slotSetAlarm() ) ); | 174 | analogClock->display( QTime::currentTime() ); |
158 | connect( snoozeBtn, SIGNAL( clicked() ), SLOT( slotSnooze() ) ); | 175 | clockLcd->setNumDigits( 5 ); |
159 | connect( alarmOffBtn, SIGNAL( clicked() ), SLOT( slotToggleAlarm() ) ); | 176 | clockLcd->setFixedWidth( clockLcd->sizeHint().width() ); |
177 | date->setText( TimeString::dateString( QDate::currentDate(), TimeString::currentDateFormat() ) ); | ||
178 | if ( qApp->desktop()->width() < 200 ) | ||
179 | date->setFont( QFont(date->font().family(), 14, QFont::Bold) ); | ||
180 | if ( qApp->desktop()->height() > 240 ) { | ||
181 | clockLcd->setFixedHeight( 30 ); | ||
182 | stopwatchLcd->setFixedHeight( 30 ); | ||
183 | } | ||
160 | 184 | ||
161 | connect( qApp, SIGNAL( appMessage(const QCString&,const QByteArray&) ), | 185 | connect( stopStart, SIGNAL(pressed()), SLOT(stopStartStopWatch()) ); |
162 | this, SLOT( appMessage(const QCString&,const QByteArray&) ) ); | 186 | connect( reset, SIGNAL(pressed()), SLOT(resetStopWatch()) ); |
163 | 187 | ||
164 | t = new QTimer( this ); | 188 | t = new QTimer( this ); |
165 | connect( t, SIGNAL( timeout() ), SLOT( updateClock() ) ); | 189 | connect( t, SIGNAL( timeout() ), SLOT( updateClock() ) ); |
166 | t->start( 1000 ); | 190 | t->start( 1000 ); |
167 | 191 | ||
192 | applyAlarmTimer = new QTimer( this ); | ||
193 | connect( applyAlarmTimer, SIGNAL(timeout()), | ||
194 | this, SLOT(applyDailyAlarm()) ); | ||
195 | |||
196 | alarmt = new QTimer( this ); | ||
197 | connect( alarmt, SIGNAL(timeout()), SLOT(alarmTimeout()) ); | ||
198 | |||
168 | connect( qApp, SIGNAL( timeChanged() ), SLOT( updateClock() ) ); | 199 | connect( qApp, SIGNAL( timeChanged() ), SLOT( updateClock() ) ); |
200 | connect( qApp, SIGNAL(timeChanged()), SLOT(applyDailyAlarm()) ); | ||
169 | 201 | ||
170 | swatch_running = FALSE; | 202 | swatch_running = FALSE; |
171 | swatch_totalms = 0; | 203 | swatch_totalms = 0; |
204 | swatch_currLap = 0; | ||
205 | swatch_dispLap = 0; | ||
206 | stopwatchLcd->setNumDigits( 8+1+sw_prec ); | ||
207 | stopwatchLcd->display( "00:00:00.00" ); | ||
208 | |||
209 | QVBoxLayout *lvb = new QVBoxLayout( lapFrame ); | ||
210 | nextLapBtn = new QToolButton( UpArrow, lapFrame ); | ||
211 | connect( nextLapBtn, SIGNAL(clicked()), this, SLOT(nextLap()) ); | ||
212 | nextLapBtn->setAccel( Key_Up ); | ||
213 | lvb->addWidget( nextLapBtn ); | ||
214 | prevLapBtn = new QToolButton( DownArrow, lapFrame ); | ||
215 | connect( prevLapBtn, SIGNAL(clicked()), this, SLOT(prevLap()) ); | ||
216 | prevLapBtn->setAccel( Key_Down ); | ||
217 | prevLapBtn->setMinimumWidth( 15 ); | ||
218 | lvb->addWidget( prevLapBtn ); | ||
219 | prevLapBtn->setEnabled( FALSE ); | ||
220 | nextLapBtn->setEnabled( FALSE ); | ||
172 | 221 | ||
173 | connect( qApp, SIGNAL( clockChanged(bool) ), this, SLOT( changeClock(bool) ) ); | 222 | reset->setEnabled( FALSE ); |
174 | 223 | ||
224 | lapLcd->setNumDigits( 8+1+sw_prec ); | ||
225 | lapLcd->display( "00:00:00.00" ); | ||
175 | 226 | ||
176 | Config config( "qpe" ); | 227 | splitLcd->setNumDigits( 8+1+sw_prec ); |
177 | config.setGroup( "Time" ); | 228 | splitLcd->display( "00:00:00.00" ); |
178 | ampm = config.readBoolEntry( "AMPM", TRUE ); | ||
179 | 229 | ||
180 | QString tmp = config.readEntry( "clockAlarmHour", "" ); | 230 | lapNumLcd->display( 1 ); |
181 | bool ok; | ||
182 | hour = tmp.toInt( &ok, 10 ); | ||
183 | tmp = config.readEntry( "clockAlarmMinute", "" ); | ||
184 | minute = tmp.toInt( &ok, 10 ); | ||
185 | 231 | ||
186 | if ( config.readEntry( "clockAlarmSet", "FALSE" ) == "TRUE" ) | 232 | lapTimer = new QTimer( this ); |
187 | { | 233 | connect( lapTimer, SIGNAL(timeout()), this, SLOT(lapTimeout()) ); |
188 | alarmOffBtn->setText( tr( "Alarm Is On" ) ); | 234 | |
189 | alarmBool = TRUE; | 235 | for (uint s = 0; s < swatch_splitms.count(); s++ ) |
190 | snoozeBtn->show(); | 236 | swatch_splitms[(int)s] = 0; |
237 | |||
238 | connect( qApp, SIGNAL(clockChanged(bool)), this, SLOT(changeClock(bool)) ); | ||
239 | |||
240 | cdGroup->hide(); // XXX implement countdown timer. | ||
241 | |||
242 | connect( dailyHour, SIGNAL(valueChanged(int)), this, SLOT(scheduleApplyDailyAlarm()) ); | ||
243 | connect( dailyMinute, SIGNAL(valueChanged(int)), this, SLOT(setDailyMinute(int)) ); | ||
244 | connect( dailyAmPm, SIGNAL(activated(int)), this, SLOT(setDailyAmPm(int)) ); | ||
245 | connect( dailyEnabled, SIGNAL(toggled(bool)), this, SLOT(enableDaily(bool)) ); | ||
246 | cdLcd->display( "00:00" ); | ||
247 | |||
248 | dailyMinute->setValidator(0); | ||
249 | |||
250 | Config cConfig( "Clock" ); // No tr | ||
251 | cConfig.setGroup( "Daily Alarm" ); | ||
252 | |||
253 | QStringList days; | ||
254 | days.append( tr("Mon", "Monday") ); | ||
255 | days.append( tr("Tue", "Tuesday") ); | ||
256 | days.append( tr("Wed", "Wednesday") ); | ||
257 | days.append( tr("Thu", "Thursday") ); | ||
258 | days.append( tr("Fri", "Friday") ); | ||
259 | days.append( tr("Sat", "Saturday") ); | ||
260 | days.append( tr("Sun", "Sunday") ); | ||
261 | |||
262 | int i; | ||
263 | QHBoxLayout *hb = new QHBoxLayout( daysFrame ); | ||
264 | for ( i = 0; i < 7; i++ ) { | ||
265 | dayBtn[i] = new QToolButton( daysFrame ); | ||
266 | hb->addWidget( dayBtn[i] ); | ||
267 | dayBtn[i]->setToggleButton( TRUE ); | ||
268 | dayBtn[i]->setOn( TRUE ); | ||
269 | dayBtn[i]->setFocusPolicy( StrongFocus ); | ||
270 | connect( dayBtn[i], SIGNAL(toggled(bool)), this, SLOT(scheduleApplyDailyAlarm()) ); | ||
271 | } | ||
272 | |||
273 | for ( i = 0; i < 7; i++ ) | ||
274 | dayBtn[dayBtnIdx(i+1)]->setText( days[i] ); | ||
275 | |||
276 | QStringList exclDays = cConfig.readListEntry( "ExcludeDays", ',' ); | ||
277 | QStringList::Iterator it; | ||
278 | for ( it = exclDays.begin(); it != exclDays.end(); ++it ) { | ||
279 | int d = (*it).toInt(); | ||
280 | if ( d >= 1 && d <= 7 ) | ||
281 | dayBtn[dayBtnIdx(d)]->setOn( FALSE ); | ||
282 | } | ||
283 | |||
284 | bool alarm = cConfig.readBoolEntry("Enabled", FALSE); | ||
285 | bool sound = cConfig.readBoolEntry("SoundEnabled", FALSE ); | ||
286 | dailyEnabled->setChecked( alarm ); | ||
287 | sndGroup->setEnabled( alarm ); | ||
288 | sndCheck->setChecked( sound ); | ||
289 | sndChoose->setEnabled( sound ); | ||
290 | sndFileName->setEnabled( sound ); | ||
291 | |||
292 | // FIXME ODP migrate to own config class.. merge config options | ||
293 | Config cfg_qpe( "qpe" ); | ||
294 | cfg_qpe.setGroup( "Time" ); | ||
295 | sndFileName->setText( cfg_qpe.readEntry( "mp3File" ) ); | ||
296 | // | ||
297 | |||
298 | int m = cConfig.readNumEntry( "Minute", 0 ); | ||
299 | dailyMinute->setValue( m ); | ||
300 | // dailyMinute->setPrefix( m <= 9 ? "0" : "" ); | ||
301 | int h = cConfig.readNumEntry( "Hour", 7 ); | ||
302 | if ( ampm ) { | ||
303 | if (h > 12) { | ||
304 | h -= 12; | ||
305 | dailyAmPm->setCurrentItem( 1 ); | ||
191 | } | 306 | } |
192 | else | 307 | if (h == 0) h = 12; |
193 | { | 308 | dailyHour->setMinValue( 1 ); |
194 | alarmOffBtn->setText( tr( "Alarm Is Off" ) ); | 309 | dailyHour->setMaxValue( 12 ); |
195 | alarmBool = FALSE; | 310 | } else { |
196 | snoozeBtn->hide(); | 311 | dailyAmPm->hide(); |
197 | } | 312 | } |
313 | dailyHour->setValue( h ); | ||
314 | |||
315 | connect( ((MySpinBox*)dailyHour)->lineEdit(), SIGNAL(textChanged(const QString&)), | ||
316 | this, SLOT(dailyEdited()) ); | ||
317 | connect( ((MySpinBox*)dailyMinute)->lineEdit(), SIGNAL(textChanged(const QString&)), | ||
318 | this, SLOT(dailyEdited()) ); | ||
319 | |||
320 | #if defined(Q_WS_QWS) && !defined(QT_NO_COP) | ||
321 | connect( qApp, SIGNAL(appMessage(const QCString&,const QByteArray&)), | ||
322 | this, SLOT(appMessage(const QCString&,const QByteArray&)) ); | ||
323 | #endif | ||
198 | 324 | ||
199 | QTimer::singleShot( 0, this, SLOT( updateClock() ) ); | 325 | QTimer::singleShot( 0, this, SLOT( updateClock() ) ); |
326 | swFrame->installEventFilter( this ); | ||
200 | 327 | ||
201 | Config cfg( "Clock" ); | 328 | init = TRUE; |
202 | cfg.setGroup( "Mode" ); | ||
203 | int mode = cfg.readBoolEntry( "clockMode"); | ||
204 | setSwatchMode( mode); | ||
205 | modeSelect( mode); | ||
206 | } | 329 | } |
207 | 330 | ||
208 | Clock::~Clock() | 331 | Clock::~Clock() |
209 | { | 332 | { |
210 | toggleScreenSaver( true ); | 333 | toggleScreenSaver( true ); |
334 | delete [] dayBtn; | ||
211 | } | 335 | } |
212 | 336 | ||
213 | void Clock::updateClock() | 337 | void Clock::updateClock() |
214 | { | 338 | { |
215 | if ( clockRB->isChecked() ) | 339 | if ( tabs->currentPageIndex() == 0 ) { |
216 | { | ||
217 | QTime tm = QDateTime::currentDateTime().time(); | 340 | QTime tm = QDateTime::currentDateTime().time(); |
218 | QString s; | 341 | QString s; |
219 | if ( ampm ) | 342 | if ( ampm ) { |
220 | { | ||
221 | int hour = tm.hour(); | 343 | int hour = tm.hour(); |
222 | if ( hour == 0 ) | 344 | if ( hour == 0 ) |
223 | hour = 12; | 345 | hour = 12; |
224 | if ( hour > 12 ) | 346 | if ( hour > 12 ) |
225 | hour -= 12; | 347 | hour -= 12; |
226 | s.sprintf( "%2d%c%02d", hour, ':', tm.minute() ); | 348 | s.sprintf( "%2d%c%02d", hour, ':', tm.minute() ); |
227 | ampmLabel->setText( ( tm.hour() >= 12 ) ? "PM" : "AM" ); | 349 | clockAmPm->setText( (tm.hour() >= 12) ? "PM" : "AM" ); |
228 | ampmLabel->show(); | 350 | clockAmPm->show(); |
229 | } | 351 | } else { |
230 | else | ||
231 | { | ||
232 | s.sprintf( "%2d%c%02d", tm.hour(), ':', tm.minute() ); | 352 | s.sprintf( "%2d%c%02d", tm.hour(), ':', tm.minute() ); |
233 | ampmLabel->hide(); | 353 | clockAmPm->hide(); |
234 | } | ||
235 | lcd->display( s ); | ||
236 | lcd->repaint( FALSE ); | ||
237 | aclock->display( QTime::currentTime() ); | ||
238 | date->setText( TimeString::longDateString( QDate::currentDate() ) ); | ||
239 | } | 354 | } |
240 | else | 355 | clockLcd->display( s ); |
241 | { | 356 | clockLcd->repaint( FALSE ); |
242 | QTime swatch_time; | 357 | analogClock->display( QTime::currentTime() ); |
243 | QString lcdtext; | 358 | date->setText( TimeString::dateString( QDate::currentDate(), TimeString::currentDateFormat() ) ); |
359 | } else if ( tabs->currentPageIndex() == 1 ) { | ||
244 | int totalms = swatch_totalms; | 360 | int totalms = swatch_totalms; |
245 | if ( swatch_running ) | 361 | if ( swatch_running ) |
246 | totalms += swatch_start.elapsed(); | 362 | totalms += swatch_start.elapsed(); |
247 | swatch_time = QTime( 0, 0, 0 ).addMSecs( totalms ); | 363 | setSwatchLcd( stopwatchLcd, totalms, !swatch_running ); |
248 | QString d = swatch_running ? QString( " " ) | 364 | QTime swatch_time = QTime(0,0,0).addMSecs(totalms); |
249 | : QString::number( totalms % 1000 + 1000 ); | 365 | analogStopwatch->display( swatch_time ); |
250 | lcdtext = swatch_time.toString() + "." + d.right( 3 ).left( sw_prec ); | 366 | if ( swatch_dispLap == swatch_currLap ) { |
251 | lcd->display( lcdtext ); | 367 | swatch_splitms[swatch_currLap] = swatch_totalms; |
252 | lcd->repaint( FALSE ); | 368 | if ( swatch_running ) |
253 | aclock->display( swatch_time ); | 369 | swatch_splitms[swatch_currLap] += swatch_start.elapsed(); |
254 | date->setText( TimeString::longDateString( QDate::currentDate() ) ); | 370 | updateLap(); |
371 | } | ||
372 | } else if ( tabs->currentPageIndex() == 2 ) { | ||
373 | // nothing. | ||
255 | } | 374 | } |
256 | } | 375 | } |
257 | 376 | ||
258 | void Clock::changeClock( bool a ) | 377 | void Clock::changeClock( bool a ) |
259 | { | 378 | { |
379 | if ( ampm != a ) { | ||
380 | int minute = dailyMinute->value(); | ||
381 | int hour = dailyHour->value(); | ||
382 | if ( ampm ) { | ||
383 | if (hour == 12) | ||
384 | hour = 0; | ||
385 | if (dailyAmPm->currentItem() == 1 ) | ||
386 | hour += 12; | ||
387 | dailyHour->setMinValue( 0 ); | ||
388 | dailyHour->setMaxValue( 23 ); | ||
389 | dailyAmPm->hide(); | ||
390 | } else { | ||
391 | if (hour > 12) { | ||
392 | hour -= 12; | ||
393 | dailyAmPm->setCurrentItem( 1 ); | ||
394 | } | ||
395 | if (hour == 0) hour = 12; | ||
396 | dailyHour->setMinValue( 1 ); | ||
397 | dailyHour->setMaxValue( 12 ); | ||
398 | dailyAmPm->show(); | ||
399 | } | ||
400 | dailyMinute->setValue( minute ); | ||
401 | dailyHour->setValue( hour ); | ||
402 | } | ||
260 | ampm = a; | 403 | ampm = a; |
261 | updateClock(); | 404 | updateClock(); |
262 | } | 405 | } |
263 | 406 | ||
264 | void Clock::clearClock( void ) | 407 | void Clock::stopStartStopWatch() |
265 | { | ||
266 | lcd->display( QTime( 0, 0, 0 ).toString() ); | ||
267 | aclock->display( QTime( 0, 0, 0 ) ); | ||
268 | } | ||
269 | |||
270 | void Clock::startSWatch() | ||
271 | { | 408 | { |
272 | if(swatch_running) { | 409 | if(swatch_running) { |
273 | stopSWatch(); | 410 | swatch_totalms += swatch_start.elapsed(); |
274 | return; | 411 | swatch_splitms[swatch_currLap] = swatch_totalms; |
275 | } | 412 | stopStart->setText( tr("Start") ); |
276 | qDebug("startSWatch()"); | 413 | reset->setText( tr("Reset") ); |
414 | reset->setEnabled( TRUE ); | ||
415 | t->stop(); | ||
416 | swatch_running = FALSE; | ||
417 | toggleScreenSaver( TRUE ); | ||
418 | updateClock(); | ||
419 | } else { | ||
277 | swatch_start.start(); | 420 | swatch_start.start(); |
278 | set->setText( tr( "Stop" ) ); | 421 | stopStart->setText( tr("Stop") ); |
422 | reset->setText( tr("Lap/Split") ); | ||
423 | reset->setEnabled( swatch_currLap < 98 ); | ||
279 | t->start( 1000 ); | 424 | t->start( 1000 ); |
280 | swatch_running = TRUE; | 425 | swatch_running = TRUE; |
281 | // disable screensaver while stop watch is running | 426 | // disable screensaver while stop watch is running |
282 | toggleScreenSaver( FALSE ); | 427 | toggleScreenSaver( FALSE ); |
283 | } | 428 | } |
429 | swatch_dispLap = swatch_currLap; | ||
430 | updateLap(); | ||
431 | prevLapBtn->setEnabled( swatch_dispLap ); | ||
432 | nextLapBtn->setEnabled( swatch_dispLap < swatch_currLap ); | ||
433 | stopStart->setAccel( Key_Return ); | ||
434 | } | ||
284 | 435 | ||
285 | void Clock::stopSWatch() | 436 | void Clock::resetStopWatch() |
286 | { | 437 | { |
287 | qDebug("stopSWatch()"); | 438 | if ( swatch_running ) { |
288 | swatch_totalms += swatch_start.elapsed(); | 439 | swatch_splitms[swatch_currLap] = swatch_totalms+swatch_start.elapsed(); |
289 | set->setText( tr( "Start" ) ); | 440 | swatch_dispLap = swatch_currLap; |
290 | t->stop(); | 441 | if ( swatch_currLap < 98 ) // allow up to 99 laps |
291 | swatch_running = FALSE; | 442 | swatch_currLap++; |
292 | toggleScreenSaver( TRUE ); | 443 | reset->setEnabled( swatch_currLap < 98 ); |
444 | updateLap(); | ||
445 | lapTimer->start( 2000, TRUE ); | ||
446 | } else { | ||
447 | swatch_start.start(); | ||
448 | swatch_totalms = 0; | ||
449 | swatch_currLap = 0; | ||
450 | swatch_dispLap = 0; | ||
451 | for ( uint i = 0; i < swatch_splitms.count(); i++ ) | ||
452 | swatch_splitms[(int)i] = 0; | ||
453 | updateLap(); | ||
293 | updateClock(); | 454 | updateClock(); |
455 | reset->setText( tr("Lap/Split") ); | ||
456 | reset->setEnabled( FALSE ); | ||
457 | } | ||
458 | prevLapBtn->setEnabled( swatch_dispLap ); | ||
459 | nextLapBtn->setEnabled( swatch_dispLap < swatch_currLap ); | ||
294 | } | 460 | } |
295 | 461 | ||
296 | 462 | void Clock::prevLap() | |
297 | void Clock::slotSet() | ||
298 | { | ||
299 | if ( t->isActive() ) | ||
300 | { | 463 | { |
301 | startSWatch(); | 464 | if ( swatch_dispLap > 0 ) { |
465 | swatch_dispLap--; | ||
466 | updateLap(); | ||
467 | prevLapBtn->setEnabled( swatch_dispLap ); | ||
468 | nextLapBtn->setEnabled( swatch_dispLap < swatch_currLap ); | ||
302 | } | 469 | } |
303 | else | 470 | } |
471 | |||
472 | void Clock::nextLap() | ||
304 | { | 473 | { |
305 | stopSWatch(); | 474 | if ( swatch_dispLap < swatch_currLap ) { |
475 | swatch_dispLap++; | ||
476 | updateLap(); | ||
477 | prevLapBtn->setEnabled( swatch_dispLap ); | ||
478 | nextLapBtn->setEnabled( swatch_dispLap < swatch_currLap ); | ||
306 | } | 479 | } |
307 | } | 480 | } |
308 | 481 | ||
309 | void Clock::slotReset() | 482 | void Clock::lapTimeout() |
310 | { | 483 | { |
311 | t->stop(); | 484 | swatch_dispLap = swatch_currLap; |
312 | swatch_start.start(); | 485 | updateLap(); |
313 | swatch_totalms = 0; | 486 | prevLapBtn->setEnabled( swatch_dispLap ); |
487 | nextLapBtn->setEnabled( swatch_dispLap < swatch_currLap ); | ||
488 | } | ||
314 | 489 | ||
315 | if ( swatch_running ) | 490 | void Clock::updateLap() |
316 | t->start( 1000 ); | 491 | { |
492 | if ( swatch_running && swatch_currLap == swatch_dispLap ) { | ||
493 | swatch_splitms[swatch_currLap] = swatch_totalms; | ||
494 | swatch_splitms[swatch_currLap] += swatch_start.elapsed(); | ||
495 | } | ||
496 | int split = swatch_splitms[swatch_dispLap]; | ||
497 | int lap; | ||
498 | if ( swatch_dispLap > 0 ) | ||
499 | lap = swatch_splitms[swatch_dispLap] - swatch_splitms[swatch_dispLap-1]; | ||
500 | else | ||
501 | lap = swatch_splitms[swatch_dispLap]; | ||
317 | 502 | ||
318 | updateClock(); | 503 | lapNumLcd->display( swatch_dispLap+1 ); |
504 | bool showMs = !swatch_running || swatch_dispLap!=swatch_currLap; | ||
505 | setSwatchLcd( lapLcd, lap, showMs ); | ||
506 | setSwatchLcd( splitLcd, split, showMs ); | ||
319 | } | 507 | } |
320 | 508 | ||
321 | void Clock::modeSelect( int m ) | 509 | void Clock::setSwatchLcd( QLCDNumber *lcd, int ms, bool showMs ) |
322 | { | 510 | { |
323 | qDebug("Clock::modeSelect( %d) ", m); | 511 | QTime swatch_time = QTime(0,0,0).addMSecs(ms); |
324 | if ( m != 0 ) | 512 | QString d = showMs ? QString::number(ms%1000+1000) : QString(" "); |
325 | { | 513 | QString lcdtext = swatch_time.toString() + "." + d.right(3).left(sw_prec); |
326 | lcd->setNumDigits( 8 + 1 + sw_prec ); | 514 | lcd->display( lcdtext ); |
327 | lcd->setMinimumWidth( lcd->sizeHint().width() ); | 515 | lcd->repaint( FALSE ); |
328 | set->setEnabled( TRUE ); | 516 | } |
329 | reset->setEnabled( TRUE ); | ||
330 | ampmLabel->hide(); | ||
331 | 517 | ||
332 | // if(m == 2) { | 518 | bool Clock::eventFilter( QObject *o, QEvent *e ) |
333 | if ( !swatch_running ) { | 519 | { |
334 | t->start( 1000 ); | 520 | if ( o == swFrame && e->type() == QEvent::Resize ) { |
521 | QResizeEvent *re = (QResizeEvent *)e; | ||
522 | delete swLayout; | ||
523 | if ( re->size().height() < 80 || re->size().height()*3 < re->size().width() ) | ||
524 | swLayout = new QHBoxLayout( swFrame ); | ||
525 | else | ||
526 | swLayout = new QVBoxLayout( swFrame ); | ||
527 | swLayout->addWidget( analogStopwatch ); | ||
528 | swLayout->addWidget( stopwatchLcd ); | ||
529 | swLayout->activate(); | ||
335 | } | 530 | } |
336 | // else | 531 | |
337 | // { | 532 | return FALSE; |
338 | // stopSWatch(); | ||
339 | // } | ||
340 | // } | ||
341 | } | 533 | } |
342 | else //clock mode | 534 | |
535 | void Clock::tabChanged( QWidget * ) | ||
343 | { | 536 | { |
344 | lcd->setNumDigits( 5 ); | 537 | if ( tabs->currentPageIndex() == 0 ) { |
345 | lcd->setMinimumWidth( lcd->sizeHint().width() ); | 538 | t->start(1000); |
346 | set->setEnabled( FALSE ); | 539 | } else if ( tabs->currentPageIndex() == 1 ) { |
347 | reset->setEnabled( FALSE ); | 540 | if ( !swatch_running ) |
541 | t->stop(); | ||
542 | stopStart->setAccel( Key_Return ); | ||
543 | } else if ( tabs->currentPageIndex() == 2 ) { | ||
348 | t->start( 1000 ); | 544 | t->start( 1000 ); |
349 | } | 545 | } |
350 | |||
351 | Config config( "Clock" ); | ||
352 | config.setGroup( "Mode" ); | ||
353 | config.writeEntry( "clockMode", m ); | ||
354 | updateClock(); | 546 | updateClock(); |
355 | } | 547 | } |
356 | 548 | ||
357 | //this sets the alarm time | 549 | void Clock::setDailyAmPm(int) |
358 | void Clock::slotSetAlarm() | ||
359 | { | 550 | { |
360 | if ( !snoozeBtn->isHidden() ) | 551 | scheduleApplyDailyAlarm(); |
361 | slotToggleAlarm(); | ||
362 | Set_Alarm *setAlarmDlg; | ||
363 | setAlarmDlg = new Set_Alarm( this, "SetAlarm", TRUE ); | ||
364 | int result = setAlarmDlg->exec(); | ||
365 | if ( result == 1 ) { | ||
366 | Config config( "qpe" ); | ||
367 | config.setGroup( "Time" ); | ||
368 | QString tmp; | ||
369 | hour = setAlarmDlg->Hour_Slider->value(); | ||
370 | minute = setAlarmDlg->Minute_Slider->value(); | ||
371 | snoozeTime = setAlarmDlg->SnoozeSlider->value(); | ||
372 | if ( ampm ) { | ||
373 | if ( hour == 12 ) | ||
374 | hour = 0; | ||
375 | |||
376 | if ( setAlarmDlg->Pm_RadioButton->isChecked() && hour < 12 ) | ||
377 | hour += 12; | ||
378 | } | ||
379 | config.writeEntry( "clockAlarmHour", tmp.setNum( hour ), 10 ); | ||
380 | config.writeEntry( "clockAlarmMinute", tmp.setNum( minute ), 10 ); | ||
381 | config.writeEntry( "clockAlarmSnooze", tmp.setNum( snoozeTime ), 10 ); | ||
382 | config.write(); | ||
383 | } | ||
384 | } | 552 | } |
385 | 553 | ||
386 | void Clock::slotSnooze() | 554 | void Clock::setDailyMinute( int m ) |
387 | { | 555 | { |
388 | bSound = FALSE; | 556 | dailyMinute->setPrefix( m <= 9 ? "0" : "" ); |
389 | int warn = 0; | ||
390 | QTime t = QTime::currentTime(); | ||
391 | QDateTime whenl( when.date(), t.addSecs( snoozeTime * 60 ) ); | ||
392 | when = whenl; | ||
393 | AlarmServer::addAlarm( when, | ||
394 | "QPE/Application/clock", | ||
395 | "alarm(QDateTime,int)", warn ); | ||
396 | |||
397 | } | 557 | } |
398 | 558 | ||
399 | //toggles alarm on/off | 559 | void Clock::dailyEdited() |
400 | void Clock::slotToggleAlarm() | ||
401 | { | 560 | { |
402 | Config config( "qpe" ); | 561 | if ( spinBoxValid(dailyMinute) && spinBoxValid(dailyHour) ) |
403 | config.setGroup( "Time" ); | 562 | scheduleApplyDailyAlarm(); |
404 | if ( alarmBool ) | ||
405 | { | ||
406 | config.writeEntry( "clockAlarmSet", "FALSE" ); | ||
407 | alarmOffBtn->setText( tr( "Alarm Is Off" ) ); | ||
408 | snoozeBtn->hide(); | ||
409 | alarmBool = FALSE; | ||
410 | alarmOff(); | ||
411 | } | ||
412 | else | 563 | else |
413 | { | 564 | applyAlarmTimer->stop(); |
414 | config.writeEntry( "clockAlarmSet", "TRUE" ); | ||
415 | alarmOffBtn->setText( tr( "Alarm Is On" ) ); | ||
416 | snoozeBtn->show(); | ||
417 | alarmBool = TRUE; | ||
418 | alarmOn(); | ||
419 | } | ||
420 | config.write(); | ||
421 | } | ||
422 | |||
423 | void Clock::alarmOn() | ||
424 | { | ||
425 | QDate d = QDate::currentDate(); | ||
426 | QTime tm( ( int ) hour, ( int ) minute, 0 ); | ||
427 | qDebug( "Time set " + tm.toString() ); | ||
428 | QTime t = QTime::currentTime(); | ||
429 | if ( t > tm ) | ||
430 | d = d.addDays( 1 ); | ||
431 | int warn = 0; | ||
432 | QDateTime whenl( d, tm ); | ||
433 | when = whenl; | ||
434 | AlarmServer::addAlarm( when, | ||
435 | "QPE/Application/clock", | ||
436 | "alarm(QDateTime,int)", warn ); | ||
437 | // setCaption( "Alarm set: " + whenl.toString() ); | ||
438 | setCaption( tr("Alarm set: %1" ).arg(whenl.toString()) ); | ||
439 | } | ||
440 | |||
441 | void Clock::alarmOff() | ||
442 | { | ||
443 | int warn = 0; | ||
444 | bSound = FALSE; | ||
445 | AlarmServer::deleteAlarm( when, | ||
446 | "QPE/Application/clock", | ||
447 | "alarm(QDateTime,int)", warn ); | ||
448 | qDebug( "Alarm Off " + when.toString() ); | ||
449 | setCaption( tr("Clock") ); | ||
450 | } | 565 | } |
451 | 566 | ||
452 | void Clock::clearTimer() | 567 | void Clock::enableDaily( bool ) |
453 | { | 568 | { |
454 | alarmOffBtn->setText( tr( "Alarm Is Off" ) ); | 569 | scheduleApplyDailyAlarm(); |
455 | alarmBool = FALSE; | ||
456 | snoozeBtn->hide(); | ||
457 | setCaption( tr("Clock") ); | ||
458 | } | 570 | } |
459 | 571 | ||
460 | void Clock::appMessage( const QCString& msg, const QByteArray& /*data*/ ) | 572 | void Clock::appMessage( const QCString &msg, const QByteArray &data ) |
461 | { | ||
462 | int stopTimer = 0; | ||
463 | int timerStay = 5000; | ||
464 | bSound = TRUE; | ||
465 | qDebug( "Message received in clock" ); | ||
466 | if ( msg == "alarm(QDateTime,int)" ) | ||
467 | { | 573 | { |
468 | Config config( "qpe" ); | 574 | if ( msg == "alarm(QDateTime,int)" ) { |
469 | config.setGroup( "Time" ); | 575 | QDataStream ds(data,IO_ReadOnly); |
470 | if ( config.readBoolEntry( "mp3Alarm", 0 ) ) | 576 | QDateTime when; |
471 | { | 577 | int t; |
472 | clearTimer(); | 578 | ds >> when >> t; |
473 | pthread_t thread; | 579 | QTime theTime( when.time() ); |
474 | pthread_create(&thread, NULL, (void * (*) (void *))startPlayer, NULL/* &*/); | 580 | if ( t == magic_daily || t == magic_snooze || |
475 | 581 | t == magic_playmp ) { | |
582 | QString msg = tr("<b>Daily Alarm:</b><p>"); | ||
583 | QString ts; | ||
584 | if ( ampm ) { | ||
585 | bool pm = FALSE; | ||
586 | int h = theTime.hour(); | ||
587 | if (h > 12) { | ||
588 | h -= 12; | ||
589 | pm = TRUE; | ||
476 | } | 590 | } |
477 | else | 591 | if (h == 0) h = 12; |
478 | { | 592 | ts.sprintf( "%02d:%02d %s", h, theTime.minute(), pm?"PM":"AM" ); |
593 | } else { | ||
594 | ts.sprintf( "%02d:%02d", theTime.hour(), theTime.minute() ); | ||
595 | } | ||
596 | msg += ts; | ||
479 | 597 | ||
598 | if (t == magic_playmp ) { | ||
599 | pthread_t thread; | ||
600 | pthread_create(&thread,NULL, (void * (*) (void *))startPlayer, NULL/* &*/ ); | ||
601 | }else { | ||
480 | Sound::soundAlarm(); | 602 | Sound::soundAlarm(); |
481 | stopTimer = startTimer( timerStay ); | 603 | alarmCount = 0; |
604 | alarmt->start( 5000 ); | ||
482 | } | 605 | } |
606 | if ( !alarmDlg ) { | ||
607 | alarmDlg = new AlarmDlg(this); | ||
483 | } | 608 | } |
609 | alarmDlg->setText(msg); | ||
484 | 610 | ||
485 | if ( msg == "timerStart()" ) | 611 | // Set for tomorrow, so user wakes up every day, even if they |
486 | { | 612 | // don't confirm the dialog. Don't set it again when snoozing. |
487 | slotStartTimer(); | 613 | if (t != magic_snooze) { |
614 | applyDailyAlarm(); | ||
488 | } | 615 | } |
489 | if ( msg == "timerStop()" ) | 616 | |
490 | { | 617 | |
491 | slotStopTimer(); | 618 | if ( !alarmDlg->isVisible() ) { |
619 | QPEApplication::execDialog(alarmDlg); | ||
620 | alarmt->stop(); | ||
492 | } | 621 | } |
493 | if ( msg == "timerReset()" ) | 622 | } else if ( t == magic_countdown ) { |
494 | { | 623 | // countdown |
495 | slotResetTimer(); | 624 | Sound::soundAlarm(); |
496 | } | 625 | } |
497 | 626 | } else if ( msg == "setDailyEnabled(int)" ) { | |
498 | show(); | 627 | QDataStream ds(data,IO_ReadOnly); |
499 | raise(); | 628 | int enableDaily; |
629 | ds >> enableDaily; | ||
630 | dailyEnabled->setChecked( enableDaily ); | ||
631 | applyDailyAlarm(); | ||
632 | } else if ( msg == "editDailyAlarm()" ) { | ||
633 | tabs->setCurrentPage(2); | ||
634 | QPEApplication::setKeepRunning(); | ||
635 | } else if (msg == "showClock()") { | ||
636 | tabs->setCurrentPage(0); | ||
500 | QPEApplication::setKeepRunning(); | 637 | QPEApplication::setKeepRunning(); |
501 | setActiveWindow(); | 638 | } else if (msg == "timerStart()" ) { |
639 | if ( !swatch_running ) | ||
640 | stopStartStopWatch(); | ||
641 | tabs->setCurrentPage(1); | ||
642 | QPEApplication::setKeepRunning(); | ||
643 | } else if (msg == "timerStop()" ) { | ||
644 | if ( swatch_running ) | ||
645 | stopStartStopWatch(); | ||
646 | tabs->setCurrentPage(1); | ||
647 | QPEApplication::setKeepRunning(); | ||
648 | } else if (msg == "timerReset()" ) { | ||
649 | resetStopWatch(); | ||
650 | tabs->setCurrentPage(1); | ||
651 | QPEApplication::setKeepRunning(); | ||
652 | } | ||
502 | } | 653 | } |
503 | 654 | ||
504 | void Clock::timerEvent( QTimerEvent *e ) | 655 | void Clock::alarmTimeout() |
505 | { | ||
506 | static int stop = 0; | ||
507 | if ( stop < 120 && bSound ) | ||
508 | { | 656 | { |
657 | if ( alarmCount < 10 ) { | ||
509 | Sound::soundAlarm(); | 658 | Sound::soundAlarm(); |
510 | stop++; | 659 | alarmCount++; |
511 | } | 660 | } else { |
512 | else | 661 | alarmCount = 0; |
513 | { | 662 | alarmt->stop(); |
514 | stop = 0; | ||
515 | killTimer( e->timerId() ); | ||
516 | clearTimer(); | ||
517 | setCaption( tr( "Clock: Alarm was missed." ) ); | ||
518 | } | 663 | } |
519 | } | 664 | } |
520 | 665 | ||
521 | AnalogClock::AnalogClock(QWidget * parent, const char * name) : QFrame( parent, name ), clear(true) | 666 | QDateTime Clock::nextAlarm( int h, int m ) |
522 | { | 667 | { |
523 | QWidget *d = QApplication::desktop(); | 668 | QDateTime now = QDateTime::currentDateTime(); |
524 | if(d->width() <= 240) | 669 | QTime at( h, m ); |
525 | bg = Resource::loadPixmap("clock/bg"); | 670 | QDateTime when( now.date(), at ); |
671 | int count = 0; | ||
672 | int dow = when.date().dayOfWeek(); | ||
673 | while ( when < now || !dayBtn[dayBtnIdx(dow)]->isOn() ) { | ||
674 | when = when.addDays( 1 ); | ||
675 | dow = when.date().dayOfWeek(); | ||
676 | if ( ++count > 7 ) | ||
677 | return QDateTime(); | ||
526 | } | 678 | } |
527 | 679 | ||
528 | QSizePolicy AnalogClock::sizePolicy() const | 680 | return when; |
529 | { | ||
530 | return QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ); | ||
531 | } | 681 | } |
532 | 682 | ||
533 | void AnalogClock::drawContents( QPainter *p ) | 683 | int Clock::dayBtnIdx( int d ) const |
534 | { | 684 | { |
535 | QRect r = contentsRect(); | 685 | if ( onMonday ) |
536 | QRect fr; | 686 | return d-1; |
537 | 687 | else if ( d == 7 ) | |
538 | p->drawPixmap(QPoint(0, 0), bg, r); | 688 | return 0; |
539 | |||
540 | if ( r. width ( ) > r. height ( )) | ||
541 | fr. setRect (( r. width ( ) - r. height ( )) / 2, r. y ( ), r. height ( ), r. height ( )); | ||
542 | else | 689 | else |
543 | fr. setRect ( r. x ( ), ( r. height ( ) - r. width ( )) / 2, r. width ( ), r. width ( )); | 690 | return d; |
544 | |||
545 | QPoint center = fr. center ( ); // ( fr.x() + fr.width() / 2, fr.y() + fr.height() / 2 ); | ||
546 | QPoint l1 ( center. x ( ), fr. y ( ) + 2 ); | ||
547 | QPoint l2 ( center. x ( ), fr. y ( ) + 8 ); | ||
548 | |||
549 | |||
550 | |||
551 | if ( clear ) | ||
552 | { | ||
553 | erase ( r ); | ||
554 | p-> setPen ( NoPen ); | ||
555 | p-> setBrush ( colorGroup ( ). color ( QColorGroup::Base )); | ||
556 | p-> drawEllipse ( fr ); | ||
557 | p-> setBrush ( NoBrush ); | ||
558 | |||
559 | // draw ticks | ||
560 | p->setPen( QPen( colorGroup ( ). color ( QColorGroup::Text ), 1 ) ); | ||
561 | for ( int i = 0; i < 12; i++ ) | ||
562 | p->drawLine( rotate( center, l1, i * 30 ), rotate( center, l2, i * 30 ) ); | ||
563 | } | 691 | } |
564 | else | ||
565 | { | ||
566 | drawPointers ( p, fr, colorGroup ( ). color ( QColorGroup::Base ), prevTime, &currTime ); | ||
567 | } | ||
568 | |||
569 | drawPointers ( p, fr, colorGroup ( ). color ( QColorGroup::Text ), currTime ); | ||
570 | 692 | ||
571 | prevTime = currTime; | 693 | void Clock::scheduleApplyDailyAlarm() |
694 | { | ||
695 | applyAlarmTimer->start( 5000, TRUE ); | ||
572 | } | 696 | } |
573 | 697 | ||
574 | void AnalogClock::drawPointers ( QPainter *p, const QRect &r, const QColor &c, const QTime &t, const QTime *t2 ) | 698 | void Clock::applyDailyAlarm() |
575 | { | 699 | { |
576 | QPoint center = r. center ( ); | 700 | if ( !init ) |
577 | 701 | return; | |
578 | QPoint h1( center. x ( ), r. y ( ) + r. height ( ) / 4 ); | ||
579 | QPoint h2( center. x ( ), center. y ( ) ); | ||
580 | 702 | ||
581 | QPoint m1( center. x ( ), r.y() + r.height() / 8 ); | 703 | applyAlarmTimer->stop(); |
582 | QPoint m2( center. x ( ), center. y ( ) ); | 704 | int minute = dailyMinute->value(); |
705 | int hour = dailyHour->value(); | ||
706 | if ( ampm ) { | ||
707 | if (hour == 12) | ||
708 | hour = 0; | ||
709 | if (dailyAmPm->currentItem() == 1 ) | ||
710 | hour += 12; | ||
711 | } | ||
583 | 712 | ||
584 | QPoint s1( center. x ( ), r. y ( ) + 8 ); | 713 | Config config( "Clock" ); |
585 | QPoint s2( center. x ( ), center. y ( ) ); | 714 | config.setGroup( "Daily Alarm" ); |
715 | config.writeEntry( "Hour", hour ); | ||
716 | config.writeEntry( "Minute", minute ); | ||
586 | 717 | ||
718 | bool enableDaily = dailyEnabled->isChecked(); | ||
719 | bool wasSound = config.readEntry( "SoundEnabled" ); | ||
720 | bool isSound = sndCheck->isChecked(); | ||
721 | int oldMagic = wasSound ? magic_playmp : magic_daily; | ||
722 | int isMagic = isSound ? magic_playmp : magic_daily; | ||
723 | config.writeEntry( "Enabled", enableDaily ); | ||
724 | config.writeEntry( "SoundEnabled", isSound ); | ||
587 | 725 | ||
588 | if ( !t2 || ( t. minute ( ) != t2-> minute ( ) || t. hour ( ) != t2-> hour ( ))) { | 726 | QString exclDays; |
589 | // draw hour pointer | 727 | int exclCount = 0; |
590 | h1 = rotate( center, h1, 30 * ( t.hour() % 12 ) + t.minute() / 2 ); | 728 | for ( int i = 1; i <= 7; i++ ) { |
591 | h2 = rotate( center, h2, 30 * ( t.hour() % 12 ) + t.minute() / 2 ); | 729 | if ( !dayBtn[dayBtnIdx(i)]->isOn() ) { |
592 | p-> setPen ( QPen ( c, 3 )); | 730 | if ( !exclDays.isEmpty() ) |
593 | p-> drawLine ( h1, h2 ); | 731 | exclDays += ","; |
732 | exclDays += QString::number( i ); | ||
733 | exclCount++; | ||
594 | } | 734 | } |
595 | |||
596 | if ( !t2 || ( t. minute ( ) != t2-> minute ( ))) { | ||
597 | // draw minute pointer | ||
598 | m1 = rotate( center, m1, t.minute() * 6 ); | ||
599 | m2 = rotate( center, m2, t.minute() * 6 ); | ||
600 | p-> setPen ( QPen ( c, 2 )); | ||
601 | p-> drawLine ( m1, m2 ); | ||
602 | } | 735 | } |
736 | config.writeEntry( "ExcludeDays", exclDays ); | ||
603 | 737 | ||
604 | if ( !t2 || ( t. second ( ) != t2-> second ( ))) { | 738 | /* try to delete both */ |
605 | // draw second pointer | 739 | AlarmServer::deleteAlarm(QDateTime(), "QPE/Application/clock", |
606 | s1 = rotate( center, s1, t.second() * 6 ); | 740 | "alarm(QDateTime,int)", oldMagic); |
607 | s2 = rotate( center, s2, t.second() * 6 ); | 741 | if ( enableDaily && exclCount < 7 ) { |
608 | p-> setPen ( QPen ( c, 1 )); | 742 | QDateTime when = nextAlarm( hour, minute ); |
609 | p-> drawLine ( s1, s2 ); | 743 | AlarmServer::addAlarm(when, "QPE/Application/clock", |
744 | "alarm(QDateTime,int)", isMagic); | ||
610 | } | 745 | } |
611 | } | 746 | } |
612 | 747 | ||
613 | void AnalogClock::display( const QTime& t ) | 748 | bool Clock::validDaysSelected(void) |
614 | { | 749 | { |
615 | currTime = t; | 750 | for ( int i = 1; i <= 7; i++ ) { |
616 | clear = false; | 751 | if ( dayBtn[dayBtnIdx(i)]->isOn() ) { |
617 | repaint( false ); | 752 | return TRUE; |
618 | clear = true; | ||
619 | } | 753 | } |
620 | 754 | } | |
621 | QPoint AnalogClock::rotate( QPoint c, QPoint p, int a ) | 755 | return FALSE; |
622 | { | ||
623 | double angle = deg2rad * ( - a + 180 ); | ||
624 | double nx = c.x() - ( p.x() - c.x() ) * cos( angle ) - | ||
625 | ( p.y() - c.y() ) * sin( angle ); | ||
626 | double ny = c.y() - ( p.y() - c.y() ) * cos( angle ) + | ||
627 | ( p.x() - c.x() ) * sin( angle ); | ||
628 | return QPoint( nx, ny ); | ||
629 | } | 756 | } |
630 | 757 | ||
631 | void Clock::slotAdjustTime() | 758 | void Clock::closeEvent( QCloseEvent *e ) |
632 | { | 759 | { |
633 | QCopEnvelope e( "QPE/System", "execute(QString)" ); | 760 | if (dailyEnabled->isChecked()) { |
634 | e << QString( "systemtime" ); | 761 | if (!validDaysSelected()) { |
762 | QMessageBox::warning(this, tr("Select Day"), | ||
763 | tr("Daily alarm requires at least\none day to be selected.")); | ||
764 | return; | ||
765 | } | ||
635 | } | 766 | } |
636 | 767 | ||
637 | void Clock::slotStartTimer() | 768 | applyDailyAlarm(); |
638 | { | 769 | ClockBase::closeEvent(e); |
639 | if ( clockRB->isChecked() ) | ||
640 | setSwatchMode( 1); | ||
641 | startSWatch(); | ||
642 | } | 770 | } |
643 | 771 | ||
644 | void Clock::slotStopTimer() | 772 | bool Clock::spinBoxValid( QSpinBox *sb ) |
645 | { | 773 | { |
646 | if ( clockRB->isChecked() ) | 774 | bool valid = TRUE; |
647 | setSwatchMode( 1); | 775 | QString tv = sb->text(); |
648 | stopSWatch(); | 776 | for ( uint i = 0; i < tv.length(); i++ ) { |
777 | if ( !tv[0].isDigit() ) | ||
778 | valid = FALSE; | ||
649 | } | 779 | } |
780 | bool ok = FALSE; | ||
781 | int v = tv.toInt( &ok ); | ||
782 | if ( !ok ) | ||
783 | valid = FALSE; | ||
784 | if ( v < sb->minValue() || v > sb->maxValue() ) | ||
785 | valid = FALSE; | ||
650 | 786 | ||
651 | void Clock::slotResetTimer() | 787 | return valid; |
652 | { | ||
653 | if ( clockRB->isChecked() ) | ||
654 | setSwatchMode( 1); | ||
655 | slotReset(); | ||
656 | } | 788 | } |
657 | 789 | ||
658 | void Clock::setSwatchMode(int mode) | 790 | void Clock::slotBrowseMp3File() { |
659 | { | 791 | Config config( "qpe" ); |
660 | qDebug("Clock::setSwatchMode( %d)", mode); | 792 | config.setGroup("Time"); |
661 | swatchRB->setChecked( mode); | 793 | |
662 | clearClock( ); | 794 | QMap<QString, QStringList> map; |
663 | modeSelect( mode ); | 795 | map.insert(tr("All"), QStringList() ); |
796 | QStringList text; | ||
797 | text << "audio/*"; | ||
798 | map.insert(tr("Audio"), text ); | ||
799 | QString str = Opie::Ui::OFileDialog::getOpenFileName( 2,"/", QString::null, map); | ||
800 | if(!str.isEmpty() ) { | ||
801 | config.writeEntry("mp3Alarm",1); | ||
802 | config.writeEntry("mp3File",str); | ||
803 | sndFileName->setText( str ); | ||
804 | scheduleApplyDailyAlarm(); | ||
805 | } | ||
664 | } | 806 | } |