1/* -*- C++ -*-
2 This file is part of the KDE libraries
3 Copyright (C) 1997 Tim D. Gilman (tdgilman@best.org)
4 (C) 1998-2001 Mirko Boehm (mirko@kde.org)
5 (C) 2007 John Layt <john@layt.net>
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20*/
21
22#ifndef KDATETABLE_H
23#define KDATETABLE_H
24
25#include <kdeui_export.h>
26
27#include <QtGui/QValidator>
28#include <QtGui/QLineEdit>
29#include <QtCore/QDateTime>
30
31#include <klocale.h>
32
33class KMenu;
34class KCalendarSystem;
35class KColorScheme;
36
37/**
38 * Frame with popup menu behavior.
39 * @author Tim Gilman, Mirko Boehm
40 */
41class KDEUI_EXPORT KPopupFrame : public QFrame
42{
43 Q_OBJECT
44protected:
45 /**
46 * Catch key press events.
47 */
48 virtual void keyPressEvent( QKeyEvent *e );
49
50public Q_SLOTS:
51 /**
52 * Close the popup window. This is called from the main widget, usually.
53 * @p r is the result returned from exec().
54 */
55 void close( int r );
56
57public:
58 /**
59 * The contructor. Creates a dialog without buttons.
60 */
61 KPopupFrame( QWidget *parent = 0 );
62
63 /**
64 * The destructor
65 */
66 ~KPopupFrame();
67
68 /**
69 * Set the main widget. You cannot set the main widget from the constructor,
70 * since it must be a child of the frame itselfes.
71 * Be careful: the size is set to the main widgets size. It is up to you to
72 * set the main widgets correct size before setting it as the main
73 * widget.
74 */
75 void setMainWidget( QWidget* m );
76
77 /**
78 * The resize event. Simply resizes the main widget to the whole
79 * widgets client size.
80 */
81 virtual void resizeEvent( QResizeEvent *resize );
82
83 /**
84 * Open the popup window at position pos.
85 */
86 void popup( const QPoint &pos );
87
88 /**
89 * Execute the popup window.
90 */
91 int exec( const QPoint &p );
92
93 /**
94 * Execute the popup window.
95 */
96 int exec( int x, int y );
97
98Q_SIGNALS:
99 void leaveModality();
100
101private:
102 class KPopupFramePrivate;
103 friend class KPopupFramePrivate;
104 KPopupFramePrivate * const d;
105
106 Q_DISABLE_COPY( KPopupFrame )
107};
108
109/**
110* Validates user-entered dates.
111*/
112class KDEUI_EXPORT KDateValidator : public QValidator
113{
114public:
115 KDateValidator( QWidget *parent = 0 );
116 virtual State validate( QString &text, int &e ) const;
117 virtual void fixup ( QString &input ) const;
118 State date( const QString &text, QDate &date ) const;
119private:
120 class KDateValidatorPrivate;
121 friend class KDateValidatorPrivate;
122 KDateValidatorPrivate * const d;
123};
124
125/**
126 * Date selection table.
127 * This is a support class for the KDatePicker class. It just
128 * draws the calendar table without titles, but could theoretically
129 * be used as a standalone.
130 *
131 * When a date is selected by the user, it emits a signal:
132 * dateSelected(QDate)
133 *
134 * \image html kdatetable.png "KDE Date Selection Table"
135 *
136 * @internal
137 * @author Tim Gilman, Mirko Boehm
138 */
139class KDEUI_EXPORT KDateTable : public QWidget
140{
141 Q_OBJECT
142 Q_PROPERTY( QDate date READ date WRITE setDate )
143//FIXME Q_PROPERTY( KCalendarSystem calendar READ calendar WRITE setCalendar USER true )
144 Q_PROPERTY( bool popupMenu READ popupMenuEnabled WRITE setPopupMenuEnabled )
145
146public:
147 /**
148 * The constructor.
149 */
150 explicit KDateTable( QWidget* parent = 0 );
151
152 /**
153 * The constructor.
154 */
155 explicit KDateTable( const QDate&, QWidget *parent = 0 );
156
157 /**
158 * The destructor.
159 */
160 ~KDateTable();
161
162 /**
163 * Returns a recommended size for the widget.
164 * To save some time, the size of the largest used cell content is
165 * calculated in each paintCell() call, since all calculations have
166 * to be done there anyway. The size is stored in maxCell. The
167 * sizeHint() simply returns a multiple of maxCell.
168 */
169 virtual QSize sizeHint() const;
170
171 /**
172 * Set the font size of the date table.
173 */
174 void setFontSize( int size );
175
176 /**
177 * Select and display this date.
178 */
179 bool setDate( const QDate &date );
180
181 // KDE5 remove the const & from the returned QDate
182 /**
183 * @returns the selected date.
184 */
185 const QDate &date() const;
186
187 /**
188 * Returns the currently selected calendar system.
189 *
190 * @return a KCalendarSystem object
191 */
192 const KCalendarSystem *calendar() const;
193
194 /**
195 * Changes the calendar system to use. Can use its own local locale if set.
196 *
197 * @param calendar the calendar system object to use, defaults to global
198 *
199 * @return @c true if the calendar system was successfully set, @c false otherwise
200 */
201 bool setCalendar( KCalendarSystem *calendar = 0 );
202
203 /**
204 * Changes the calendar system to use. Will always use global locale.
205 *
206 * @param calendarType the calendar system type to use
207 *
208 * @return @c true if the calendar system was successfully set, @c false otherwise
209 */
210 bool setCalendar( const QString &calendarType );
211
212 /**
213 * @since 4.6
214 *
215 * Changes the calendar system to use. Will always use global locale.
216 *
217 * @param calendarSystem the calendar system to use
218 * @return @c true if the calendar system was successfully set, @c false otherwise
219 */
220 bool setCalendarSystem( KLocale::CalendarSystem calendarSystem );
221
222 /**
223 * Enables a popup menu when right clicking on a date.
224 *
225 * When it's enabled, this object emits a aboutToShowContextMenu signal
226 * where you can fill in the menu items.
227 */
228 void setPopupMenuEnabled( bool enable );
229
230 /**
231 * Returns if the popup menu is enabled or not
232 */
233 bool popupMenuEnabled() const;
234
235 enum BackgroundMode { NoBgMode = 0, RectangleMode, CircleMode };
236
237 /**
238 * Makes a given date be painted with a given foregroundColor, and background
239 * (a rectangle, or a circle/ellipse) in a given color.
240 */
241 void setCustomDatePainting( const QDate &date, const QColor &fgColor,
242 BackgroundMode bgMode = NoBgMode, const QColor &bgColor = QColor() );
243
244 /**
245 * Unsets the custom painting of a date so that the date is painted as usual.
246 */
247 void unsetCustomDatePainting( const QDate &date );
248
249protected:
250 /**
251 * calculate the position of the cell in the matrix for the given date.
252 * The result is the 0-based index.
253 */
254 virtual int posFromDate( const QDate &date );
255
256 /**
257 * calculate the date that is displayed at a given cell in the matrix. pos is the
258 * 0-based index in the matrix. Inverse function to posForDate().
259 */
260 virtual QDate dateFromPos( int pos );
261
262 virtual void paintEvent( QPaintEvent *e );
263
264 /**
265 * React on mouse clicks that select a date.
266 */
267 virtual void mousePressEvent( QMouseEvent *e );
268 virtual void wheelEvent( QWheelEvent *e );
269 virtual void keyPressEvent( QKeyEvent *e );
270 virtual void focusInEvent( QFocusEvent *e );
271 virtual void focusOutEvent( QFocusEvent *e );
272
273 /**
274 * Cell highlight on mouse hovering
275 */
276 virtual bool event(QEvent *e);
277
278Q_SIGNALS:
279 /**
280 * The selected date changed.
281 */
282 void dateChanged( const QDate &date );
283
284 /**
285 * This function behaves essentially like the one above.
286 * The selected date changed.
287 * @param cur The current date
288 * @param old The date before the date was changed
289 */
290 void dateChanged( const QDate &cur, const QDate &old );
291
292 /**
293 * A date has been selected by clicking on the table.
294 */
295 void tableClicked();
296
297 /**
298 * A popup menu for a given date is about to be shown (as when the user
299 * right clicks on that date and the popup menu is enabled). Connect
300 * the slot where you fill the menu to this signal.
301 */
302 void aboutToShowContextMenu( KMenu *menu, const QDate &date );
303
304private:
305 Q_PRIVATE_SLOT( d, void nextMonth() )
306 Q_PRIVATE_SLOT( d, void previousMonth() )
307 Q_PRIVATE_SLOT( d, void beginningOfMonth() )
308 Q_PRIVATE_SLOT( d, void endOfMonth() )
309 Q_PRIVATE_SLOT( d, void beginningOfWeek() )
310 Q_PRIVATE_SLOT( d, void endOfWeek() )
311
312private:
313 class KDateTablePrivate;
314 friend class KDateTablePrivate;
315 KDateTablePrivate * const d;
316
317 void init( const QDate &date );
318 void initAccels();
319 void paintCell( QPainter *painter, int row, int col, const KColorScheme &colorScheme );
320
321 Q_DISABLE_COPY( KDateTable )
322};
323
324#endif // KDATETABLE_H
325