2 * latecancel.cpp - widget to specify cancellation if late
4 * Copyright © 2004,2005,2007-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.
22 #include "latecancel.h"
26 #include <KCalCore/Duration>
27 using namespace KCalCore
;
29 #include <KLocalizedString>
31 #include <QStackedWidget>
32 #include <QVBoxLayout>
33 #include <QHBoxLayout>
37 // Collect these widget labels together to ensure consistent wording and
38 // translations across different modules.
39 QString
LateCancelSelector::i18n_chk_CancelIfLate() { return i18nc("@option:check", "Cancel if late"); }
40 QString
LateCancelSelector::i18n_chk_AutoCloseWin() { return i18nc("@option:check", "Auto-close window after this time"); }
41 QString
LateCancelSelector::i18n_chk_AutoCloseWinLC() { return i18nc("@option:check", "Auto-close window after late-cancellation time"); }
44 LateCancelSelector::LateCancelSelector(bool allowHourMinute
, QWidget
* parent
)
48 mAutoCloseShown(false)
50 QString whatsThis
= xi18nc("@info:whatsthis",
51 "<para>If checked, the alarm will be canceled if it cannot be triggered within the "
52 "specified period after its scheduled time. Possible reasons for not triggering "
53 "include your being logged off, X not running, or <application>KAlarm</application> not running.</para>"
54 "<para>If unchecked, the alarm will be triggered at the first opportunity after "
55 "its scheduled time, regardless of how late it is.</para>");
57 QVBoxLayout
* topLayout
= new QVBoxLayout(this);
58 topLayout
->setMargin(0);
59 topLayout
->setSpacing(style()->pixelMetric(QStyle::PM_DefaultLayoutSpacing
));
61 mStack
= new QStackedWidget(this);
62 topLayout
->addWidget(mStack
, 0, Qt::AlignLeft
);
63 mCheckboxFrame
= new QFrame();
64 mStack
->addWidget(mCheckboxFrame
);
65 QHBoxLayout
* hlayout
= new QHBoxLayout(mCheckboxFrame
);
66 hlayout
->setMargin(0);
67 mCheckbox
= new CheckBox(i18n_chk_CancelIfLate(), mCheckboxFrame
);
68 connect(mCheckbox
, &CheckBox::toggled
, this, &LateCancelSelector::slotToggled
);
69 connect(mCheckbox
, &CheckBox::toggled
, this, &LateCancelSelector::changed
);
70 mCheckbox
->setWhatsThis(whatsThis
);
71 hlayout
->addWidget(mCheckbox
, 0, Qt::AlignLeft
);
73 mTimeSelectorFrame
= new QFrame();
74 mStack
->addWidget(mTimeSelectorFrame
);
75 hlayout
= new QHBoxLayout(mTimeSelectorFrame
);
76 hlayout
->setMargin(0);
77 mTimeSelector
= new TimeSelector(i18nc("@option:check Cancel if late by 10 minutes", "Cancel if late by"),
78 whatsThis
, i18nc("@info:whatsthis", "Enter how late will cause the alarm to be canceled"),
79 allowHourMinute
, mTimeSelectorFrame
);
80 connect(mTimeSelector
, &TimeSelector::toggled
, this, &LateCancelSelector::slotToggled
);
81 connect(mTimeSelector
, &TimeSelector::valueChanged
, this, &LateCancelSelector::changed
);
82 hlayout
->addWidget(mTimeSelector
, 0, Qt::AlignLeft
);
84 hlayout
= new QHBoxLayout();
85 hlayout
->setMargin(0);
86 hlayout
->addSpacing(3 * style()->pixelMetric(QStyle::PM_DefaultLayoutSpacing
));
87 topLayout
->addLayout(hlayout
);
88 mAutoClose
= new CheckBox(i18n_chk_AutoCloseWin(), this);
89 connect(mAutoClose
, &CheckBox::toggled
, this, &LateCancelSelector::changed
);
90 mAutoClose
->setWhatsThis(i18nc("@info:whatsthis", "Automatically close the alarm window after the expiry of the late-cancellation period"));
91 hlayout
->addWidget(mAutoClose
);
92 hlayout
->addStretch();
95 mAutoClose
->setEnabled(false);
98 /******************************************************************************
99 * Set the read-only status.
101 void LateCancelSelector::setReadOnly(bool ro
)
103 if ((int)ro
!= (int)mReadOnly
)
106 mCheckbox
->setReadOnly(mReadOnly
);
107 mTimeSelector
->setReadOnly(mReadOnly
);
108 mAutoClose
->setReadOnly(mReadOnly
);
112 int LateCancelSelector::minutes() const
114 return mTimeSelector
->period().asSeconds() / 60;
117 void LateCancelSelector::setMinutes(int minutes
, bool dateOnly
, TimePeriod::Units defaultUnits
)
119 slotToggled(minutes
);
121 if (minutes
% (24*60))
122 period
= Duration(minutes
* 60, Duration::Seconds
);
124 period
= Duration(minutes
/ (24*60), Duration::Days
);
125 mTimeSelector
->setPeriod(period
, dateOnly
, defaultUnits
);
128 void LateCancelSelector::setDateOnly(bool dateOnly
)
130 if (dateOnly
!= mDateOnly
)
132 mDateOnly
= dateOnly
;
133 if (mTimeSelector
->isChecked()) // don't change when it's not visible
134 mTimeSelector
->setDateOnly(dateOnly
);
138 void LateCancelSelector::showAutoClose(bool show
)
144 mAutoCloseShown
= show
;
148 bool LateCancelSelector::isAutoClose() const
150 return mAutoCloseShown
&& mAutoClose
->isEnabled() && mAutoClose
->isChecked();
153 void LateCancelSelector::setAutoClose(bool autoClose
)
155 mAutoClose
->setChecked(autoClose
);
158 /******************************************************************************
159 * Called when either of the checkboxes is toggled.
161 void LateCancelSelector::slotToggled(bool on
)
163 mCheckbox
->setChecked(on
);
164 mTimeSelector
->setChecked(on
);
167 mTimeSelector
->setDateOnly(mDateOnly
);
168 mStack
->setCurrentWidget(mTimeSelectorFrame
);
171 mStack
->setCurrentWidget(mCheckboxFrame
);
172 mAutoClose
->setEnabled(on
);