kalarm

alarmtimewidget.cpp

00001 /*
00002  *  alarmtimewidget.cpp  -  alarm date/time entry widget
00003  *  Program:  kalarm
00004  *  Copyright © 2001-2006 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             QDateTime dt(now.date(), QTime(now.time().hour(), now.time().minute(), 0));   // round down to nearest minute
00401             maxVal = dt.secsTo(mMaxDateTime) / 60;
00402             if (maxVal > maxDelayTime)
00403                 maxVal = maxDelayTime;
00404         }
00405     }
00406     mDelayTimeEdit->setMaxValue(maxVal);
00407 }
00408 
00409 /******************************************************************************
00410 *  Set the status for whether a time is specified, or just a date.
00411 */
00412 void AlarmTimeWidget::setAnyTime()
00413 {
00414     int old = mAnyTime;
00415     mAnyTime = (mAtTimeRadio->isOn() && mAnyTimeAllowed && mAnyTimeCheckBox && mAnyTimeCheckBox->isChecked()) ? 1 : 0;
00416     if (mAnyTime != old)
00417         emit anyTimeToggled(mAnyTime);
00418 }
00419 
00420 /******************************************************************************
00421 *  Enable/disable the "any time" checkbox.
00422 */
00423 void AlarmTimeWidget::enableAnyTime(bool enable)
00424 {
00425     if (mAnyTimeCheckBox)
00426     {
00427         mAnyTimeAllowed = enable;
00428         bool at = mAtTimeRadio->isOn();
00429         mAnyTimeCheckBox->setEnabled(enable && at);
00430         if (at)
00431             mTimeEdit->setEnabled(!enable || !mAnyTimeCheckBox->isChecked());
00432         setAnyTime();
00433     }
00434 }
00435 
00436 /******************************************************************************
00437 *  Called every minute to update the alarm time data entry fields.
00438 *  If the maximum date/time has been reached, a 'pastMax()' signal is emitted.
00439 */
00440 void AlarmTimeWidget::slotTimer()
00441 {
00442     QDateTime now;
00443     if (mMinDateTimeIsNow)
00444     {
00445         // Make sure that the minimum date is updated when the day changes
00446         now = QDateTime::currentDateTime();
00447         mDateEdit->setMinDate(now.date());
00448     }
00449     if (mMaxDateTime.isValid())
00450     {
00451         if (!now.isValid())
00452             now = QDateTime::currentDateTime();
00453         if (!mPastMax)
00454         {
00455             // Check whether the maximum date/time has now been reached
00456             if (now.date() >= mMaxDateTime.date())
00457             {
00458                 // The current date has reached or has passed the maximum date
00459                 if (now.date() > mMaxDateTime.date()
00460                 ||  !mAnyTime && now.time() > mTimeEdit->maxTime())
00461                 {
00462                     mPastMax = true;
00463                     emit pastMax();
00464                 }
00465                 else if (mMinDateTimeIsNow  &&  !mMinMaxTimeSet)
00466                 {
00467                     // The minimum date/time tracks the clock, so set the minimum
00468                     // and maximum times
00469                     setMaxMinTimeIf(now);
00470                 }
00471             }
00472         }
00473         setMaxDelayTime(now);
00474     }
00475 
00476     if (mAtTimeRadio->isOn())
00477         dateTimeChanged();
00478     else
00479         delayTimeChanged(mDelayTimeEdit->value());
00480 }
00481 
00482 
00483 /******************************************************************************
00484 *  Called when the At or After time radio button states have been set.
00485 *  Updates the appropriate edit box.
00486 */
00487 void AlarmTimeWidget::slotButtonSet(int)
00488 {
00489     bool at = mAtTimeRadio->isOn();
00490     mDateEdit->setEnabled(at);
00491     mTimeEdit->setEnabled(at && (!mAnyTimeAllowed || !mAnyTimeCheckBox || !mAnyTimeCheckBox->isChecked()));
00492     if (mAnyTimeCheckBox)
00493         mAnyTimeCheckBox->setEnabled(at && mAnyTimeAllowed);
00494     // Ensure that the value of the delay edit box is > 0.
00495     QDateTime dt(mDateEdit->date(), mTimeEdit->time());
00496     int minutes = (QDateTime::currentDateTime().secsTo(dt) + 59) / 60;
00497     if (minutes <= 0)
00498         mDelayTimeEdit->setValid(true);
00499     mDelayTimeEdit->setEnabled(!at);
00500     setAnyTime();
00501 }
00502 
00503 /******************************************************************************
00504 *  Called after the mAnyTimeCheckBox checkbox has been toggled.
00505 */
00506 void AlarmTimeWidget::slotAnyTimeToggled(bool on)
00507 {
00508     mTimeEdit->setEnabled((!mAnyTimeAllowed || !on) && mAtTimeRadio->isOn());
00509     setAnyTime();
00510 }
00511 
00512 /******************************************************************************
00513 *  Called when the date or time edit box values have changed.
00514 *  Updates the time delay edit box accordingly.
00515 */
00516 void AlarmTimeWidget::dateTimeChanged()
00517 {
00518     QDateTime dt(mDateEdit->date(), mTimeEdit->time());
00519     int minutes = (QDateTime::currentDateTime().secsTo(dt) + 59) / 60;
00520     bool blocked = mDelayTimeEdit->signalsBlocked();
00521     mDelayTimeEdit->blockSignals(true);     // prevent infinite recursion between here and delayTimeChanged()
00522     if (minutes <= 0  ||  minutes > mDelayTimeEdit->maxValue())
00523         mDelayTimeEdit->setValid(false);
00524     else
00525         mDelayTimeEdit->setValue(minutes);
00526     mDelayTimeEdit->blockSignals(blocked);
00527 }
00528 
00529 /******************************************************************************
00530 *  Called when the delay time edit box value has changed.
00531 *  Updates the Date and Time edit boxes accordingly.
00532 */
00533 void AlarmTimeWidget::delayTimeChanged(int minutes)
00534 {
00535     if (mDelayTimeEdit->isValid())
00536     {
00537         QDateTime dt = QDateTime::currentDateTime().addSecs(minutes * 60);
00538         bool blockedT = mTimeEdit->signalsBlocked();
00539         bool blockedD = mDateEdit->signalsBlocked();
00540         mTimeEdit->blockSignals(true);     // prevent infinite recursion between here and dateTimeChanged()
00541         mDateEdit->blockSignals(true);
00542         mTimeEdit->setValue(dt.time());
00543         mDateEdit->setDate(dt.date());
00544         mTimeEdit->blockSignals(blockedT);
00545         mDateEdit->blockSignals(blockedD);
00546     }
00547 }
KDE Home | KDE Accessibility Home | Description of Access Keys