Warning, file /pim/kalarm/src/timeselector.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  *  timeselector.cpp  -  widget to optionally set a time period
0003  *  Program:  kalarm
0004  *  SPDX-FileCopyrightText: 2004-2023 David Jarvie <djarvie@kde.org>
0005  *
0006  *  SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #include "timeselector.h"
0010 
0011 #include "lib/checkbox.h"
0012 #include "lib/combobox.h"
0013 
0014 #include <QHBoxLayout>
0015 
0016 using namespace KCalendarCore;
0017 
0018 
0019 TimeSelector::TimeSelector(const QString& selectText, const QString& selectWhatsThis,
0020                            const QString& valueWhatsThis, bool allowHourMinute, QWidget* parent)
0021     : QFrame(parent)
0022 {
0023     auto layout = new QHBoxLayout(this);
0024     layout->setContentsMargins(0, 0, 0, 0);
0025     mSelect = new CheckBox(selectText, this);
0026     connect(mSelect, &CheckBox::toggled, this, &TimeSelector::selectToggled);
0027     mSelect->setWhatsThis(selectWhatsThis);
0028     layout->addWidget(mSelect);
0029 
0030     QWidget* box = new QWidget(this);    // to group widgets for QWhatsThis text
0031     layout->addWidget(box);
0032     auto boxLayout = new QHBoxLayout(box);
0033     boxLayout->setContentsMargins(0, 0, 0, 0);
0034     mPeriod = new TimePeriod(allowHourMinute ? TimePeriod::ShowMinutes : TimePeriod::NoMinutes, box);
0035     boxLayout->addWidget(mPeriod);
0036     mPeriod->setSelectOnStep(false);
0037     connect(mPeriod, &TimePeriod::valueChanged, this, &TimeSelector::periodChanged);
0038     mSelect->setFocusWidget(mPeriod);
0039     mPeriod->setEnabled(false);
0040 
0041     box->setWhatsThis(valueWhatsThis);
0042     layout->addStretch();
0043 }
0044 
0045 /******************************************************************************
0046 * Create a ComboBox used to select the time period's sign.
0047 * The caller is responsible for populating the ComboBox and handling its value.
0048 */
0049 ComboBox* TimeSelector::createSignCombo()
0050 {
0051     delete mSignWidget;
0052     QWidget* p = mPeriod->parentWidget();
0053     mSignWidget = new ComboBox(p);
0054     mSignWidget->setEnabled(mPeriod->isEnabled());
0055     p->layout()->addWidget(mSignWidget);
0056     return mSignWidget;
0057 }
0058 
0059 /******************************************************************************
0060 * Set the read-only status.
0061 */
0062 void TimeSelector::setReadOnly(bool ro)
0063 {
0064     if (ro != mReadOnly)
0065     {
0066         mReadOnly = ro;
0067         mSelect->setReadOnly(mReadOnly);
0068         mPeriod->setReadOnly(mReadOnly);
0069         if (mSignWidget)
0070             mSignWidget->setReadOnly(mReadOnly);
0071     }
0072 }
0073 
0074 bool TimeSelector::isChecked() const
0075 {
0076     return mSelect->isChecked();
0077 }
0078 
0079 void TimeSelector::setChecked(bool on)
0080 {
0081     if (on != mSelect->isChecked())
0082     {
0083         mSelect->setChecked(on);
0084         Q_EMIT valueChanged(period());
0085     }
0086 }
0087 
0088 void TimeSelector::setMaximum(int hourmin, int days)
0089 {
0090     mPeriod->setMaximum(hourmin, days);
0091 }
0092 
0093 void TimeSelector::setDateOnly(bool dateOnly)
0094 {
0095     mPeriod->setDateOnly(dateOnly);
0096 }
0097 
0098 /******************************************************************************
0099  * Get the specified number of minutes.
0100  * Reply = 0 if unselected.
0101  */
0102 Duration TimeSelector::period() const
0103 {
0104     return mSelect->isChecked() ? mPeriod->period() : Duration(0);
0105 }
0106 
0107 /******************************************************************************
0108 * Initialise the controls with a specified time period.
0109 * If minutes = 0, it will be deselected.
0110 * The time unit combo-box is initialised to 'defaultUnits', but if 'dateOnly'
0111 * is true, it will never be initialised to hours/minutes.
0112 */
0113 void TimeSelector::setPeriod(const Duration& period, bool dateOnly, TimePeriod::Units defaultUnits)
0114 {
0115     bool havePeriod = !period.isNull();
0116     mSelect->setChecked(havePeriod);
0117     mPeriod->setEnabled(havePeriod);
0118     if (mSignWidget)
0119         mSignWidget->setEnabled(havePeriod);
0120     mPeriod->setPeriod(period, dateOnly, defaultUnits);
0121 }
0122 
0123 /******************************************************************************
0124 * Set the input focus on the count field.
0125 */
0126 void TimeSelector::setFocusOnCount()
0127 {
0128     mPeriod->setFocusOnCount();
0129 }
0130 
0131 /******************************************************************************
0132 * Called when the TimeSelector checkbox is toggled.
0133 */
0134 void TimeSelector::selectToggled(bool on)
0135 {
0136     mPeriod->setEnabled(on);
0137     if (mSignWidget)
0138         mSignWidget->setEnabled(on);
0139     if (on)
0140         mPeriod->setFocus();
0141     Q_EMIT toggled(on);
0142     Q_EMIT valueChanged(period());
0143 }
0144 
0145 /******************************************************************************
0146 * Called when the period value changes.
0147 */
0148 void TimeSelector::periodChanged(const Duration& period)
0149 {
0150     if (mSelect->isChecked())
0151         Q_EMIT valueChanged(period);
0152 }
0153 
0154 #include "moc_timeselector.cpp"
0155 
0156 // vim: et sw=4: