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 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20#ifndef KPTDATETBL_H
21#define KPTDATETBL_H
22
23#include "../kplatomodels_export.h"
24
25#include <QValidator>
26#include <QLineEdit>
27#include <QDateTime>
28#include <QHash>
29#include <QList>
30#include <QStyleOptionViewItem>
31#include <QStyleOptionHeader>
32#include <QFrame>
33
34class KMenu;
35
36namespace KPlato
37{
38
39class KDateTableDataModel;
40class KDateTableDateDelegate;
41class KDateTableWeekDayDelegate;
42class KDateTableWeekNumberDelegate;
43
44class StyleOptionHeader;
45class StyleOptionViewItem;
46
47class KPLATOMODELS_EXPORT Frame: public QFrame
48{
49 Q_OBJECT
50public:
51 Frame( QWidget *parent = 0 );
52
53public slots:
54 void updateFocus( QFocusEvent *e );
55
56protected:
57 virtual void paintEvent(QPaintEvent *e);
58 void drawFrame(QPainter *p);
59};
60
61
62/**
63 * Frame with popup menu behavior.
64 * @author Tim Gilman, Mirko Boehm
65 */
66class KPopupFrame : public QFrame
67{
68 Q_OBJECT
69protected:
70 /**
71 * Catch key press events.
72 */
73 virtual void keyPressEvent(QKeyEvent* e);
74
75public Q_SLOTS:
76 /**
77 * Close the popup window. This is called from the main widget, usually.
78 * @p r is the result returned from exec().
79 */
80 void close(int r);
81public:
82 /**
83 * The contructor. Creates a dialog without buttons.
84 */
85 KPopupFrame(QWidget* parent=0);
86 /**
87 * The destructor
88 */
89 ~KPopupFrame();
90 /**
91 * Set the main widget. You cannot set the main widget from the constructor,
92 * since it must be a child of the frame itselfes.
93 * Be careful: the size is set to the main widgets size. It is up to you to
94 * set the main widgets correct size before setting it as the main
95 * widget.
96 */
97 void setMainWidget(QWidget* m);
98 /**
99 * The resize event. Simply resizes the main widget to the whole
100 * widgets client size.
101 */
102 virtual void resizeEvent(QResizeEvent*);
103 /**
104 * Open the popup window at position pos.
105 */
106 void popup(const QPoint &pos);
107 /**
108 * Execute the popup window.
109 */
110 int exec(const QPoint &p);
111 /**
112 * Execute the popup window.
113 */
114 int exec(int x, int y);
115
116Q_SIGNALS:
117 void leaveModality();
118
119private:
120 class KPopupFramePrivate;
121 friend class KPopupFramePrivate;
122 KPopupFramePrivate * const d;
123
124 Q_DISABLE_COPY(KPopupFrame)
125};
126
127/**
128* Validates user-entered dates.
129*/
130class KDateValidator : public QValidator
131{
132public:
133 KDateValidator(QWidget* parent=0);
134 virtual State validate(QString&, int&) const;
135 virtual void fixup ( QString & input ) const;
136 State date(const QString&, QDate&) const;
137};
138
139/**
140 * Date selection table.
141 * This is a support class for the KDatePicker class. It just
142 * draws the calendar table without titles, but could theoretically
143 * be used as a standalone.
144 *
145 * When a date is selected by the user, it emits a signal:
146 * dateSelected(QDate)
147 *
148 * @internal
149 * @author Tim Gilman, Mirko Boehm
150 */
151class KPLATOMODELS_EXPORT KDateTable : public QWidget
152{
153 Q_OBJECT
154 Q_PROPERTY( QDate date READ date WRITE setDate )
155 Q_PROPERTY( bool popupMenu READ popupMenuEnabled WRITE setPopupMenuEnabled )
156
157public:
158 /**
159 * The constructor.
160 */
161 explicit KDateTable(QWidget* parent = 0);
162
163 /**
164 * The constructor.
165 */
166 explicit KDateTable(const QDate&, QWidget* parent = 0);
167
168 /**
169 * The destructor.
170 */
171 ~KDateTable();
172
173 /**
174 * Returns a recommended size for the widget.
175 * To save some time, the size of the largest used cell content is
176 * calculated in each paintCell() call, since all calculations have
177 * to be done there anyway. The size is stored in maxCell. The
178 * sizeHint() simply returns a multiple of maxCell.
179 */
180 virtual QSize sizeHint() const;
181 /**
182 * Set the font size of the date table.
183 */
184 void setFontSize(int size);
185 /**
186 * Select and display this date.
187 */
188 bool setDate(const QDate&);
189
190 /**
191 * @returns the selected date.
192 */
193 const QDate& date() const;
194
195 /**
196 * Enables a popup menu when right clicking on a date.
197 *
198 * When it's enabled, this object emits a aboutToShowContextMenu signal
199 * where you can fill in the menu items.
200 */
201 void setPopupMenuEnabled( bool enable );
202
203 /**
204 * Returns if the popup menu is enabled or not
205 */
206 bool popupMenuEnabled() const;
207
208 enum BackgroundMode { NoBgMode=0, RectangleMode, CircleMode };
209
210 /**
211 * Makes a given date be painted with a given foregroundColor, and background
212 * (a rectangle, or a circle/ellipse) in a given color.
213 */
214 void setCustomDatePainting( const QDate &date, const QColor &fgColor, BackgroundMode bgMode=NoBgMode, const QColor &bgColor=QColor());
215
216 /**
217 * Unsets the custom painting of a date so that the date is painted as usual.
218 */
219 void unsetCustomDatePainting( const QDate &date );
220
221 //----->
222 enum ItemDataRole { DisplayRole_1 = Qt::UserRole + 1 };
223
224 enum SelectionMode { SingleSelection, ExtendedSelection };
225 void setSelectionMode( SelectionMode mode );
226
227 void setModel( KDateTableDataModel *model );
228 KDateTableDataModel *model() const;
229
230 void setDateDelegate( KDateTableDateDelegate *delegate );
231 void setDateDelegate( const QDate &date, KDateTableDateDelegate *delegate );
232 void setWeekDayDelegate( KDateTableWeekDayDelegate *delegate );
233 void setWeekNumberDelegate( KDateTableWeekNumberDelegate *delegate );
234 void setWeekNumbersEnabled( bool enable );
235
236 void setStyleOptionDate( const StyleOptionViewItem &so );
237 void setStyleOptionWeekDay( const StyleOptionHeader &so );
238 void setStyleOptionWeekNumber( const StyleOptionHeader &so );
239
240 void setGridEnabled( bool enable );
241 //<-----
242
243protected:
244 /**
245 * calculate the position of the cell in the matrix for the given date. The result is the 0-based index.
246 */
247 virtual int posFromDate( const QDate &date );
248 /**
249 * calculate the date that is displayed at a given cell in the matrix. pos is the
250 * 0-based index in the matrix. Inverse function to posForDate().
251 */
252 virtual QDate dateFromPos( int pos );
253
254 virtual void paintEvent(QPaintEvent *e);
255 /**
256 * React on mouse clicks that select a date.
257 */
258 virtual void mousePressEvent(QMouseEvent *);
259 virtual void wheelEvent( QWheelEvent * e );
260 virtual void keyPressEvent( QKeyEvent *e );
261 virtual void focusInEvent( QFocusEvent *e );
262 virtual void focusOutEvent( QFocusEvent *e );
263
264 virtual bool event( QEvent *e );
265
266Q_SIGNALS:
267 /**
268 * The selected date changed.
269 */
270 void dateChanged(const QDate&);
271 /**
272 * This function behaves essentially like the one above.
273 * The selected date changed.
274 * @param cur The current date
275 * @param old The date before the date was changed
276 */
277 void dateChanged(const QDate& cur, const QDate& old);
278 /**
279 * A date has been selected by clicking on the table.
280 */
281 void tableClicked();
282
283 /**
284 * A popup menu for a given date is about to be shown (as when the user
285 * right clicks on that date and the popup menu is enabled). Connect
286 * the slot where you fill the menu to this signal.
287 */
288 void aboutToShowContextMenu( KMenu * menu, const QDate &date );
289
290 /**
291 * A popup menu for selected dates is about to be shown.
292 * Connect the slot where you fill the menu to this signal.
293 */
294 void aboutToShowContextMenu( KMenu * menu, const QList<QDate>& );
295
296 //----->
297 void selectionChanged( const QList<QDate>& );
298
299 void focusChanged( QFocusEvent *e );
300
301protected slots:
302 void slotReset();
303 void slotDataChanged( const QDate &start, const QDate &end );
304 //<------
305
306private:
307 Q_PRIVATE_SLOT(d, void nextMonth())
308 Q_PRIVATE_SLOT(d, void previousMonth())
309 Q_PRIVATE_SLOT(d, void beginningOfMonth())
310 Q_PRIVATE_SLOT(d, void endOfMonth())
311 Q_PRIVATE_SLOT(d, void beginningOfWeek())
312 Q_PRIVATE_SLOT(d, void endOfWeek())
313
314private:
315 class KDateTablePrivate;
316 friend class KDateTablePrivate;
317 KDateTablePrivate * const d;
318
319 void initAccels();
320 void paintCell(QPainter *painter, int row, int col);
321 void init();
322
323 Q_DISABLE_COPY(KDateTable)
324};
325
326//----->
327class KPLATOMODELS_EXPORT KDateTableDataModel : public QObject
328{
329 Q_OBJECT
330public:
331 KDateTableDataModel( QObject *parent );
332 ~KDateTableDataModel();
333
334 /// Fetch data for @p date, @p dataType specifies the type of data
335 virtual QVariant data( const QDate &date, int role = Qt::DisplayRole, int dataType = -1 ) const;
336 virtual QVariant weekDayData( int day, int role = Qt::DisplayRole ) const;
337 virtual QVariant weekNumberData( int week, int role = Qt::DisplayRole ) const;
338
339signals:
340 void reset();
341 void dataChanged( const QDate &start, const QDate &end );
342
343};
344
345//-------
346class KPLATOMODELS_EXPORT KDateTableDateDelegate : public QObject
347{
348 Q_OBJECT
349public:
350 KDateTableDateDelegate( QObject *parent = 0 );
351 ~KDateTableDateDelegate() {}
352
353 virtual QRectF paint( QPainter *painter, const StyleOptionViewItem &option, const QDate &date, KDateTableDataModel *model );
354
355 virtual QVariant data( const QDate &date, int role, KDateTableDataModel *model );
356};
357
358class KPLATOMODELS_EXPORT KDateTableCustomDateDelegate : public KDateTableDateDelegate
359{
360 Q_OBJECT
361public:
362 KDateTableCustomDateDelegate( QObject *parent = 0 );
363 ~KDateTableCustomDateDelegate() {}
364
365 virtual QRectF paint( QPainter *painter, const StyleOptionViewItem &option, const QDate &date, KDateTableDataModel *model );
366
367private:
368 friend class KDateTable;
369 QColor fgColor;
370 QColor bgColor;
371 KDateTable::BackgroundMode bgMode;
372
373};
374
375class KPLATOMODELS_EXPORT KDateTableWeekDayDelegate : public QObject
376{
377 Q_OBJECT
378public:
379 KDateTableWeekDayDelegate( QObject *parent = 0 );
380 ~KDateTableWeekDayDelegate() {}
381
382 virtual QRectF paint( QPainter *painter, const StyleOptionHeader &option, int weekday, KDateTableDataModel *model );
383
384 virtual QVariant data( int day, int role, KDateTableDataModel *model );
385};
386
387class KPLATOMODELS_EXPORT KDateTableWeekNumberDelegate : public QObject
388{
389 Q_OBJECT
390public:
391 KDateTableWeekNumberDelegate( QObject *parent = 0 );
392 ~KDateTableWeekNumberDelegate() {}
393
394 virtual QRectF paint( QPainter *painter, const StyleOptionHeader &option, int week, KDateTableDataModel *model );
395
396 virtual QVariant data( int week, int role, KDateTableDataModel *model );
397};
398
399class StyleOptionHeader : public QStyleOptionHeader
400{
401public:
402 StyleOptionHeader() : QStyleOptionHeader() {}
403
404 QRectF rectF;
405};
406
407class StyleOptionViewItem : public QStyleOptionViewItemV3
408{
409public:
410 StyleOptionViewItem()
411 : QStyleOptionViewItemV3()
412 {}
413 StyleOptionViewItem( const StyleOptionViewItem &style )
414 : QStyleOptionViewItemV3( style )
415 {
416 rectF = style.rectF;
417 }
418
419 QRectF rectF;
420};
421
422} //namespace KPlato
423
424#endif // KDATETBL_H
425