source: src/mvc/delegate.h @ 8860ff4

qt5
Last change on this file since 8860ff4 was 03ff157, checked in by fortefr <fortefr@…>, 13 years ago

Map button/compass icon added

  • Property mode set to 100644
File size: 2.3 KB
Line 
1#ifndef DELEGATE_H
2#define DELEGATE_H
3
4#include <QItemDelegate>
5#include <QTreeView>
6#include <QPointer>
7
8class Delegate : public QItemDelegate
9{
10    Q_OBJECT
11
12    public:
13
14        enum ControlId
15        {
16            ControlNone = 0,
17            FavouriteControl,
18            AlarmControl,
19            MapControl
20        };
21
22        class Control
23        {
24            public:
25                Control(ControlId aControlId, const QString &aImageName)
26                    : mId(aControlId)
27                    , mImage(new QImage(aImageName))
28                    , mDrawPoint(QPoint(0,0))
29                { }
30                inline QImage *image() const { return mImage; }
31                inline void setDrawPoint(const QPoint &aPoint) { mDrawPoint = aPoint; }
32                inline QPoint drawPoint(const QRect &aRect) const // for painter to draw Control
33                {
34                    return QPoint(aRect.x()+aRect.width(),aRect.y()) + mDrawPoint;
35                }
36                inline QRect drawRect(const QRect &aRect) const // helper for determining if Control was clicked
37                {
38                    return QRect(drawPoint(aRect), drawPoint(aRect)+QPoint(mImage->size().width(),mImage->size().height()));
39                }
40            private:
41                ControlId mId;
42                QImage *mImage;
43                QPoint mDrawPoint; // relative 'start-drawing' position (may hold negative values)
44        };
45
46        Delegate(QTreeView *aParent); // the delegate 'owner' has to be specified in the constructor - it's used to obtain visualRect of selected item/index
47        ~Delegate();
48
49        void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
50        QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
51        //
52        Delegate::ControlId whichControlClicked(const QModelIndex &aIndex, const QPoint &aPoint) const;
53        bool isPointFromRect(const QPoint &aPoint, const QRect &aRect) const;
54
55    private:
56        bool hasParent( const QModelIndex &index ) const;
57        bool isLast( const QModelIndex &index ) const;
58        bool isExpanded( const QModelIndex &index ) const;
59        void defineControls();
60
61    private:
62        QPointer<QTreeView> mViewPtr;
63        QMap<ControlId,Control*> mControls;
64};
65
66#endif /* DELEGATE_H */
67
Note: See TracBrowser for help on using the repository browser.