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 <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
00040
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
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
00079
00080
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();
00089 else
00090 displayDialog();
00091 }
00092
00093
00094
00095
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();
00107 }
00108 else
00109 setOn(mInterval && mCount);
00110 }
00111
00112
00113
00114
00115
00116
00117
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();
00139 }
00140
00141
00142
00143
00144
00145
00146
00147 static const int MAX_COUNT = 9999;
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
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
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);
00245 if (on)
00246 intervalChanged(interval);
00247 mCount->setValue(count);
00248 mDuration->setMinutes(count * interval, mDateOnly, units);
00249 mCountButton->setChecked(true);
00250 }
00251 mTimeSelector->setEnabled(mMaxDuration);
00252 }
00253
00254
00255
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
00269
00270 int RepetitionDlg::interval() const
00271 {
00272 return mTimeSelector->minutes();
00273 }
00274
00275
00276
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;
00289 }
00290
00291
00292
00293
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
00309
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
00326
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
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
00354
00355 void RepetitionDlg::typeClicked()
00356 {
00357 if (mTimeSelector->isChecked())
00358 {
00359 mCount->setEnabled(mCountButton->isOn());
00360 mDuration->setEnabled(mDurationButton->isOn());
00361 }
00362 }