1
/* This file is part of the KDE project
2
Copyright (C) 2006 - 2009 Dag Andersen <calligra-devel@kde.org>
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.
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.
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.
20
#ifndef KPTITEMMODELBASE_H
21
#define KPTITEMMODELBASE_H
23
#include "kplatomodels_export.h"
25
#include "kptglobal.h"
26
#include "kpttreecombobox.h"
28
#include <QAbstractItemModel>
29
#include <QStyledItemDelegate>
32
#include <KoXmlReaderForward.h>
37
/// The main namespace
42
class ScheduleManager;
44
/// Namespace for item delegate specific enums
47
/// For selector delegate
48
enum EditorType { EnumEditor, TimeEditor };
49
/// Controls action when editor is closed. See QAbstractItemDelegate::EndEditHint.
51
NoHint = QAbstractItemDelegate::NoHint,
52
EditNextItem = QAbstractItemDelegate::EditNextItem,
53
EditPreviousItem = QAbstractItemDelegate::EditPreviousItem,
54
SubmitModelCache = QAbstractItemDelegate::SubmitModelCache,
55
RevertModelCache = QAbstractItemDelegate::RevertModelCache,
63
/// ItemDelegate implements improved control over closeEditor
64
class KPLATOMODELS_EXPORT ItemDelegate : public QStyledItemDelegate
69
explicit ItemDelegate(QObject *parent = 0)
70
: QStyledItemDelegate( parent ),
71
m_lastHint( Delegate::NoHint )
74
/// Extend EndEditHint for movement from edited item to next item to edit
75
Delegate::EndEditHint endEditHint() const { return m_lastHint; }
76
/// Increase the sizehint height a little to give room for editors
77
QSize sizeHint( const QStyleOptionViewItem & option, const QModelIndex & index ) const;
80
/// Implements arrow key navigation
81
bool eventFilter(QObject *object, QEvent *event);
83
// virtual void drawFocus(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect ) const;
86
Delegate::EndEditHint m_lastHint;
89
class KPLATOMODELS_EXPORT CheckStateItemDelegate : public ItemDelegate
93
explicit CheckStateItemDelegate(QObject *parent = 0);
96
bool editorEvent( QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index );
99
class KPLATOMODELS_EXPORT DateTimeCalendarDelegate : public ItemDelegate
103
explicit DateTimeCalendarDelegate(QObject *parent = 0);
105
QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const;
106
void setEditorData( QWidget *editor, const QModelIndex &index ) const;
107
void setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const;
108
void updateEditorGeometry( QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index ) const;
112
class KPLATOMODELS_EXPORT ProgressBarDelegate : public ItemDelegate
116
explicit ProgressBarDelegate(QObject *parent = 0);
118
~ProgressBarDelegate();
120
void paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const;
121
QSize sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const;
123
QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const;
124
void setEditorData( QWidget *editor, const QModelIndex &index ) const;
125
void setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const;
126
void updateEditorGeometry( QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index ) const;
129
void initStyleOptionProgressBar( QStyleOptionProgressBar *option, const QModelIndex &index ) const;
133
class Slider : public QSlider {
136
explicit Slider( QWidget *parent );
138
void updateTip( int value );
141
class KPLATOMODELS_EXPORT SelectorDelegate : public ItemDelegate
145
explicit SelectorDelegate(QObject *parent = 0);
147
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
149
void setEditorData(QWidget *editor, const QModelIndex &index) const;
150
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
152
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
155
class KPLATOMODELS_EXPORT EnumDelegate : public ItemDelegate
159
explicit EnumDelegate(QObject *parent = 0);
161
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
163
void setEditorData(QWidget *editor, const QModelIndex &index) const;
164
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
166
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
169
//------------------------------------
170
class KPLATOMODELS_EXPORT RequieredResourceDelegate : public ItemDelegate
174
explicit RequieredResourceDelegate(QObject *parent = 0);
176
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
178
void setEditorData(QWidget *editor, const QModelIndex &index) const;
179
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
181
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
185
class KPLATOMODELS_EXPORT DurationSpinBoxDelegate : public ItemDelegate
189
explicit DurationSpinBoxDelegate(QObject *parent = 0);
191
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
193
void setEditorData(QWidget *editor, const QModelIndex &index) const;
194
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
196
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
199
class KPLATOMODELS_EXPORT SpinBoxDelegate : public ItemDelegate
203
explicit SpinBoxDelegate(QObject *parent = 0);
205
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
207
void setEditorData(QWidget *editor, const QModelIndex &index) const;
208
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
210
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
213
class KPLATOMODELS_EXPORT DoubleSpinBoxDelegate : public ItemDelegate
217
explicit DoubleSpinBoxDelegate(QObject *parent = 0);
219
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
221
void setEditorData(QWidget *editor, const QModelIndex &index) const;
222
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
224
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
227
class KPLATOMODELS_EXPORT MoneyDelegate : public ItemDelegate
231
explicit MoneyDelegate(QObject *parent = 0);
233
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
235
void setEditorData(QWidget *editor, const QModelIndex &index) const;
236
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
238
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
241
class KPLATOMODELS_EXPORT TimeDelegate : public ItemDelegate
245
explicit TimeDelegate(QObject *parent = 0);
247
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
249
void setEditorData(QWidget *editor, const QModelIndex &index) const;
250
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
252
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
255
class KPLATOMODELS_EXPORT ItemModelBase : public QAbstractItemModel
259
// FIXME: Refactor, This is a copy from protected enum in QAbstractItemView
260
enum DropIndicatorPosition {
261
OnItem, /*QAbstractItemView::OnItem*/ /// The item will be dropped on the index.
262
AboveItem, /*QAbstractItemView::AboveItem*/ /// The item will be dropped above the index.
263
BelowItem, /*QAbstractItemView::BelowItem*/ /// The item will be dropped below the index.
264
OnViewport /*QAbstractItemView::OnViewport*/ /// The item will be dropped onto a region of the viewport with no items if acceptDropsOnView is set.
267
explicit ItemModelBase( QObject *parent = 0 );
268
virtual ~ItemModelBase();
270
virtual const QMetaEnum columnMap() const { return QMetaEnum(); }
271
Project *project() const { return m_project; }
272
ScheduleManager *scheduleManager() const { return m_manager; }
273
bool isReadWrite() { return m_readWrite; }
274
void setReadOnly( int column, bool ro ) { m_columnROMap[ column ] = ro; }
275
/// Returns true if @p column has been set to ReadOnly.
276
bool isColumnReadOnly( int column ) const { return m_columnROMap.contains( column ) && m_columnROMap[ column ]; }
279
* Check if the @p data is allowed to be dropped on @p index,
280
* @p dropIndicatorPosition indicates position relative @p index.
282
* Base implementation checks flags and mimetypes.
284
virtual bool dropAllowed( const QModelIndex &index, int dropIndicatorPosition, const QMimeData *data );
286
/// Create the correct delegate for @p column. @p parent is the delegates parent widget.
287
/// If default should be used, return 0.
288
virtual QAbstractItemDelegate *createDelegate( int column, QWidget *parent ) const { Q_UNUSED(column); Q_UNUSED(parent); return 0; }
290
QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const;
291
bool setData( const QModelIndex &index, const QVariant &value, int role );
292
QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
294
/// Return the sortorder to be used for @p column
295
virtual int sortRole( int /*column*/ ) const { return Qt::DisplayRole; }
298
/// Connect to this signal if your model modifies data using undo commands.
299
void executeCommand( KUndo2Command* );
302
virtual void setProject( Project *project );
303
virtual void setScheduleManager( ScheduleManager *sm );
304
virtual void setReadWrite( bool rw ) { m_readWrite = rw; }
305
/// Reimplement if your model can be refreshed
306
virtual void refresh() {}
309
virtual void slotLayoutToBeChanged();
310
virtual void slotLayoutChanged();
312
void projectDeleted();
315
ScheduleManager *m_manager;
317
QMap<int, bool> m_columnROMap;
321
} // namespace KPlato