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
->setEnabled(mPeriod
->isEnabled());
73 p
->layout()->addWidget(mSignWidget
);
77 /******************************************************************************
78 * Set the read-only status.
80 void TimeSelector::setReadOnly(bool ro
)
82 if ((int)ro
!= (int)mReadOnly
)
85 mSelect
->setReadOnly(mReadOnly
);
86 mPeriod
->setReadOnly(mReadOnly
);
88 mSignWidget
->setReadOnly(mReadOnly
);
92 bool TimeSelector::isChecked() const
94 return mSelect
->isChecked();
97 void TimeSelector::setChecked(bool on
)
99 if (on
!= mSelect
->isChecked())
101 mSelect
->setChecked(on
);
102 Q_EMIT
valueChanged(period());
106 void TimeSelector::setMaximum(int hourmin
, int days
)
108 mPeriod
->setMaximum(hourmin
, days
);
111 void TimeSelector::setDateOnly(bool dateOnly
)
113 mPeriod
->setDateOnly(dateOnly
);
116 /******************************************************************************
117 * Get the specified number of minutes.
118 * Reply = 0 if unselected.
120 Duration
TimeSelector::period() const
122 return mSelect
->isChecked() ? mPeriod
->period() : Duration(0);
125 /******************************************************************************
126 * Initialise the controls with a specified time period.
127 * If minutes = 0, it will be deselected.
128 * The time unit combo-box is initialised to 'defaultUnits', but if 'dateOnly'
129 * is true, it will never be initialised to hours/minutes.
131 void TimeSelector::setPeriod(const Duration
& period
, bool dateOnly
, TimePeriod::Units defaultUnits
)
133 bool havePeriod
= !period
.isNull();
134 mSelect
->setChecked(havePeriod
);
135 mPeriod
->setEnabled(havePeriod
);
137 mSignWidget
->setEnabled(havePeriod
);
138 mPeriod
->setPeriod(period
, dateOnly
, defaultUnits
);
141 /******************************************************************************
142 * Set the input focus on the count field.
144 void TimeSelector::setFocusOnCount()
146 mPeriod
->setFocusOnCount();
149 /******************************************************************************
150 * Called when the TimeSelector checkbox is toggled.
152 void TimeSelector::selectToggled(bool on
)
154 mPeriod
->setEnabled(on
);
156 mSignWidget
->setEnabled(on
);
160 Q_EMIT
valueChanged(period());
163 /******************************************************************************
164 * Called when the period value changes.
166 void TimeSelector::periodChanged(const Duration
& period
)
168 if (mSelect
->isChecked())
169 Q_EMIT
valueChanged(period
);