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