kalarm

repetition.cpp

00001 /*
00002  *  repetition.cpp  -  pushbutton and dialogue to specify alarm sub-repetition
00003  *  Program:  kalarm
00004  *  Copyright © 2004,2005,2007,2008 by David Jarvie <djarvie@kde.org>
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 <qwhatsthis.h>
00025 
00026 #include <kdebug.h>
00027 #include <kdialog.h>
00028 #include <klocale.h>
00029 
00030 #include "buttongroup.h"
00031 #include "radiobutton.h"
00032 #include "spinbox.h"
00033 #include "timeperiod.h"
00034 #include "timeselector.h"
00035 #include "repetition.moc"
00036 
00037 
00038 /*=============================================================================
00039 = Class RepetitionButton
00040 = Button to display the Alarm Sub-Repetition dialogue.
00041 =============================================================================*/
00042 
00043 RepetitionButton::RepetitionButton(const QString& caption, bool waitForInitialisation, QWidget* parent, const char* name)
00044     : QPushButton(caption, parent, name),
00045       mDialog(0),
00046       mInterval(0),
00047       mCount(0),
00048       mMaxDuration(-1),
00049       mDateOnly(false),
00050       mWaitForInit(waitForInitialisation),
00051       mReadOnly(false)
00052 {
00053     setToggleButton(true);
00054     setOn(false);
00055     connect(this, SIGNAL(clicked()), SLOT(slotPressed()));
00056 }
00057 
00058 void RepetitionButton::set(int interval, int count)
00059 {
00060     mInterval = interval;
00061     mCount = count;
00062     setOn(mInterval && mCount);
00063 }
00064 
00065 /******************************************************************************
00066 *  Set the data for the dialog.
00067 */
00068 void RepetitionButton::set(int interval, int count, bool dateOnly, int maxDuration)
00069 {
00070     mInterval    = interval;
00071     mCount       = count;
00072     mMaxDuration = maxDuration;
00073     mDateOnly    = dateOnly;
00074     setOn(mInterval && mCount);
00075 }
00076 
00077 /******************************************************************************
00078 *  Create the alarm repetition dialog.
00079 *  If 'waitForInitialisation' is true, the dialog won't be displayed until set()
00080 *  is called to initialise its data.
00081 */
00082 void RepetitionButton::activate(bool waitForInitialisation)
00083 {
00084     if (!mDialog)
00085         mDialog = new RepetitionDlg(i18n("Alarm Sub-Repetition"), mReadOnly, this);
00086     mDialog->set(mInterval, mCount, mDateOnly, mMaxDuration);
00087     if (waitForInitialisation)
00088         emit needsInitialisation();     // request dialog initialisation
00089     else
00090         displayDialog();    // display the dialog now
00091 }
00092 
00093 /******************************************************************************
00094 *  Set the data for the dialog and display it.
00095 *  To be called only after needsInitialisation() has been emitted.
00096 */
00097 void RepetitionButton::initialise(int interval, int count, bool dateOnly, int maxDuration)
00098 {
00099     mInterval    = (maxDuration > 0 && interval > maxDuration) ? 1 : interval;
00100     mCount       = count;
00101     mMaxDuration = maxDuration;
00102     mDateOnly    = dateOnly;
00103     if (mDialog)
00104     {
00105         mDialog->set(interval, count, dateOnly, maxDuration);
00106         displayDialog();    // display the dialog now
00107     }
00108     else
00109         setOn(mInterval && mCount);
00110 }
00111 
00112 /******************************************************************************
00113 *  Display the alarm sub-repetition dialog.
00114 *  Alarm repetition has the following restrictions:
00115 *  1) Not allowed for a repeat-at-login alarm
00116 *  2) For a date-only alarm, the repeat interval must be a whole number of days.
00117 *  3) The overall repeat duration must be less than the recurrence interval.
00118 */
00119 void RepetitionButton::displayDialog()
00120 {
00121     kdDebug(5950) << "RepetitionButton::displayDialog()" << endl;
00122     bool change = false;
00123     if (mReadOnly)
00124     {
00125         mDialog->setReadOnly(true);
00126         mDialog->exec();
00127     }
00128     else if (mDialog->exec() == QDialog::Accepted)
00129     {
00130         mCount    = mDialog->count();
00131         mInterval = mDialog->interval();
00132         change = true;
00133     }
00134     setOn(mInterval && mCount);
00135     delete mDialog;
00136     mDialog = 0;
00137     if (change)
00138         emit changed();   // delete dialog first, or initialise() will redisplay dialog
00139 }
00140 
00141 
00142 /*=============================================================================
00143 = Class RepetitionDlg
00144 = Alarm sub-repetition dialogue.
00145 =============================================================================*/
00146 
00147 static const int MAX_COUNT = 9999;    // maximum range for count spinbox
00148 
00149 
00150 RepetitionDlg::RepetitionDlg(const QString& caption, bool readOnly, QWidget* parent, const char* name)
00151     : KDialogBase(parent, name, true, caption, Ok|Cancel),
00152       mMaxDuration(-1),
00153       mDateOnly(false),
00154       mReadOnly(readOnly)
00155 {
00156     int spacing = spacingHint();
00157     QWidget* page = new QWidget(this);
00158     setMainWidget(page);
00159     QVBoxLayout* topLayout = new QVBoxLayout(page, 0, spacing);
00160 
00161     mTimeSelector = new TimeSelector(i18n("Repeat every 10 minutes", "&Repeat every"), QString::null,
00162                       i18n("Instead of the alarm triggering just once at each recurrence, "
00163                            "checking this option makes the alarm trigger multiple times at each recurrence."),
00164                       i18n("Enter the time between repetitions of the alarm"),
00165                       true, page);
00166     mTimeSelector->setFixedSize(mTimeSelector->sizeHint());
00167     connect(mTimeSelector, SIGNAL(valueChanged(int)), SLOT(intervalChanged(int)));
00168     connect(mTimeSelector, SIGNAL(toggled(bool)), SLOT(repetitionToggled(bool)));
00169     topLayout->addWidget(mTimeSelector, 0, Qt::AlignAuto);
00170 
00171     mButtonGroup = new ButtonGroup(page, "buttonGroup");
00172     connect(mButtonGroup, SIGNAL(buttonSet(int)), SLOT(typeClicked()));
00173     topLayout->addWidget(mButtonGroup);
00174 
00175     QBoxLayout* vlayout = new QVBoxLayout(mButtonGroup, marginHint(), spacing);
00176     QBoxLayout* layout = new QHBoxLayout(vlayout, spacing);
00177     mCountButton = new RadioButton(i18n("&Number of repetitions:"), mButtonGroup);
00178     mCountButton->setFixedSize(mCountButton->sizeHint());
00179     QWhatsThis::add(mCountButton,
00180           i18n("Check to specify the number of times the alarm should repeat after each recurrence"));
00181     layout->addWidget(mCountButton);
00182     mCount = new SpinBox(1, MAX_COUNT, 1, mButtonGroup);
00183     mCount->setFixedSize(mCount->sizeHint());
00184     mCount->setLineShiftStep(10);
00185     mCount->setSelectOnStep(false);
00186     connect(mCount, SIGNAL(valueChanged(int)), SLOT(countChanged(int)));
00187     QWhatsThis::add(mCount,
00188           i18n("Enter the number of times to trigger the alarm after its initial occurrence"));
00189     layout->addWidget(mCount);
00190     mCountButton->setFocusWidget(mCount);
00191     layout->addStretch();
00192 
00193     layout = new QHBoxLayout(vlayout, spacing);
00194     mDurationButton = new RadioButton(i18n("&Duration:"), mButtonGroup);
00195     mDurationButton->setFixedSize(mDurationButton->sizeHint());
00196     QWhatsThis::add(mDurationButton,
00197           i18n("Check to specify how long the alarm is to be repeated"));
00198     layout->addWidget(mDurationButton);
00199     mDuration = new TimePeriod(true, mButtonGroup);
00200     mDuration->setFixedSize(mDuration->sizeHint());
00201     connect(mDuration, SIGNAL(valueChanged(int)), SLOT(durationChanged(int)));
00202     QWhatsThis::add(mDuration,
00203           i18n("Enter the length of time to repeat the alarm"));
00204     layout->addWidget(mDuration);
00205     mDurationButton->setFocusWidget(mDuration);
00206     layout->addStretch();
00207 
00208     mCountButton->setChecked(true);
00209     repetitionToggled(false);
00210     setReadOnly(mReadOnly);
00211 }
00212 
00213 /******************************************************************************
00214 *  Set the state of all controls to reflect the data in the specified alarm.
00215 */
00216 void RepetitionDlg::set(int interval, int count, bool dateOnly, int maxDuration)
00217 {
00218     if (!interval)
00219         count = 0;
00220     else if (!count)
00221         interval = 0;
00222     if (dateOnly != mDateOnly)
00223     {
00224         mDateOnly = dateOnly;
00225         mTimeSelector->setDateOnly(mDateOnly);
00226         mDuration->setDateOnly(mDateOnly);
00227     }
00228     mMaxDuration = maxDuration;
00229     if (mMaxDuration)
00230     {
00231         int maxhm = (mMaxDuration > 0) ? mMaxDuration : 9999;
00232         int maxdw = (mMaxDuration > 0) ? mMaxDuration / 1440 : 9999;
00233         mTimeSelector->setMaximum(maxhm, maxdw);
00234         mDuration->setMaximum(maxhm, maxdw);
00235     }
00236     // Set the units - needed later if the control is unchecked initially.
00237     TimePeriod::Units units = mDateOnly ? TimePeriod::DAYS : TimePeriod::HOURS_MINUTES;
00238     mTimeSelector->setMinutes(interval, mDateOnly, units);
00239     if (!mMaxDuration  ||  !count)
00240         mTimeSelector->setChecked(false);
00241     else
00242     {
00243         bool on = mTimeSelector->isChecked();
00244         repetitionToggled(on);    // enable/disable controls
00245         if (on)
00246             intervalChanged(interval);    // ensure mCount range is set
00247         mCount->setValue(count);
00248         mDuration->setMinutes(count * interval, mDateOnly, units);
00249         mCountButton->setChecked(true);
00250     }
00251     mTimeSelector->setEnabled(mMaxDuration);
00252 }
00253 
00254 /******************************************************************************
00255 *  Set the read-only status.
00256 */
00257 void RepetitionDlg::setReadOnly(bool ro)
00258 {
00259     ro = ro || mReadOnly;
00260     mTimeSelector->setReadOnly(ro);
00261     mCountButton->setReadOnly(ro);
00262     mCount->setReadOnly(ro);
00263     mDurationButton->setReadOnly(ro);
00264     mDuration->setReadOnly(ro);
00265 }
00266 
00267 /******************************************************************************
00268 *  Get the period between repetitions in minutes.
00269 */
00270 int RepetitionDlg::interval() const
00271 {
00272     return mTimeSelector->minutes();
00273 }
00274 
00275 /******************************************************************************
00276 *  Set the entered repeat count.
00277 */
00278 int RepetitionDlg::count() const
00279 {
00280     int interval = mTimeSelector->minutes();
00281     if (interval)
00282     {
00283         if (mCountButton->isOn())
00284             return mCount->value();
00285         if (mDurationButton->isOn())
00286             return mDuration->minutes() / interval;
00287     }
00288     return 0;    // no repetition
00289 }
00290 
00291 /******************************************************************************
00292 *  Called when the time interval widget has changed value.
00293 *  Adjust the maximum repetition count accordingly.
00294 */
00295 void RepetitionDlg::intervalChanged(int minutes)
00296 {
00297     if (mTimeSelector->isChecked()  &&  minutes > 0)
00298     {
00299         mCount->setRange(1, (mMaxDuration >= 0 ? mMaxDuration / minutes : MAX_COUNT));
00300         if (mCountButton->isOn())
00301             countChanged(mCount->value());
00302         else
00303             durationChanged(mDuration->minutes());
00304     }
00305 }
00306 
00307 /******************************************************************************
00308 *  Called when the count spinbox has changed value.
00309 *  Adjust the duration accordingly.
00310 */
00311 void RepetitionDlg::countChanged(int count)
00312 {
00313     int interval = mTimeSelector->minutes();
00314     if (interval)
00315     {
00316         bool blocked = mDuration->signalsBlocked();
00317         mDuration->blockSignals(true);
00318         mDuration->setMinutes(count * interval, mDateOnly,
00319                               (mDateOnly ? TimePeriod::DAYS : TimePeriod::HOURS_MINUTES));
00320         mDuration->blockSignals(blocked);
00321     }
00322 }
00323 
00324 /******************************************************************************
00325 *  Called when the duration widget has changed value.
00326 *  Adjust the count accordingly.
00327 */
00328 void RepetitionDlg::durationChanged(int minutes)
00329 {
00330     int interval = mTimeSelector->minutes();
00331     if (interval)
00332     {
00333         bool blocked = mCount->signalsBlocked();
00334         mCount->blockSignals(true);
00335         mCount->setValue(minutes / interval);
00336         mCount->blockSignals(blocked);
00337     }
00338 }
00339 
00340 /******************************************************************************
00341 *  Called when the time period widget is toggled on or off.
00342 */
00343 void RepetitionDlg::repetitionToggled(bool on)
00344 {
00345     if (mMaxDuration == 0)
00346         on = false;
00347     mButtonGroup->setEnabled(on);
00348     mCount->setEnabled(on  &&  mCountButton->isOn());
00349     mDuration->setEnabled(on  &&  mDurationButton->isOn());
00350 }
00351 
00352 /******************************************************************************
00353 *  Called when one of the count or duration radio buttons is toggled.
00354 */
00355 void RepetitionDlg::typeClicked()
00356 {
00357     if (mTimeSelector->isChecked())
00358     {
00359         mCount->setEnabled(mCountButton->isOn());
00360         mDuration->setEnabled(mDurationButton->isOn());
00361     }
00362 }
KDE Home | KDE Accessibility Home | Description of Access Keys