1 /* This file is part of the KDE project
2 Copyright (C) 2005 - 2007, 2011 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 KPTRESOURCEAPPOINTMENTSMODEL_H
21#define KPTRESOURCEAPPOINTMENTSMODEL_H
22
23#include "kplatomodels_export.h"
24
25#include <kptitemmodelbase.h>
26#include "kpteffortcostmap.h"
27
28
29namespace KPlato
30{
31
32class View;
33class Project;
34class Node;
35class Appointment;
36class AppointmentInterval;
37class Resource;
38class ResourceGroup;
39class ScheduleManager;
40class MainSchedule;
41class Calendar;
42
43/**
44 The ResourceAppointmentsItemModel organizes appointments
45 as hours booked per day (or week, month).
46 It handles both internal and external appointments.
47*/
48class KPLATOMODELS_EXPORT ResourceAppointmentsItemModel : public ItemModelBase
49{
50 Q_OBJECT
51public:
52 explicit ResourceAppointmentsItemModel( QObject *parent = 0 );
53 ~ResourceAppointmentsItemModel();
54
55 virtual void setProject( Project *project );
56 long id() const;
57
58 virtual Qt::ItemFlags flags( const QModelIndex & index ) const;
59
60 virtual QModelIndex parent( const QModelIndex & index ) const;
61 virtual QModelIndex index( int row, int column, const QModelIndex & parent = QModelIndex() ) const;
62 QModelIndex index( const ResourceGroup *group ) const;
63 QModelIndex index( const Resource *resource ) const;
64
65 virtual int columnCount( const QModelIndex & parent = QModelIndex() ) const;
66 virtual int rowCount( const QModelIndex & parent = QModelIndex() ) const;
67
68 virtual QVariant data( const QModelIndex & index, int role = Qt::DisplayRole ) const;
69 virtual bool setData( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole );
70
71
72 virtual QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
73
74
75 QObject *object( const QModelIndex &index ) const;
76 Node *node( const QModelIndex &index ) const;
77 Appointment *appointment( const QModelIndex &index ) const;
78 QModelIndex createAppointmentIndex( int row, int col, void *ptr ) const;
79 Appointment *externalAppointment( const QModelIndex &index ) const;
80 QModelIndex createExternalAppointmentIndex( int row, int col, void *ptr ) const;
81 Resource *resource( const QModelIndex &index ) const;
82 QModelIndex createResourceIndex( int row, int col, void *ptr ) const;
83 ResourceGroup *resourcegroup( const QModelIndex &index ) const;
84 QModelIndex createGroupIndex( int row, int col, void *ptr ) const;
85
86 void refresh();
87 void refreshData();
88
89 QDate startDate() const;
90 QDate endDate() const;
91
92 Resource *parent( const Appointment *a ) const;
93 int rowNumber( Resource *res, Appointment *a ) const;
94 void setShowInternalAppointments( bool show );
95 bool showInternalAppointments() const { return m_showInternal; }
96 void setShowExternalAppointments( bool show );
97 bool showExternalAppointments() const { return m_showExternal; }
98
99signals:
100 void refreshed();
101 void appointmentInserted( Resource*, Appointment* );
102
103public slots:
104 virtual void setScheduleManager( ScheduleManager *sm );
105
106protected slots:
107 void slotResourceChanged( Resource* );
108 void slotResourceGroupChanged( ResourceGroup * );
109 void slotResourceGroupToBeInserted( const ResourceGroup *group, int row );
110 void slotResourceGroupInserted( const ResourceGroup *group );
111 void slotResourceGroupToBeRemoved( const ResourceGroup *group );
112 void slotResourceGroupRemoved( const ResourceGroup *group );
113 void slotResourceToBeInserted( const ResourceGroup *group, int row );
114 void slotResourceInserted( const Resource *resource );
115 void slotResourceToBeRemoved( const Resource *resource );
116 void slotResourceRemoved( const Resource *resource );
117 void slotCalendarChanged( Calendar* cal );
118 void slotProjectCalculated( ScheduleManager *sm );
119
120 void slotAppointmentToBeInserted( Resource *r, int row );
121 void slotAppointmentInserted( Resource*, Appointment* );
122 void slotAppointmentToBeRemoved( Resource *r, int row );
123 void slotAppointmentRemoved();
124 void slotAppointmentChanged( Resource *r, Appointment *a );
125
126protected:
127 QVariant notUsed( const ResourceGroup *res, int role ) const;
128
129 QVariant name( const Resource *res, int role ) const;
130 QVariant name( const ResourceGroup *res, int role ) const;
131 QVariant name( const Node *node, int role ) const;
132 QVariant name( const Appointment *appointment, int role ) const;
133
134 QVariant total( const Resource *res, int role ) const;
135 QVariant total( const Resource *res, const QDate &date, int role ) const;
136 QVariant total( const Appointment *a, int role ) const;
137
138 QVariant assignment( const Appointment *a, const QDate &date, int role ) const;
139
140private:
141 int m_columnCount;
142 QMap<const Appointment*, EffortCostMap> m_effortMap;
143 QMap<const Appointment*, EffortCostMap> m_externalEffortMap;
144 QDate m_start;
145 QDate m_end;
146
147 ResourceGroup *m_group; // Used for sanity checks
148 Resource *m_resource; // Used for sanity checks
149
150 bool m_showInternal;
151 bool m_showExternal;
152};
153
154/**
155 The ResourceAppointmentsRowModel returns each appointment interval as a new row.
156*/
157class KPLATOMODELS_EXPORT ResourceAppointmentsRowModel : public ItemModelBase
158{
159 Q_OBJECT
160 Q_ENUMS( Properties )
161public:
162 enum Properties {
163 Name = 0,
164 Type,
165 StartTime,
166 EndTime,
167 Load
168 };
169 const QMetaEnum columnMap() const;
170
171 explicit ResourceAppointmentsRowModel( QObject *parent = 0 );
172 ~ResourceAppointmentsRowModel();
173
174 virtual void setProject( Project *project );
175 long id() const;
176
177 virtual int columnCount( const QModelIndex & parent = QModelIndex() ) const;
178 virtual int rowCount( const QModelIndex & parent = QModelIndex() ) const;
179
180 QModelIndex parent( const QModelIndex &idx = QModelIndex() ) const;
181 QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const;
182
183 virtual QVariant data( const QModelIndex & index, int role = Qt::DisplayRole ) const;
184 virtual QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
185
186 /// If @p index is a resource, return it's parent group, else 0
187 ResourceGroup *parentGroup( const QModelIndex &index ) const;
188 /// If @p idx is a resource group, return it, else 0
189 ResourceGroup *resourcegroup( const QModelIndex &idx ) const;
190 /// If @p idx is an appointment, return it's parent resource, else 0
191 Resource *parentResource( const QModelIndex &idx ) const;
192 /// If @p idx is a resource, return it, else 0
193 Resource *resource( const QModelIndex &idx ) const;
194 /// If @p idx is an appointment interval, return it's parent appointment, else 0
195 Appointment *parentAppointment( const QModelIndex &idx ) const;
196 /// If @p idx is an appointment, return it, else 0
197 Appointment *appointment( const QModelIndex &idx ) const;
198 /// If @p idx is an appointment interval, return it, else 0
199 AppointmentInterval *interval( const QModelIndex &idx ) const;
200
201 QModelIndex index( ResourceGroup *g ) const;
202 QModelIndex index( Resource *r ) const;
203 QModelIndex index( Appointment *a ) const;
204
205 /// If @p idx is an appointment, return the node, else 0
206 Node *node( const QModelIndex &idx ) const;
207
208 /// Return the sortorder to be used for @p column
209 virtual int sortRole( int column ) const;
210
211 class Private;
212
213public slots:
214 virtual void setScheduleManager( ScheduleManager *sm );
215
216protected slots:
217 void slotResourceToBeInserted( const ResourceGroup *group, int row );
218 void slotResourceInserted( const Resource *r );
219 void slotResourceToBeRemoved( const Resource *r );
220 void slotResourceRemoved( const Resource *resource );
221 void slotResourceGroupToBeInserted( const ResourceGroup *group, int row );
222 void slotResourceGroupInserted( const ResourceGroup* );
223 void slotResourceGroupToBeRemoved( const ResourceGroup *group );
224 void slotResourceGroupRemoved( const ResourceGroup *group );
225 void slotAppointmentToBeInserted( Resource *r, int row );
226 void slotAppointmentInserted( Resource *r, Appointment *a );
227 void slotAppointmentToBeRemoved( Resource *r, int row );
228 void slotAppointmentRemoved();
229 void slotAppointmentChanged( Resource *r, Appointment *a );
230 void slotProjectCalculated( ScheduleManager *sm );
231
232protected:
233 QModelIndex createGroupIndex( int row, int column, Project *project );
234 QModelIndex createResourceIndex( int row, int column, ResourceGroup *g );
235 QModelIndex createAppointmentIndex( int row, int column, Resource *r );
236 QModelIndex createIntervalIndex( int row, int column, Appointment *a );
237
238protected:
239 QMap<void*, Private*> m_datamap;
240 MainSchedule *m_schedule;
241};
242
243/**
244 The ResourceAppointmentsGanttModel specialized for use by KDGantt
245*/
246class KPLATOMODELS_EXPORT ResourceAppointmentsGanttModel : public ResourceAppointmentsRowModel
247{
248 Q_OBJECT
249public:
250 explicit ResourceAppointmentsGanttModel( QObject *parent = 0 );
251 ~ResourceAppointmentsGanttModel();
252
253 virtual QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const;
254
255protected:
256 QVariant data( const ResourceGroup *g, int column, int role = Qt::DisplayRole ) const;
257 QVariant data( const Resource *r, int column, int role = Qt::DisplayRole ) const;
258 QVariant data( const Appointment *a, int column, int role = Qt::DisplayRole ) const;
259 QVariant data( const AppointmentInterval *a, int column, int role = Qt::DisplayRole ) const;
260};
261
262} //KPlato namespace
263
264#endif // KPTRESOURCEAPPOINTMENTSMODEL_H
265