work on favourite
[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) const // for painter to draw Control
35                 {
36                     return QPoint(aRect.x()+aRect.width(),aRect.y()) + mDrawPoint;
37                 }
38                 inline QRect drawRect(const QRect &aRect) const // helper for determining if Control was clicked
39                 {
40                     return QRect(drawPoint(aRect), drawPoint(aRect)+QPoint(mImage->size().width(),mImage->size().height()));
41                 }
42             private:
43                 ControlId mId;
44                 QImage *mImage;
45                 QPoint mDrawPoint; // relative 'start-drawing' position (may hold negative values)
46         };
47
48         Delegate(QTreeView *aParent); // the delegate 'owner' has to be specified in the constructor - it's used to obtain visualRect of selected item/index
49         ~Delegate();
50
51         void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
52         QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
53         //
54         Delegate::ControlId whichControlClicked(const QModelIndex &aIndex, const QPoint &aPoint) const;
55         bool isPointFromRect(const QPoint &aPoint, const QRect &aRect) const;
56
57     private:
58         bool hasParent( const QModelIndex &index ) const;
59         bool isLast( const QModelIndex &index ) const;
60         bool isExpanded( const QModelIndex &index ) const;
61         void defineControls();
62
63     private:
64         QPointer<QTreeView> mViewPtr;
65         QMap<ControlId,Control*> mControls;
66 };
67
68 #endif /* DELEGATE_H */
69