2 * timeselector.cpp - widget to optionally set a time period
4 * Copyright © 2004-2016 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.
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
)
36 mSignWidget(Q_NULLPTR
),
39 QHBoxLayout
* layout
= new QHBoxLayout(this);
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
);
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()
70 QWidget
* p
= mPeriod
->parentWidget();
71 mSignWidget
= new ComboBox(p
);
72 mSignWidget
= new ComboBox(p
);
73 mSignWidget
->setEnabled(mPeriod
->isEnabled());
74 p
->layout()->addWidget(mSignWidget
);
78 /******************************************************************************
79 * Set the read-only status.
81 void TimeSelector::setReadOnly(bool ro
)
83 if ((int)ro
!= (int)mReadOnly
)
86 mSelect
->setReadOnly(mReadOnly
);
87 mPeriod
->setReadOnly(mReadOnly
);
89 mSignWidget
->setReadOnly(mReadOnly
);
93 bool TimeSelector::isChecked() const
95 return mSelect
->isChecked();
98 void TimeSelector::setChecked(bool on
)
100 if (on
!= mSelect
->isChecked())
102 mSelect
->setChecked(on
);
103 Q_EMIT
valueChanged(period());
107 void TimeSelector::setMaximum(int hourmin
, int days
)
109 mPeriod
->setMaximum(hourmin
, days
);
112 void TimeSelector::setDateOnly(bool dateOnly
)
114 mPeriod
->setDateOnly(dateOnly
);
117 /******************************************************************************
118 * Get the specified number of minutes.
119 * Reply = 0 if unselected.
121 Duration
TimeSelector::period() const
123 return mSelect
->isChecked() ? mPeriod
->period() : Duration(0);
126 /******************************************************************************
127 * Initialise the controls with a specified time period.
128 * If minutes = 0, it will be deselected.
129 * The time unit combo-box is initialised to 'defaultUnits', but if 'dateOnly'
130 * is true, it will never be initialised to hours/minutes.
132 void TimeSelector::setPeriod(const Duration
& period
, bool dateOnly
, TimePeriod::Units defaultUnits
)
134 bool havePeriod
= !period
.isNull();
135 mSelect
->setChecked(havePeriod
);
136 mPeriod
->setEnabled(havePeriod
);
138 mSignWidget
->setEnabled(havePeriod
);
139 mPeriod
->setPeriod(period
, dateOnly
, defaultUnits
);
142 /******************************************************************************
143 * Set the input focus on the count field.
145 void TimeSelector::setFocusOnCount()
147 mPeriod
->setFocusOnCount();
150 /******************************************************************************
151 * Called when the TimeSelector checkbox is toggled.
153 void TimeSelector::selectToggled(bool on
)
155 mPeriod
->setEnabled(on
);
157 mSignWidget
->setEnabled(on
);
161 Q_EMIT
valueChanged(period());
164 /******************************************************************************
165 * Called when the period value changes.
167 void TimeSelector::periodChanged(const Duration
& period
)
169 if (mSelect
->isChecked())
170 Q_EMIT
valueChanged(period
);