kalarm

timeselector.cpp
1/*
2 * timeselector.cpp - widget to optionally set a time period
3 * Program: kalarm
4 * Copyright (C) 2004 by David Jarvie <software@astrojar.org.uk>
5 *
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.
10 *
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.
15 *
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.
19 */
20
21#include "kalarm.h"
22
23#include <tqlayout.h>
24#include <tqlabel.h>
25#include <tqhbox.h>
26#include <tqwhatsthis.h>
27
28#include <tdelocale.h>
29#include <kdialog.h>
30#include <kdebug.h>
31
32#include "checkbox.h"
33#include "timeselector.moc"
34
35
36TimeSelector::TimeSelector(const TQString& selectText, const TQString& postfix, const TQString& selectWhatsThis,
37 const TQString& valueWhatsThis, bool allowHourMinute, TQWidget* parent, const char* name)
38 : TQFrame(parent, name),
39 mLabel(0),
40 mReadOnly(false)
41{
42 setFrameStyle(TQFrame::NoFrame);
43 TQVBoxLayout* topLayout = new TQVBoxLayout(this, 0, KDialog::spacingHint());
44 TQHBoxLayout* layout = new TQHBoxLayout(topLayout, KDialog::spacingHint());
45 mSelect = new CheckBox(selectText, this);
46 mSelect->setFixedSize(mSelect->sizeHint());
47 connect(mSelect, TQ_SIGNAL(toggled(bool)), TQ_SLOT(selectToggled(bool)));
48 TQWhatsThis::add(mSelect, selectWhatsThis);
49 layout->addWidget(mSelect);
50
51 TQHBox* box = new TQHBox(this); // to group widgets for TQWhatsThis text
52 box->setSpacing(KDialog::spacingHint());
53 layout->addWidget(box);
54 mPeriod = new TimePeriod(allowHourMinute, box);
55 mPeriod->setFixedSize(mPeriod->sizeHint());
56 mPeriod->setSelectOnStep(false);
57 connect(mPeriod, TQ_SIGNAL(valueChanged(int)), TQ_SLOT(periodChanged(int)));
58 mSelect->setFocusWidget(mPeriod);
59 mPeriod->setEnabled(false);
60
61 if (!postfix.isEmpty())
62 {
63 mLabel = new TQLabel(postfix, box);
64 TQWhatsThis::add(box, valueWhatsThis);
65 mLabel->setEnabled(false);
66 }
67}
68
69/******************************************************************************
70* Set the read-only status.
71*/
72void TimeSelector::setReadOnly(bool ro)
73{
74 if ((int)ro != (int)mReadOnly)
75 {
76 mReadOnly = ro;
77 mSelect->setReadOnly(mReadOnly);
78 mPeriod->setReadOnly(mReadOnly);
79 }
80}
81
82bool TimeSelector::isChecked() const
83{
84 return mSelect->isChecked();
85}
86
87void TimeSelector::setChecked(bool on)
88{
89 if (on != mSelect->isChecked())
90 {
91 mSelect->setChecked(on);
92 emit valueChanged(minutes());
93 }
94}
95
96void TimeSelector::setMaximum(int hourmin, int days)
97{
98 mPeriod->setMaximum(hourmin, days);
99}
100
101void TimeSelector::setDateOnly(bool dateOnly)
102{
103 mPeriod->setDateOnly(dateOnly);
104}
105
106/******************************************************************************
107 * Get the specified number of minutes.
108 * Reply = 0 if unselected.
109 */
110int TimeSelector::minutes() const
111{
112 return mSelect->isChecked() ? mPeriod->minutes() : 0;
113}
114
115/******************************************************************************
116* Initialise the controls with a specified time period.
117* If minutes = 0, it will be deselected.
118* The time unit combo-box is initialised to 'defaultUnits', but if 'dateOnly'
119* is true, it will never be initialised to hours/minutes.
120*/
121void TimeSelector::setMinutes(int minutes, bool dateOnly, TimePeriod::Units defaultUnits)
122{
123 mSelect->setChecked(minutes);
124 mPeriod->setEnabled(minutes);
125 if (mLabel)
126 mLabel->setEnabled(minutes);
127 mPeriod->setMinutes(minutes, dateOnly, defaultUnits);
128}
129
130/******************************************************************************
131* Set the input focus on the count field.
132*/
133void TimeSelector::setFocusOnCount()
134{
135 mPeriod->setFocusOnCount();
136}
137
138/******************************************************************************
139* Called when the TimeSelector checkbox is toggled.
140*/
141void TimeSelector::selectToggled(bool on)
142{
143 mPeriod->setEnabled(on);
144 if (mLabel)
145 mLabel->setEnabled(on);
146 if (on)
147 mPeriod->setFocus();
148 emit toggled(on);
149 emit valueChanged(minutes());
150}
151
152/******************************************************************************
153* Called when the period value changes.
154*/
155void TimeSelector::periodChanged(int minutes)
156{
157 if (mSelect->isChecked())
158 emit valueChanged(minutes);
159}