File indexing completed on 2024-04-28 03:59:04

0001 /*  -*- C++ -*-
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 1997 Tim D. Gilman <tdgilman@best.org>
0004     SPDX-FileCopyrightText: 1998-2001 Mirko Boehm <mirko@kde.org>
0005     SPDX-FileCopyrightText: 2007 John Layt <john@layt.net>
0006 
0007     SPDX-License-Identifier: LGPL-2.0-or-later
0008 */
0009 
0010 #ifndef KDATETABLE_H
0011 #define KDATETABLE_H
0012 
0013 #include <QWidget>
0014 #include <memory>
0015 
0016 class QMenu;
0017 
0018 /**
0019  * @internal
0020  * Date selection table.
0021  * This is a support class for the KDatePicker class.  It just
0022  * draws the calendar table without titles, but could theoretically
0023  * be used as a standalone.
0024  *
0025  * When a date is selected by the user, it emits a signal:
0026  * dateSelected(QDate)
0027  *
0028  * \image html kdatetable.png "KDE Date Selection Table"
0029  *
0030  * @author Tim Gilman, Mirko Boehm
0031  */
0032 class KDateTable : public QWidget
0033 {
0034     Q_OBJECT
0035     Q_PROPERTY(QDate date READ date WRITE setDate)
0036     Q_PROPERTY(bool popupMenu READ popupMenuEnabled WRITE setPopupMenuEnabled)
0037 
0038 public:
0039     /**
0040      * The constructor.
0041      */
0042     explicit KDateTable(QWidget *parent = nullptr);
0043 
0044     /**
0045      * The constructor.
0046      */
0047     explicit KDateTable(const QDate &, QWidget *parent = nullptr);
0048 
0049     /**
0050      * The destructor.
0051      */
0052     ~KDateTable() override;
0053 
0054     /**
0055      * Returns a recommended size for the widget.
0056      * To save some time, the size of the largest used cell content is
0057      * calculated in each paintCell() call, since all calculations have
0058      * to be done there anyway. The size is stored in maxCell. The
0059      * sizeHint() simply returns a multiple of maxCell.
0060      */
0061     QSize sizeHint() const override;
0062 
0063     /**
0064      * Set the font size of the date table.
0065      */
0066     void setFontSize(int size);
0067 
0068     /**
0069      * Select and display this date.
0070      */
0071     bool setDate(const QDate &date);
0072 
0073     /**
0074      * @returns the selected date.
0075      */
0076     const QDate &date() const;
0077 
0078     /**
0079      * Enables a popup menu when right clicking on a date.
0080      *
0081      * When it's enabled, this object emits a aboutToShowContextMenu signal
0082      * where you can fill in the menu items.
0083      */
0084     void setPopupMenuEnabled(bool enable);
0085 
0086     /**
0087      * Returns if the popup menu is enabled or not
0088      */
0089     bool popupMenuEnabled() const;
0090 
0091     enum BackgroundMode { NoBgMode = 0, RectangleMode, CircleMode };
0092 
0093     /**
0094      * Makes a given date be painted with a given foregroundColor, and background
0095      * (a rectangle, or a circle/ellipse) in a given color.
0096      */
0097     void setCustomDatePainting(const QDate &date, const QColor &fgColor, BackgroundMode bgMode = NoBgMode, const QColor &bgColor = QColor());
0098 
0099     /**
0100      * Unsets the custom painting of a date so that the date is painted as usual.
0101      */
0102     void unsetCustomDatePainting(const QDate &date);
0103 
0104 protected:
0105     /**
0106      * calculate the position of the cell in the matrix for the given date.
0107      * The result is the 0-based index.
0108      */
0109     virtual int posFromDate(const QDate &date);
0110 
0111     /**
0112      * calculate the date that is displayed at a given cell in the matrix. pos is the
0113      * 0-based index in the matrix. Inverse function to posForDate().
0114      */
0115     virtual QDate dateFromPos(int pos);
0116 
0117     void paintEvent(QPaintEvent *e) override;
0118 
0119     /**
0120      * React on mouse clicks that select a date.
0121      */
0122     void mousePressEvent(QMouseEvent *e) override;
0123     void wheelEvent(QWheelEvent *e) override;
0124     void keyPressEvent(QKeyEvent *e) override;
0125     void focusInEvent(QFocusEvent *e) override;
0126     void focusOutEvent(QFocusEvent *e) override;
0127 
0128     /**
0129      * Cell highlight on mouse hovering
0130      */
0131     bool event(QEvent *e) override;
0132 
0133 Q_SIGNALS:
0134     /**
0135      * The selected date changed.
0136      */
0137     void dateChanged(const QDate &date);
0138 
0139     /**
0140      * A date has been selected by clicking on the table.
0141      */
0142     void tableClicked();
0143 
0144     /**
0145      * A popup menu for a given date is about to be shown (as when the user
0146      * right clicks on that date and the popup menu is enabled). Connect
0147      * the slot where you fill the menu to this signal.
0148      */
0149     void aboutToShowContextMenu(QMenu *menu, const QDate &date);
0150 
0151 private:
0152     class KDateTablePrivate;
0153     friend class KDateTablePrivate;
0154     std::unique_ptr<KDateTablePrivate> const d;
0155 
0156     void initWidget(const QDate &date);
0157     void initAccels();
0158     void paintCell(QPainter *painter, int row, int col);
0159 
0160     Q_DISABLE_COPY(KDateTable)
0161 };
0162 
0163 #endif // KDATETABLE_H