File indexing completed on 2024-05-12 16:44:03

0001 /*
0002     SPDX-FileCopyrightText: 2000-2003 Michael Edwardes <mte@users.sourceforge.net>
0003     SPDX-FileCopyrightText: 2001 Felix Rodriguez <frodriguez@users.sourceforge.net>
0004     SPDX-FileCopyrightText: 2017-2018 Łukasz Wojniłowicz <lukasz.wojnilowicz@gmail.com>
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #ifndef KMYMONEYDATEINPUT_H
0009 #define KMYMONEYDATEINPUT_H
0010 
0011 // ----------------------------------------------------------------------------
0012 // QT Includes
0013 
0014 #include <QWidget>
0015 #include <QDate>
0016 #include <QDateEdit>
0017 
0018 // ----------------------------------------------------------------------------
0019 // KDE Includes
0020 
0021 // ----------------------------------------------------------------------------
0022 // Project Includes
0023 
0024 #include "kmm_widgets_export.h"
0025 
0026 // Ideas neatly taken from korganizer
0027 // Respective authors are credited.
0028 // Some ideas/code have been borrowed from Calendar-0.13 (phoenix.bmedesign.com/~qt)
0029 
0030 namespace KMyMoney {
0031 /**
0032   * Provided to be able to catch the focusOut events before the contents gets changed
0033   */
0034 class OldDateEdit : public QDateEdit
0035 {
0036     Q_OBJECT
0037 public:
0038     explicit OldDateEdit(const QDate& date, QWidget* parent = nullptr);
0039     void setInitialSection(Section section) {
0040         m_initialSection = section;
0041     }
0042 
0043 protected:
0044     /** if the date was cleared (a state which is not supported by QDateEdit)
0045       * make sure that a date can be entered again
0046       */
0047     void keyPressEvent(QKeyEvent* k) final override;
0048 
0049     /** reimplemented for internal reasons */
0050     bool event(QEvent* e) final override;
0051 
0052     /** reimplemented for internal reasons */
0053     bool focusNextPrevChild(bool next) final override;
0054 
0055     /** reimplemented for internal reasons */
0056     void focusInEvent(QFocusEvent *event) final override;
0057 
0058 private:
0059     QDateEdit::Section  m_initialSection;
0060     enum {
0061         Created,
0062         GotFocus,
0063         FirstMousePress
0064     }                   m_initStage;
0065 };
0066 }; // namespace
0067 
0068 /**
0069   * This class provides the general widget used for date selection
0070   * throughout the KMyMoney project. It provides an QDateEdit widget
0071   * which is based on an edit field with spin boxes and adds a QPushButton
0072   * to open a KDatePicker.
0073   */
0074 class KMM_WIDGETS_EXPORT KMyMoneyDateInput : public QWidget
0075 {
0076     Q_OBJECT
0077     Q_PROPERTY(QDate date READ date WRITE setDate STORED false NOTIFY dateChanged)
0078 
0079 public:
0080     explicit KMyMoneyDateInput(QWidget* parent = nullptr, Qt::AlignmentFlag flags = Qt::AlignLeft);
0081     ~KMyMoneyDateInput();
0082 
0083     /**
0084       * Returns the selected date in the widget. If the widget is not
0085       * showing a date, a QDate() object is returned which has an invalid date.
0086       */
0087     QDate date() const;
0088 
0089     /**
0090       * Set the date shown in the widget to @a date. If @a date is invalid,
0091       * no text will be shown. The internal widget will use 1.1.1800 for this
0092       * special case, as the standard QDateEdit widget does not support an
0093       * invalid date as of Qt4 anymore, but we need it anyway for multi transaction
0094       * edit.
0095       */
0096     void setDate(QDate date);
0097 
0098     void setMaximumDate(const QDate& max);
0099 
0100     /**
0101       * Setup the widget with @a date. This date is stored internally and
0102       * can be reloaded using resetDate().
0103       *
0104       * @sa setDate, resetDate
0105       */
0106     void loadDate(const QDate& date);
0107 
0108     /**
0109       * Setup the widget with the date loaded using loadDate().
0110       *
0111       *  @sa loadDate
0112       */
0113     void resetDate();
0114 
0115     QWidget* focusWidget() const;
0116     void setRange(const QDate & min, const QDate & max);
0117     void markAsBadDate(bool bad = false, const QColor& = QColor());
0118 
0119 Q_SIGNALS:
0120     void dateChanged(const QDate& date);
0121 
0122 protected:
0123     /**
0124       * - increments/decrements the date upon +/- key input
0125       * - increments/decrements the date upon Up/Down key input
0126       * - sets the date to current date when the 'T' key is pressed.
0127       *   The actual key for this to happen might be overridden through
0128       *   an i18n package. The 'T'-key is always possible.
0129       */
0130     void keyPressEvent(QKeyEvent* k) override;
0131     void showEvent(QShowEvent* event) override;
0132 
0133     /** To intercept events sent to focusWidget() */
0134     bool eventFilter(QObject *o, QEvent *e) override;
0135 
0136 
0137 protected Q_SLOTS:
0138     void slotDateChosen(QDate date);
0139     void toggleDatePicker();
0140 
0141 private Q_SLOTS:
0142     void slotDateChosenRef(const QDate& date);
0143     void fixSize();
0144 
0145 private:
0146     struct Private;
0147     Private * const d;
0148 };
0149 
0150 #endif
0151