Akonadi Contacts

kdatepickerpopup.cpp
1 /*
2  This file is part of Contact Editor.
3 
4  SPDX-FileCopyrightText: 2004 Bram Schoenmakers <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8 
9 #include "kdatepickerpopup_p.h"
10 
11 #include <KDatePicker>
12 #include <KLocalizedString>
13 
14 #include <QDateTime>
15 #include <QWidgetAction>
16 
17 class KDatePickerAction : public QWidgetAction
18 {
19  Q_OBJECT
20 public:
21  KDatePickerAction(KDatePicker *widget, QObject *parent)
22  : QWidgetAction(parent)
23  , mDatePicker(widget)
24  , mOriginalParent(widget->parentWidget())
25  {
26  }
27 
28 protected:
29  QWidget *createWidget(QWidget *parent) override
30  {
31  mDatePicker->setParent(parent);
32  return mDatePicker;
33  }
34 
35  void deleteWidget(QWidget *widget) override
36  {
37  if (widget != mDatePicker) {
38  return;
39  }
40 
41  mDatePicker->setParent(mOriginalParent);
42  }
43 
44 private:
45  KDatePicker *const mDatePicker;
46  QWidget *const mOriginalParent;
47 };
48 
49 KDatePickerPopup::KDatePickerPopup(Items items, const QDate &date, QWidget *parent)
50  : QMenu(parent)
51 {
52  mItems = items;
53  mDate = date;
54  mDatePicker = new KDatePicker(this);
55  mDatePicker->setCloseButton(false);
56 
57  connect(mDatePicker, &KDatePicker::dateEntered, this, &KDatePickerPopup::slotDateChanged);
58  connect(mDatePicker, &KDatePicker::dateSelected, this, &KDatePickerPopup::slotDateChanged);
59 
60  mDatePicker->setDate(date);
61 
62  buildMenu();
63 }
64 
65 void KDatePickerPopup::buildMenu()
66 {
67  if (isVisible()) {
68  return;
69  }
70  clear();
71 
72  if (mItems & DatePicker) {
73  addAction(new KDatePickerAction(mDatePicker, this));
74 
75  if ((mItems & NoDate) || (mItems & Words)) {
76  addSeparator();
77  }
78  }
79 
80  if (mItems & Words) {
81  addAction(i18nc("@option today", "&Today"), this, [this]() {
82  slotToday();
83  });
84  addAction(i18nc("@option tomorrow", "To&morrow"), this, [this]() {
85  slotTomorrow();
86  });
87  addAction(i18nc("@option next week", "Next &Week"), this, [this]() {
88  slotNextWeek();
89  });
90  addAction(i18nc("@option next month", "Next M&onth"), this, [this]() {
91  slotNextMonth();
92  });
93 
94  if (mItems & NoDate) {
95  addSeparator();
96  }
97  }
98 
99  if (mItems & NoDate) {
100  addAction(i18nc("@option do not specify a date", "No Date"), this, [this]() {
101  slotNoDate();
102  });
103  }
104 }
105 
106 KDatePicker *KDatePickerPopup::datePicker() const
107 {
108  return mDatePicker;
109 }
110 
111 void KDatePickerPopup::setDate(const QDate &date)
112 {
113  mDatePicker->setDate(date);
114 }
115 
116 void KDatePickerPopup::slotDateChanged(const QDate &date)
117 {
118  if (date != mDate) {
119  Q_EMIT dateChanged(date);
120  }
121  hide();
122 }
123 
124 void KDatePickerPopup::slotToday()
125 {
126  Q_EMIT dateChanged(QDate::currentDate());
127 }
128 
129 void KDatePickerPopup::slotTomorrow()
130 {
131  Q_EMIT dateChanged(QDate::currentDate().addDays(1));
132 }
133 
134 void KDatePickerPopup::slotNoDate()
135 {
136  Q_EMIT dateChanged(QDate());
137 }
138 
139 void KDatePickerPopup::slotNextWeek()
140 {
141  Q_EMIT dateChanged(QDate::currentDate().addDays(7));
142 }
143 
144 void KDatePickerPopup::slotNextMonth()
145 {
146  Q_EMIT dateChanged(QDate::currentDate().addMonths(1));
147 }
148 
149 #include "kdatepickerpopup.moc"
150 #include "moc_kdatepickerpopup_p.cpp"
bool setDate(const QDate &date)
void setParent(QWidget *parent)
QWidget * parentWidget() const const
Q_OBJECTQ_OBJECT
void dateSelected(const QDate &date)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void dateEntered(const QDate &date)
virtual QWidget * createWidget(QWidget *parent)
QDate currentDate()
virtual void deleteWidget(QWidget *widget)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Fri Jun 18 2021 23:08:56 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.