source: src/mvc/delegate.h @ 92995da

qt5
Last change on this file since 92995da was 92995da, checked in by pavelpa <pavelpa@…>, 13 years ago

just some directory renaming

  • renamed 'model' to 'mvc' (Model-View-Controller), since it contains also 'delegate' and 'view'
  • 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        };
20
21        class Control
22        {
23            public:
24                Control(ControlId aControlId, const QString &aImageName)
25                    : mId(aControlId)
26                    , mImage(new QImage(aImageName))
27                    , mDrawPoint(QPoint(0,0))
28                { }
29                inline QImage *image() const { return mImage; }
30                inline void setDrawPoint(const QPoint &aPoint) { mDrawPoint = aPoint; }
31                inline QPoint drawPoint(const QRect &aRect) const // for painter to draw Control
32                {
33                    return QPoint(aRect.x()+aRect.width(),aRect.y()) + mDrawPoint;
34                }
35                inline QRect drawRect(const QRect &aRect) const // helper for determining if Control was clicked
36                {
37                    return QRect(drawPoint(aRect), drawPoint(aRect)+QPoint(mImage->size().width(),mImage->size().height()));
38                }
39            private:
40                ControlId mId;
41                QImage *mImage;
42                QPoint mDrawPoint; // relative 'start-drawing' position (may hold negative values)
43        };
44
45        Delegate(QTreeView *aParent); // the delegate 'owner' has to be specified in the constructor - it's used to obtain visualRect of selected item/index
46        ~Delegate();
47
48        void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
49        QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
50        //
51        Delegate::ControlId whichControlClicked(const QModelIndex &aIndex, const QPoint &aPoint) const;
52        bool isPointFromRect(const QPoint &aPoint, const QRect &aRect) const;
53
54    private:
55        bool hasParent( const QModelIndex &index ) const;
56        bool isLast( const QModelIndex &index ) const;
57        bool isExpanded( const QModelIndex &index ) const;
58        void defineControls();
59
60    private:
61        QPointer<QTreeView> mViewPtr;
62        QMap<ControlId,Control*> mControls;
63};
64
65#endif /* DELEGATE_H */
66
Note: See TracBrowser for help on using the repository browser.