2 * timeselector.cpp - widget to optionally set a time period
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.
25 #include "timeselector.moc"
32 #include <QVBoxLayout>
33 #include <QHBoxLayout>
36 using namespace KCalCore
;
42 TimeSelector::TimeSelector(const QString
& selectText
, const QString
& selectWhatsThis
,
43 const QString
& valueWhatsThis
, bool allowHourMinute
, QWidget
* parent
)
48 QHBoxLayout
* layout
= new QHBoxLayout(this);
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);
64 connect(mPeriod
, SIGNAL(valueChanged(KCalCore::Duration
)), SLOT(periodChanged(KCalCore::Duration
)));
66 connect(mPeriod
, SIGNAL(valueChanged(KCal::Duration
)), SLOT(periodChanged(KCal::Duration
)));
68 mSelect
->setFocusWidget(mPeriod
);
69 mPeriod
->setEnabled(false);
71 box
->setWhatsThis(valueWhatsThis
);
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()
82 mSignWidget
= new ComboBox(mPeriod
->parentWidget());
83 mSignWidget
->setEnabled(mPeriod
->isEnabled());
87 /******************************************************************************
88 * Set the read-only status.
90 void TimeSelector::setReadOnly(bool ro
)
92 if ((int)ro
!= (int)mReadOnly
)
95 mSelect
->setReadOnly(mReadOnly
);
96 mPeriod
->setReadOnly(mReadOnly
);
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
);
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
);
165 mSignWidget
->setEnabled(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
);