1 | #ifndef DELEGATE_H |
---|
2 | #define DELEGATE_H |
---|
3 | |
---|
4 | #include <QItemDelegate> |
---|
5 | #include <QTreeView> |
---|
6 | #include <QPointer> |
---|
7 | |
---|
8 | class Delegate : public QItemDelegate |
---|
9 | { |
---|
10 | Q_OBJECT |
---|
11 | |
---|
12 | public: |
---|
13 | |
---|
14 | enum ControlId |
---|
15 | { |
---|
16 | ControlNone = 0, |
---|
17 | FavouriteControlOn, |
---|
18 | FavouriteControlOff, |
---|
19 | AlarmControlOn, |
---|
20 | AlarmControlOff, |
---|
21 | MapControl, |
---|
22 | WarningControlOn, |
---|
23 | WarningControlOff |
---|
24 | }; |
---|
25 | |
---|
26 | class Control |
---|
27 | { |
---|
28 | public: |
---|
29 | Control(ControlId aControlId, const QString &aImageName) |
---|
30 | : mId(aControlId) |
---|
31 | , mImage(new QImage(aImageName)) |
---|
32 | , mDrawPoint(QPoint(0,0)) |
---|
33 | { } |
---|
34 | inline QImage *image() const { return mImage; } |
---|
35 | inline void setDrawPoint(const QPoint &aPoint) { mDrawPoint = aPoint; } |
---|
36 | inline QPoint drawPoint(const QRect &aRect = QRect()) const // for painter to draw Control |
---|
37 | { |
---|
38 | if(aRect == QRect()) // null rectangle |
---|
39 | return mDrawPoint; // returns relative drawing point |
---|
40 | else |
---|
41 | return QPoint(aRect.x()+aRect.width(),aRect.y()) + mDrawPoint; // returns absolute drawing point |
---|
42 | } |
---|
43 | inline QRect drawRect(const QRect &aRect) const // helper for determining if Control was clicked |
---|
44 | { |
---|
45 | return QRect(drawPoint(aRect), drawPoint(aRect)+QPoint(mImage->size().width(),mImage->size().height())); |
---|
46 | } |
---|
47 | private: |
---|
48 | ControlId mId; |
---|
49 | QImage *mImage; |
---|
50 | QPoint mDrawPoint; // relative 'start-drawing' position (may hold negative values) |
---|
51 | }; |
---|
52 | |
---|
53 | Delegate(QTreeView *aParent); // the delegate 'owner' has to be specified in the constructor - it's used to obtain visualRect of selected item/index |
---|
54 | ~Delegate(); |
---|
55 | |
---|
56 | void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const; |
---|
57 | QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const; |
---|
58 | // |
---|
59 | Delegate::ControlId whichControlClicked(const QModelIndex &aIndex, const QPoint &aPoint) const; |
---|
60 | bool isPointFromRect(const QPoint &aPoint, const QRect &aRect) const; |
---|
61 | |
---|
62 | private: |
---|
63 | bool hasParent( const QModelIndex &index ) const; |
---|
64 | bool isLast( const QModelIndex &index ) const; |
---|
65 | bool isExpanded( const QModelIndex &index ) const; |
---|
66 | void defineControls(); |
---|
67 | // TODO: the better place for these methods would be 'eventmodel' |
---|
68 | // they are used in 'paint' method and so it's better to obtain number of |
---|
69 | // favourities/alarms once when the data has changed and not to call |
---|
70 | // these methods which iterate over all Events in corresponding group |
---|
71 | // every time it requires them |
---|
72 | int numberOfFavourities(const QModelIndex &index) const; |
---|
73 | int numberOfAlarms(const QModelIndex &index) const; |
---|
74 | bool hasTimeConflict(const QModelIndex &index, const QModelIndex &parent) const; |
---|
75 | |
---|
76 | private: |
---|
77 | QPointer<QTreeView> mViewPtr; |
---|
78 | QMap<ControlId,Control*> mControls; |
---|
79 | }; |
---|
80 | |
---|
81 | #endif /* DELEGATE_H */ |
---|
82 | |
---|