timeselector.cpp
00001 /* 00002 * timeselector.cpp - widget to optionally set a time period 00003 * Program: kalarm 00004 * Copyright (C) 2004 by David Jarvie <software@astrojar.org.uk> 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License along 00017 * with this program; if not, write to the Free Software Foundation, Inc., 00018 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include "kalarm.h" 00022 00023 #include <tqlayout.h> 00024 #include <tqlabel.h> 00025 #include <tqhbox.h> 00026 #include <tqwhatsthis.h> 00027 00028 #include <klocale.h> 00029 #include <kdialog.h> 00030 #include <kdebug.h> 00031 00032 #include "checkbox.h" 00033 #include "timeselector.moc" 00034 00035 00036 TimeSelector::TimeSelector(const TQString& selectText, const TQString& postfix, const TQString& selectWhatsThis, 00037 const TQString& valueWhatsThis, bool allowHourMinute, TQWidget* parent, const char* name) 00038 : TQFrame(parent, name), 00039 mLabel(0), 00040 mReadOnly(false) 00041 { 00042 setFrameStyle(TQFrame::NoFrame); 00043 TQVBoxLayout* topLayout = new TQVBoxLayout(this, 0, KDialog::spacingHint()); 00044 TQHBoxLayout* layout = new TQHBoxLayout(topLayout, KDialog::spacingHint()); 00045 mSelect = new CheckBox(selectText, this); 00046 mSelect->setFixedSize(mSelect->sizeHint()); 00047 connect(mSelect, TQT_SIGNAL(toggled(bool)), TQT_SLOT(selectToggled(bool))); 00048 TQWhatsThis::add(mSelect, selectWhatsThis); 00049 layout->addWidget(mSelect); 00050 00051 TQHBox* box = new TQHBox(this); // to group widgets for TQWhatsThis text 00052 box->setSpacing(KDialog::spacingHint()); 00053 layout->addWidget(box); 00054 mPeriod = new TimePeriod(allowHourMinute, box); 00055 mPeriod->setFixedSize(mPeriod->sizeHint()); 00056 mPeriod->setSelectOnStep(false); 00057 connect(mPeriod, TQT_SIGNAL(valueChanged(int)), TQT_SLOT(periodChanged(int))); 00058 mSelect->setFocusWidget(mPeriod); 00059 mPeriod->setEnabled(false); 00060 00061 if (!postfix.isEmpty()) 00062 { 00063 mLabel = new TQLabel(postfix, box); 00064 TQWhatsThis::add(box, valueWhatsThis); 00065 mLabel->setEnabled(false); 00066 } 00067 } 00068 00069 /****************************************************************************** 00070 * Set the read-only status. 00071 */ 00072 void TimeSelector::setReadOnly(bool ro) 00073 { 00074 if ((int)ro != (int)mReadOnly) 00075 { 00076 mReadOnly = ro; 00077 mSelect->setReadOnly(mReadOnly); 00078 mPeriod->setReadOnly(mReadOnly); 00079 } 00080 } 00081 00082 bool TimeSelector::isChecked() const 00083 { 00084 return mSelect->isChecked(); 00085 } 00086 00087 void TimeSelector::setChecked(bool on) 00088 { 00089 if (on != mSelect->isChecked()) 00090 { 00091 mSelect->setChecked(on); 00092 emit valueChanged(minutes()); 00093 } 00094 } 00095 00096 void TimeSelector::setMaximum(int hourmin, int days) 00097 { 00098 mPeriod->setMaximum(hourmin, days); 00099 } 00100 00101 void TimeSelector::setDateOnly(bool dateOnly) 00102 { 00103 mPeriod->setDateOnly(dateOnly); 00104 } 00105 00106 /****************************************************************************** 00107 * Get the specified number of minutes. 00108 * Reply = 0 if unselected. 00109 */ 00110 int TimeSelector::minutes() const 00111 { 00112 return mSelect->isChecked() ? mPeriod->minutes() : 0; 00113 } 00114 00115 /****************************************************************************** 00116 * Initialise the controls with a specified time period. 00117 * If minutes = 0, it will be deselected. 00118 * The time unit combo-box is initialised to 'defaultUnits', but if 'dateOnly' 00119 * is true, it will never be initialised to hours/minutes. 00120 */ 00121 void TimeSelector::setMinutes(int minutes, bool dateOnly, TimePeriod::Units defaultUnits) 00122 { 00123 mSelect->setChecked(minutes); 00124 mPeriod->setEnabled(minutes); 00125 if (mLabel) 00126 mLabel->setEnabled(minutes); 00127 mPeriod->setMinutes(minutes, dateOnly, defaultUnits); 00128 } 00129 00130 /****************************************************************************** 00131 * Set the input focus on the count field. 00132 */ 00133 void TimeSelector::setFocusOnCount() 00134 { 00135 mPeriod->setFocusOnCount(); 00136 } 00137 00138 /****************************************************************************** 00139 * Called when the TimeSelector checkbox is toggled. 00140 */ 00141 void TimeSelector::selectToggled(bool on) 00142 { 00143 mPeriod->setEnabled(on); 00144 if (mLabel) 00145 mLabel->setEnabled(on); 00146 if (on) 00147 mPeriod->setFocus(); 00148 emit toggled(on); 00149 emit valueChanged(minutes()); 00150 } 00151 00152 /****************************************************************************** 00153 * Called when the period value changes. 00154 */ 00155 void TimeSelector::periodChanged(int minutes) 00156 { 00157 if (mSelect->isChecked()) 00158 emit valueChanged(minutes); 00159 }