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