source: src/mvc/delegate.h @ 68b2df2

qt5
Last change on this file since 68b2df2 was 68b2df2, checked in by gregor herrmann <gregoa@…>, 10 years ago

Add copyright to source.

  • Property mode set to 100644
File size: 3.9 KB
Line 
1/*
2 * Copyright (C) 2010 Ixonos Plc.
3 * Copyright (C) 2011 Philipp Spitzer, gregor herrmann
4 *
5 * This file is part of ConfClerk.
6 *
7 * ConfClerk is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation, either version 2 of the License, or (at your option)
10 * any later version.
11 *
12 * ConfClerk is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * ConfClerk.  If not, see <http://www.gnu.org/licenses/>.
19 */
20#ifndef DELEGATE_H
21#define DELEGATE_H
22
23#include <QItemDelegate>
24#include <QTreeView>
25#include <QPointer>
26
27class Delegate : public QItemDelegate
28{
29    Q_OBJECT
30
31    public:
32
33        enum ControlId
34        {
35            ControlNone = 0,
36            FavouriteControlOn,
37            FavouriteControlOff,
38            AlarmControlOn,
39            AlarmControlOff,
40            MapControl,
41            WarningControl
42        };
43
44        class Control
45        {
46            public:
47                Control(ControlId aControlId, const QString &aImageName, const Control* prev_control);
48
49                inline QImage *image() const { return mImage; }
50                inline void setDrawPoint(const QPoint &aPoint) { mDrawPoint = aPoint; }
51                inline QRect drawRect(const QRect &aRect) const // helper for determining if Control was clicked
52                {
53                    return QRect(drawPoint(aRect), drawPoint(aRect)+QPoint(mImage->size().width(),mImage->size().height()));
54                }
55                void paint(QPainter* painter, const QRect rect);
56
57                bool enabled() const { return mEnabled; }
58                void setEnabled(bool v) { mEnabled = v; }
59            private:
60                inline QPoint drawPoint(const QRect &aRect = QRect()) const // for painter to draw Control
61                {
62                    if(aRect == QRect()) // null rectangle
63                        return mDrawPoint; // returns relative drawing point
64                    else
65                        return QPoint(aRect.x()+aRect.width(),aRect.y()) + mDrawPoint; // returns absolute drawing point
66                }
67
68                ControlId mId;
69                QImage *mImage;
70                QPoint mDrawPoint; // relative 'start-drawing' position (may hold negative values)
71                bool mEnabled;
72        };
73
74        Delegate(QTreeView *aParent); // the delegate 'owner' has to be specified in the constructor - it's used to obtain visualRect of selected item/index
75        ~Delegate();
76
77        void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
78        QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
79        //
80        Delegate::ControlId whichControlClicked(const QModelIndex &aIndex, const QPoint &aPoint) const;
81        bool isPointFromRect(const QPoint &aPoint, const QRect &aRect) const;
82
83    private:
84        bool hasParent( const QModelIndex &index ) const;
85        bool isLast( const QModelIndex &index ) const;
86        bool isExpanded( const QModelIndex &index ) const;
87        void defineControls();
88        // TODO: the better place for these methods would be 'eventmodel'
89        // they are used in 'paint' method and so it's better to obtain number of
90        // favourities/alarms once when the data has changed and not to call
91        // these methods which iterate over all Events in corresponding group
92        // every time it requires them
93        int numberOfFavourities(const QModelIndex &index) const;
94        int numberOfAlarms(const QModelIndex &index) const;
95
96    private:
97        QPointer<QTreeView> mViewPtr;
98        QMap<ControlId,Control*> mControls;
99};
100
101#endif /* DELEGATE_H */
102
Note: See TracBrowser for help on using the repository browser.