Factor out the shared parts of the agent action manager setup.
[kdepim.git] / kalarm / timeselector.cpp
blob919f25aafe9b54bcf134653c036c575caa61bdd5
1 /*
2 * timeselector.cpp - widget to optionally set a time period
3 * Program: kalarm
4 * Copyright © 2004,2005,2007,2009-2011 by David Jarvie <djarvie@kde.org>
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.
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.
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.
21 #include "kalarm.h"
23 #include "checkbox.h"
24 #include "combobox.h"
25 #include "timeselector.moc"
27 #include <klocale.h>
28 #include <kdialog.h>
29 #include <khbox.h>
30 #include <kdebug.h>
32 #include <QVBoxLayout>
33 #include <QHBoxLayout>
35 #ifdef USE_AKONADI
36 using namespace KCalCore;
37 #else
38 using namespace KCal;
39 #endif
42 TimeSelector::TimeSelector(const QString& selectText, const QString& selectWhatsThis,
43 const QString& valueWhatsThis, bool allowHourMinute, QWidget* parent)
44 : QFrame(parent),
45 mSignWidget(0),
46 mReadOnly(false)
48 QHBoxLayout* layout = new QHBoxLayout(this);
49 layout->setMargin(0);
50 layout->setSpacing(KDialog::spacingHint());
51 mSelect = new CheckBox(selectText, this);
52 mSelect->setFixedSize(mSelect->sizeHint());
53 connect(mSelect, SIGNAL(toggled(bool)), SLOT(selectToggled(bool)));
54 mSelect->setWhatsThis(selectWhatsThis);
55 layout->addWidget(mSelect);
57 KHBox* box = new KHBox(this); // to group widgets for QWhatsThis text
58 box->setSpacing(KDialog::spacingHint());
59 layout->addWidget(box);
60 mPeriod = new TimePeriod(allowHourMinute, box);
61 mPeriod->setFixedSize(mPeriod->sizeHint());
62 mPeriod->setSelectOnStep(false);
63 #ifdef USE_AKONADI
64 connect(mPeriod, SIGNAL(valueChanged(KCalCore::Duration)), SLOT(periodChanged(KCalCore::Duration)));
65 #else
66 connect(mPeriod, SIGNAL(valueChanged(KCal::Duration)), SLOT(periodChanged(KCal::Duration)));
67 #endif
68 mSelect->setFocusWidget(mPeriod);
69 mPeriod->setEnabled(false);
71 box->setWhatsThis(valueWhatsThis);
72 layout->addStretch();
75 /******************************************************************************
76 * Create a ComboBox used to select the time period's sign.
77 * The caller is responsible for populating the ComboBox and handling its value.
79 ComboBox* TimeSelector::createSignCombo()
81 delete mSignWidget;
82 mSignWidget = new ComboBox(mPeriod->parentWidget());
83 mSignWidget->setEnabled(mPeriod->isEnabled());
84 return mSignWidget;
87 /******************************************************************************
88 * Set the read-only status.
90 void TimeSelector::setReadOnly(bool ro)
92 if ((int)ro != (int)mReadOnly)
94 mReadOnly = ro;
95 mSelect->setReadOnly(mReadOnly);
96 mPeriod->setReadOnly(mReadOnly);
97 if (mSignWidget)
98 mSignWidget->setReadOnly(mReadOnly);
102 bool TimeSelector::isChecked() const
104 return mSelect->isChecked();
107 void TimeSelector::setChecked(bool on)
109 if (on != mSelect->isChecked())
111 mSelect->setChecked(on);
112 emit valueChanged(period());
116 void TimeSelector::setMaximum(int hourmin, int days)
118 mPeriod->setMaximum(hourmin, days);
121 void TimeSelector::setDateOnly(bool dateOnly)
123 mPeriod->setDateOnly(dateOnly);
126 /******************************************************************************
127 * Get the specified number of minutes.
128 * Reply = 0 if unselected.
130 Duration TimeSelector::period() const
132 return mSelect->isChecked() ? mPeriod->period() : Duration(0);
135 /******************************************************************************
136 * Initialise the controls with a specified time period.
137 * If minutes = 0, it will be deselected.
138 * The time unit combo-box is initialised to 'defaultUnits', but if 'dateOnly'
139 * is true, it will never be initialised to hours/minutes.
141 void TimeSelector::setPeriod(const Duration& period, bool dateOnly, TimePeriod::Units defaultUnits)
143 mSelect->setChecked(period);
144 mPeriod->setEnabled(period);
145 if (mSignWidget)
146 mSignWidget->setEnabled(period);
147 mPeriod->setPeriod(period, dateOnly, defaultUnits);
150 /******************************************************************************
151 * Set the input focus on the count field.
153 void TimeSelector::setFocusOnCount()
155 mPeriod->setFocusOnCount();
158 /******************************************************************************
159 * Called when the TimeSelector checkbox is toggled.
161 void TimeSelector::selectToggled(bool on)
163 mPeriod->setEnabled(on);
164 if (mSignWidget)
165 mSignWidget->setEnabled(on);
166 if (on)
167 mPeriod->setFocus();
168 emit toggled(on);
169 emit valueChanged(period());
172 /******************************************************************************
173 * Called when the period value changes.
175 void TimeSelector::periodChanged(const Duration& period)
177 if (mSelect->isChecked())
178 emit valueChanged(period);
181 // vim: et sw=4: