1/* This file is part of the KDE project
2 Copyright (C) 2007 Dag Andersen <danders@get2net.dk>
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 NODEITEMMODEL_H
21#define NODEITEMMODEL_H
22
23#include "kptitemmodelbase.h"
24#include "kptschedule.h"
25#include "kptworkpackagemodel.h"
26
27#include <QDate>
28#include <QMetaEnum>
29#include <QSortFilterProxyModel>
30#include <qstandarditemmodel.h>
31
32class KUndo2Command;
33class QMimeData;
34class QModelIndex;
35class QMimeData;
36
37namespace KPlato
38{
39
40class Project;
41class Node;
42class Estimate;
43
44class KPLATOMODELS_EXPORT NodeModel : public QObject
45{
46 Q_OBJECT
47 Q_ENUMS( Properties )
48public:
49 NodeModel();
50 ~NodeModel() {}
51
52 enum Properties {
53 NodeName = 0,
54 NodeType,
55 NodeResponsible,
56 NodeAllocation,
57 NodeEstimateType,
58 NodeEstimateCalendar,
59 NodeEstimate,
60 NodeOptimisticRatio,
61 NodePessimisticRatio,
62 NodeRisk,
63 NodeConstraint,
64 NodeConstraintStart,
65 NodeConstraintEnd,
66 NodeRunningAccount,
67 NodeStartupAccount,
68 NodeStartupCost,
69 NodeShutdownAccount,
70 NodeShutdownCost,
71 NodeDescription,
72
73 // Based on edited values
74 NodeExpected,
75 NodeVarianceEstimate,
76 NodeOptimistic,
77 NodePessimistic,
78
79 // After scheduling
80 NodeStartTime,
81 NodeEndTime,
82 NodeEarlyStart,
83 NodeEarlyFinish,
84 NodeLateStart,
85 NodeLateFinish,
86 NodePositiveFloat,
87 NodeFreeFloat,
88 NodeNegativeFloat,
89 NodeStartFloat,
90 NodeFinishFloat,
91 NodeAssignments,
92
93 // Based on scheduled values
94 NodeDuration,
95 NodeVarianceDuration,
96 NodeOptimisticDuration,
97 NodePessimisticDuration,
98
99 // Completion
100 NodeStatus,
101 NodeCompleted,
102 NodePlannedEffort,
103 NodeActualEffort,
104 NodeRemainingEffort,
105 NodePlannedCost,
106 NodeActualCost,
107 NodeActualStart,
108 NodeStarted,
109 NodeActualFinish,
110 NodeFinished,
111 NodeStatusNote,
112
113 // Scheduling errors
114 NodeSchedulingStatus,
115 NodeNotScheduled,
116 NodeAssignmentMissing,
117 NodeResourceOverbooked,
118 NodeResourceUnavailable,
119 NodeConstraintsError,
120 NodeEffortNotMet,
121 NodeSchedulingError,
122
123 NodeWBSCode,
124 NodeLevel,
125
126 // Performance
127 NodeBCWS,
128 NodeBCWP,
129 NodeACWP,
130 NodePerformanceIndex,
131 //
132 NodeCritical,
133 NodeCriticalPath,
134
135 // Info from latest work package transmission
136 WPOwnerName,
137 WPTransmitionStatus,
138 WPTransmitionTime
139 };
140 const QMetaEnum columnMap() const;
141
142 void setProject( Project *project );
143 void setManager( ScheduleManager *sm );
144 Project *project() const { return m_project; }
145 ScheduleManager *manager() const { return m_manager; }
146 long id() const { return m_manager == 0 ? -1 : m_manager->scheduleId(); }
147
148 QVariant data( const Node *node, int property, int role = Qt::DisplayRole ) const;
149 KUndo2Command *setData( Node *node, int property, const QVariant & value, int role = Qt::EditRole );
150
151 static QVariant headerData( int section, int role = Qt::DisplayRole );
152
153 int propertyCount() const;
154
155 void setNow( const QDate &now ) { m_now = now; }
156 QDate now() const { return m_now; }
157
158 QVariant name( const Node *node, int role ) const;
159 QVariant leader( const Node *node, int role ) const;
160 QVariant allocation( const Node *node, int role ) const;
161 QVariant description( const Node *node, int role ) const;
162 QVariant type( const Node *node, int role ) const;
163 QVariant constraint( const Node *node, int role ) const;
164 QVariant constraintStartTime( const Node *node, int role ) const;
165 QVariant constraintEndTime( const Node *node, int role ) const;
166 QVariant estimateType( const Node *node, int role ) const;
167 QVariant estimateCalendar( const Node *node, int role ) const;
168 QVariant estimate( const Node *node, int role ) const;
169 QVariant optimisticRatio( const Node *node, int role ) const;
170 QVariant pessimisticRatio( const Node *node, int role ) const;
171 QVariant riskType( const Node *node, int role ) const;
172 QVariant runningAccount( const Node *node, int role ) const;
173 QVariant startupAccount( const Node *node, int role ) const;
174 QVariant startupCost( const Node *node, int role ) const;
175 QVariant shutdownAccount( const Node *node, int role ) const;
176 QVariant shutdownCost( const Node *node, int role ) const;
177
178 QVariant startTime( const Node *node, int role ) const;
179 QVariant endTime( const Node *node, int role ) const;
180
181 QVariant duration( const Node *node, int role ) const;
182 QVariant varianceDuration( const Node *node, int role ) const;
183 QVariant varianceEstimate( const Estimate *est, int role ) const;
184 QVariant optimisticDuration( const Node *node, int role ) const;
185 QVariant optimisticEstimate( const Estimate *est, int role ) const;
186 QVariant pertExpected( const Estimate *est, int role ) const;
187 QVariant pessimisticDuration( const Node *node, int role ) const;
188 QVariant pessimisticEstimate( const Estimate *est, int role ) const;
189
190 QVariant earlyStart( const Node *node, int role ) const;
191 QVariant earlyFinish( const Node *node, int role ) const;
192 QVariant lateStart( const Node *node, int role ) const;
193 QVariant lateFinish( const Node *node, int role ) const;
194 QVariant positiveFloat( const Node *node, int role ) const;
195 QVariant freeFloat( const Node *node, int role ) const;
196 QVariant negativeFloat( const Node *node, int role ) const;
197 QVariant startFloat( const Node *node, int role ) const;
198 QVariant finishFloat( const Node *node, int role ) const;
199 QVariant assignedResources( const Node *node, int role ) const;
200
201 QVariant status( const Node *node, int role ) const;
202 QVariant completed( const Node *node, int role ) const;
203 QVariant startedTime( const Node *node, int role ) const;
204 QVariant isStarted( const Node *node, int role ) const;
205 QVariant finishedTime( const Node *node, int role ) const;
206 QVariant isFinished( const Node *node, int role ) const;
207 QVariant plannedEffortTo( const Node *node, int role ) const;
208 QVariant actualEffortTo( const Node *node, int role ) const;
209 QVariant remainingEffort( const Node *node, int role ) const;
210 QVariant plannedCostTo( const Node *node, int role ) const;
211 QVariant actualCostTo( const Node *node, int role ) const;
212 QVariant note( const Node *node, int role ) const;
213
214 /// The nodes scheduling status
215 QVariant nodeSchedulingStatus( const Node *node, int role ) const;
216 /// Set if the node has not been scheduled
217 QVariant nodeIsNotScheduled( const Node *node, int role ) const;
218 /// Set if EffortType == Effort, but no resource is requested
219 QVariant resourceIsMissing( const Node *node, int role ) const;
220 /// Set if the assigned resource is overbooked
221 QVariant resourceIsOverbooked( const Node *node, int role ) const;
222 /// Set if the requested resource is not available
223 QVariant resourceIsNotAvailable( const Node *node, int role ) const;
224 /// Set if the task cannot be scheduled to fullfil all the constraints
225 QVariant schedulingConstraintsError( const Node *node, int role ) const;
226 /// Resources could not fullfil estimate
227 QVariant effortNotMet( const Node *node, int role ) const;
228 /// Other scheduling error occurred
229 QVariant schedulingError( const Node *node, int role ) const;
230
231 QVariant wbsCode( const Node *node, int role ) const;
232 QVariant nodeLevel( const Node *node, int role ) const;
233
234 QVariant nodeBCWS( const Node *node, int role ) const;
235 QVariant nodeBCWP( const Node *node, int role ) const;
236 QVariant nodeACWP( const Node *node, int role ) const;
237 QVariant nodePerformanceIndex( const Node *node, int role ) const;
238
239 QVariant nodeIsCritical( const Node *node, int role ) const;
240 QVariant nodeInCriticalPath( const Node *node, int role ) const;
241
242 QVariant wpOwnerName( const Node *node, int role ) const;
243 QVariant wpTransmitionStatus( const Node *node, int role ) const;
244 QVariant wpTransmitionTime( const Node *node, int role ) const;
245
246 KUndo2Command *setName( Node *node, const QVariant &value, int role );
247 KUndo2Command *setLeader( Node *node, const QVariant &value, int role );
248 KUndo2Command *setAllocation( Node *node, const QVariant &value, int role );
249 KUndo2Command *setDescription( Node *node, const QVariant &value, int role );
250 KUndo2Command *setType( Node *node, const QVariant &value, int role );
251 KUndo2Command *setConstraint( Node *node, const QVariant &value, int role );
252 KUndo2Command *setConstraintStartTime( Node *node, const QVariant &value, int role );
253 KUndo2Command *setConstraintEndTime( Node *node, const QVariant &value, int role );
254 KUndo2Command *setEstimateType( Node *node, const QVariant &value, int role );
255 KUndo2Command *setEstimateCalendar( Node *node, const QVariant &value, int role );
256 KUndo2Command *setEstimate( Node *node, const QVariant &value, int role );
257 KUndo2Command *setOptimisticRatio( Node *node, const QVariant &value, int role );
258 KUndo2Command *setPessimisticRatio( Node *node, const QVariant &value, int role );
259 KUndo2Command *setRiskType( Node *node, const QVariant &value, int role );
260 KUndo2Command *setRunningAccount( Node *node, const QVariant &value, int role );
261 KUndo2Command *setStartupAccount( Node *node, const QVariant &value, int role );
262 KUndo2Command *setStartupCost( Node *node, const QVariant &value, int role );
263 KUndo2Command *setShutdownAccount( Node *node, const QVariant &value, int role );
264 KUndo2Command *setShutdownCost( Node *node, const QVariant &value, int role );
265 KUndo2Command *setCompletion( Node *node, const QVariant &value, int role );
266 KUndo2Command *setActualEffort( Node *node, const QVariant &value, int role );
267 KUndo2Command *setRemainingEffort( Node *node, const QVariant &value, int role );
268 KUndo2Command *setStartedTime( Node *node, const QVariant &value, int role );
269 KUndo2Command *setFinishedTime( Node *node, const QVariant &value, int role );
270
271private:
272 Project *m_project;
273 ScheduleManager *m_manager;
274 QDate m_now;
275 int m_prec;
276};
277
278class KPLATOMODELS_EXPORT NodeItemModel : public ItemModelBase
279{
280 Q_OBJECT
281public:
282 explicit NodeItemModel( QObject *parent = 0 );
283 ~NodeItemModel();
284
285 /// Returns a column number/- name map for this model
286 virtual const QMetaEnum columnMap() const { return m_nodemodel.columnMap(); }
287
288 ScheduleManager *manager() const { return m_nodemodel.manager(); }
289 long id() const { return m_nodemodel.id(); }
290
291 virtual Qt::ItemFlags flags( const QModelIndex & index ) const;
292
293 virtual QModelIndex parent( const QModelIndex & index ) const;
294 virtual QModelIndex index( int row, int column, const QModelIndex & parent = QModelIndex() ) const;
295 virtual QModelIndex index( const Node *node, int column = 0 ) const;
296
297 virtual int columnCount( const QModelIndex & parent = QModelIndex() ) const;
298 virtual int rowCount( const QModelIndex & parent = QModelIndex() ) const;
299
300 virtual QVariant data( const QModelIndex & index, int role = Qt::DisplayRole ) const;
301 virtual bool setData( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole );
302
303
304 virtual QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
305
306 virtual QMimeData * mimeData( const QModelIndexList & indexes ) const;
307 virtual QStringList mimeTypes () const;
308 virtual Qt::DropActions supportedDropActions() const;
309 virtual bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent );
310
311 Node *node( const QModelIndex &index ) const;
312 QAbstractItemDelegate *createDelegate( int column, QWidget *parent ) const;
313
314 QModelIndex insertTask( Node *node, Node *after );
315 QModelIndex insertSubtask( Node *node, Node *parent );
316
317 QList<Node*> nodeList( QDataStream &stream );
318 QList<Resource*> resourceList( QDataStream &stream );
319 static QList<Node*> removeChildNodes( const QList<Node*> &nodes );
320 bool dropAllowed( Node *on, const QMimeData *data );
321
322 virtual bool dropAllowed( const QModelIndex &index, int dropIndicatorPosition, const QMimeData *data );
323
324 bool projectShown() const { return m_projectshown; }
325
326 /// Return the sortorder to be used for @p column
327 virtual int sortRole( int column ) const;
328
329signals:
330 void nodeInserted( Node *node );
331 void projectShownChanged( bool );
332
333public slots:
334 virtual void setProject( Project *project );
335 virtual void setScheduleManager( ScheduleManager *sm );
336 void setShowProject( bool on );
337
338protected slots:
339 virtual void slotWbsDefinitionChanged();
340 virtual void slotNodeChanged( Node* );
341 virtual void slotNodeToBeInserted( Node *node, int row );
342 virtual void slotNodeInserted( Node *node );
343 virtual void slotNodeToBeRemoved( Node *node );
344 virtual void slotNodeRemoved( Node *node );
345
346 virtual void slotNodeToBeMoved( Node *node, int pos, Node *newParent, int newPos );
347 virtual void slotNodeMoved( Node *node );
348
349 virtual void slotLayoutChanged();
350 virtual void slotProjectCalulated( ScheduleManager *sm );
351
352protected:
353 virtual bool setType( Node *node, const QVariant &value, int role );
354 bool setCompletion( Node *node, const QVariant &value, int role );
355 bool setAllocation( Node *node, const QVariant &value, int role );
356
357 bool dropResourceMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent );
358 bool dropProjectMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent );
359 KUndo2Command *createAllocationCommand( Task &task, const QList<Resource*> &lst );
360 bool dropUrlMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent );
361 bool importProjectFile( const KUrl &url, Qt::DropAction action, int row, int column, const QModelIndex &parent );
362
363protected:
364 Node *m_node; // for sanety check
365 NodeModel m_nodemodel;
366 bool m_projectshown;
367};
368
369//--------------------------------------
370class KPLATOMODELS_EXPORT GanttItemModel : public NodeItemModel
371{
372 Q_OBJECT
373public:
374 enum GanttModelRoles { SpecialItemTypeRole = Qt::UserRole + 123 }; //FIXME
375
376 explicit GanttItemModel(QObject *parent = 0);
377 ~GanttItemModel();
378
379 virtual int rowCount( const QModelIndex &parent ) const;
380 using NodeItemModel::index;
381 virtual QModelIndex index( int row, int column, const QModelIndex &parent ) const;
382 QModelIndex parent( const QModelIndex &idx ) const;
383 virtual QVariant data( const QModelIndex & index, int role = Qt::DisplayRole ) const;
384
385 void setShowSpecial( bool on ) { m_showSpecial = on; }
386 bool showSpecial() const { return m_showSpecial; }
387
388private:
389 bool m_showSpecial;
390 QMap<Node*, void*> parentmap;
391};
392
393// TODO: Rename, this is now a flat node item model
394class KPLATOMODELS_EXPORT MilestoneItemModel : public ItemModelBase
395{
396 Q_OBJECT
397public:
398 explicit MilestoneItemModel( QObject *parent = 0 );
399 ~MilestoneItemModel();
400
401 /// Returns a column number/- name map for this model
402 virtual const QMetaEnum columnMap() const { return m_nodemodel.columnMap(); }
403
404 ScheduleManager *manager() const { return m_nodemodel.manager(); }
405 long id() const { return m_nodemodel.id(); }
406
407 virtual Qt::ItemFlags flags( const QModelIndex & index ) const;
408
409 virtual QModelIndex parent( const QModelIndex & index ) const;
410 virtual QModelIndex index( int row, int column, const QModelIndex & parent = QModelIndex() ) const;
411 virtual QModelIndex index( const Node *node ) const;
412
413 virtual int columnCount( const QModelIndex & parent = QModelIndex() ) const;
414 virtual int rowCount( const QModelIndex & parent = QModelIndex() ) const;
415
416 virtual QVariant data( const QModelIndex & index, int role = Qt::DisplayRole ) const;
417 virtual bool setData( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole );
418
419
420 virtual QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
421
422 virtual QMimeData * mimeData( const QModelIndexList & indexes ) const;
423 virtual QStringList mimeTypes () const;
424 virtual Qt::DropActions supportedDropActions() const;
425 virtual bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent );
426
427 Node *node( const QModelIndex &index ) const;
428 virtual QAbstractItemDelegate *createDelegate( int column, QWidget *parent ) const;
429
430 QModelIndex insertTask( Node *node, Node *after );
431 QModelIndex insertSubtask( Node *node, Node *parent );
432
433 QList<Node*> nodeList( QDataStream &stream );
434 static QList<Node*> removeChildNodes( const QList<Node*> &nodes );
435 bool dropAllowed( Node *on, const QMimeData *data );
436
437 virtual bool dropAllowed( const QModelIndex &index, int dropIndicatorPosition, const QMimeData *data );
438
439 QList<Node*> mileStones() const;
440
441public slots:
442 virtual void setProject( Project *project );
443 virtual void setScheduleManager( ScheduleManager *sm );
444
445protected slots:
446 void slotNodeChanged( Node* );
447 void slotNodeToBeInserted( Node *node, int row );
448 void slotNodeInserted( Node *node );
449 void slotNodeToBeRemoved( Node *node );
450 void slotNodeRemoved( Node *node );
451 void slotNodeToBeMoved( Node *node, int pos, Node *newParent, int newPos );
452 void slotNodeMoved( Node *node );
453
454 void slotLayoutChanged();
455 void slotWbsDefinitionChanged();
456
457protected:
458 bool resetData();
459 void resetModel();
460
461private:
462 NodeModel m_nodemodel;
463 QMap<QString, Node*> m_nodemap;
464};
465
466class KPLATOMODELS_EXPORT NodeSortFilterProxyModel : public QSortFilterProxyModel
467{
468 Q_OBJECT
469public:
470 NodeSortFilterProxyModel( ItemModelBase* model, QObject *parent, bool filterUnscheduled = true );
471
472 ItemModelBase *itemModel() const;
473 void setFilterUnscheduled( bool on );
474 bool filterUnscheduled() const { return m_filterUnscheduled; }
475
476protected:
477 bool filterAcceptsRow ( int source_row, const QModelIndex & source_parent ) const;
478
479private:
480 NodeItemModel *m_model;
481 bool m_filterUnscheduled;
482};
483
484class KPLATOMODELS_EXPORT TaskModuleModel : public QAbstractItemModel
485{
486 Q_OBJECT
487public:
488 explicit TaskModuleModel(QObject *parent = 0);
489
490 void addTaskModule( Project *project );
491
492 Qt::ItemFlags flags( const QModelIndex &idx ) const;
493 int columnCount( const QModelIndex &idx = QModelIndex() ) const;
494 int rowCount( const QModelIndex &idx = QModelIndex() ) const;
495 QVariant data( const QModelIndex &idx, int role = Qt::DisplayRole ) const;
496 QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
497 QModelIndex parent( const QModelIndex &idx ) const;
498 QModelIndex index( int row, int column, const QModelIndex &parent ) const;
499 QStringList mimeTypes() const;
500 bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent );
501 QMimeData *mimeData( const QModelIndexList &idx ) const;
502
503 bool importProject( const KUrl &url, bool emitsignal = true );
504
505public slots:
506 void loadTaskModules( const QStringList &files );
507
508signals:
509 void executeCommand( KUndo2Command *cmd );
510 void saveTaskModule( const KUrl &url, Project *project );
511 void removeTaskModule( const KUrl &url );
512
513protected:
514 void stripProject( Project *project ) const;
515
516private:
517 QList<Project*> m_modules;
518};
519
520} //namespace KPlato
521
522#endif //NODEITEMMODEL_H
523