SVN_SILENT made messages (.desktop file) - always resolve ours
[kdepim.git] / kalarm / timeselector.cpp
blobd9077b4bb747866de5bcfbd0e6e89842f82d6f33
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.h"
27 #include <QHBoxLayout>
28 #include "kalarm_debug.h"
30 using namespace KCalCore;
33 TimeSelector::TimeSelector(const QString& selectText, const QString& selectWhatsThis,
34 const QString& valueWhatsThis, bool allowHourMinute, QWidget* parent)
35 : QFrame(parent),
36 mSignWidget(Q_NULLPTR),
37 mReadOnly(false)
39 QHBoxLayout* layout = new QHBoxLayout(this);
40 layout->setMargin(0);
41 mSelect = new CheckBox(selectText, this);
42 mSelect->setFixedSize(mSelect->sizeHint());
43 connect(mSelect, &CheckBox::toggled, this, &TimeSelector::selectToggled);
44 mSelect->setWhatsThis(selectWhatsThis);
45 layout->addWidget(mSelect);
47 QWidget* box = new QWidget(this); // to group widgets for QWhatsThis text
48 layout->addWidget(box);
49 QHBoxLayout* boxLayout = new QHBoxLayout(box);
50 boxLayout->setMargin(0);
51 mPeriod = new TimePeriod(allowHourMinute, box);
52 boxLayout->addWidget(mPeriod);
53 mPeriod->setFixedSize(mPeriod->sizeHint());
54 mPeriod->setSelectOnStep(false);
55 connect(mPeriod, &TimePeriod::valueChanged, this, &TimeSelector::periodChanged);
56 mSelect->setFocusWidget(mPeriod);
57 mPeriod->setEnabled(false);
59 box->setWhatsThis(valueWhatsThis);
60 layout->addStretch();
63 /******************************************************************************
64 * Create a ComboBox used to select the time period's sign.
65 * The caller is responsible for populating the ComboBox and handling its value.
67 ComboBox* TimeSelector::createSignCombo()
69 delete mSignWidget;
70 mSignWidget = new ComboBox(mPeriod->parentWidget());
71 mSignWidget->setEnabled(mPeriod->isEnabled());
72 return mSignWidget;
75 /******************************************************************************
76 * Set the read-only status.
78 void TimeSelector::setReadOnly(bool ro)
80 if ((int)ro != (int)mReadOnly)
82 mReadOnly = ro;
83 mSelect->setReadOnly(mReadOnly);
84 mPeriod->setReadOnly(mReadOnly);
85 if (mSignWidget)
86 mSignWidget->setReadOnly(mReadOnly);
90 bool TimeSelector::isChecked() const
92 return mSelect->isChecked();
95 void TimeSelector::setChecked(bool on)
97 if (on != mSelect->isChecked())
99 mSelect->setChecked(on);
100 Q_EMIT valueChanged(period());
104 void TimeSelector::setMaximum(int hourmin, int days)
106 mPeriod->setMaximum(hourmin, days);
109 void TimeSelector::setDateOnly(bool dateOnly)
111 mPeriod->setDateOnly(dateOnly);
114 /******************************************************************************
115 * Get the specified number of minutes.
116 * Reply = 0 if unselected.
118 Duration TimeSelector::period() const
120 return mSelect->isChecked() ? mPeriod->period() : Duration(0);
123 /******************************************************************************
124 * Initialise the controls with a specified time period.
125 * If minutes = 0, it will be deselected.
126 * The time unit combo-box is initialised to 'defaultUnits', but if 'dateOnly'
127 * is true, it will never be initialised to hours/minutes.
129 void TimeSelector::setPeriod(const Duration& period, bool dateOnly, TimePeriod::Units defaultUnits)
131 mSelect->setChecked(period);
132 mPeriod->setEnabled(period);
133 if (mSignWidget)
134 mSignWidget->setEnabled(period);
135 mPeriod->setPeriod(period, dateOnly, defaultUnits);
138 /******************************************************************************
139 * Set the input focus on the count field.
141 void TimeSelector::setFocusOnCount()
143 mPeriod->setFocusOnCount();
146 /******************************************************************************
147 * Called when the TimeSelector checkbox is toggled.
149 void TimeSelector::selectToggled(bool on)
151 mPeriod->setEnabled(on);
152 if (mSignWidget)
153 mSignWidget->setEnabled(on);
154 if (on)
155 mPeriod->setFocus();
156 Q_EMIT toggled(on);
157 Q_EMIT valueChanged(period());
160 /******************************************************************************
161 * Called when the period value changes.
163 void TimeSelector::periodChanged(const Duration& period)
165 if (mSelect->isChecked())
166 Q_EMIT valueChanged(period);
169 // vim: et sw=4: