kalarm

latecancel.cpp
1 /*
2  * latecancel.cpp - widget to specify cancellation if late
3  * Program: kalarm
4  * Copyright (C) 2004, 2005 by David Jarvie <software@astrojar.org.uk>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20 
21 #include "kalarm.h"
22 
23 #include <tqwidgetstack.h>
24 #include <tqlayout.h>
25 #include <tqwhatsthis.h>
26 #include <tdelocale.h>
27 #include <kdialog.h>
28 
29 #include "checkbox.h"
30 #include "latecancel.moc"
31 
32 
33 // Collect these widget labels together to ensure consistent wording and
34 // translations across different modules.
35 TQString LateCancelSelector::i18n_CancelIfLate() { return i18n("Cancel if late"); }
36 TQString LateCancelSelector::i18n_n_CancelIfLate() { return i18n("Ca&ncel if late"); }
37 TQString LateCancelSelector::i18n_AutoCloseWin() { return i18n("Auto-close window after this time"); }
38 TQString LateCancelSelector::i18n_AutoCloseWinLC() { return i18n("Auto-close window after late-cancelation time"); }
39 TQString LateCancelSelector::i18n_i_AutoCloseWinLC() { return i18n("Auto-close w&indow after late-cancelation time"); }
40 
41 
42 LateCancelSelector::LateCancelSelector(bool allowHourMinute, TQWidget* parent, const char* name)
43  : TQFrame(parent, name),
44  mDateOnly(false),
45  mReadOnly(false),
46  mAutoCloseShown(false)
47 {
48  TQString whatsThis = i18n("If checked, the alarm will be canceled if it cannot be triggered within the "
49  "specified period after its scheduled time. Possible reasons for not triggering "
50  "include your being logged off, X not running, or the alarm daemon not running.\n\n"
51  "If unchecked, the alarm will be triggered at the first opportunity after "
52  "its scheduled time, regardless of how late it is.");
53 
54  setFrameStyle(TQFrame::NoFrame);
55  mLayout = new TQVBoxLayout(this, 0, KDialog::spacingHint());
56 
57  mStack = new TQWidgetStack(this);
58  mCheckboxFrame = new TQFrame(mStack);
59  mCheckboxFrame->setFrameStyle(TQFrame::NoFrame);
60  mStack->addWidget(mCheckboxFrame, 1);
61  TQBoxLayout* layout = new TQVBoxLayout(mCheckboxFrame, 0, 0);
62  mCheckbox = new CheckBox(i18n_n_CancelIfLate(), mCheckboxFrame);
63  mCheckbox->setFixedSize(mCheckbox->sizeHint());
64  connect(mCheckbox, TQ_SIGNAL(toggled(bool)), TQ_SLOT(slotToggled(bool)));
65  TQWhatsThis::add(mCheckbox, whatsThis);
66  layout->addWidget(mCheckbox, 0, TQt::AlignAuto);
67 
68  mTimeSelectorFrame = new TQFrame(mStack);
69  mTimeSelectorFrame->setFrameStyle(TQFrame::NoFrame);
70  mStack->addWidget(mTimeSelectorFrame, 2);
71  layout = new TQVBoxLayout(mTimeSelectorFrame, 0, 0);
72  mTimeSelector = new TimeSelector(i18n("Cancel if late by 10 minutes", "Ca&ncel if late by"), TQString(),
73  whatsThis, i18n("Enter how late will cause the alarm to be canceled"),
74  allowHourMinute, mTimeSelectorFrame);
75  connect(mTimeSelector, TQ_SIGNAL(toggled(bool)), TQ_SLOT(slotToggled(bool)));
76  layout->addWidget(mTimeSelector);
77  mLayout->addWidget(mStack);
78 
79  layout = new TQHBoxLayout(mLayout, KDialog::spacingHint());
80  layout->addSpacing(3*KDialog::spacingHint());
81  mAutoClose = new CheckBox(i18n_AutoCloseWin(), this);
82  mAutoClose->setFixedSize(mAutoClose->sizeHint());
83  TQWhatsThis::add(mAutoClose, i18n("Automatically close the alarm window after the expiry of the late-cancelation period"));
84  layout->addWidget(mAutoClose);
85  layout->addStretch();
86 
87  mAutoClose->hide();
88  mAutoClose->setEnabled(false);
89 }
90 
91 /******************************************************************************
92 * Set the read-only status.
93 */
94 void LateCancelSelector::setReadOnly(bool ro)
95 {
96  if ((int)ro != (int)mReadOnly)
97  {
98  mReadOnly = ro;
99  mCheckbox->setReadOnly(mReadOnly);
100  mTimeSelector->setReadOnly(mReadOnly);
101  mAutoClose->setReadOnly(mReadOnly);
102  }
103 }
104 
105 int LateCancelSelector::minutes() const
106 {
107  return mTimeSelector->minutes();
108 }
109 
110 void LateCancelSelector::setMinutes(int minutes, bool dateOnly, TimePeriod::Units defaultUnits)
111 {
112  slotToggled(minutes);
113  mTimeSelector->setMinutes(minutes, dateOnly, defaultUnits);
114 }
115 
116 void LateCancelSelector::setDateOnly(bool dateOnly)
117 {
118  if (dateOnly != mDateOnly)
119  {
120  mDateOnly = dateOnly;
121  if (mTimeSelector->isChecked()) // don't change when it's not visible
122  mTimeSelector->setDateOnly(dateOnly);
123  }
124 }
125 
126 void LateCancelSelector::showAutoClose(bool show)
127 {
128  if (show)
129  mAutoClose->show();
130  else
131  mAutoClose->hide();
132  mAutoCloseShown = show;
133  mLayout->activate();
134 }
135 
136 bool LateCancelSelector::isAutoClose() const
137 {
138  return mAutoCloseShown && mAutoClose->isEnabled() && mAutoClose->isChecked();
139 }
140 
141 void LateCancelSelector::setAutoClose(bool autoClose)
142 {
143  mAutoClose->setChecked(autoClose);
144 }
145 
146 /******************************************************************************
147 * Called when either of the checkboxes is toggled.
148 */
149 void LateCancelSelector::slotToggled(bool on)
150 {
151  mCheckbox->setChecked(on);
152  mTimeSelector->setChecked(on);
153  if (on)
154  {
155  mTimeSelector->setDateOnly(mDateOnly);
156  mStack->raiseWidget(mTimeSelectorFrame);
157  }
158  else
159  mStack->raiseWidget(mCheckboxFrame);
160  mAutoClose->setEnabled(on);
161 }