File indexing completed on 2024-04-28 15:32:00

0001 /*
0002     SPDX-FileCopyrightText: 2011 John Layt <john@layt.net>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef KDATECOMBOBOX_H
0008 #define KDATECOMBOBOX_H
0009 
0010 #include <kwidgetsaddons_export.h>
0011 
0012 #include <QComboBox>
0013 #include <QLocale>
0014 #include <memory>
0015 
0016 /**
0017  * @class KDateComboBox kdatecombobox.h KDateComboBox
0018  *
0019  * @short A combobox for dates.
0020  */
0021 class KWIDGETSADDONS_EXPORT KDateComboBox : public QComboBox
0022 {
0023     Q_OBJECT
0024 
0025     Q_PROPERTY(QDate date READ date WRITE setDate NOTIFY dateChanged USER true)
0026     Q_PROPERTY(QDate minimumDate READ minimumDate WRITE setMinimumDate RESET resetMinimumDate)
0027     Q_PROPERTY(QDate maximumDate READ maximumDate WRITE setMaximumDate RESET resetMaximumDate)
0028     Q_PROPERTY(Options options READ options WRITE setOptions)
0029 
0030 public:
0031     /**
0032      * Options provided by the widget
0033      * @see options()
0034      * @see setOptions()
0035      * @see Options
0036      */
0037     enum Option {
0038         EditDate = 0x0001, /**< Allow the user to manually edit the date in the combo line edit */
0039         SelectDate = 0x0002, /**< Allow the user to select the date from a drop-down menu */
0040         DatePicker = 0x0004, /**< Show a date picker in the drop-down */
0041         DateKeywords = 0x0008, /**< Show date keywords in the drop-down */
0042         WarnOnInvalid = 0x0010, /**< Show a warning on focus out if the date is invalid */
0043     };
0044     /**
0045      * Stores a combination of #Option values.
0046      */
0047     Q_DECLARE_FLAGS(Options, Option)
0048     Q_FLAG(Options)
0049 
0050     /**
0051      * Create a new KDateComboBox widget
0052      *
0053      * By default the EditDate, SelectDate, DatePicker and DateKeywords options
0054      * are enabled, the ShortDate format is used and the date is set to the
0055      * current date.
0056      */
0057     explicit KDateComboBox(QWidget *parent = nullptr);
0058 
0059     /**
0060      * Destroy the widget
0061      */
0062     ~KDateComboBox() override;
0063 
0064     /**
0065      * Return the currently selected date
0066      *
0067      * @return the currently selected date
0068      */
0069     QDate date() const;
0070 
0071     /**
0072      * Return if the current user input is valid
0073      *
0074      * If the user input is null then it is not valid
0075      *
0076      * @return if the current user input is valid
0077      *
0078      * @see isNull()
0079      */
0080     bool isValid() const;
0081 
0082     /**
0083      * Return if the current user input is null
0084      *
0085      * @return if the current user input is null
0086      *
0087      * @see isValid()
0088      */
0089     bool isNull() const;
0090 
0091     /**
0092      * Return the currently set widget options
0093      *
0094      * @return the currently set widget options
0095      */
0096     Options options() const;
0097 
0098     /**
0099      * Return the currently set date display format
0100      *
0101      * By default this is the Short Format
0102      *
0103      * @return the currently set date format
0104      */
0105     QLocale::FormatType displayFormat() const;
0106 
0107     /**
0108      * Return the current minimum date
0109      *
0110      * @return the current minimum date
0111      */
0112     QDate minimumDate() const;
0113 
0114     /**
0115      * Return the current maximum date
0116      *
0117      * @return the current maximum date
0118      */
0119     QDate maximumDate() const;
0120 
0121     /**
0122      * Return the map of dates listed in the drop-down and their displayed
0123      * string forms.
0124      *
0125      * @return the select date map
0126      *
0127      * @see setDateMap()
0128      */
0129     QMap<QDate, QString> dateMap() const;
0130 
0131 Q_SIGNALS:
0132 
0133     /**
0134      * Signal if the date has been manually entered (by typing a date and losing focus, or pressing Enter)
0135      * or selected by the user (using the popup selector, or up, down, page up, page down keys, or the mouse wheel).
0136      *
0137      * The emitted date may be invalid.
0138      *
0139      * @param date the new date
0140      */
0141     void dateEntered(const QDate &date);
0142 
0143     /**
0144      * Signal if the date has been changed either manually by the user
0145      * or programmatically.
0146      *
0147      * The emitted date may be invalid.
0148      *
0149      * @param date the new date
0150      */
0151     void dateChanged(const QDate &date);
0152 
0153     /**
0154      * Signal if the date is being manually edited by the user.
0155      *
0156      * The emitted date may be invalid, or may not yet be what the user intends as the final date.
0157      *
0158      * @param date the new date
0159      */
0160     void dateEdited(const QDate &date);
0161 
0162 public Q_SLOTS:
0163 
0164     /**
0165      * Set the currently selected date
0166      *
0167      * You can set an invalid date or a date outside the valid range, validity
0168      * checking is only done via isValid().
0169      *
0170      * @param date the new date
0171      */
0172     void setDate(const QDate &date);
0173 
0174     /**
0175      * Set the new widget options
0176      *
0177      * @param options the new widget options
0178      */
0179     void setOptions(Options options);
0180 
0181     /**
0182      * Sets the date format to display.
0183      *
0184      * By default is the Short Format.
0185      *
0186      * @param format the date format to use
0187      */
0188     void setDisplayFormat(QLocale::FormatType format);
0189 
0190     /**
0191      * Set the valid date range to be applied by isValid().
0192      *
0193      * Both dates must be valid and the minimum date must be less than or equal
0194      * to the maximum date, otherwise the date range will not be set.
0195      *
0196      * @param minDate the minimum date
0197      * @param maxDate the maximum date
0198      * @param minWarnMsg the minimum warning message
0199      * @param maxWarnMsg the maximum warning message
0200      */
0201     void setDateRange(const QDate &minDate, const QDate &maxDate, const QString &minWarnMsg = QString(), const QString &maxWarnMsg = QString());
0202 
0203     /**
0204      * Reset the minimum and maximum date to the default values.
0205      * @see setDateRange()
0206      */
0207     void resetDateRange();
0208 
0209     /**
0210      * Set the minimum allowed date.
0211      *
0212      * If the date is invalid, or greater than current maximum,
0213      * then the minimum will not be set.
0214      *
0215      * @param minDate the minimum date
0216      * @param minWarnMsg the minimum warning message
0217      *
0218      * @see minimumDate()
0219      * @see maximumDate()
0220      * @see setMaximumDate()
0221      * @see setDateRange()
0222      */
0223     void setMinimumDate(const QDate &minDate, const QString &minWarnMsg = QString());
0224 
0225     /**
0226      * Reset the minimum date to the default.
0227      *
0228      * The default is to have no minimum date.
0229      */
0230     void resetMinimumDate();
0231 
0232     /**
0233      * Set the maximum allowed date.
0234      *
0235      * If the date is invalid, or less than current minimum,
0236      * then the maximum will not be set.
0237      *
0238      * @param maxDate the maximum date
0239      * @param maxWarnMsg the maximum warning message
0240      *
0241      * @see minimumDate()
0242      * @see maximumDate()
0243      * @see setMaximumDate()
0244      * @see setDateRange()
0245      */
0246     void setMaximumDate(const QDate &maxDate, const QString &maxWarnMsg = QString());
0247 
0248     /**
0249      * Reset the maximum date to the default
0250      *
0251      * The default is to have no maximum date.
0252      */
0253     void resetMaximumDate();
0254 
0255     /**
0256      * Set the list of dates able to be selected from the drop-down and the
0257      * string form to display for those dates, e.g. "2010-01-01" and "Yesterday".
0258      *
0259      * Any invalid or duplicate dates will be used, the list will NOT be
0260      * sorted, and the minimum and maximum date will not be affected.
0261      *
0262      * The @p dateMap is keyed by the date to be listed and the value is the
0263      * string to be displayed.  If you want the date to be displayed in the
0264      * default date format then the string should be null.  If you want a
0265      * separator to be displayed then set the string to "separator".
0266      *
0267      * @param dateMap the map of dates able to be selected
0268      *
0269      * @see dateMap()
0270      */
0271     void setDateMap(QMap<QDate, QString> dateMap);
0272 
0273 protected:
0274     bool eventFilter(QObject *object, QEvent *event) override;
0275     void showPopup() override;
0276     void hidePopup() override;
0277     void mousePressEvent(QMouseEvent *event) override;
0278     void wheelEvent(QWheelEvent *event) override;
0279     void keyPressEvent(QKeyEvent *event) override;
0280     void focusInEvent(QFocusEvent *event) override;
0281     void focusOutEvent(QFocusEvent *event) override;
0282     void resizeEvent(QResizeEvent *event) override;
0283 
0284     /**
0285      * Assign the date for the widget.
0286      *
0287      * Virtual to allow sub-classes to apply extra validation rules.
0288      *
0289      * @param date the new date
0290      */
0291     virtual void assignDate(const QDate &date);
0292 
0293 private:
0294     friend class KDateComboBoxPrivate;
0295     std::unique_ptr<class KDateComboBoxPrivate> const d;
0296 };
0297 
0298 Q_DECLARE_OPERATORS_FOR_FLAGS(KDateComboBox::Options)
0299 
0300 #endif // KDATECOMBOBOX_H