24 #include <tqgroupbox.h>
26 #include <tqpushbutton.h>
27 #include <tqwhatsthis.h>
30 #include <tdemessagebox.h>
31 #include <tdelocale.h>
36 #include "radiobutton.h"
37 #include "synchtimer.h"
39 #include "timespinbox.h"
40 #include "alarmtimewidget.moc"
42 static const TQTime time_23_59(23, 59);
45 const int AlarmTimeWidget::maxDelayTime = 999*60 + 59;
47 TQString AlarmTimeWidget::i18n_w_TimeFromNow() {
return i18n(
"Time from no&w:"); }
48 TQString AlarmTimeWidget::i18n_TimeAfterPeriod()
50 return i18n(
"Enter the length of time (in hours and minutes) after "
51 "the current time to schedule the alarm.");
58 AlarmTimeWidget::AlarmTimeWidget(
const TQString& groupBoxTitle,
int mode, TQWidget* parent,
const char* name)
59 : ButtonGroup(groupBoxTitle, parent, name),
60 mMinDateTimeIsNow(false),
70 AlarmTimeWidget::AlarmTimeWidget(
int mode, TQWidget* parent,
const char* name)
71 : ButtonGroup(parent, name),
72 mMinDateTimeIsNow(false),
76 setFrameStyle(TQFrame::NoFrame);
80 void AlarmTimeWidget::init(
int mode)
82 static const TQString recurText = i18n(
"For a simple repetition, enter the date/time of the first occurrence.\n"
83 "If a recurrence is configured, the start date/time will be adjusted "
84 "to the first recurrence on or after the entered date/time.");
86 connect(
this, TQ_SIGNAL(buttonSet(
int)), TQ_SLOT(slotButtonSet(
int)));
87 TQBoxLayout* topLayout =
new TQVBoxLayout(
this, 0, KDialog::spacingHint());
88 if (!title().isEmpty())
90 topLayout->setMargin(KDialog::marginHint());
91 topLayout->addSpacing(fontMetrics().lineSpacing()/2);
95 mAtTimeRadio =
new RadioButton(((mode & DEFER_TIME) ? i18n(
"&Defer to date/time:") : i18n(
"At &date/time:")),
this,
"atTimeRadio");
96 mAtTimeRadio->setFixedSize(mAtTimeRadio->sizeHint());
97 TQWhatsThis::add(mAtTimeRadio,
98 ((mode & DEFER_TIME) ? i18n(
"Reschedule the alarm to the specified date and time.")
99 : i18n(
"Schedule the alarm at the specified date and time.")));
102 mDateEdit =
new DateEdit(
this);
103 mDateEdit->setFixedSize(mDateEdit->sizeHint());
104 connect(mDateEdit, TQ_SIGNAL(dateEntered(
const TQDate&)), TQ_SLOT(dateTimeChanged()));
105 static const TQString enterDateText = i18n(
"Enter the date to schedule the alarm.");
106 TQWhatsThis::add(mDateEdit, ((mode & DEFER_TIME) ? enterDateText
107 : TQString(
"%1\n%2").arg(enterDateText).arg(recurText)));
108 mAtTimeRadio->setFocusWidget(mDateEdit);
111 TQHBox* timeBox =
new TQHBox(
this);
112 timeBox->setSpacing(2*KDialog::spacingHint());
113 mTimeEdit =
new TimeEdit(timeBox);
114 mTimeEdit->setFixedSize(mTimeEdit->sizeHint());
115 connect(mTimeEdit, TQ_SIGNAL(valueChanged(
int)), TQ_SLOT(dateTimeChanged()));
116 static const TQString enterTimeText = i18n(
"Enter the time to schedule the alarm.");
117 TQWhatsThis::add(mTimeEdit,
118 ((mode & DEFER_TIME) ? TQString(
"%1\n\n%2").arg(enterTimeText).arg(TimeSpinBox::shiftWhatsThis())
119 : TQString(
"%1\n%2\n\n%3").arg(enterTimeText).arg(recurText).arg(TimeSpinBox::shiftWhatsThis())));
122 if (mode & DEFER_TIME)
124 mAnyTimeAllowed =
false;
125 mAnyTimeCheckBox = 0;
129 mAnyTimeAllowed =
true;
130 mAnyTimeCheckBox =
new CheckBox(i18n(
"An&y time"), timeBox);
131 mAnyTimeCheckBox->setFixedSize(mAnyTimeCheckBox->sizeHint());
132 connect(mAnyTimeCheckBox, TQ_SIGNAL(toggled(
bool)), TQ_SLOT(slotAnyTimeToggled(
bool)));
133 TQWhatsThis::add(mAnyTimeCheckBox, i18n(
"Schedule the alarm for any time during the day"));
137 mAfterTimeRadio =
new RadioButton(((mode & DEFER_TIME) ? i18n(
"Defer for time &interval:") : i18n_w_TimeFromNow()),
138 this,
"afterTimeRadio");
139 mAfterTimeRadio->setFixedSize(mAfterTimeRadio->sizeHint());
140 TQWhatsThis::add(mAfterTimeRadio,
141 ((mode & DEFER_TIME) ? i18n(
"Reschedule the alarm for the specified time interval after now.")
142 : i18n(
"Schedule the alarm after the specified time interval from now.")));
145 mDelayTimeEdit =
new TimeSpinBox(1, maxDelayTime,
this);
146 mDelayTimeEdit->setValue(maxDelayTime);
147 mDelayTimeEdit->setFixedSize(mDelayTimeEdit->sizeHint());
148 connect(mDelayTimeEdit, TQ_SIGNAL(valueChanged(
int)), TQ_SLOT(delayTimeChanged(
int)));
149 TQWhatsThis::add(mDelayTimeEdit,
150 ((mode & DEFER_TIME) ? TQString(
"%1\n\n%2").arg(i18n_TimeAfterPeriod()).arg(TimeSpinBox::shiftWhatsThis())
151 : TQString(
"%1\n%2\n\n%3").arg(i18n_TimeAfterPeriod()).arg(recurText).arg(TimeSpinBox::shiftWhatsThis())));
152 mAfterTimeRadio->setFocusWidget(mDelayTimeEdit);
157 TQGridLayout* grid =
new TQGridLayout(topLayout, 2, 2, KDialog::spacingHint());
158 grid->addWidget(mAtTimeRadio, 0, 0);
159 grid->addWidget(mDateEdit, 0, 1, TQt::AlignAuto);
160 grid->addWidget(timeBox, 1, 1, TQt::AlignAuto);
161 grid->setColStretch(2, 1);
162 topLayout->addStretch();
163 TQBoxLayout* layout =
new TQHBoxLayout(topLayout, KDialog::spacingHint());
164 layout->addWidget(mAfterTimeRadio);
165 layout->addWidget(mDelayTimeEdit);
166 layout->addStretch();
170 TQGridLayout* grid =
new TQGridLayout(topLayout, 2, 3, KDialog::spacingHint());
171 grid->addWidget(mAtTimeRadio, 0, 0, TQt::AlignAuto);
172 grid->addWidget(mDateEdit, 0, 1, TQt::AlignAuto);
173 grid->addWidget(timeBox, 0, 2, TQt::AlignAuto);
174 grid->setRowStretch(0, 1);
175 grid->addWidget(mAfterTimeRadio, 1, 0, TQt::AlignAuto);
176 grid->addWidget(mDelayTimeEdit, 1, 1, TQt::AlignAuto);
177 grid->setColStretch(3, 1);
178 topLayout->addStretch();
182 setButton(
id(mAtTimeRadio));
185 MinuteTimer::connect(
this, TQ_SLOT(slotTimer()));
191 void AlarmTimeWidget::setReadOnly(
bool ro)
193 mAtTimeRadio->setReadOnly(ro);
194 mDateEdit->setReadOnly(ro);
195 mTimeEdit->setReadOnly(ro);
196 if (mAnyTimeCheckBox)
197 mAnyTimeCheckBox->setReadOnly(ro);
198 mAfterTimeRadio->setReadOnly(ro);
199 mDelayTimeEdit->setReadOnly(ro);
205 void AlarmTimeWidget::selectTimeFromNow(
int minutes)
207 mAfterTimeRadio->setChecked(
true);
210 mDelayTimeEdit->setValue(minutes);
222 DateTime AlarmTimeWidget::getDateTime(
int* minsFromNow,
bool checkExpired,
bool showErrorMessage, TQWidget** errorWidget)
const
228 TQTime nowt = TQTime::currentTime();
229 TQDateTime now(TQDate::currentDate(), TQTime(nowt.hour(), nowt.minute()));
230 if (mAtTimeRadio->isOn())
232 bool anyTime = mAnyTimeAllowed && mAnyTimeCheckBox && mAnyTimeCheckBox->isChecked();
233 if (!mDateEdit->isValid() || !mTimeEdit->isValid())
236 if (!mDateEdit->isValid())
238 if (showErrorMessage)
239 KMessageBox::sorry(
const_cast<AlarmTimeWidget*
>(
this), i18n(
"Invalid date"));
241 *errorWidget = mDateEdit;
245 if (showErrorMessage)
246 KMessageBox::sorry(
const_cast<AlarmTimeWidget*
>(
this), i18n(
"Invalid time"));
248 *errorWidget = mTimeEdit;
256 result = mDateEdit->date();
257 if (checkExpired && result.date() < now.date())
259 if (showErrorMessage)
260 KMessageBox::sorry(
const_cast<AlarmTimeWidget*
>(
this), i18n(
"Alarm date has already expired"));
262 *errorWidget = mDateEdit;
268 result.set(mDateEdit->date(), mTimeEdit->time());
269 if (checkExpired && result <= TQDateTime(now.addSecs(1)))
271 if (showErrorMessage)
272 KMessageBox::sorry(
const_cast<AlarmTimeWidget*
>(
this), i18n(
"Alarm time has already expired"));
274 *errorWidget = mTimeEdit;
282 if (!mDelayTimeEdit->isValid())
284 if (showErrorMessage)
285 KMessageBox::sorry(
const_cast<AlarmTimeWidget*
>(
this), i18n(
"Invalid time"));
287 *errorWidget = mDelayTimeEdit;
290 int delayMins = mDelayTimeEdit->value();
292 *minsFromNow = delayMins;
293 return TQDateTime(now.addSecs(delayMins * 60));
300 void AlarmTimeWidget::setDateTime(
const DateTime& dt)
302 if (dt.date().isValid())
304 mTimeEdit->setValue(dt.time());
305 mDateEdit->setDate(dt.date());
310 mTimeEdit->setValid(
false);
311 mDateEdit->setInvalid();
312 mDelayTimeEdit->setValid(
false);
314 if (mAnyTimeCheckBox)
316 bool anyTime = dt.isDateOnly();
318 mAnyTimeAllowed =
true;
319 mAnyTimeCheckBox->setChecked(anyTime);
327 void AlarmTimeWidget::setMinDateTimeIsCurrent()
329 mMinDateTimeIsNow =
true;
330 mMinDateTime = TQDateTime();
331 TQDateTime now = TQDateTime::currentDateTime();
332 mDateEdit->setMinDate(now.date());
333 setMaxMinTimeIf(now);
340 void AlarmTimeWidget::setMinDateTime(
const TQDateTime& dt)
342 mMinDateTimeIsNow =
false;
344 mDateEdit->setMinDate(dt.date());
345 setMaxMinTimeIf(TQDateTime::currentDateTime());
352 void AlarmTimeWidget::setMaxDateTime(
const DateTime& dt)
355 if (dt.isValid() && dt.isDateOnly())
356 mMaxDateTime = dt.dateTime().addSecs(24*3600 - 60);
358 mMaxDateTime = dt.dateTime();
359 mDateEdit->setMaxDate(mMaxDateTime.date());
360 TQDateTime now = TQDateTime::currentDateTime();
361 setMaxMinTimeIf(now);
362 setMaxDelayTime(now);
369 void AlarmTimeWidget::setMaxMinTimeIf(
const TQDateTime& now)
372 TQTime maxt = time_23_59;
373 mMinMaxTimeSet =
false;
374 if (mMaxDateTime.isValid())
378 if (mMinDateTimeIsNow)
379 minDT = now.addSecs(60);
380 else if (mMinDateTime.isValid())
381 minDT = mMinDateTime;
384 if (set && mMaxDateTime.date() == minDT.date())
388 mint = minDT.time().hour()*60 + minDT.time().minute();
389 maxt = mMaxDateTime.time();
390 mMinMaxTimeSet =
true;
393 mTimeEdit->setMinValue(mint);
394 mTimeEdit->setMaxValue(maxt);
395 mTimeEdit->setWrapping(!mint && maxt == time_23_59);
402 void AlarmTimeWidget::setMaxDelayTime(
const TQDateTime& now)
404 int maxVal = maxDelayTime;
405 if (mMaxDateTime.isValid())
407 if (now.date().daysTo(mMaxDateTime.date()) < 100)
409 TQDateTime dt(now.date(), TQTime(now.time().hour(), now.time().minute(), 0));
410 maxVal = dt.secsTo(mMaxDateTime) / 60;
411 if (maxVal > maxDelayTime)
412 maxVal = maxDelayTime;
415 mDelayTimeEdit->setMaxValue(maxVal);
421 void AlarmTimeWidget::setAnyTime()
424 mAnyTime = (mAtTimeRadio->isOn() && mAnyTimeAllowed && mAnyTimeCheckBox && mAnyTimeCheckBox->isChecked()) ? 1 : 0;
426 emit anyTimeToggled(mAnyTime);
432 void AlarmTimeWidget::enableAnyTime(
bool enable)
434 if (mAnyTimeCheckBox)
436 mAnyTimeAllowed = enable;
437 bool at = mAtTimeRadio->isOn();
438 mAnyTimeCheckBox->setEnabled(enable && at);
440 mTimeEdit->setEnabled(!enable || !mAnyTimeCheckBox->isChecked());
449 void AlarmTimeWidget::slotTimer()
452 if (mMinDateTimeIsNow)
455 now = TQDateTime::currentDateTime();
456 mDateEdit->setMinDate(now.date());
458 if (mMaxDateTime.isValid())
461 now = TQDateTime::currentDateTime();
465 if (now.date() >= mMaxDateTime.date())
468 if (now.date() > mMaxDateTime.date()
469 || (!mAnyTime && now.time() > mTimeEdit->maxTime()))
474 else if (mMinDateTimeIsNow && !mMinMaxTimeSet)
478 setMaxMinTimeIf(now);
482 setMaxDelayTime(now);
485 if (mAtTimeRadio->isOn())
488 delayTimeChanged(mDelayTimeEdit->value());
496 void AlarmTimeWidget::slotButtonSet(
int)
498 bool at = mAtTimeRadio->isOn();
499 mDateEdit->setEnabled(at);
500 mTimeEdit->setEnabled(at && (!mAnyTimeAllowed || !mAnyTimeCheckBox || !mAnyTimeCheckBox->isChecked()));
501 if (mAnyTimeCheckBox)
502 mAnyTimeCheckBox->setEnabled(at && mAnyTimeAllowed);
504 TQDateTime dt(mDateEdit->date(), mTimeEdit->time());
505 int minutes = (TQDateTime::currentDateTime().secsTo(dt) + 59) / 60;
507 mDelayTimeEdit->setValid(
true);
508 mDelayTimeEdit->setEnabled(!at);
515 void AlarmTimeWidget::slotAnyTimeToggled(
bool on)
517 mTimeEdit->setEnabled((!mAnyTimeAllowed || !on) && mAtTimeRadio->isOn());
525 void AlarmTimeWidget::dateTimeChanged()
527 TQDateTime dt(mDateEdit->date(), mTimeEdit->time());
528 int minutes = (TQDateTime::currentDateTime().secsTo(dt) + 59) / 60;
529 bool blocked = mDelayTimeEdit->signalsBlocked();
530 mDelayTimeEdit->blockSignals(
true);
531 if (minutes <= 0 || minutes > mDelayTimeEdit->maxValue())
532 mDelayTimeEdit->setValid(
false);
534 mDelayTimeEdit->setValue(minutes);
535 mDelayTimeEdit->blockSignals(blocked);
542 void AlarmTimeWidget::delayTimeChanged(
int minutes)
544 if (mDelayTimeEdit->isValid())
546 TQDateTime dt = TQDateTime::currentDateTime().addSecs(minutes * 60);
547 bool blockedT = mTimeEdit->signalsBlocked();
548 bool blockedD = mDateEdit->signalsBlocked();
549 mTimeEdit->blockSignals(
true);
550 mDateEdit->blockSignals(
true);
551 mTimeEdit->setValue(dt.time());
552 mDateEdit->setDate(dt.date());
553 mTimeEdit->blockSignals(blockedT);
554 mDateEdit->blockSignals(blockedD);