File indexing completed on 2025-01-05 03:51:06
0001 /* ============================================================ 0002 * 0003 * This file is a part of digiKam project 0004 * https://www.digikam.org 0005 * 0006 * Date : 2004-04-21 0007 * Description : a menu widget to pick a date. 0008 * 0009 * SPDX-FileCopyrightText: 2004 by Bram Schoenmakers <bramschoenmakers at kde dot nl> 0010 * SPDX-FileCopyrightText: 2006 by Mikolaj Machowski <mikmach at wp dot pl> 0011 * SPDX-FileCopyrightText: 2011-2024 by Gilles Caulier <caulier dot gilles at gmail dot com> 0012 * 0013 * SPDX-License-Identifier: GPL-2.0-or-later 0014 * 0015 * ============================================================ */ 0016 0017 #include "ddatepickerpopup.h" 0018 0019 // Qt includes 0020 0021 #include <QWidgetAction> 0022 0023 // KDE includes 0024 0025 #include <klocalizedstring.h> 0026 0027 namespace Digikam 0028 { 0029 0030 class Q_DECL_HIDDEN DDatePickerAction : public QWidgetAction 0031 { 0032 Q_OBJECT 0033 0034 public: 0035 0036 DDatePickerAction(DDatePicker* const widget, QObject* const parent) 0037 : QWidgetAction (parent), 0038 m_datePicker (widget), 0039 m_originalParent(widget->parentWidget()) 0040 { 0041 } 0042 0043 protected: 0044 0045 QWidget* createWidget(QWidget* const parent) override 0046 { 0047 m_datePicker->setParent(parent); 0048 0049 return m_datePicker; 0050 } 0051 0052 void deleteWidget(QWidget* const widget) override 0053 { 0054 if (widget != m_datePicker) 0055 { 0056 return; 0057 } 0058 0059 m_datePicker->setParent(m_originalParent); 0060 } 0061 0062 private: 0063 0064 DDatePicker* m_datePicker; 0065 QWidget* m_originalParent; 0066 }; 0067 0068 // --------------------------------------------------------------------------------------- 0069 0070 class Q_DECL_HIDDEN DDatePickerPopup::Private 0071 { 0072 public: 0073 0074 explicit Private() 0075 : datePicker(nullptr) 0076 { 0077 } 0078 0079 DDatePicker* datePicker; 0080 Items items; 0081 }; 0082 0083 DDatePickerPopup::DDatePickerPopup(Items items, const QDate& date, QWidget* const parent) 0084 : QMenu(parent), 0085 d (new Private) 0086 { 0087 d->items = items; 0088 d->datePicker = new DDatePicker(this); 0089 d->datePicker->setCloseButton(false); 0090 0091 connect(d->datePicker, &DDatePicker::dateEntered, 0092 this, &DDatePickerPopup::slotDateChanged); 0093 0094 connect(d->datePicker, &DDatePicker::dateSelected, 0095 this, &DDatePickerPopup::slotDateChanged); 0096 0097 d->datePicker->setDate(date); 0098 0099 buildMenu(); 0100 } 0101 0102 DDatePickerPopup::~DDatePickerPopup() 0103 { 0104 delete d; 0105 } 0106 0107 void DDatePickerPopup::buildMenu() 0108 { 0109 if (isVisible()) 0110 { 0111 return; 0112 } 0113 0114 clear(); 0115 0116 if (d->items & DatePicker) 0117 { 0118 addAction(new DDatePickerAction(d->datePicker, this)); 0119 0120 if ((d->items & NoDate ) || (d->items & Words)) 0121 { 0122 addSeparator(); 0123 } 0124 } 0125 0126 if (d->items & Words) 0127 { 0128 addAction(i18nc("@item: date menu", "&Today"), this, SLOT(slotToday())); 0129 addAction(i18nc("@item: date menu", "To&morrow"), this, SLOT(slotTomorrow())); 0130 addAction(i18nc("@item: date menu", "Next &Week"), this, SLOT(slotNextWeek())); 0131 addAction(i18nc("@item: date menu", "Next M&onth"), this, SLOT(slotNextMonth())); 0132 addAction(i18nc("@item: date menu", "Y&esterday"), this, SLOT(slotYesterday())); 0133 addAction(i18nc("@item: date menu", "Last &Monday"), this, SLOT(slotPrevMonday())); 0134 addAction(i18nc("@item: date menu", "Last &Friday"), this, SLOT(slotPrevFriday())); 0135 addAction(i18nc("@item: date menu", "Last &Week"), this, SLOT(slotPrevWeek())); 0136 addAction(i18nc("@item: date menu", "Last M&onth"), this, SLOT(slotPrevMonth())); 0137 0138 if (d->items & NoDate) 0139 { 0140 addSeparator(); 0141 } 0142 } 0143 0144 if (d->items & NoDate) 0145 { 0146 addAction(i18nc("@item: date menu", "No Date"), this, SLOT(slotNoDate())); 0147 } 0148 } 0149 0150 DDatePicker* DDatePickerPopup::datePicker() const 0151 { 0152 return d->datePicker; 0153 } 0154 0155 void DDatePickerPopup::setDate(const QDate& date) 0156 { 0157 d->datePicker->setDate(date); 0158 } 0159 0160 #if 0 0161 void DDatePickerPopup::setItems(int items) 0162 { 0163 d->items = items; 0164 buildMenu(); 0165 } 0166 #endif 0167 0168 int DDatePickerPopup::items() const 0169 { 0170 return d->items; 0171 } 0172 0173 void DDatePickerPopup::slotDateChanged(const QDate& date) 0174 { 0175 Q_EMIT dateChanged(date); 0176 hide(); 0177 } 0178 0179 void DDatePickerPopup::slotToday() 0180 { 0181 Q_EMIT dateChanged(QDate::currentDate()); 0182 } 0183 0184 void DDatePickerPopup::slotTomorrow() 0185 { 0186 Q_EMIT dateChanged(QDate::currentDate().addDays(1)); 0187 } 0188 0189 void DDatePickerPopup::slotNoDate() 0190 { 0191 Q_EMIT dateChanged(QDate()); 0192 } 0193 0194 void DDatePickerPopup::slotNextWeek() 0195 { 0196 Q_EMIT dateChanged(QDate::currentDate().addDays(7)); 0197 } 0198 0199 void DDatePickerPopup::slotNextMonth() 0200 { 0201 Q_EMIT dateChanged(QDate::currentDate().addMonths(1)); 0202 } 0203 0204 void DDatePickerPopup::slotYesterday() 0205 { 0206 Q_EMIT dateChanged(QDate::currentDate().addDays(-1)); 0207 } 0208 0209 void DDatePickerPopup::slotPrevFriday() 0210 { 0211 QDate date = QDate::currentDate(); 0212 int day = date.dayOfWeek(); 0213 0214 if (day < 6) 0215 { 0216 date = date.addDays(5 - 7 - day); 0217 } 0218 else 0219 { 0220 date = date.addDays(5 - day); 0221 } 0222 0223 Q_EMIT dateChanged(date); 0224 } 0225 0226 void DDatePickerPopup::slotPrevMonday() 0227 { 0228 QDate date = QDate::currentDate(); 0229 Q_EMIT dateChanged(date.addDays(1 - date.dayOfWeek())); 0230 } 0231 0232 void DDatePickerPopup::slotPrevWeek() 0233 { 0234 Q_EMIT dateChanged(QDate::currentDate().addDays(-7)); 0235 } 0236 0237 void DDatePickerPopup::slotPrevMonth() 0238 { 0239 Q_EMIT dateChanged(QDate::currentDate().addMonths(-1)); 0240 } 0241 0242 } // namespace Digikam 0243 0244 #include "ddatepickerpopup.moc" 0245 0246 #include "moc_ddatepickerpopup.cpp"