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