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 <[email protected]>
5  SPDX-FileContributor: Kevin Krammer <[email protected]>
6 
7  SPDX-FileCopyrightText: 2015 SĂ©rgio Martins <[email protected]>
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 
28 using namespace KCalUtils;
29 using namespace KCalUtils::RecurrenceActions;
30 using namespace KCalendarCore;
31 
32 class ScopeWidget : public QWidget
33 {
34  Q_OBJECT
35 public:
36  ScopeWidget(int availableChoices, const QDateTime &dateTime, QWidget *parent)
37  : 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  Q_REQUIRED_RESULT int checkedChoices() const;
68 
69 private:
70  const int mAvailableChoices;
71  Ui_RecurrenceActionsScopeWidget mUi;
72 };
73 
74 ScopeWidget::~ScopeWidget()
75 {
76 }
77 
78 void ScopeWidget::setMessage(const QString &message)
79 {
80  mUi.messageLabel->setText(message);
81 }
82 
83 void 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 
90 void 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 
100 int 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 
117 int 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 
136 static QDialog *
137 createDialog(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);
147  okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
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(QStringLiteral("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);
208  okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
209  buttonBox->button(QDialogButtonBox::Ok)->setDefault(true);
210 
211  bool checkboxResult = false;
212  int result =
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(QStringLiteral("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);
242  okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
243  buttonBox->button(QDialogButtonBox::Ok)->setDefault(true);
244 
245  bool checkboxResult = false;
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"
@ AllOccurrences
Scope does include all occurrences (past, present and future)
QTimeZone timeZone() const const
virtual void reject()
void setShortcut(const QKeySequence &key)
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.
void initFrom(const QWidget *widget)
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.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
static void assign(QPushButton *button, const KGuiItem &item)
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.
char * toString(const T &value)
@ NoOccurrence
Scope does not apply to any occurrence.
virtual void accept()
Key_Return
void setupUi(QWidget *widget)
PM_MessageBoxIconSize
typedef StandardButtons
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.
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)
@ FutureOccurrences
Scope does include occurrences after the given/selected occurrence.
Utility functions for dealing with recurrences.
QPixmap pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state) const const
QPushButton * button(QDialogButtonBox::StandardButton which) const const
@ PastOccurrences
Scope does include occurrences before the given/selected occurrence.
QDate date() const const
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void setDefault(bool)
T * data() const const
SP_MessageBoxQuestion
QString message
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Mon Oct 3 2022 04:06:22 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.