[ca90cb1] | 1 | /* |
---|
| 2 | * Copyright (C) 2010 Ixonos Plc. |
---|
[de5d0f1] | 3 | * Copyright (C) 2011-2017 Philipp Spitzer, gregor herrmann, Stefan Stahl |
---|
[ca90cb1] | 4 | * |
---|
[6df32f2] | 5 | * This file is part of ConfClerk. |
---|
[ca90cb1] | 6 | * |
---|
[6df32f2] | 7 | * ConfClerk is free software: you can redistribute it and/or modify it |
---|
[ca90cb1] | 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 | * |
---|
[6df32f2] | 12 | * ConfClerk is distributed in the hope that it will be useful, but |
---|
[ca90cb1] | 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 |
---|
[6df32f2] | 18 | * ConfClerk. If not, see <http://www.gnu.org/licenses/>. |
---|
[ca90cb1] | 19 | */ |
---|
[66327a0] | 20 | #ifndef DELEGATE_H |
---|
| 21 | #define DELEGATE_H |
---|
| 22 | |
---|
[79a7671] | 23 | #include "qglobal.h" |
---|
| 24 | #if QT_VERSION >= 0x050000 |
---|
[4b6ae6b] | 25 | #include <QtWidgets> |
---|
[79a7671] | 26 | #else |
---|
| 27 | #include <QtGui> |
---|
| 28 | #endif |
---|
[66327a0] | 29 | |
---|
| 30 | class Delegate : public QItemDelegate |
---|
| 31 | { |
---|
| 32 | Q_OBJECT |
---|
| 33 | |
---|
| 34 | public: |
---|
| 35 | |
---|
| 36 | enum ControlId |
---|
| 37 | { |
---|
| 38 | ControlNone = 0, |
---|
[7b3cd0e] | 39 | FavouriteControlStrong, |
---|
| 40 | FavouriteControlWeak, |
---|
| 41 | FavouriteControlNo, |
---|
[680a4da] | 42 | AlarmControlOn, |
---|
| 43 | AlarmControlOff, |
---|
[336fa33] | 44 | WarningControl |
---|
[66327a0] | 45 | }; |
---|
| 46 | |
---|
| 47 | class Control |
---|
| 48 | { |
---|
| 49 | public: |
---|
[525448c] | 50 | Control(ControlId aControlId, const QString &aImageName, const Control* prev_control); |
---|
| 51 | |
---|
[66327a0] | 52 | inline QImage *image() const { return mImage; } |
---|
| 53 | inline void setDrawPoint(const QPoint &aPoint) { mDrawPoint = aPoint; } |
---|
[525448c] | 54 | inline QRect drawRect(const QRect &aRect) const // helper for determining if Control was clicked |
---|
| 55 | { |
---|
| 56 | return QRect(drawPoint(aRect), drawPoint(aRect)+QPoint(mImage->size().width(),mImage->size().height())); |
---|
| 57 | } |
---|
| 58 | void paint(QPainter* painter, const QRect rect); |
---|
| 59 | |
---|
[a6a9e0b] | 60 | bool enabled() const { return mEnabled; } |
---|
| 61 | void setEnabled(bool v) { mEnabled = v; } |
---|
[525448c] | 62 | private: |
---|
[ffd878f] | 63 | inline QPoint drawPoint(const QRect &aRect = QRect()) const // for painter to draw Control |
---|
[66327a0] | 64 | { |
---|
[ffd878f] | 65 | if(aRect == QRect()) // null rectangle |
---|
| 66 | return mDrawPoint; // returns relative drawing point |
---|
| 67 | else |
---|
| 68 | return QPoint(aRect.x()+aRect.width(),aRect.y()) + mDrawPoint; // returns absolute drawing point |
---|
[66327a0] | 69 | } |
---|
[525448c] | 70 | |
---|
[66327a0] | 71 | ControlId mId; |
---|
| 72 | QImage *mImage; |
---|
| 73 | QPoint mDrawPoint; // relative 'start-drawing' position (may hold negative values) |
---|
[a6a9e0b] | 74 | bool mEnabled; |
---|
[66327a0] | 75 | }; |
---|
| 76 | |
---|
| 77 | Delegate(QTreeView *aParent); // the delegate 'owner' has to be specified in the constructor - it's used to obtain visualRect of selected item/index |
---|
| 78 | ~Delegate(); |
---|
| 79 | |
---|
| 80 | void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const; |
---|
| 81 | QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const; |
---|
| 82 | // |
---|
| 83 | Delegate::ControlId whichControlClicked(const QModelIndex &aIndex, const QPoint &aPoint) const; |
---|
| 84 | bool isPointFromRect(const QPoint &aPoint, const QRect &aRect) const; |
---|
| 85 | |
---|
| 86 | private: |
---|
| 87 | bool hasParent( const QModelIndex &index ) const; |
---|
| 88 | bool isLast( const QModelIndex &index ) const; |
---|
| 89 | bool isExpanded( const QModelIndex &index ) const; |
---|
| 90 | void defineControls(); |
---|
[4be95b8] | 91 | // TODO: the better place for these methods would be 'eventmodel' |
---|
| 92 | // they are used in 'paint' method and so it's better to obtain number of |
---|
| 93 | // favourities/alarms once when the data has changed and not to call |
---|
| 94 | // these methods which iterate over all Events in corresponding group |
---|
| 95 | // every time it requires them |
---|
| 96 | int numberOfFavourities(const QModelIndex &index) const; |
---|
| 97 | int numberOfAlarms(const QModelIndex &index) const; |
---|
[66327a0] | 98 | |
---|
| 99 | private: |
---|
| 100 | QPointer<QTreeView> mViewPtr; |
---|
| 101 | QMap<ControlId,Control*> mControls; |
---|
| 102 | }; |
---|
| 103 | |
---|
| 104 | #endif /* DELEGATE_H */ |
---|
| 105 | |
---|