00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "kalarm.h"
00022
00023 #include <qlayout.h>
00024 #include <qgroupbox.h>
00025 #include <qhbox.h>
00026 #include <qpushbutton.h>
00027 #include <qwhatsthis.h>
00028
00029 #include <kdialog.h>
00030 #include <kmessagebox.h>
00031 #include <klocale.h>
00032
00033 #include "checkbox.h"
00034 #include "dateedit.h"
00035 #include "datetime.h"
00036 #include "radiobutton.h"
00037 #include "synchtimer.h"
00038 #include "timeedit.h"
00039 #include "timespinbox.h"
00040 #include "alarmtimewidget.moc"
00041
00042 static const QTime time_23_59(23, 59);
00043
00044
00045 const int AlarmTimeWidget::maxDelayTime = 99*60 + 59;
00046
00047 QString AlarmTimeWidget::i18n_w_TimeFromNow() { return i18n("Time from no&w:"); }
00048 QString AlarmTimeWidget::i18n_TimeAfterPeriod()
00049 {
00050 return i18n("Enter the length of time (in hours and minutes) after "
00051 "the current time to schedule the alarm.");
00052 }
00053
00054
00055
00056
00057
00058 AlarmTimeWidget::AlarmTimeWidget(const QString& groupBoxTitle, int mode, QWidget* parent, const char* name)
00059 : ButtonGroup(groupBoxTitle, parent, name),
00060 mMinDateTimeIsNow(false),
00061 mPastMax(false),
00062 mMinMaxTimeSet(false)
00063 {
00064 init(mode);
00065 }
00066
00067
00068
00069
00070 AlarmTimeWidget::AlarmTimeWidget(int mode, QWidget* parent, const char* name)
00071 : ButtonGroup(parent, name),
00072 mMinDateTimeIsNow(false),
00073 mPastMax(false),
00074 mMinMaxTimeSet(false)
00075 {
00076 setFrameStyle(QFrame::NoFrame);
00077 init(mode);
00078 }
00079
00080 void AlarmTimeWidget::init(int mode)
00081 {
00082 static const QString recurText = i18n("For a simple repetition, enter the date/time of the first occurrence.\n"
00083 "If a recurrence is configured, the start date/time will be adjusted "
00084 "to the first recurrence on or after the entered date/time.");
00085
00086 connect(this, SIGNAL(buttonSet(int)), SLOT(slotButtonSet(int)));
00087 QBoxLayout* topLayout = new QVBoxLayout(this, 0, KDialog::spacingHint());
00088 if (!title().isEmpty())
00089 {
00090 topLayout->setMargin(KDialog::marginHint());
00091 topLayout->addSpacing(fontMetrics().lineSpacing()/2);
00092 }
00093
00094
00095 mAtTimeRadio = new RadioButton(((mode & DEFER_TIME) ? i18n("&Defer to date/time:") : i18n("At &date/time:")), this, "atTimeRadio");
00096 mAtTimeRadio->setFixedSize(mAtTimeRadio->sizeHint());
00097 QWhatsThis::add(mAtTimeRadio,
00098 ((mode & DEFER_TIME) ? i18n("Reschedule the alarm to the specified date and time.")
00099 : i18n("Schedule the alarm at the specified date and time.")));
00100
00101
00102 mDateEdit = new DateEdit(this);
00103 mDateEdit->setFixedSize(mDateEdit->sizeHint());
00104 connect(mDateEdit, SIGNAL(dateChanged(const QDate&)), SLOT(dateTimeChanged()));
00105 static const QString enterDateText = i18n("Enter the date to schedule the alarm.");
00106 QWhatsThis::add(mDateEdit, ((mode & DEFER_TIME) ? enterDateText
00107 : QString("%1\n%2").arg(enterDateText).arg(recurText)));
00108 mAtTimeRadio->setFocusWidget(mDateEdit);
00109
00110
00111 QHBox* timeBox = new QHBox(this);
00112 timeBox->setSpacing(2*KDialog::spacingHint());
00113 mTimeEdit = new TimeEdit(timeBox);
00114 mTimeEdit->setFixedSize(mTimeEdit->sizeHint());
00115 connect(mTimeEdit, SIGNAL(valueChanged(int)), SLOT(dateTimeChanged()));
00116 static const QString enterTimeText = i18n("Enter the time to schedule the alarm.");
00117 QWhatsThis::add(mTimeEdit,
00118 ((mode & DEFER_TIME) ? QString("%1\n\n%2").arg(enterTimeText).arg(TimeSpinBox::shiftWhatsThis())
00119 : QString("%1\n%2\n\n%3").arg(enterTimeText).arg(recurText).arg(TimeSpinBox::shiftWhatsThis())));
00120
00121 mAnyTime = -1;
00122 if (mode & DEFER_TIME)
00123 {
00124 mAnyTimeAllowed = false;
00125 mAnyTimeCheckBox = 0;
00126 }
00127 else
00128 {
00129 mAnyTimeAllowed = true;
00130 mAnyTimeCheckBox = new CheckBox(i18n("An&y time"), timeBox);
00131 mAnyTimeCheckBox->setFixedSize(mAnyTimeCheckBox->sizeHint());
00132 connect(mAnyTimeCheckBox, SIGNAL(toggled(bool)), SLOT(slotAnyTimeToggled(bool)));
00133 QWhatsThis::add(mAnyTimeCheckBox, i18n("Schedule the alarm for any time during the day"));
00134 }
00135
00136
00137 mAfterTimeRadio = new RadioButton(((mode & DEFER_TIME) ? i18n("Defer for time &interval:") : i18n_w_TimeFromNow()),
00138 this, "afterTimeRadio");
00139 mAfterTimeRadio->setFixedSize(mAfterTimeRadio->sizeHint());
00140 QWhatsThis::add(mAfterTimeRadio,
00141 ((mode & DEFER_TIME) ? i18n("Reschedule the alarm for the specified time interval after now.")
00142 : i18n("Schedule the alarm after the specified time interval from now.")));
00143
00144
00145 mDelayTimeEdit = new TimeSpinBox(1, maxDelayTime, this);
00146 mDelayTimeEdit->setValue(1439);
00147 mDelayTimeEdit->setFixedSize(mDelayTimeEdit->sizeHint());
00148 connect(mDelayTimeEdit, SIGNAL(valueChanged(int)), SLOT(delayTimeChanged(int)));
00149 QWhatsThis::add(mDelayTimeEdit,
00150 ((mode & DEFER_TIME) ? QString("%1\n\n%2").arg(i18n_TimeAfterPeriod()).arg(TimeSpinBox::shiftWhatsThis())
00151 : QString("%1\n%2\n\n%3").arg(i18n_TimeAfterPeriod()).arg(recurText).arg(TimeSpinBox::shiftWhatsThis())));
00152 mAfterTimeRadio->setFocusWidget(mDelayTimeEdit);
00153
00154
00155 if (mode & NARROW)
00156 {
00157 QGridLayout* grid = new QGridLayout(topLayout, 2, 2, KDialog::spacingHint());
00158 grid->addWidget(mAtTimeRadio, 0, 0);
00159 grid->addWidget(mDateEdit, 0, 1, Qt::AlignAuto);
00160 grid->addWidget(timeBox, 1, 1, Qt::AlignAuto);
00161 grid->setColStretch(2, 1);
00162 topLayout->addStretch();
00163 QBoxLayout* layout = new QHBoxLayout(topLayout, KDialog::spacingHint());
00164 layout->addWidget(mAfterTimeRadio);
00165 layout->addWidget(mDelayTimeEdit);
00166 layout->addStretch();
00167 }
00168 else
00169 {
00170 QGridLayout* grid = new QGridLayout(topLayout, 2, 3, KDialog::spacingHint());
00171 grid->addWidget(mAtTimeRadio, 0, 0, Qt::AlignAuto);
00172 grid->addWidget(mDateEdit, 0, 1, Qt::AlignAuto);
00173 grid->addWidget(timeBox, 0, 2, Qt::AlignAuto);
00174 grid->setRowStretch(0, 1);
00175 grid->addWidget(mAfterTimeRadio, 1, 0, Qt::AlignAuto);
00176 grid->addWidget(mDelayTimeEdit, 1, 1, Qt::AlignAuto);
00177 grid->setColStretch(3, 1);
00178 topLayout->addStretch();
00179 }
00180
00181
00182 setButton(id(mAtTimeRadio));
00183
00184
00185 MinuteTimer::connect(this, SLOT(slotTimer()));
00186 }
00187
00188
00189
00190
00191 void AlarmTimeWidget::setReadOnly(bool ro)
00192 {
00193 mAtTimeRadio->setReadOnly(ro);
00194 mDateEdit->setReadOnly(ro);
00195 mTimeEdit->setReadOnly(ro);
00196 if (mAnyTimeCheckBox)
00197 mAnyTimeCheckBox->setReadOnly(ro);
00198 mAfterTimeRadio->setReadOnly(ro);
00199 mDelayTimeEdit->setReadOnly(ro);
00200 }
00201
00202
00203
00204
00205 void AlarmTimeWidget::selectTimeFromNow()
00206 {
00207 mAfterTimeRadio->setChecked(true);
00208 slotButtonSet(1);
00209 }
00210
00211
00212
00213
00214
00215
00216
00217
00218 DateTime AlarmTimeWidget::getDateTime(bool checkExpired, bool showErrorMessage, QWidget** errorWidget) const
00219 {
00220 if (errorWidget)
00221 *errorWidget = 0;
00222 QTime nowt = QTime::currentTime();
00223 QDateTime now(QDate::currentDate(), QTime(nowt.hour(), nowt.minute()));
00224 if (mAtTimeRadio->isOn())
00225 {
00226 bool anyTime = mAnyTimeAllowed && mAnyTimeCheckBox && mAnyTimeCheckBox->isChecked();
00227 if (!mDateEdit->isValid() || !mTimeEdit->isValid())
00228 {
00229
00230 if (!mDateEdit->isValid())
00231 {
00232 if (showErrorMessage)
00233 KMessageBox::sorry(const_cast<AlarmTimeWidget*>(this), i18n("Invalid date"));
00234 if (errorWidget)
00235 *errorWidget = mDateEdit;
00236 }
00237 else
00238 {
00239 if (showErrorMessage)
00240 KMessageBox::sorry(const_cast<AlarmTimeWidget*>(this), i18n("Invalid time"));
00241 if (errorWidget)
00242 *errorWidget = mTimeEdit;
00243 }
00244 return DateTime();
00245 }
00246
00247 DateTime result;
00248 if (anyTime)
00249 {
00250 result = mDateEdit->date();
00251 if (checkExpired && result.date() < now.date())
00252 {
00253 if (showErrorMessage)
00254 KMessageBox::sorry(const_cast<AlarmTimeWidget*>(this), i18n("Alarm date has already expired"));
00255 if (errorWidget)
00256 *errorWidget = mDateEdit;
00257 return DateTime();
00258 }
00259 }
00260 else
00261 {
00262 result.set(mDateEdit->date(), mTimeEdit->time());
00263 if (checkExpired && result <= now.addSecs(1))
00264 {
00265 if (showErrorMessage)
00266 KMessageBox::sorry(const_cast<AlarmTimeWidget*>(this), i18n("Alarm time has already expired"));
00267 if (errorWidget)
00268 *errorWidget = mTimeEdit;
00269 return DateTime();
00270 }
00271 }
00272 return result;
00273 }
00274 else
00275 {
00276 if (!mDelayTimeEdit->isValid())
00277 {
00278 if (showErrorMessage)
00279 KMessageBox::sorry(const_cast<AlarmTimeWidget*>(this), i18n("Invalid time"));
00280 if (errorWidget)
00281 *errorWidget = mDelayTimeEdit;
00282 return DateTime();
00283 }
00284 return now.addSecs(mDelayTimeEdit->value() * 60);
00285 }
00286 }
00287
00288
00289
00290
00291 void AlarmTimeWidget::setDateTime(const DateTime& dt)
00292 {
00293 if (dt.date().isValid())
00294 {
00295 mTimeEdit->setValue(dt.time());
00296 mDateEdit->setDate(dt.date());
00297 dateTimeChanged();
00298 }
00299 else
00300 {
00301 mTimeEdit->setValid(false);
00302 mDateEdit->setInvalid();
00303 mDelayTimeEdit->setValid(false);
00304 }
00305 if (mAnyTimeCheckBox)
00306 {
00307 bool anyTime = dt.isDateOnly();
00308 if (anyTime)
00309 mAnyTimeAllowed = true;
00310 mAnyTimeCheckBox->setChecked(anyTime);
00311 setAnyTime();
00312 }
00313 }
00314
00315
00316
00317
00318 void AlarmTimeWidget::setMinDateTimeIsCurrent()
00319 {
00320 mMinDateTimeIsNow = true;
00321 mMinDateTime = QDateTime();
00322 QDateTime now = QDateTime::currentDateTime();
00323 mDateEdit->setMinDate(now.date());
00324 setMaxMinTimeIf(now);
00325 }
00326
00327
00328
00329
00330
00331 void AlarmTimeWidget::setMinDateTime(const QDateTime& dt)
00332 {
00333 mMinDateTimeIsNow = false;
00334 mMinDateTime = dt;
00335 mDateEdit->setMinDate(dt.date());
00336 setMaxMinTimeIf(QDateTime::currentDateTime());
00337 }
00338
00339
00340
00341
00342
00343 void AlarmTimeWidget::setMaxDateTime(const DateTime& dt)
00344 {
00345 mPastMax = false;
00346 if (dt.isValid() && dt.isDateOnly())
00347 mMaxDateTime = dt.dateTime().addSecs(24*3600 - 60);
00348 else
00349 mMaxDateTime = dt.dateTime();
00350 mDateEdit->setMaxDate(mMaxDateTime.date());
00351 QDateTime now = QDateTime::currentDateTime();
00352 setMaxMinTimeIf(now);
00353 setMaxDelayTime(now);
00354 }
00355
00356
00357
00358
00359
00360 void AlarmTimeWidget::setMaxMinTimeIf(const QDateTime& now)
00361 {
00362 int mint = 0;
00363 QTime maxt = time_23_59;
00364 mMinMaxTimeSet = false;
00365 if (mMaxDateTime.isValid())
00366 {
00367 bool set = true;
00368 QDateTime minDT;
00369 if (mMinDateTimeIsNow)
00370 minDT = now.addSecs(60);
00371 else if (mMinDateTime.isValid())
00372 minDT = mMinDateTime;
00373 else
00374 set = false;
00375 if (set && mMaxDateTime.date() == minDT.date())
00376 {
00377
00378
00379 mint = minDT.time().hour()*60 + minDT.time().minute();
00380 maxt = mMaxDateTime.time();
00381 mMinMaxTimeSet = true;
00382 }
00383 }
00384 mTimeEdit->setMinValue(mint);
00385 mTimeEdit->setMaxValue(maxt);
00386 mTimeEdit->setWrapping(!mint && maxt == time_23_59);
00387 }
00388
00389
00390
00391
00392
00393 void AlarmTimeWidget::setMaxDelayTime(const QDateTime& now)
00394 {
00395 int maxVal = maxDelayTime;
00396 if (mMaxDateTime.isValid())
00397 {
00398 if (now.date().daysTo(mMaxDateTime.date()) < 100)
00399 {
00400 maxVal = now.secsTo(mMaxDateTime) / 60;
00401 if (maxVal > maxDelayTime)
00402 maxVal = maxDelayTime;
00403 }
00404 }
00405 mDelayTimeEdit->setMaxValue(maxVal);
00406 }
00407
00408
00409
00410
00411 void AlarmTimeWidget::setAnyTime()
00412 {
00413 int old = mAnyTime;
00414 mAnyTime = (mAtTimeRadio->isOn() && mAnyTimeAllowed && mAnyTimeCheckBox && mAnyTimeCheckBox->isChecked()) ? 1 : 0;
00415 if (mAnyTime != old)
00416 emit anyTimeToggled(mAnyTime);
00417 }
00418
00419
00420
00421
00422 void AlarmTimeWidget::enableAnyTime(bool enable)
00423 {
00424 if (mAnyTimeCheckBox)
00425 {
00426 mAnyTimeAllowed = enable;
00427 bool at = mAtTimeRadio->isOn();
00428 mAnyTimeCheckBox->setEnabled(enable && at);
00429 if (at)
00430 mTimeEdit->setEnabled(!enable || !mAnyTimeCheckBox->isChecked());
00431 setAnyTime();
00432 }
00433 }
00434
00435
00436
00437
00438
00439 void AlarmTimeWidget::slotTimer()
00440 {
00441 QDateTime now;
00442 if (mMinDateTimeIsNow)
00443 {
00444
00445 now = QDateTime::currentDateTime();
00446 mDateEdit->setMinDate(now.date());
00447 }
00448 if (mMaxDateTime.isValid())
00449 {
00450 if (!now.isValid())
00451 now = QDateTime::currentDateTime();
00452 if (!mPastMax)
00453 {
00454
00455 if (now.date() >= mMaxDateTime.date())
00456 {
00457
00458 if (now.date() > mMaxDateTime.date()
00459 || !mAnyTime && now.time() > mTimeEdit->maxTime())
00460 {
00461 mPastMax = true;
00462 emit pastMax();
00463 }
00464 else if (mMinDateTimeIsNow && !mMinMaxTimeSet)
00465 {
00466
00467
00468 setMaxMinTimeIf(now);
00469 }
00470 }
00471 }
00472 setMaxDelayTime(now);
00473 }
00474
00475 if (mAtTimeRadio->isOn())
00476 dateTimeChanged();
00477 else
00478 delayTimeChanged(mDelayTimeEdit->value());
00479 }
00480
00481
00482
00483
00484
00485
00486 void AlarmTimeWidget::slotButtonSet(int)
00487 {
00488 bool at = mAtTimeRadio->isOn();
00489 mDateEdit->setEnabled(at);
00490 mTimeEdit->setEnabled(at && (!mAnyTimeAllowed || !mAnyTimeCheckBox || !mAnyTimeCheckBox->isChecked()));
00491 if (mAnyTimeCheckBox)
00492 mAnyTimeCheckBox->setEnabled(at && mAnyTimeAllowed);
00493
00494 QDateTime dt(mDateEdit->date(), mTimeEdit->time());
00495 int minutes = (QDateTime::currentDateTime().secsTo(dt) + 59) / 60;
00496 if (minutes <= 0)
00497 mDelayTimeEdit->setValid(true);
00498 mDelayTimeEdit->setEnabled(!at);
00499 setAnyTime();
00500 }
00501
00502
00503
00504
00505 void AlarmTimeWidget::slotAnyTimeToggled(bool on)
00506 {
00507 mTimeEdit->setEnabled((!mAnyTimeAllowed || !on) && mAtTimeRadio->isOn());
00508 setAnyTime();
00509 }
00510
00511
00512
00513
00514
00515 void AlarmTimeWidget::dateTimeChanged()
00516 {
00517 QDateTime dt(mDateEdit->date(), mTimeEdit->time());
00518 int minutes = (QDateTime::currentDateTime().secsTo(dt) + 59) / 60;
00519 bool blocked = mDelayTimeEdit->signalsBlocked();
00520 mDelayTimeEdit->blockSignals(true);
00521 if (minutes <= 0 || minutes > mDelayTimeEdit->maxValue())
00522 mDelayTimeEdit->setValid(false);
00523 else
00524 mDelayTimeEdit->setValue(minutes);
00525 mDelayTimeEdit->blockSignals(blocked);
00526 }
00527
00528
00529
00530
00531
00532 void AlarmTimeWidget::delayTimeChanged(int minutes)
00533 {
00534 if (mDelayTimeEdit->isValid())
00535 {
00536 QDateTime dt = QDateTime::currentDateTime().addSecs(minutes * 60);
00537 bool blockedT = mTimeEdit->signalsBlocked();
00538 bool blockedD = mDateEdit->signalsBlocked();
00539 mTimeEdit->blockSignals(true);
00540 mDateEdit->blockSignals(true);
00541 mTimeEdit->setValue(dt.time());
00542 mDateEdit->setDate(dt.date());
00543 mTimeEdit->blockSignals(blockedT);
00544 mDateEdit->blockSignals(blockedD);
00545 }
00546 }