1 /* This file is part of the Calligra project
2  * Copyright (C) 2006 Sebastian Sauer <mail@dipe.org>
3  * Copyright (c) 2008, 2011 Dag Andersen <danders@get2net>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB.  If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 #ifndef SCRIPTING_PROJECT_H
22 #define SCRIPTING_PROJECT_H
23 
24 #include <QObject>
25 #include <QMap>
26 #include <QVariant>
27 #include <QVariantList>
28 
29 #include "Module.h"
30 #include "Node.h"
31 
32 #include "kptproject.h"
33 #include "kptnodeitemmodel.h"
34 #include "kptresourcemodel.h"
35 #include "kptaccountsmodel.h"
36 #include "kptcalendarmodel.h"
37 
38 namespace KPlato {
39     class Account;
40     class Calendar;
41     class Project;
42     class Node;
43     class ResourceGroup;
44     class Resource;
45     class ScheduleManager;
46     class MacroCommand;
47 }
48 
49 namespace Scripting {
50     class Account;
51     class Calendar;
52     class ResourceGroup;
53     class Resource;
54     class Schedule;
55 
56     /**
57     * The Project class represents a KPlato project.
58     */
59     class Project : public Node
60     {
61             Q_OBJECT
62         public:
63             enum Roles {
64                 DisplayRole = Qt::DisplayRole,
65                 DecorationRole = Qt::DecorationRole,
66                 EditRole = Qt::EditRole,
67                 ToolTipRole = Qt::ToolTipRole,
68                 StatusTipRole = Qt::StatusTipRole,
69                 WhatsThisRole = Qt::WhatsThisRole,
70                 SizeHintRole = Qt::SizeHintRole,
71                 FontRole = Qt::FontRole,
72                 TextAlignmentRole = Qt::TextAlignmentRole,
73                 BackgroundRole = Qt::BackgroundRole,
74                 BackgroundColorRole = Qt::BackgroundColorRole,
75                 ForegroundRole = Qt::ForegroundRole,
76                 TextColorRole = Qt::TextColorRole,
77                 CheckStateRole = Qt::CheckStateRole,
78                 AccessibleTextRole = Qt::AccessibleTextRole,
79                 AccessibleDescriptionRole = Qt::AccessibleDescriptionRole
80             };
81             Q_ENUM(Roles)
82 
83             Project(Module* module, KPlato::Project *project);
84             virtual ~Project();
85 
kplatoProject()86             KPlato::Project *kplatoProject() const { return static_cast<KPlato::Project*>(m_node); }
87             void addCommand(KUndo2Command *cmd);
88 
89         public Q_SLOTS:
90             /// Return the default calendar, 0 if no default calendar is set
91             QObject *defaultCalendar();
92             /// Set default calendar to @p calendar
93             void setDefaultCalendar(Calendar *calendar);
94 
95             /// Return data, default role and schedule
96             QVariant data(QObject *object, const QString &property);
97             /// Return data
98             QVariant data(QObject *object, const QString &property, const QString &role, qlonglong scheduleId = -1);
99             /// Set the @p object @p property to @p data using @p role. Default @p role is Qt::EditRole
100             /// Returns a string as result of the operation:
101             /// 'Success'   The operation succeeded
102             /// 'ReadOnly'  The @p property for this @p object is not editable
103             /// 'Invalid'   Invalid indata (e.g. unknown @p object or the @p object does not belong to this project)
104             /// 'Error'     Setting data failed for some other reason
105             QVariant setData(QObject *object, const QString &property, const QVariant &data, const QString &role = "EditRole");
106             /// Return header text
107             QVariant headerData(int objectType, const QString &property, const QString &role = "DisplayRole");
108 
109             /// Return number of schedule managers
110             int scheduleCount() const;
111             /// Return schedule manager at @p index
112             QObject *scheduleAt(int index);
113 
114             /// Returns the names of all task properties
115             QStringList taskPropertyList();
116 
117             /// Returns task header data for @p property
118             QVariant taskHeaderData(const QString &property, const QString &role = "DisplayRole");
119 
120             /// Number of tasks
121             int taskCount() const;
122             /// Return the task at @p index
123             QObject *taskAt(int index);
124             /// Find task with identity @p id
125             QObject *findTask(const QString &id);
126             /// Create a copy of @p copy, add the new task to @p parent after the task @p after
127             QObject *createTask(const QObject *copy, QObject *parent, QObject *after);
128             /// Create a new task and add it to @p parent after the task @p after
129             QObject *createTask(QObject *parent, QObject *after = 0);
130 
131             /// Returns the names of all resource properties
132             QStringList resourcePropertyList();
133 
134             /// Returns resource header data for @p property
135             QVariant resourceHeaderData(const QString &property, const QString &role = "DisplayRole");
136 
137             /// Number of resource groups
138             int resourceGroupCount() const;
139             /// Return the resource group at @p index
140             QObject *resourceGroupAt(int index);
141             /// Find resource group with identity @p id
142             QObject *findResourceGroup(const QString &id);
143             /// Create a copy of resource group @p group and insert it into the project
144             /// If a group with the same identity as the @p group already exists, 0 is returned
145             QObject *createResourceGroup(QObject *group);
146             /// Create a new resource group and insert it into the project
147             QObject *createResourceGroup();
148 
149             /// Find resource with identity @p id
150             QObject *findResource(const QString &id);
151             /// Create a copy of @p resource and add to @p group
152             /// If @p resource is a Team resource, the team members must exist.
153             /// If a resource with the same identity as the @p resource already exists, 0 is returned
154             QObject *createResource(QObject *group, QObject *resource);
155             /// Create a new resource and add to @p group
156             QObject *createResource(QObject *group);
157             /// Add an external appointment to @p resource
158             void addExternalAppointment(QObject *resource, const QVariant &id, const QString &name, const QVariantList &lst);
159             /// Clear the resources @p resource external appointments to project with identity @p id
160             void clearExternalAppointments(QObject *resource, const QString &id);
161             /// Clear all the resources external appointments to project with identity @p id
162             void clearExternalAppointments(const QString &id);
163             /// Clear all the resources external appointments to all projects
164             void clearExternalAppointments();
165             /// Return a <id, name> map of external projects
166             QVariantList externalProjects();
167 
168             /// Return the header data of calendars
169             QVariant calendarHeaderData(const QString &property, const QString &role = "DisplayRole");
170 
171             /// Returns the names of all resource properties
172             QStringList calendarPropertyList();
173 
174             /// Number of calendars
175             int calendarCount() const;
176             /// Return the calendar at @p index
177             QObject *calendarAt(int index);
178             /// Find calendar with identity @p id
179             QObject *findCalendar(const QString &id);
180             /// Create a copy of @p calendar and add to @p parent
181             /// If a calendar with the same id as @p calendar, 0 is returned
182             /// If parent is 0 it is added to the project
183             QObject *createCalendar(QObject *calendar, QObject *parent);
184             /// Create a calendar and add it to @p parent
185             /// If parent is 0 it is added to the project
186             QObject *createCalendar(QObject *parent = 0);
187 
188             /// Returns the names of all resource properties
189             QStringList accountPropertyList();
190 
191             /// Return the header data of accounts
192             QVariant accountHeaderData(const QString &property, const QString &role = "DisplayRole");
193 
194             /// Number of accounts
195             int accountCount() const;
196             /// Return the account at @p index
197             QObject *accountAt(int index);
198             /// Find account with identity @p id
199             QObject *findAccount(const QString &id);
200             /// Create account
201             QObject *createAccount(QObject *parent);
202 
203         public:
204             /// Return the Scripting::Node that interfaces the KPlato::Node @p node (create if necessary)
205             Node *node(KPlato::Node *node);
206             /// Return the data of @p node
207             QVariant nodeData(const KPlato::Node *node, const QString &property, const QString &role, long schedule = -1);
208             /// Set node data
209             QVariant setNodeData(KPlato::Node *node, const QString &property, const QVariant &data, const QString &role);
210 
211             /// Return ResourceGroup that interfaces the @p group (create if necessary)
212             QObject *resourceGroup(KPlato::ResourceGroup *group);
213             /// Return the data of resource group @p group
214             QVariant resourceGroupData(const KPlato::ResourceGroup *group, const QString &property, const QString &role, long schedule = -1);
215             /// Set resource group data
216             QVariant setResourceGroupData(KPlato::ResourceGroup *group, const QString &property, const QVariant &data, const QString &role);
217 
218             /// Return Resource that interfaces the @p resource (create if necessary)
219             QObject *resource(KPlato::Resource *resource);
220             /// Return the data of @p resource
221             QVariant resourceData(const KPlato::Resource *resource, const QString &property, const QString &role, long schedule = -1);
222             /// Set resource data
223             QVariant setResourceData(KPlato::Resource *resource, const QString &property, const QVariant &data, const QString &role);
224 
225             /// Return the Scripting::Calendar that interfaces the KPlato::Calendar @p cal
226             Calendar *calendar(KPlato::Calendar *cal);
227             /// Return the data of @p account
228             QVariant calendarData(const KPlato::Calendar *calendar, const QString &property, const QString &role, long = -1);
229             /// Set calendar data
230             QVariant setCalendarData(KPlato::Calendar *calendar, const QString &property, const QVariant &data, const QString &role);
231 
232             /// Return the Scripting::Schedule that interfaces the KPlato::ScheduleManager @p sch
233             QObject *schedule(KPlato::ScheduleManager *sch);
234 
235             /// Return the Scripting::Account that interfaces the KPlato::Account @p acc
236             QObject *account(KPlato::Account *acc);
237             /// Return the data of @p account
238             QVariant accountData(const KPlato::Account *account, const QString &property, const QString &role, long = -1);
239             /// Set account data
240             QVariant setAccountData(KPlato::Account *account, const QString &property, const QVariant &data, const QString &role);
241 
242         protected:
243             int nodeColumnNumber(const QString &property) const;
244             /// Map program role to a role that fetches data in a format that can be used in setData()
245             int programRole(const QMap<int, int> &map, int column) const;
246 
247             int resourceColumnNumber(const QString &property) const;
248 
249             int calendarColumnNumber(const QString &property) const;
250 
251             int accountColumnNumber(const QString &property) const;
252 
253         private Q_SLOTS:
254             void slotAddCommand(KUndo2Command*);
255 
256         private:
257             int stringToRole(const QString &role, int programrole = Qt::DisplayRole) const;
258 
259         private:
260             Module *m_module;
261 
262             KPlato::NodeItemModel m_nodeModel;
263             QMap<KPlato::Node*, Node*> m_nodes;
264             QMap<int, int> m_nodeprogramroles;
265 
266             KPlato::ResourceItemModel m_resourceModel;
267             QMap<KPlato::ResourceGroup*, ResourceGroup*> m_groups;
268             QMap<KPlato::Resource*, Resource*> m_resources;
269             QMap<int, int> m_resourceprogramroles;
270 
271             KPlato::CalendarExtendedItemModel m_calendarModel;
272             QMap<KPlato::Calendar*, Calendar*> m_calendars;
273             QMap<int, int> m_calendarprogramroles;
274 
275             QMap<KPlato::ScheduleManager*, Schedule*> m_schedules;
276 
277             KPlato::AccountItemModel m_accountModel;
278             QMap<KPlato::Account*, Account*> m_accounts;
279 
280             KPlato::MacroCommand *m_command;
281     };
282 
283 }
284 
285 #endif
286