KCalUtils

recurrenceactions.cpp
1/*
2 This file is part of the kcal library.
3
4 SPDX-FileCopyrightText: Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
5 SPDX-FileContributor: Kevin Krammer <krake@kdab.com>
6
7 SPDX-FileCopyrightText: 2015 SĂ©rgio Martins <iamsergio@gmail.com>
8
9 SPDX-License-Identifier: LGPL-2.0-or-later
10*/
11
12#include "recurrenceactions.h"
13
14#include "ui_recurrenceactionsscopewidget.h"
15
16#include <KGuiItem>
17#include <KLocalizedString>
18#include <KMessageBox>
19
20#include <QDialog>
21#include <QDialogButtonBox>
22#include <QPointer>
23#include <QPushButton>
24#include <QStyle>
25#include <QStyleOption>
26#include <QVBoxLayout>
27
28using namespace KCalUtils;
29using namespace KCalUtils::RecurrenceActions;
30using namespace KCalendarCore;
31
32class ScopeWidget : public QWidget
33{
35public:
36 ScopeWidget(int availableChoices, const QDateTime &dateTime, QWidget *parent)
38 , mAvailableChoices(availableChoices)
39 {
40 mUi.setupUi(this);
41
42 if ((mAvailableChoices & PastOccurrences) == 0) {
43 mUi.checkBoxPast->hide();
44 } else {
45 mUi.checkBoxPast->setText(
46 i18nc("@option:check calendar items before a certain date", "Items before %1", QLocale().toString(dateTime, QLocale::ShortFormat)));
47 }
48 if ((mAvailableChoices & SelectedOccurrence) == 0) {
49 mUi.checkBoxSelected->hide();
50 } else {
51 mUi.checkBoxSelected->setText(i18nc("@option:check currently selected calendar item", "Selected item"));
52 }
53 if ((mAvailableChoices & FutureOccurrences) == 0) {
54 mUi.checkBoxFuture->hide();
55 } else {
56 mUi.checkBoxFuture->setText(
57 i18nc("@option:check calendar items after a certain date", "Items after %1", QLocale().toString(dateTime, QLocale::ShortFormat)));
58 }
59 }
60
61 ~ScopeWidget() override;
62
63 void setMessage(const QString &message);
64 void setIcon(const QIcon &icon);
65
66 void setCheckedChoices(int choices);
67 [[nodiscard]] int checkedChoices() const;
68
69private:
70 const int mAvailableChoices;
71 Ui_RecurrenceActionsScopeWidget mUi;
72};
73
74ScopeWidget::~ScopeWidget()
75{
76}
77
78void ScopeWidget::setMessage(const QString &message)
79{
80 mUi.messageLabel->setText(message);
81}
82
83void ScopeWidget::setIcon(const QIcon &icon)
84{
85 QStyleOption option;
86 option.initFrom(this);
87 mUi.iconLabel->setPixmap(icon.pixmap(style()->pixelMetric(QStyle::PM_MessageBoxIconSize, &option, this)));
88}
89
90void ScopeWidget::setCheckedChoices(int choices)
91{
92 // mask with available ones
93 choices &= mAvailableChoices;
94
95 mUi.checkBoxPast->setChecked((choices & PastOccurrences) != 0);
96 mUi.checkBoxSelected->setChecked((choices & SelectedOccurrence) != 0);
97 mUi.checkBoxFuture->setChecked((choices & FutureOccurrences) != 0);
98}
99
100int ScopeWidget::checkedChoices() const
101{
102 int result = NoOccurrence;
103
104 if (mUi.checkBoxPast->isChecked()) {
105 result |= PastOccurrences;
106 }
107 if (mUi.checkBoxSelected->isChecked()) {
108 result |= SelectedOccurrence;
109 }
110 if (mUi.checkBoxFuture->isChecked()) {
111 result |= FutureOccurrences;
112 }
113
114 return result;
115}
116
117int RecurrenceActions::availableOccurrences(const Incidence::Ptr &incidence, const QDateTime &selectedOccurrence)
118{
119 int result = NoOccurrence;
120
121 if (incidence->recurrence()->recursOn(selectedOccurrence.date(), selectedOccurrence.timeZone())) {
122 result |= SelectedOccurrence;
123 }
124
125 if (incidence->recurrence()->getPreviousDateTime(selectedOccurrence).isValid()) {
126 result |= PastOccurrences;
127 }
128
129 if (incidence->recurrence()->getNextDateTime(selectedOccurrence).isValid()) {
130 result |= FutureOccurrences;
131 }
132
133 return result;
134}
135
136static QDialog *
137createDialog(QDialogButtonBox::StandardButtons buttons, const QString &caption, QWidget *mainWidget, QDialogButtonBox **buttonBox, QWidget *parent)
138{
139 QPointer<QDialog> dialog = new QDialog(parent);
140 dialog->setWindowTitle(caption);
141 auto mainLayout = new QVBoxLayout();
142 dialog->setLayout(mainLayout);
143
144 *buttonBox = new QDialogButtonBox(buttons, parent);
145 QPushButton *okButton = (*buttonBox)->button(QDialogButtonBox::Ok);
146 okButton->setDefault(true);
150 (*buttonBox)->button(QDialogButtonBox::Ok)->setDefault(true);
151
152 if (mainWidget) {
153 mainLayout->addWidget(mainWidget);
154 }
155
156 mainLayout->addWidget(*buttonBox);
157
158 return dialog;
159}
160
162 const QString &message,
163 const QString &caption,
164 const KGuiItem &action,
165 int availableChoices,
166 int preselectedChoices,
167 QWidget *parent)
168{
170 auto widget = new ScopeWidget(availableChoices, selectedOccurrence, nullptr);
171 QDialogButtonBox *buttonBox = nullptr;
172 auto dialog = createDialog(buttons, caption, widget, &buttonBox, parent);
173
174 KGuiItem::assign(buttonBox->button(QDialogButtonBox::Ok), action);
175
176 widget->setMessage(message);
177 widget->setIcon(widget->style()->standardIcon(QStyle::SP_MessageBoxQuestion));
178 widget->setCheckedChoices(preselectedChoices);
179
180 const int result = dialog->exec();
181 if (dialog) {
182 dialog->deleteLater();
183 }
184
185 if (result == QDialog::Rejected) {
186 return NoOccurrence;
187 }
188
189 return widget->checkedChoices();
190}
191
193 const QString &caption,
194 const KGuiItem &actionSelected,
195 const KGuiItem &actionAll,
196 QWidget *parent)
197{
198 QPointer<QDialog> dialog = new QDialog(parent);
199 dialog->setWindowTitle(caption);
201 dialog->setObjectName(QLatin1StringView("RecurrenceActions::questionSelectedAllCancel"));
202
203 KGuiItem::assign(buttonBox->button(QDialogButtonBox::Yes), actionSelected);
204 KGuiItem::assign(buttonBox->button(QDialogButtonBox::Ok), actionAll);
205
206 QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
207 okButton->setDefault(true);
209 buttonBox->button(QDialogButtonBox::Ok)->setDefault(true);
210
211 bool checkboxResult = false;
212 int result =
213 KMessageBox::createKMessageBox(dialog, buttonBox, QMessageBox::Question, message, QStringList(), QString(), &checkboxResult, KMessageBox::Notify);
214
215 switch (result) {
217 return SelectedOccurrence;
219 return AllOccurrences;
220 default:
221 return NoOccurrence;
222 }
223}
224
226 const QString &caption,
227 const KGuiItem &actionSelected,
228 const KGuiItem &actionFuture,
229 const KGuiItem &actionAll,
230 QWidget *parent)
231{
232 QPointer<QDialog> dialog = new QDialog(parent);
233 dialog->setWindowTitle(caption);
234
236 dialog->setObjectName(QLatin1StringView("RecurrenceActions::questionSelectedFutureAllCancel"));
237 KGuiItem::assign(buttonBox->button(QDialogButtonBox::Yes), actionSelected);
238 KGuiItem::assign(buttonBox->button(QDialogButtonBox::No), actionFuture);
239 KGuiItem::assign(buttonBox->button(QDialogButtonBox::Ok), actionAll);
240 QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
241 okButton->setDefault(true);
243 buttonBox->button(QDialogButtonBox::Ok)->setDefault(true);
244
245 bool checkboxResult = false;
247 KMessageBox::createKMessageBox(dialog, buttonBox, QMessageBox::Question, message, QStringList(), QString(), &checkboxResult, KMessageBox::Notify);
248
249 switch (result) {
251 return SelectedOccurrence;
253 return FutureOccurrences;
255 return AllOccurrences;
256 default:
257 return NoOccurrence;
258 }
259
260 return NoOccurrence;
261}
262
263#include "recurrenceactions.moc"
static void assign(QPushButton *button, const KGuiItem &item)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
char * toString(const EngineQuery &query)
Utility functions for dealing with recurrences.
KCALUTILS_EXPORT int availableOccurrences(const KCalendarCore::Incidence::Ptr &incidence, const QDateTime &selectedOccurrence)
Checks what scope an action could be applied on for a given incidence.
KCALUTILS_EXPORT int questionSelectedFutureAllCancel(const QString &message, const QString &caption, const KGuiItem &actionSelected, const KGuiItem &actionFuture, const KGuiItem &actionAll, QWidget *parent)
Presents a message box with three action choices and cancel to the user.
KCALUTILS_EXPORT int questionMultipleChoice(const QDateTime &selectedOccurrence, const QString &message, const QString &caption, const KGuiItem &action, int availableChoices, int preselectedChoices, QWidget *parent)
Presents a multiple choice scope selection dialog to the user.
@ SelectedOccurrence
Scope does include the given/selected occurrence.
@ AllOccurrences
Scope does include all occurrences (past, present and future)
@ FutureOccurrences
Scope does include occurrences after the given/selected occurrence.
@ PastOccurrences
Scope does include occurrences before the given/selected occurrence.
@ NoOccurrence
Scope does not apply to any occurrence.
KCALUTILS_EXPORT int questionSelectedAllCancel(const QString &message, const QString &caption, const KGuiItem &actionSelected, const KGuiItem &actionAll, QWidget *parent)
Presents a message box with two action choices and cancel to the user.
QDialogButtonBox::StandardButton createKMessageBox(QDialog *dialog, QDialogButtonBox *buttons, const QIcon &icon, const QString &text, const QStringList &strlist, const QString &ask, bool *checkboxReturn, Options options, const QString &details=QString(), QMessageBox::Icon notifyType=QMessageBox::Information)
void setShortcut(const QKeySequence &key)
QDate date() const const
QTimeZone timeZone() const const
virtual void accept()
virtual void reject()
typedef StandardButtons
QPushButton * button(StandardButton which) const const
QPixmap pixmap(QWindow *window, const QSize &size, Mode mode, State state) const const
Q_OBJECTQ_OBJECT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
T * data() const const
void setDefault(bool)
PM_MessageBoxIconSize
SP_MessageBoxQuestion
void initFrom(const QWidget *widget)
Key_Return
QStyle * style() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:20:39 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.