File indexing completed on 2024-04-28 15:32:01

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