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