source: src/mvc/delegate.h @ 11f3f87

qt5
Last change on this file since 11f3f87 was 11f3f87, checked in by gregor herrmann <gregoa@…>, 8 years ago

Update copyright notices.

  • Property mode set to 100644
File size: 3.9 KB
Line 
1/*
2 * Copyright (C) 2010 Ixonos Plc.
3 * Copyright (C) 2011-2014 Philipp Spitzer, gregor herrmann, Stefan Stahl
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 <QtGui>
24
25class Delegate : public QItemDelegate
26{
27    Q_OBJECT
28
29    public:
30
31        enum ControlId
32        {
33            ControlNone = 0,
34            FavouriteControlOn,
35            FavouriteControlOff,
36            AlarmControlOn,
37            AlarmControlOff,
38            WarningControl
39        };
40
41        class Control
42        {
43            public:
44                Control(ControlId aControlId, const QString &aImageName, const Control* prev_control);
45
46                inline QImage *image() const { return mImage; }
47                inline void setDrawPoint(const QPoint &aPoint) { mDrawPoint = aPoint; }
48                inline QRect drawRect(const QRect &aRect) const // helper for determining if Control was clicked
49                {
50                    return QRect(drawPoint(aRect), drawPoint(aRect)+QPoint(mImage->size().width(),mImage->size().height()));
51                }
52                void paint(QPainter* painter, const QRect rect);
53
54                bool enabled() const { return mEnabled; }
55                void setEnabled(bool v) { mEnabled = v; }
56            private:
57                inline QPoint drawPoint(const QRect &aRect = QRect()) const // for painter to draw Control
58                {
59                    if(aRect == QRect()) // null rectangle
60                        return mDrawPoint; // returns relative drawing point
61                    else
62                        return QPoint(aRect.x()+aRect.width(),aRect.y()) + mDrawPoint; // returns absolute drawing point
63                }
64
65                ControlId mId;
66                QImage *mImage;
67                QPoint mDrawPoint; // relative 'start-drawing' position (may hold negative values)
68                bool mEnabled;
69        };
70
71        Delegate(QTreeView *aParent); // the delegate 'owner' has to be specified in the constructor - it's used to obtain visualRect of selected item/index
72        ~Delegate();
73
74        void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
75        QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
76        //
77        Delegate::ControlId whichControlClicked(const QModelIndex &aIndex, const QPoint &aPoint) const;
78        bool isPointFromRect(const QPoint &aPoint, const QRect &aRect) const;
79
80    private:
81        bool hasParent( const QModelIndex &index ) const;
82        bool isLast( const QModelIndex &index ) const;
83        bool isExpanded( const QModelIndex &index ) const;
84        void defineControls();
85        // TODO: the better place for these methods would be 'eventmodel'
86        // they are used in 'paint' method and so it's better to obtain number of
87        // favourities/alarms once when the data has changed and not to call
88        // these methods which iterate over all Events in corresponding group
89        // every time it requires them
90        int numberOfFavourities(const QModelIndex &index) const;
91        int numberOfAlarms(const QModelIndex &index) const;
92
93    private:
94        QPointer<QTreeView> mViewPtr;
95        QMap<ControlId,Control*> mControls;
96};
97
98#endif /* DELEGATE_H */
99
Note: See TracBrowser for help on using the repository browser.