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

source code of kwidgetsaddons/src/kdatetable_p.h