| 1 | /* -*- C++ -*- |
| 2 | This file is part of the KDE libraries |
| 3 | SPDX-FileCopyrightText: 1997 Tim D. Gilman <tdgilman@best.org> |
| 4 | SPDX-FileCopyrightText: 1998-2001 Mirko Boehm <mirko@kde.org> |
| 5 | SPDX-FileCopyrightText: 2007 John Layt <john@layt.net> |
| 6 | |
| 7 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 8 | */ |
| 9 | |
| 10 | #ifndef KDATETABLE_H |
| 11 | #define KDATETABLE_H |
| 12 | |
| 13 | #include <QWidget> |
| 14 | #include <memory> |
| 15 | |
| 16 | class ; |
| 17 | |
| 18 | /*! |
| 19 | * \internal |
| 20 | * Date selection table. |
| 21 | * This is a support class for the KDatePicker class. It just |
| 22 | * draws the calendar table without titles, but could theoretically |
| 23 | * be used as a standalone. |
| 24 | * |
| 25 | * When a date is selected by the user, it emits a signal: |
| 26 | * dateSelected(QDate) |
| 27 | * |
| 28 | * \image kdatetable.png "KDE Date Selection Table" |
| 29 | */ |
| 30 | class KDateTable : public QWidget |
| 31 | { |
| 32 | Q_OBJECT |
| 33 | Q_PROPERTY(QDate date READ date WRITE setDate) |
| 34 | Q_PROPERTY(bool popupMenu READ popupMenuEnabled WRITE setPopupMenuEnabled) |
| 35 | |
| 36 | public: |
| 37 | /*! |
| 38 | * The constructor. |
| 39 | */ |
| 40 | explicit KDateTable(QWidget *parent = nullptr); |
| 41 | |
| 42 | /*! |
| 43 | * The constructor. |
| 44 | */ |
| 45 | explicit KDateTable(const QDate &, QWidget *parent = nullptr); |
| 46 | |
| 47 | /*! |
| 48 | * The destructor. |
| 49 | */ |
| 50 | ~KDateTable() override; |
| 51 | |
| 52 | /*! |
| 53 | * Returns a recommended size for the widget. |
| 54 | * To save some time, the size of the largest used cell content is |
| 55 | * calculated in each paintCell() call, since all calculations have |
| 56 | * to be done there anyway. The size is stored in maxCell. The |
| 57 | * sizeHint() simply returns a multiple of maxCell. |
| 58 | */ |
| 59 | QSize sizeHint() const override; |
| 60 | |
| 61 | /*! |
| 62 | * Set the font size of the date table. |
| 63 | */ |
| 64 | void setFontSize(int size); |
| 65 | |
| 66 | /*! |
| 67 | * Select and display this date. |
| 68 | */ |
| 69 | bool setDate(const QDate &date); |
| 70 | |
| 71 | /*! |
| 72 | * Returns the selected date. |
| 73 | */ |
| 74 | const QDate &date() const; |
| 75 | |
| 76 | /*! |
| 77 | * Enables a popup menu when right clicking on a date. |
| 78 | * |
| 79 | * When it's enabled, this object emits a aboutToShowContextMenu signal |
| 80 | * where you can fill in the menu items. |
| 81 | */ |
| 82 | void (bool enable); |
| 83 | |
| 84 | /*! |
| 85 | * Returns if the popup menu is enabled or not |
| 86 | */ |
| 87 | bool () const; |
| 88 | |
| 89 | enum BackgroundMode { |
| 90 | NoBgMode = 0, |
| 91 | RectangleMode, |
| 92 | CircleMode |
| 93 | }; |
| 94 | |
| 95 | /*! |
| 96 | * Makes a given date be painted with a given foregroundColor, and background |
| 97 | * (a rectangle, or a circle/ellipse) in a given color. |
| 98 | */ |
| 99 | void setCustomDatePainting(const QDate &date, const QColor &fgColor, BackgroundMode bgMode = NoBgMode, const QColor &bgColor = QColor()); |
| 100 | |
| 101 | /*! |
| 102 | * Unsets the custom painting of a date so that the date is painted as usual. |
| 103 | */ |
| 104 | void unsetCustomDatePainting(const QDate &date); |
| 105 | |
| 106 | /** |
| 107 | * Sets the valid date range. Dates outside this range will be styled differently and cannot be selected. |
| 108 | */ |
| 109 | void setDateRange(const QDate &minDate, const QDate &maxDate); |
| 110 | |
| 111 | protected: |
| 112 | /*! |
| 113 | * calculate the position of the cell in the matrix for the given date. |
| 114 | * The result is the 0-based index. |
| 115 | */ |
| 116 | virtual int posFromDate(const QDate &date); |
| 117 | |
| 118 | /*! |
| 119 | * calculate the date that is displayed at a given cell in the matrix. pos is the |
| 120 | * 0-based index in the matrix. Inverse function to posForDate(). |
| 121 | */ |
| 122 | virtual QDate dateFromPos(int pos); |
| 123 | |
| 124 | void paintEvent(QPaintEvent *e) override; |
| 125 | |
| 126 | /*! |
| 127 | * React on mouse clicks that select a date. |
| 128 | */ |
| 129 | void mousePressEvent(QMouseEvent *e) override; |
| 130 | void wheelEvent(QWheelEvent *e) override; |
| 131 | void keyPressEvent(QKeyEvent *e) override; |
| 132 | void focusInEvent(QFocusEvent *e) override; |
| 133 | void focusOutEvent(QFocusEvent *e) override; |
| 134 | |
| 135 | /*! |
| 136 | * Cell highlight on mouse hovering |
| 137 | */ |
| 138 | bool event(QEvent *e) override; |
| 139 | |
| 140 | Q_SIGNALS: |
| 141 | /*! |
| 142 | * The selected date changed. |
| 143 | */ |
| 144 | void dateChanged(const QDate &date); |
| 145 | |
| 146 | /*! |
| 147 | * A date has been selected by clicking on the table. |
| 148 | */ |
| 149 | void tableClicked(); |
| 150 | |
| 151 | /*! |
| 152 | * A popup menu for a given date is about to be shown (as when the user |
| 153 | * right clicks on that date and the popup menu is enabled). Connect |
| 154 | * the slot where you fill the menu to this signal. |
| 155 | */ |
| 156 | void (QMenu *, const QDate &date); |
| 157 | |
| 158 | private: |
| 159 | class KDateTablePrivate; |
| 160 | friend class KDateTablePrivate; |
| 161 | std::unique_ptr<KDateTablePrivate> const d; |
| 162 | |
| 163 | void initWidget(const QDate &date); |
| 164 | void initAccels(); |
| 165 | void paintCell(QPainter *painter, int row, int col); |
| 166 | |
| 167 | Q_DISABLE_COPY(KDateTable) |
| 168 | }; |
| 169 | |
| 170 | #endif // KDATETABLE_H |
| 171 | |