1 | /* |
---|
2 | * Copyright (C) 2010 Ixonos Plc. |
---|
3 | * |
---|
4 | * This file is part of fosdem-schedule. |
---|
5 | * |
---|
6 | * fosdem-schedule is free software: you can redistribute it and/or modify it |
---|
7 | * under the terms of the GNU General Public License as published by the Free |
---|
8 | * Software Foundation, either version 2 of the License, or (at your option) |
---|
9 | * any later version. |
---|
10 | * |
---|
11 | * fosdem-schedule is distributed in the hope that it will be useful, but |
---|
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
---|
13 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
---|
14 | * more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License along with |
---|
17 | * fosdem-schedule. If not, see <http://www.gnu.org/licenses/>. |
---|
18 | */ |
---|
19 | #ifndef DELEGATE_H |
---|
20 | #define DELEGATE_H |
---|
21 | |
---|
22 | #include <QItemDelegate> |
---|
23 | #include <QTreeView> |
---|
24 | #include <QPointer> |
---|
25 | |
---|
26 | class Delegate : public QItemDelegate |
---|
27 | { |
---|
28 | Q_OBJECT |
---|
29 | |
---|
30 | public: |
---|
31 | |
---|
32 | enum ControlId |
---|
33 | { |
---|
34 | ControlNone = 0, |
---|
35 | FavouriteControlOn, |
---|
36 | FavouriteControlOff, |
---|
37 | AlarmControlOn, |
---|
38 | AlarmControlOff, |
---|
39 | MapControl, |
---|
40 | WarningControl |
---|
41 | }; |
---|
42 | |
---|
43 | class Control |
---|
44 | { |
---|
45 | public: |
---|
46 | Control(ControlId aControlId, const QString &aImageName, const Control* prev_control); |
---|
47 | |
---|
48 | inline QImage *image() const { return mImage; } |
---|
49 | inline void setDrawPoint(const QPoint &aPoint) { mDrawPoint = aPoint; } |
---|
50 | inline QRect drawRect(const QRect &aRect) const // helper for determining if Control was clicked |
---|
51 | { |
---|
52 | return QRect(drawPoint(aRect), drawPoint(aRect)+QPoint(mImage->size().width(),mImage->size().height())); |
---|
53 | } |
---|
54 | void paint(QPainter* painter, const QRect rect); |
---|
55 | |
---|
56 | private: |
---|
57 | inline QPoint drawPoint(const QRect &aRect = QRect()) const // for painter to draw Control |
---|
58 | { |
---|
59 | if(aRect == QRect()) // null rectangle |
---|
60 | return mDrawPoint; // returns relative drawing point |
---|
61 | else |
---|
62 | return QPoint(aRect.x()+aRect.width(),aRect.y()) + mDrawPoint; // returns absolute drawing point |
---|
63 | } |
---|
64 | |
---|
65 | ControlId mId; |
---|
66 | QImage *mImage; |
---|
67 | QPoint mDrawPoint; // relative 'start-drawing' position (may hold negative values) |
---|
68 | }; |
---|
69 | |
---|
70 | Delegate(QTreeView *aParent); // the delegate 'owner' has to be specified in the constructor - it's used to obtain visualRect of selected item/index |
---|
71 | ~Delegate(); |
---|
72 | |
---|
73 | void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const; |
---|
74 | QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const; |
---|
75 | // |
---|
76 | Delegate::ControlId whichControlClicked(const QModelIndex &aIndex, const QPoint &aPoint) const; |
---|
77 | bool isPointFromRect(const QPoint &aPoint, const QRect &aRect) const; |
---|
78 | |
---|
79 | private: |
---|
80 | bool hasParent( const QModelIndex &index ) const; |
---|
81 | bool isLast( const QModelIndex &index ) const; |
---|
82 | bool isExpanded( const QModelIndex &index ) const; |
---|
83 | void defineControls(); |
---|
84 | // TODO: the better place for these methods would be 'eventmodel' |
---|
85 | // they are used in 'paint' method and so it's better to obtain number of |
---|
86 | // favourities/alarms once when the data has changed and not to call |
---|
87 | // these methods which iterate over all Events in corresponding group |
---|
88 | // every time it requires them |
---|
89 | int numberOfFavourities(const QModelIndex &index) const; |
---|
90 | int numberOfAlarms(const QModelIndex &index) const; |
---|
91 | |
---|
92 | private: |
---|
93 | QPointer<QTreeView> mViewPtr; |
---|
94 | QMap<ControlId,Control*> mControls; |
---|
95 | }; |
---|
96 | |
---|
97 | #endif /* DELEGATE_H */ |
---|
98 | |
---|