File indexing completed on 2024-04-28 15:39:43

0001 // SPDX-FileCopyrightText: 2003-2022 Jesper K. Pedersen <blackie@kde.org>
0002 //
0003 // SPDX-License-Identifier: GPL-2.0-or-later
0004 
0005 /**
0006  * A date editing widget that consists of an editable combo box.
0007  * The combo box contains the date in text form, and clicking the combo
0008  * box arrow will display a 'popup' style date picker.
0009  *
0010  * This widget also supports advanced features like allowing the user
0011  * to type in the day name to get the date. The following keywords
0012  * are supported (in the native language): tomorrow, yesterday, today,
0013  * monday, tuesday, wednesday, thursday, friday, saturday, sunday.
0014  *
0015  * @author Cornelius Schumacher <schumacher@kde.org>
0016  * @author Mike Pilone <mpilone@slac.com>
0017  * @author David Jarvie <software@astrojar.org.uk>
0018  * @author Jesper Pedersen <blackie@kde.org>
0019  */
0020 #ifndef ANNOTATIONDIALOG_DATEEDIT_H
0021 #define ANNOTATIONDIALOG_DATEEDIT_H
0022 
0023 #include <DB/ImageDate.h>
0024 
0025 #include <QComboBox>
0026 #include <QEvent>
0027 #include <QMouseEvent>
0028 #include <qmap.h>
0029 
0030 class QEvent;
0031 class KDatePicker;
0032 
0033 namespace AnnotationDialog
0034 {
0035 
0036 class DateEdit : public QComboBox
0037 {
0038     Q_OBJECT
0039 public:
0040     explicit DateEdit(bool isStartEdit, QWidget *parent = nullptr);
0041     ~DateEdit() override;
0042 
0043     /** @return True if the date in the text edit is valid,
0044      * false otherwise. This will not modify the display of the date,
0045      * but only check for validity.
0046      */
0047     bool inputIsValid() const;
0048 
0049     /** @return The date entered. This will not
0050      * modify the display of the date, but only return it.
0051      */
0052     QDate date() const;
0053 
0054     /** Sets the date.
0055      *
0056      * @param date The new date to display. This date must be valid or
0057      * it will not be displayed.
0058      */
0059     void setDate(const QDate &date);
0060 
0061     /** @return The default date used if no valid date has been set or entered.
0062      */
0063     QDate defaultDate() const;
0064 
0065     /** Sets the default date to use if no valid date has been set or entered.
0066      * If no default date has been set, the current date is used as the default.
0067      * @param date The default date.
0068      */
0069     void setDefaultDate(const QDate &date);
0070 
0071     /** @param handleInvalid If true the date edit accepts invalid dates
0072      * and displays them as the empty ("") string. It also returns an invalid date.
0073      * If false (default) invalid dates are not accepted and instead the date
0074      * of today will be returned.
0075      */
0076     void setHandleInvalid(bool handleInvalid);
0077 
0078     /** @return True if the widget is accepts invalid dates, false otherwise. */
0079     bool handlesInvalid() const;
0080 
0081     /** Sets whether the widget is read-only for the user. If read-only, the date
0082      * picker pop-up is inactive, and the displayed date cannot be edited.
0083      * @param readOnly True to set the widget read-only, false to set it read-write.
0084      */
0085     void setReadOnly(bool readOnly);
0086 
0087     /** @return True if the widget is read-only, false if read-write. */
0088     bool isReadOnly() const;
0089 
0090     /** Called when a new date has been entered, to validate its value.
0091      * @param newDate The new date which has been entered.
0092      * @return True to accept the new date, false to reject the new date.
0093      * If false is returned, the value reverts to what it was before the
0094      * new date was entered.
0095      */
0096     virtual bool validate(const QDate &newDate);
0097 
0098     void showPopup() override;
0099 
0100 Q_SIGNALS:
0101     /** This signal is emitted whenever the user modifies the date. This
0102      * may not get emitted until the user presses enter in the line edit or
0103      * focus leaves the widget (i.e. the user confirms their selection).
0104      */
0105     void dateChanged(QDate);
0106     void dateChanged(const DB::ImageDate &);
0107 
0108     /** This signal is emitted whenever the user enters an invalid date.
0109      */
0110     void invalidDateEntered();
0111 
0112 protected Q_SLOTS:
0113     void dateSelected(QDate);
0114     void dateEntered(QDate);
0115     void lineEnterPressed();
0116     void slotTextChanged(const QString &);
0117     void mousePressEvent(QMouseEvent *) override;
0118 
0119 private:
0120     void keyPressEvent(QKeyEvent *event) override;
0121     bool eventFilter(QObject *o, QEvent *e) override;
0122     bool readDate(QDate &result, QDate *end) const;
0123 
0124     /** Maps the text that the user can enter to the offset in days from
0125      * today. For example, the text 'tomorrow' is mapped to +1.
0126      */
0127     QMap<QString, int> m_KeywordMap;
0128     bool m_TextChanged;
0129     bool m_HandleInvalid;
0130 
0131     KDatePicker *m_DatePicker;
0132     QFrame *m_DateFrame;
0133     QDate m_defaultValue;
0134     QDate m_value;
0135     bool m_ReadOnly;
0136     bool m_DiscardNextMousePress;
0137     bool m_IsStartEdit;
0138 };
0139 
0140 }
0141 
0142 #endif
0143 // vi:expandtab:tabstop=4 shiftwidth=4: