File indexing completed on 2024-05-12 05:07:58

0001 /*
0002     SPDX-FileCopyrightText: 2016-2022 Thomas Baumgart <tbaumgart@kde.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #ifndef KMYMONEYDATEEDIT_H
0007 #define KMYMONEYDATEEDIT_H
0008 
0009 #include "kmm_base_widgets_export.h"
0010 
0011 // ----------------------------------------------------------------------------
0012 // Qt Includes
0013 
0014 #include <QDateEdit>
0015 #include <QMetaMethod>
0016 
0017 // ----------------------------------------------------------------------------
0018 // KDE Includes
0019 
0020 #include <KDateComboBox>
0021 
0022 class QKeyEvent;
0023 class KMyMoneyDateEditPrivate;
0024 /**
0025  * This class provides a date entry widget with a lot more
0026  * functionality than found in QDateEdit or KDateComboBox.
0027  *
0028  * - Use Cursor up/down keys to advance day, month or year
0029  * - Use +/- keys to advance date across months/years
0030  * - Pressing T sets today's date
0031  * - Moving cursor selects sections
0032  * - Enter date from keyboard (only day and month are
0033  *   needed, the year will be amended by the current year
0034  * - works together with WidgetHintFrame to identify
0035  *   invalid dates during input by emitting dateValidityChanged()
0036  *   signal
0037  * - Allows entering a date via keyboard
0038  * - If only day and month are entered, the current year
0039  *   will be appended when focus is left
0040  * - Two digit year entry will be enhanced with current century
0041  * - provides global option to select initial edit section
0042  *   (day, month or year) depending on application settings
0043  *   see setInitialSection()
0044  * - supports completely empty date to identify no-change
0045  *   (needs to be enabled, default is off). See setAllowEmptyDate()
0046  */
0047 class KMM_BASE_WIDGETS_EXPORT KMyMoneyDateEdit : public KDateComboBox
0048 {
0049     Q_OBJECT
0050     Q_PROPERTY(QDate date READ date WRITE setDate)
0051 
0052 public:
0053     explicit KMyMoneyDateEdit(QWidget* parent = nullptr);
0054     virtual ~KMyMoneyDateEdit();
0055 
0056     void keyPressEvent(QKeyEvent* keyEvent) override;
0057 
0058     /**
0059      * Switches the mode to allow complete empty date (@a allowEmptyDate
0060      * equals @c true) or not. In case it is not allowed, an invalid date
0061      * will be replaced with the current date.
0062      */
0063     void setAllowEmptyDate(bool allowEmptyDate);
0064 
0065     /**
0066      * Return the currently entered date. If isNull() returns
0067      * @c true, this will always return an invalid date even
0068      * though isValid() returns @c true. This is the case when
0069      * allowEmptyDate is @c true and the edit area is empty.
0070      *
0071      * @return the currently entered date
0072      */
0073     QDate date() const;
0074 
0075     /**
0076      * Return if the current user input is valid. Depending
0077      * on the setting of allowEmptyDate, the behavior varies.
0078      * If allowEmptyDate is @c false (the default) the return
0079      * value is @c false if isNull() returns @c true. If
0080      * allowEmptyDate is @c true the return value is @c true
0081      * in this case.
0082      *
0083      * @returns if the current user input is valid or not
0084      *
0085      * @see isNull(), setAllowEmptyDate()
0086      */
0087     bool isValid() const;
0088 
0089     /**
0090      * Globally set the @a section which is selected when a date
0091      * is passed using setDate(). If an invalid section is passed
0092      * the DaySection is used which is also the default.
0093      *
0094      * @param section One of QDateEdit::DaySection, QDateEdit::MonthSection or QDateEdit::YearSection.
0095      *
0096      * @sa QDateEdit::Section, setDate()
0097      */
0098     void setInitialSection(QDateEdit::Section section);
0099 
0100     /**
0101      * Returns the global setting of the initial section
0102      *
0103      * @returns One of DaySection, MonthSection or YearSection.
0104      */
0105     QDateEdit::Section initialSection() const;
0106 
0107 public Q_SLOTS:
0108     /**
0109      * Sets the date shown to @a date. The behavior when passing
0110      * an invalid date depends on a prior call to setAllowEmptyDate().
0111      * If called with @c false (the default) this method will set
0112      * the date to QDate::currentDate(). If ccalled with @c true
0113      * it clears the edit area to blank.
0114      *
0115      * @sa setAllowEmptyDate(), date()
0116      */
0117     void setDate(const QDate& date);
0118 
0119     /**
0120      * Overridden to force the display format to always be
0121      * short format.
0122      */
0123     void setDisplayFormat(QLocale::FormatType format);
0124 
0125 Q_SIGNALS:
0126     /**
0127      * This signal is send out if the validity of the date
0128      * changes. It is also send out when a new object is
0129      * connected to this signal.
0130      */
0131     void dateValidityChanged(const QDate& date);
0132 
0133 protected:
0134     void focusOutEvent(QFocusEvent* event) override;
0135     void focusInEvent(QFocusEvent* event) override;
0136     void connectNotify(const QMetaMethod& signal) override;
0137 
0138 private:
0139     KMyMoneyDateEditPrivate* d;
0140 };
0141 #endif // KMYMONEYDATEEDIT_H