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