1 | /* This file is part of the KDE project |
2 | Copyright (C) 2006 - 2009 Dag Andersen <calligra-devel@kde.org> |
3 | |
4 | This library is free software; you can redistribute it and/or |
5 | modify it under the terms of the GNU Library General Public |
6 | License as published by the Free Software Foundation; either |
7 | version 2 of the License, or (at your option) any later version. |
8 | |
9 | This library is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | Library General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU Library General Public License |
15 | along with this library; see the file COPYING.LIB. If not, write to |
16 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
17 | * Boston, MA 02110-1301, USA. |
18 | */ |
19 | |
20 | #ifndef KPTITEMMODELBASE_H |
21 | #define KPTITEMMODELBASE_H |
22 | |
23 | #include "kplatomodels_export.h" |
24 | |
25 | #include "kptglobal.h" |
26 | #include "kpttreecombobox.h" |
27 | |
28 | #include <QAbstractItemModel> |
29 | #include <QStyledItemDelegate> |
30 | #include <QMetaEnum> |
31 | #include <QEvent> |
32 | #include <QComboBox> |
33 | |
34 | #include <KoXmlReaderForward.h> |
35 | |
36 | class KoDocument; |
37 | class QDomElement; |
38 | class KUndo2Command; |
39 | class QPainter; |
40 | class QModelIndex; |
41 | |
42 | class QItemSelection; |
43 | |
44 | /// The main namespace |
45 | namespace KPlato |
46 | { |
47 | |
48 | class Project; |
49 | class ScheduleManager; |
50 | |
51 | /// Namespace for item delegate specific enums |
52 | namespace Delegate |
53 | { |
54 | /// For selector delegate |
55 | enum EditorType { EnumEditor, TimeEditor }; |
56 | /// Controls action when editor is closed. See QAbstractItemDelegate::EndEditHint. |
57 | enum EndEditHint { |
58 | NoHint = QAbstractItemDelegate::NoHint, |
59 | EditNextItem = QAbstractItemDelegate::EditNextItem, |
60 | EditPreviousItem = QAbstractItemDelegate::EditPreviousItem, |
61 | SubmitModelCache = QAbstractItemDelegate::SubmitModelCache, |
62 | RevertModelCache = QAbstractItemDelegate::RevertModelCache, |
63 | EditLeftItem = 100, |
64 | EditRightItem = 101, |
65 | EditDownItem = 102, |
66 | EditUpItem = 103 |
67 | }; |
68 | } |
69 | |
70 | /// ItemDelegate implements improved control over closeEditor |
71 | class KPLATOMODELS_EXPORT ItemDelegate : public QStyledItemDelegate |
72 | { |
73 | Q_OBJECT |
74 | public: |
75 | /// Constructor |
76 | explicit ItemDelegate(QObject *parent = 0) |
77 | : QStyledItemDelegate( parent ), |
78 | m_lastHint( Delegate::NoHint ) |
79 | {} |
80 | |
81 | /// Extend EndEditHint for movement from edited item to next item to edit |
82 | Delegate::EndEditHint endEditHint() const { return m_lastHint; } |
83 | /// Increase the sizehint height a little to give room for editors |
84 | QSize sizeHint( const QStyleOptionViewItem & option, const QModelIndex & index ) const; |
85 | |
86 | protected: |
87 | /// Implements arrow key navigation |
88 | bool eventFilter(QObject *object, QEvent *event); |
89 | /// Draw custom focus |
90 | // virtual void drawFocus(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect ) const; |
91 | |
92 | private: |
93 | Delegate::EndEditHint m_lastHint; |
94 | }; |
95 | |
96 | class KPLATOMODELS_EXPORT CheckStateItemDelegate : public ItemDelegate |
97 | { |
98 | Q_OBJECT |
99 | public: |
100 | explicit CheckStateItemDelegate(QObject *parent = 0); |
101 | |
102 | protected: |
103 | bool editorEvent( QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index ); |
104 | }; |
105 | |
106 | class KPLATOMODELS_EXPORT DateTimeCalendarDelegate : public ItemDelegate |
107 | { |
108 | Q_OBJECT |
109 | public: |
110 | explicit DateTimeCalendarDelegate(QObject *parent = 0); |
111 | |
112 | QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const; |
113 | void setEditorData( QWidget *editor, const QModelIndex &index ) const; |
114 | void setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const; |
115 | void updateEditorGeometry( QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index ) const; |
116 | |
117 | }; |
118 | |
119 | class KPLATOMODELS_EXPORT ProgressBarDelegate : public ItemDelegate |
120 | { |
121 | Q_OBJECT |
122 | public: |
123 | explicit ProgressBarDelegate(QObject *parent = 0); |
124 | |
125 | ~ProgressBarDelegate(); |
126 | |
127 | void paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const; |
128 | QSize sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const; |
129 | |
130 | QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const; |
131 | void setEditorData( QWidget *editor, const QModelIndex &index ) const; |
132 | void setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const; |
133 | void updateEditorGeometry( QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index ) const; |
134 | |
135 | protected: |
136 | void initStyleOptionProgressBar( QStyleOptionProgressBar *option, const QModelIndex &index ) const; |
137 | |
138 | }; |
139 | |
140 | class Slider : public QSlider { |
141 | Q_OBJECT |
142 | public: |
143 | explicit Slider( QWidget *parent ); |
144 | private slots: |
145 | void updateTip( int value ); |
146 | }; |
147 | |
148 | class KPLATOMODELS_EXPORT SelectorDelegate : public ItemDelegate |
149 | { |
150 | Q_OBJECT |
151 | public: |
152 | explicit SelectorDelegate(QObject *parent = 0); |
153 | |
154 | QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const; |
155 | |
156 | void setEditorData(QWidget *editor, const QModelIndex &index) const; |
157 | void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const; |
158 | |
159 | void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const; |
160 | }; |
161 | |
162 | class KPLATOMODELS_EXPORT EnumDelegate : public ItemDelegate |
163 | { |
164 | Q_OBJECT |
165 | public: |
166 | explicit EnumDelegate(QObject *parent = 0); |
167 | |
168 | QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const; |
169 | |
170 | void setEditorData(QWidget *editor, const QModelIndex &index) const; |
171 | void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const; |
172 | |
173 | void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const; |
174 | }; |
175 | |
176 | //------------------------------------ |
177 | class KPLATOMODELS_EXPORT RequieredResourceDelegate : public ItemDelegate |
178 | { |
179 | Q_OBJECT |
180 | public: |
181 | explicit RequieredResourceDelegate(QObject *parent = 0); |
182 | |
183 | QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const; |
184 | |
185 | void setEditorData(QWidget *editor, const QModelIndex &index) const; |
186 | void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const; |
187 | |
188 | void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const; |
189 | }; |
190 | |
191 | |
192 | class KPLATOMODELS_EXPORT DurationSpinBoxDelegate : public ItemDelegate |
193 | { |
194 | Q_OBJECT |
195 | public: |
196 | explicit DurationSpinBoxDelegate(QObject *parent = 0); |
197 | |
198 | QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const; |
199 | |
200 | void setEditorData(QWidget *editor, const QModelIndex &index) const; |
201 | void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const; |
202 | |
203 | void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const; |
204 | }; |
205 | |
206 | class KPLATOMODELS_EXPORT SpinBoxDelegate : public ItemDelegate |
207 | { |
208 | Q_OBJECT |
209 | public: |
210 | explicit SpinBoxDelegate(QObject *parent = 0); |
211 | |
212 | QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const; |
213 | |
214 | void setEditorData(QWidget *editor, const QModelIndex &index) const; |
215 | void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const; |
216 | |
217 | void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const; |
218 | }; |
219 | |
220 | class KPLATOMODELS_EXPORT DoubleSpinBoxDelegate : public ItemDelegate |
221 | { |
222 | Q_OBJECT |
223 | public: |
224 | explicit DoubleSpinBoxDelegate(QObject *parent = 0); |
225 | |
226 | QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const; |
227 | |
228 | void setEditorData(QWidget *editor, const QModelIndex &index) const; |
229 | void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const; |
230 | |
231 | void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const; |
232 | }; |
233 | |
234 | class KPLATOMODELS_EXPORT MoneyDelegate : public ItemDelegate |
235 | { |
236 | Q_OBJECT |
237 | public: |
238 | explicit MoneyDelegate(QObject *parent = 0); |
239 | |
240 | QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const; |
241 | |
242 | void setEditorData(QWidget *editor, const QModelIndex &index) const; |
243 | void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const; |
244 | |
245 | void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const; |
246 | }; |
247 | |
248 | class KPLATOMODELS_EXPORT TimeDelegate : public ItemDelegate |
249 | { |
250 | Q_OBJECT |
251 | public: |
252 | explicit TimeDelegate(QObject *parent = 0); |
253 | |
254 | QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const; |
255 | |
256 | void setEditorData(QWidget *editor, const QModelIndex &index) const; |
257 | void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const; |
258 | |
259 | void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const; |
260 | }; |
261 | |
262 | class KPLATOMODELS_EXPORT ItemModelBase : public QAbstractItemModel |
263 | { |
264 | Q_OBJECT |
265 | public: |
266 | // FIXME: Refactor, This is a copy from protected enum in QAbstractItemView |
267 | enum DropIndicatorPosition { |
268 | OnItem, /*QAbstractItemView::OnItem*/ /// The item will be dropped on the index. |
269 | AboveItem, /*QAbstractItemView::AboveItem*/ /// The item will be dropped above the index. |
270 | BelowItem, /*QAbstractItemView::BelowItem*/ /// The item will be dropped below the index. |
271 | OnViewport /*QAbstractItemView::OnViewport*/ /// The item will be dropped onto a region of the viewport with no items if acceptDropsOnView is set. |
272 | }; |
273 | |
274 | explicit ItemModelBase( QObject *parent = 0 ); |
275 | virtual ~ItemModelBase(); |
276 | |
277 | virtual const QMetaEnum columnMap() const { return QMetaEnum(); } |
278 | Project *project() const { return m_project; } |
279 | ScheduleManager *scheduleManager() const { return m_manager; } |
280 | bool isReadWrite() { return m_readWrite; } |
281 | void setReadOnly( int column, bool ro ) { m_columnROMap[ column ] = ro; } |
282 | /// Returns true if @p column has been set to ReadOnly. |
283 | bool isColumnReadOnly( int column ) const { return m_columnROMap.contains( column ) && m_columnROMap[ column ]; } |
284 | |
285 | /** |
286 | * Check if the @p data is allowed to be dropped on @p index, |
287 | * @p dropIndicatorPosition indicates position relative @p index. |
288 | * |
289 | * Base implementation checks flags and mimetypes. |
290 | */ |
291 | virtual bool dropAllowed( const QModelIndex &index, int dropIndicatorPosition, const QMimeData *data ); |
292 | |
293 | /// Create the correct delegate for @p column. @p parent is the delegates parent widget. |
294 | /// If default should be used, return 0. |
295 | virtual QAbstractItemDelegate *createDelegate( int column, QWidget *parent ) const { Q_UNUSED(column); Q_UNUSED(parent); return 0; } |
296 | |
297 | QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const; |
298 | bool setData( const QModelIndex &index, const QVariant &value, int role ); |
299 | QVariant ( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const; |
300 | |
301 | /// Return the sortorder to be used for @p column |
302 | virtual int sortRole( int /*column*/ ) const { return Qt::DisplayRole; } |
303 | |
304 | signals: |
305 | /// Connect to this signal if your model modifies data using undo commands. |
306 | void executeCommand( KUndo2Command* ); |
307 | |
308 | public slots: |
309 | virtual void setProject( Project *project ); |
310 | virtual void setScheduleManager( ScheduleManager *sm ); |
311 | virtual void setReadWrite( bool rw ) { m_readWrite = rw; } |
312 | /// Reimplement if your model can be refreshed |
313 | virtual void refresh() {} |
314 | |
315 | protected slots: |
316 | virtual void slotLayoutToBeChanged(); |
317 | virtual void slotLayoutChanged(); |
318 | |
319 | protected: |
320 | Project *m_project; |
321 | ScheduleManager *m_manager; |
322 | bool m_readWrite; |
323 | QMap<int, bool> m_columnROMap; |
324 | }; |
325 | |
326 | |
327 | } // namespace KPlato |
328 | |
329 | #endif |
330 | |