Debian Codegraph @

src/libs/kernel/commands/RemoveResourceCmd.cpp

raw
1 /* This file is part of the KDE project
2 * SPDX-FileCopyrightText: 2019 Dag Andersen <[email protected]>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7 // clazy:excludeall=qstring-arg
8 #include "RemoveResourceCmd.h"
9 #include "RemoveParentGroupCmd.h"
10 #include "Resource.h"
11 #include "kptproject.h"
12 #include "kptaccount.h"
13 #include "kptappointment.h"
14 #include "kptresourcerequest.h"
15 #include "kptcommand.h"
16 #include "kptdebug.h"
17
18
19 using namespace KPlato;
20
21
22 RemoveResourceCmd::RemoveResourceCmd(Resource *resource, const KUndo2MagicString& name)
23 : AddResourceCmd(resource->project(), resource, name)
24 {
25 //debugPlan<<resource;
26 m_mine = false;
27 m_requests = m_resource->requests();
28
29 if (m_project) {
30 const auto schedules = m_project->schedules();
31 for (Schedule * s : schedules) {
32 Schedule *rs = resource->findSchedule(s->id());
33 if (rs && ! rs->isDeleted()) {
34 debugPlan<<s->name();
35 addSchScheduled(s);
36 }
37 }
38 const auto resources = m_project->resourceList();
39 for (auto r : resources) {
40 auto required = r->requiredIds();
41 if (required.contains(resource->id())) {
42 required.removeAll(resource->id());
43 m_preCmd.addCommand(new ModifyRequiredResourcesCmd(r, required));
44 }
45 auto teams = r->teamMemberIds();
46 if (teams.contains(resource->id())) {
47 m_preCmd.addCommand(new RemoveResourceTeamCmd(r, resource->id()));
48 }
49 }
50 }
51 if (resource->account()) {
52 m_postCmd.addCommand(new ResourceModifyAccountCmd(*resource, resource->account(), nullptr));
53 }
54 for (ResourceRequest *r : std::as_const(m_requests)) {
55 m_preCmd.addCommand(new RemoveResourceRequestCmd(r));
56 //debugPlan<<"Remove request for"<<r->resource()->name();
57 }
58 const auto parentGroups = resource->parentGroups();
59 for (ResourceGroup *g : parentGroups) {
60 m_preCmd.addCommand(new RemoveParentGroupCmd(resource, g));
61 }
62 }
63
64 RemoveResourceCmd::~RemoveResourceCmd()
65 {
66 while (!m_appointments.isEmpty())
67 delete m_appointments.takeFirst();
68 }
69
70 void RemoveResourceCmd::execute()
71 {
72 m_preCmd.redo();
73 AddResourceCmd::unexecute();
74 m_postCmd.redo();
75 setSchScheduled(false);
76 }
77
78 void RemoveResourceCmd::unexecute()
79 {
80 m_postCmd.undo();
81 AddResourceCmd::execute();
82 m_preCmd.undo();
83 setSchScheduled();
84 }