File indexing completed on 2024-05-26 05:16:44

0001 /*
0002  *  latecancel.cpp  -  widget to specify cancellation if late
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 "latecancel.h"
0010 
0011 #include "lib/checkbox.h"
0012 
0013 #include <KCalendarCore/Duration>
0014 using namespace KCalendarCore;
0015 
0016 #include <KLocalizedString>
0017 
0018 #include <QStackedWidget>
0019 #include <QVBoxLayout>
0020 #include <QHBoxLayout>
0021 
0022 
0023 // Collect these widget labels together to ensure consistent wording and
0024 // translations across different modules.
0025 QString LateCancelSelector::i18n_chk_CancelIfLate()    { return i18nc("@option:check", "Cancel if late"); }
0026 QString LateCancelSelector::i18n_chk_AutoCloseWin()    { return i18nc("@option:check", "Auto-close window after this time"); }
0027 QString LateCancelSelector::i18n_chk_AutoCloseWinLC()  { return i18nc("@option:check", "Auto-close window after late-cancellation time"); }
0028 
0029 
0030 LateCancelSelector::LateCancelSelector(bool allowHourMinute, QWidget* parent)
0031     : QFrame(parent)
0032 {
0033     QString whatsThis = xi18nc("@info:whatsthis",
0034                               "<para>If checked, the alarm will be canceled if it cannot be triggered within the "
0035                              "specified period after its scheduled time. Possible reasons for not triggering "
0036                              "include your being logged off, X not running, or <application>KAlarm</application> not running.</para>"
0037                              "<para>If unchecked, the alarm will be triggered at the first opportunity after "
0038                              "its scheduled time, regardless of how late it is.</para>");
0039 
0040     auto topLayout = new QVBoxLayout(this);
0041     topLayout->setContentsMargins(0, 0, 0, 0);
0042 
0043     mStack = new QStackedWidget(this);
0044     topLayout->addWidget(mStack, 0, Qt::AlignLeft);
0045     mCheckboxFrame = new QFrame();
0046     mStack->addWidget(mCheckboxFrame);
0047     auto hlayout = new QHBoxLayout(mCheckboxFrame);
0048     hlayout->setContentsMargins(0, 0, 0, 0);
0049     mCheckbox = new CheckBox(i18n_chk_CancelIfLate(), mCheckboxFrame);
0050     connect(mCheckbox, &CheckBox::toggled, this, &LateCancelSelector::slotToggled);
0051     connect(mCheckbox, &CheckBox::toggled, this, &LateCancelSelector::changed);
0052     mCheckbox->setWhatsThis(whatsThis);
0053     hlayout->addWidget(mCheckbox, 0, Qt::AlignLeft);
0054 
0055     mTimeSelectorFrame = new QFrame();
0056     mStack->addWidget(mTimeSelectorFrame);
0057     hlayout = new QHBoxLayout(mTimeSelectorFrame);
0058     hlayout->setContentsMargins(0, 0, 0, 0);
0059     mTimeSelector = new TimeSelector(i18nc("@option:check Cancel if late by 10 minutes", "Cancel if late by"),
0060                                      whatsThis, i18nc("@info:whatsthis", "Enter how late will cause the alarm to be canceled"),
0061                                      allowHourMinute, mTimeSelectorFrame);
0062     connect(mTimeSelector, &TimeSelector::toggled, this, &LateCancelSelector::slotToggled);
0063     connect(mTimeSelector, &TimeSelector::valueChanged, this, &LateCancelSelector::changed);
0064     hlayout->addWidget(mTimeSelector, 0, Qt::AlignLeft);
0065 
0066     mAutoCloseLayout = new QHBoxLayout();
0067     const int indent = CheckBox::textIndent(mCheckbox);
0068     if (layoutDirection() == Qt::LeftToRight)
0069         mAutoCloseLayout->setContentsMargins(indent, 0, 0, 0);
0070     else
0071         mAutoCloseLayout->setContentsMargins(0, 0, indent, 0);
0072     topLayout->addLayout(mAutoCloseLayout);
0073     mAutoClose = new CheckBox(i18n_chk_AutoCloseWin(), this);
0074     connect(mAutoClose, &CheckBox::toggled, this, &LateCancelSelector::changed);
0075     mAutoClose->setWhatsThis(i18nc("@info:whatsthis", "Automatically close the alarm window after the expiry of the late-cancellation period"));
0076     mAutoCloseLayout->addWidget(mAutoClose);
0077     mAutoCloseLayout->addStretch();
0078 
0079     mAutoClose->hide();
0080     mAutoClose->setEnabled(false);
0081 }
0082 
0083 /******************************************************************************
0084 * Set the read-only status.
0085 */
0086 void LateCancelSelector::setReadOnly(bool ro)
0087 {
0088     if (ro != mReadOnly)
0089     {
0090         mReadOnly = ro;
0091         mCheckbox->setReadOnly(mReadOnly);
0092         mTimeSelector->setReadOnly(mReadOnly);
0093         mAutoClose->setReadOnly(mReadOnly);
0094     }
0095 }
0096 
0097 /******************************************************************************
0098 * Add a widget to the layout, right adjusted, beside Auto-close checkbox.
0099 */
0100 void LateCancelSelector::addWidget(QWidget* widget)
0101 {
0102     mAutoCloseLayout->addWidget(widget);
0103 }
0104 
0105 int LateCancelSelector::minutes() const
0106 {
0107     return mTimeSelector->period().asSeconds() / 60;
0108 }
0109 
0110 void LateCancelSelector::setMinutes(int minutes, bool dateOnly, TimePeriod::Units defaultUnits)
0111 {
0112     slotToggled(minutes);
0113     Duration period;
0114     if (minutes % (24*60))
0115         period = Duration(minutes * 60, Duration::Seconds);
0116     else
0117         period = Duration(minutes / (24*60), Duration::Days);
0118     mTimeSelector->setPeriod(period, dateOnly, defaultUnits);
0119 }
0120 
0121 void LateCancelSelector::setDateOnly(bool dateOnly)
0122 {
0123     if (dateOnly != mDateOnly)
0124     {
0125         mDateOnly = dateOnly;
0126         if (mTimeSelector->isChecked())      // don't change when it's not visible
0127             mTimeSelector->setDateOnly(dateOnly);
0128     }
0129 }
0130 
0131 void LateCancelSelector::showAutoClose(bool show)
0132 {
0133     if (show)
0134         mAutoClose->show();
0135     else
0136         mAutoClose->hide();
0137     mAutoCloseShown = show;
0138     updateGeometry();
0139 }
0140 
0141 bool LateCancelSelector::isAutoClose() const
0142 {
0143     return mAutoCloseShown  &&  mAutoClose->isEnabled()  &&  mAutoClose->isChecked();
0144 }
0145 
0146 void LateCancelSelector::setAutoClose(bool autoClose)
0147 {
0148     mAutoClose->setChecked(autoClose);
0149 }
0150 
0151 /******************************************************************************
0152 * Called when either of the checkboxes is toggled.
0153 */
0154 void LateCancelSelector::slotToggled(bool on)
0155 {
0156     mCheckbox->setChecked(on);
0157     mTimeSelector->setChecked(on);
0158     if (on)
0159     {
0160         mTimeSelector->setDateOnly(mDateOnly);
0161         mStack->setCurrentWidget(mTimeSelectorFrame);
0162     }
0163     else
0164         mStack->setCurrentWidget(mCheckboxFrame);
0165     mAutoClose->setEnabled(on);
0166 }
0167 
0168 #include "moc_latecancel.cpp"
0169 
0170 // vim: et sw=4: