kalarm

alarmtimewidget.cpp

00001 /*
00002  *  alarmtimewidget.cpp  -  alarm date/time entry widget
00003  *  Program:  kalarm
00004  *  Copyright (C) 2001 - 2005 by David Jarvie <software@astrojar.org.uk>
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License along
00017  *  with this program; if not, write to the Free Software Foundation, Inc.,
00018  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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;    // < 100 hours
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 *  Construct a widget with a group box and title.
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 *  Construct a widget without a group box or title.
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     // At time radio button/label
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     // Date edit box
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     // Time edit box and Any time checkbox
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;    // current status is uninitialised
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     // 'Time from now' radio button/label
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     // Delay time spin box
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     // Set up the layout, either narrow or wide
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     // Initialise the radio button statuses
00182     setButton(id(mAtTimeRadio));
00183 
00184     // Timeout every minute to update alarm time fields.
00185     MinuteTimer::connect(this, SLOT(slotTimer()));
00186 }
00187 
00188 /******************************************************************************
00189 * Set or clear read-only status for the controls
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 * Select the "Time from now" radio button.
00204 */
00205 void AlarmTimeWidget::selectTimeFromNow()
00206 {
00207     mAfterTimeRadio->setChecked(true);
00208     slotButtonSet(1);
00209 }
00210 
00211 /******************************************************************************
00212 *  Fetch the entered date/time.
00213 *  If 'checkExpired' is true and the entered value <= current time, an error occurs.
00214 *  In this case, if 'showErrorMessage' is true, output an error message.
00215 *  'errorWidget' if non-null, is set to point to the widget containing the error.
00216 *  Reply = invalid date/time if error.
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             // The date and/or time is invalid
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 *  Set the date/time.
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();     // update the delay time edit box
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 *  Set the minimum date/time to track the current time.
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 *  Set the minimum date/time, adjusting the entered date/time if necessary.
00329 *  If 'dt' is invalid, any current minimum date/time is cleared.
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 *  Set the maximum date/time, adjusting the entered date/time if necessary.
00341 *  If 'dt' is invalid, any current maximum date/time is cleared.
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 *  If the minimum and maximum date/times fall on the same date, set the minimum
00358 *  and maximum times in the time edit box.
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             // The minimum and maximum times are on the same date, so
00378             // constrain the time value.
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 *  Set the maximum value for the delay time edit box, depending on the maximum
00391 *  value for the date/time.
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)    // avoid possible 32-bit overflow on secsTo()
00399         {
00400             maxVal = now.secsTo(mMaxDateTime) / 60;
00401             if (maxVal > maxDelayTime)
00402                 maxVal = maxDelayTime;
00403         }
00404     }
00405     mDelayTimeEdit->setMaxValue(maxVal);
00406 }
00407 
00408 /******************************************************************************
00409 *  Set the status for whether a time is specified, or just a date.
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 *  Enable/disable the "any time" checkbox.
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 *  Called every minute to update the alarm time data entry fields.
00437 *  If the maximum date/time has been reached, a 'pastMax()' signal is emitted.
00438 */
00439 void AlarmTimeWidget::slotTimer()
00440 {
00441     QDateTime now;
00442     if (mMinDateTimeIsNow)
00443     {
00444         // Make sure that the minimum date is updated when the day changes
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             // Check whether the maximum date/time has now been reached
00455             if (now.date() >= mMaxDateTime.date())
00456             {
00457                 // The current date has reached or has passed the maximum date
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                     // The minimum date/time tracks the clock, so set the minimum
00467                     // and maximum times
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 *  Called when the At or After time radio button states have been set.
00484 *  Updates the appropriate edit box.
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     // Ensure that the value of the delay edit box is > 0.
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 *  Called after the mAnyTimeCheckBox checkbox has been toggled.
00504 */
00505 void AlarmTimeWidget::slotAnyTimeToggled(bool on)
00506 {
00507     mTimeEdit->setEnabled((!mAnyTimeAllowed || !on) && mAtTimeRadio->isOn());
00508     setAnyTime();
00509 }
00510 
00511 /******************************************************************************
00512 *  Called when the date or time edit box values have changed.
00513 *  Updates the time delay edit box accordingly.
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);     // prevent infinite recursion between here and delayTimeChanged()
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 *  Called when the delay time edit box value has changed.
00530 *  Updates the Date and Time edit boxes accordingly.
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);     // prevent infinite recursion between here and dateTimeChanged()
00540         mDateEdit->blockSignals(true);
00541         mTimeEdit->setValue(dt.time());
00542         mDateEdit->setDate(dt.date());
00543         mTimeEdit->blockSignals(blockedT);
00544         mDateEdit->blockSignals(blockedD);
00545     }
00546 }
KDE Home | KDE Accessibility Home | Description of Access Keys