1/* This file is part of the KDE project
2 Copyright (C) 2007 Dag Andersen <danders@get2net>
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 KPTRESOURCEMODEL_H
21#define KPTRESOURCEMODEL_H
22
23#include "kplatomodels_export.h"
24
25#include <kptitemmodelbase.h>
26
27#include <QSortFilterProxyModel>
28#include <QMetaEnum>
29
30class QByteArray;
31class QPoint;
32
33namespace KIO {
34 class Job;
35}
36class KJob;
37
38namespace KPlato
39{
40
41class Project;
42class Resource;
43class ResourceGroup;
44class Calendar;
45class Task;
46class Node;
47
48class KPLATOMODELS_EXPORT ResourceModel : public QObject
49{
50 Q_OBJECT
51 Q_ENUMS( Properties )
52public:
53 explicit ResourceModel( QObject *parent = 0 );
54 ~ResourceModel();
55
56 enum Properties {
57 ResourceName = 0,
58 ResourceType,
59 ResourceInitials,
60 ResourceEmail,
61 ResourceCalendar,
62 ResourceLimit,
63 ResourceAvailableFrom,
64 ResourceAvailableUntil,
65 ResourceNormalRate,
66 ResourceOvertimeRate,
67 ResourceAccount
68 };
69
70 const QMetaEnum columnMap() const;
71 void setProject( Project *project );
72 int propertyCount() const;
73 QVariant data( const Resource *resource, int property, int role = Qt::DisplayRole ) const;
74 QVariant data( const ResourceGroup *group, int property, int role = Qt::DisplayRole ) const;
75 static QVariant headerData( int section, int role = Qt::DisplayRole );
76
77 QVariant name( const Resource *res, int role ) const;
78 QVariant type( const Resource *res, int role ) const;
79 QVariant initials( const Resource *res, int role ) const;
80 QVariant email( const Resource *res, int role ) const;
81 QVariant calendar( const Resource *res, int role ) const;
82 QVariant units( const Resource *res, int role ) const;
83 QVariant availableFrom( const Resource *res, int role ) const;
84 QVariant availableUntil( const Resource *res, int role ) const;
85 QVariant normalRate( const Resource *res, int role ) const;
86 QVariant overtimeRate( const Resource *res, int role ) const;
87 QVariant account( const Resource *res, int role ) const;
88
89 QVariant name( const ResourceGroup *res, int role ) const;
90 QVariant type( const ResourceGroup *res, int role ) const;
91
92private:
93 Project *m_project;
94};
95
96class KPLATOMODELS_EXPORT ResourceItemModel : public ItemModelBase
97{
98 Q_OBJECT
99public:
100 explicit ResourceItemModel( QObject *parent = 0 );
101 ~ResourceItemModel();
102
103 virtual const QMetaEnum columnMap() const { return m_model.columnMap(); }
104
105 virtual void setProject( Project *project );
106
107 virtual Qt::ItemFlags flags( const QModelIndex & index ) const;
108
109 virtual QModelIndex parent( const QModelIndex & index ) const;
110 virtual QModelIndex index( int row, int column, const QModelIndex & parent = QModelIndex() ) const;
111 QModelIndex index( const ResourceGroup *group, int column = 0 ) const;
112 QModelIndex index( const Resource *resource, int column = 0 ) const;
113
114 virtual int columnCount( const QModelIndex & parent = QModelIndex() ) const;
115 virtual int rowCount( const QModelIndex & parent = QModelIndex() ) const;
116
117 virtual QVariant data( const QModelIndex & index, int role = Qt::DisplayRole ) const;
118 virtual bool setData( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole );
119
120
121 virtual QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
122
123 virtual QStringList mimeTypes () const;
124 virtual Qt::DropActions supportedDropActions() const;
125 virtual bool dropAllowed( const QModelIndex &index, int dropIndicatorPosition, const QMimeData *data );
126 virtual bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent );
127 QMimeData *mimeData( const QModelIndexList & indexes ) const;
128
129 QAbstractItemDelegate *createDelegate( int col, QWidget *parent ) const;
130
131 QObject *object( const QModelIndex &index ) const;
132 ResourceGroup *group( const QModelIndex &index ) const;
133 Resource *resource( const QModelIndex &index ) const;
134 QModelIndex insertGroup( ResourceGroup *g );
135 QModelIndex insertResource( ResourceGroup *g, Resource *r, Resource *after = 0 );
136
137 virtual int sortRole( int column ) const;
138
139protected slots:
140 void slotResourceChanged( Resource* );
141 void slotResourceGroupChanged( ResourceGroup * );
142 void slotResourceGroupToBeInserted( const ResourceGroup *group, int row );
143 void slotResourceGroupInserted( const ResourceGroup *group );
144 void slotResourceGroupToBeRemoved( const ResourceGroup *group );
145 void slotResourceGroupRemoved( const ResourceGroup *group );
146 void slotResourceToBeInserted( const ResourceGroup *group, int row );
147 void slotResourceInserted( const Resource *resource );
148 void slotResourceToBeRemoved( const Resource *resource );
149 void slotResourceRemoved( const Resource *resource );
150 void slotCalendarChanged( Calendar* cal );
151 void slotLayoutChanged();
152
153 void slotDataArrived( KIO::Job *job, const QByteArray &data );
154 void slotJobFinished( KJob *job );
155
156protected:
157 QVariant notUsed( const ResourceGroup *res, int role ) const;
158
159 QVariant name( const ResourceGroup *res, int role ) const;
160 bool setName( Resource *res, const QVariant &value, int role );
161 bool setName( ResourceGroup *res, const QVariant &value, int role );
162
163 QVariant type( const ResourceGroup *res, int role ) const;
164 bool setType( Resource *res, const QVariant &value, int role );
165 bool setType( ResourceGroup *res, const QVariant &value, int role );
166
167 bool setInitials( Resource *res, const QVariant &value, int role );
168 bool setEmail( Resource *res, const QVariant &value, int role );
169 bool setCalendar( Resource *res, const QVariant &value, int role );
170 bool setUnits( Resource *res, const QVariant &value, int role );
171 bool setAvailableFrom( Resource *res, const QVariant &value, int role );
172 bool setAvailableUntil( Resource *res, const QVariant &value, int role );
173 bool setNormalRate( Resource *res, const QVariant &value, int role );
174 bool setOvertimeRate( Resource *res, const QVariant &value, int role );
175 bool setAccount( Resource *res, const QVariant &value, int role );
176
177 QList<Resource*> resourceList( QDataStream &stream );
178
179 bool createResources( ResourceGroup *group, const QByteArray &data );
180
181private:
182 ResourceGroup *m_group; // Used for sanity checks
183 Resource *m_resource; // Used for sanity checks
184 ResourceModel m_model;
185
186 struct DropData {
187 Qt::DropAction action;
188 int row;
189 int column;
190 QModelIndex parent;
191 QByteArray data;
192 } m_dropData;
193 QMap<KJob*, DropData> m_dropDataMap;
194};
195
196class KPLATOMODELS_EXPORT ResourceItemSFModel : public QSortFilterProxyModel
197{
198 Q_OBJECT
199public:
200 explicit ResourceItemSFModel(QObject *parent = 0);
201
202 void setProject( Project *project );
203 Resource *resource( const QModelIndex &index ) const;
204 using QAbstractProxyModel::index;
205 QModelIndex index( Resource *r ) const;
206
207 Qt::ItemFlags flags( const QModelIndex & index ) const;
208
209 void addFilteredResource( const Resource *r );
210protected:
211 bool filterAcceptsRow( int source_row, const QModelIndex & source_parent ) const;
212
213 QList<const Resource*> m_filteredResources;
214};
215
216class KPLATOMODELS_EXPORT AllocatedResourceItemModel : public QSortFilterProxyModel
217{
218 Q_OBJECT
219public:
220 explicit AllocatedResourceItemModel(QObject *parent = 0);
221
222 int columnCount( const QModelIndex &idx ) const;
223
224 Project *project() const;
225 Task *task() const;
226 Resource *resource( const QModelIndex &index ) const;
227 using QAbstractProxyModel::index;
228 QModelIndex index( Resource *r ) const;
229
230 Qt::ItemFlags flags( const QModelIndex & index ) const;
231 QVariant headerData( int section, Qt::Orientation orientation, int role ) const;
232 QVariant data( const QModelIndex &idx, int role ) const;
233
234public slots:
235 void setProject( Project *project );
236 void setTask( Task *task );
237
238signals:
239 void expandAll();
240 void resizeColumnToContents( int );
241
242protected slots:
243 void slotNodeChanged( Node *n );
244
245protected:
246 bool filterAcceptsRow( int source_row, const QModelIndex & source_parent ) const;
247 void reset();
248 QObject *object( const QModelIndex &idx ) const;
249
250 QVariant allocation( const Resource *r, int role ) const;
251 QVariant allocation( const ResourceGroup *g, int role ) const;
252
253 Task *m_task;
254};
255
256} //KPlato namespace
257
258#endif
259