~ubuntu-branches/debian/sid/calligraplan/sid

« back to all changes in this revision

Viewing changes to src/libs/models/kptitemmodelbase.h

  • Committer: Package Import Robot
  • Author(s): Pino Toscano
  • Date: 2018-02-01 18:20:19 UTC
  • Revision ID: package-import@ubuntu.com-20180201182019-1qo7qaim5wejm5k9
Tags: upstream-3.1.0
ImportĀ upstreamĀ versionĀ 3.1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
32
#include <KoXmlReaderForward.h>
 
33
 
 
34
class KUndo2Command;
 
35
 
 
36
 
 
37
/// The main namespace
 
38
namespace KPlato
 
39
{
 
40
 
 
41
class Project;
 
42
class ScheduleManager;
 
43
 
 
44
/// Namespace for item delegate specific enums
 
45
namespace Delegate
 
46
{
 
47
    /// For selector delegate
 
48
    enum EditorType { EnumEditor, TimeEditor };
 
49
    /// Controls action when editor is closed. See QAbstractItemDelegate::EndEditHint.
 
50
    enum EndEditHint { 
 
51
        NoHint = QAbstractItemDelegate::NoHint,
 
52
        EditNextItem = QAbstractItemDelegate::EditNextItem,
 
53
        EditPreviousItem = QAbstractItemDelegate::EditPreviousItem,
 
54
        SubmitModelCache = QAbstractItemDelegate::SubmitModelCache,
 
55
        RevertModelCache = QAbstractItemDelegate::RevertModelCache,
 
56
        EditLeftItem = 100,
 
57
        EditRightItem = 101,
 
58
        EditDownItem = 102,
 
59
        EditUpItem = 103
 
60
    };
 
61
}
 
62
 
 
63
/// ItemDelegate implements improved control over closeEditor
 
64
class KPLATOMODELS_EXPORT ItemDelegate : public QStyledItemDelegate
 
65
{
 
66
    Q_OBJECT
 
67
public:
 
68
    /// Constructor
 
69
    explicit ItemDelegate(QObject *parent = 0)
 
70
    : QStyledItemDelegate( parent ),
 
71
    m_lastHint( Delegate::NoHint )
 
72
    {}
 
73
    
 
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;
 
78
 
 
79
protected:
 
80
    /// Implements arrow key navigation
 
81
    bool eventFilter(QObject *object, QEvent *event);
 
82
    /// Draw custom focus
 
83
//    virtual void drawFocus(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect ) const;
 
84
    
 
85
private:
 
86
    Delegate::EndEditHint m_lastHint;
 
87
};
 
88
 
 
89
class KPLATOMODELS_EXPORT CheckStateItemDelegate : public ItemDelegate
 
90
{
 
91
    Q_OBJECT
 
92
public:
 
93
    explicit CheckStateItemDelegate(QObject *parent = 0);
 
94
 
 
95
protected:
 
96
    bool editorEvent( QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index );
 
97
};
 
98
 
 
99
class KPLATOMODELS_EXPORT DateTimeCalendarDelegate : public ItemDelegate
 
100
{
 
101
  Q_OBJECT
 
102
public:
 
103
    explicit DateTimeCalendarDelegate(QObject *parent = 0);
 
104
 
 
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;
 
109
 
 
110
};
 
111
 
 
112
class KPLATOMODELS_EXPORT ProgressBarDelegate : public ItemDelegate
 
113
{
 
114
  Q_OBJECT
 
115
public:
 
116
    explicit ProgressBarDelegate(QObject *parent = 0);
 
117
 
 
118
    ~ProgressBarDelegate();
 
119
 
 
120
    void paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const;
 
121
    QSize sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const;
 
122
 
 
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;
 
127
 
 
128
protected:
 
129
    void initStyleOptionProgressBar( QStyleOptionProgressBar *option, const QModelIndex &index ) const;
 
130
 
 
131
};
 
132
 
 
133
class Slider : public QSlider {
 
134
    Q_OBJECT
 
135
public:
 
136
    explicit Slider( QWidget *parent );
 
137
private Q_SLOTS:
 
138
    void updateTip( int value );
 
139
};
 
140
 
 
141
class KPLATOMODELS_EXPORT SelectorDelegate : public ItemDelegate
 
142
{
 
143
    Q_OBJECT
 
144
public:
 
145
    explicit SelectorDelegate(QObject *parent = 0);
 
146
 
 
147
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
 
148
 
 
149
    void setEditorData(QWidget *editor, const QModelIndex &index) const;
 
150
    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
 
151
 
 
152
    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
 
153
};
 
154
 
 
155
class KPLATOMODELS_EXPORT EnumDelegate : public ItemDelegate
 
156
{
 
157
    Q_OBJECT
 
158
public:
 
159
    explicit EnumDelegate(QObject *parent = 0);
 
160
 
 
161
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
 
162
 
 
163
    void setEditorData(QWidget *editor, const QModelIndex &index) const;
 
164
    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
 
165
 
 
166
    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
 
167
};
 
168
 
 
169
//------------------------------------
 
170
class KPLATOMODELS_EXPORT RequieredResourceDelegate : public ItemDelegate
 
171
{
 
172
    Q_OBJECT
 
173
public:
 
174
    explicit RequieredResourceDelegate(QObject *parent = 0);
 
175
 
 
176
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
 
177
 
 
178
    void setEditorData(QWidget *editor, const QModelIndex &index) const;
 
179
    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
 
180
 
 
181
    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
 
182
};
 
183
 
 
184
 
 
185
class KPLATOMODELS_EXPORT DurationSpinBoxDelegate : public ItemDelegate
 
186
{
 
187
    Q_OBJECT
 
188
public:
 
189
    explicit DurationSpinBoxDelegate(QObject *parent = 0);
 
190
 
 
191
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
 
192
 
 
193
    void setEditorData(QWidget *editor, const QModelIndex &index) const;
 
194
    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
 
195
 
 
196
    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
 
197
};
 
198
 
 
199
class KPLATOMODELS_EXPORT SpinBoxDelegate : public ItemDelegate
 
200
{
 
201
    Q_OBJECT
 
202
public:
 
203
    explicit SpinBoxDelegate(QObject *parent = 0);
 
204
 
 
205
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
 
206
 
 
207
    void setEditorData(QWidget *editor, const QModelIndex &index) const;
 
208
    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
 
209
 
 
210
    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
 
211
};
 
212
 
 
213
class KPLATOMODELS_EXPORT DoubleSpinBoxDelegate : public ItemDelegate
 
214
{
 
215
    Q_OBJECT
 
216
public:
 
217
    explicit DoubleSpinBoxDelegate(QObject *parent = 0);
 
218
 
 
219
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
 
220
 
 
221
    void setEditorData(QWidget *editor, const QModelIndex &index) const;
 
222
    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
 
223
 
 
224
    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
 
225
};
 
226
 
 
227
class KPLATOMODELS_EXPORT MoneyDelegate : public ItemDelegate
 
228
{
 
229
    Q_OBJECT
 
230
public:
 
231
    explicit MoneyDelegate(QObject *parent = 0);
 
232
 
 
233
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
 
234
 
 
235
    void setEditorData(QWidget *editor, const QModelIndex &index) const;
 
236
    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
 
237
 
 
238
    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
 
239
};
 
240
 
 
241
class KPLATOMODELS_EXPORT TimeDelegate : public ItemDelegate
 
242
{
 
243
    Q_OBJECT
 
244
public:
 
245
    explicit TimeDelegate(QObject *parent = 0);
 
246
 
 
247
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
 
248
 
 
249
    void setEditorData(QWidget *editor, const QModelIndex &index) const;
 
250
    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
 
251
 
 
252
    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
 
253
};
 
254
 
 
255
class KPLATOMODELS_EXPORT ItemModelBase : public QAbstractItemModel
 
256
{
 
257
    Q_OBJECT
 
258
public:
 
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.
 
265
    };
 
266
 
 
267
    explicit ItemModelBase( QObject *parent = 0 );
 
268
    virtual ~ItemModelBase();
 
269
 
 
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 ]; }
 
277
 
 
278
    /**
 
279
     * Check if the @p data is allowed to be dropped on @p index,
 
280
     * @p dropIndicatorPosition indicates position relative @p index.
 
281
     *
 
282
     * Base implementation checks flags and mimetypes.
 
283
     */
 
284
    virtual bool dropAllowed( const QModelIndex &index, int dropIndicatorPosition, const QMimeData *data );
 
285
    
 
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; }
 
289
 
 
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;
 
293
 
 
294
    /// Return the sortorder to be used for @p column
 
295
    virtual int sortRole( int /*column*/ ) const { return Qt::DisplayRole; }
 
296
 
 
297
Q_SIGNALS:
 
298
    /// Connect to this signal if your model modifies data using undo commands.
 
299
    void executeCommand( KUndo2Command* );
 
300
    
 
301
public Q_SLOTS:
 
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() {}
 
307
 
 
308
protected Q_SLOTS:
 
309
    virtual void slotLayoutToBeChanged();
 
310
    virtual void slotLayoutChanged();
 
311
 
 
312
    void projectDeleted();
 
313
protected:
 
314
    Project *m_project;
 
315
    ScheduleManager *m_manager;
 
316
    bool m_readWrite;
 
317
    QMap<int, bool> m_columnROMap;
 
318
};
 
319
 
 
320
 
 
321
} // namespace KPlato
 
322
 
 
323
#endif