| 1 |
/* This file is part of the KDE project |
| 2 |
* SPDX-FileCopyrightText: 2004-2007 Dag Andersen <[email protected]> |
| 3 |
* SPDX-FileCopyrightText: 2011 Dag Andersen <[email protected]> |
| 4 |
* SPDX-FileCopyrightText: 2016 Dag Andersen <[email protected]> |
| 5 |
* SPDX-FileCopyrightText: 2019 Dag Andersen <[email protected]> |
| 6 |
* |
| 7 |
* SPDX-License-Identifier: LGPL-2.0-or-later |
| 8 |
*/ |
| 9 |
|
| 10 |
// clazy:excludeall=qstring-arg |
| 11 |
#include "InsertProjectCmd.h" |
| 12 |
|
| 13 |
#include "kptcommand.h" |
| 14 |
#include "AddResourceCmd.h" |
| 15 |
#include "AddParentGroupCmd.h" |
| 16 |
#include "RemoveParentGroupCmd.h" |
| 17 |
#include "RemoveResourceCmd.h" |
| 18 |
// #include "kptaccount.h" |
| 19 |
// #include "kptappointment.h" |
| 20 |
// #include "kptproject.h" |
| 21 |
// #include "kpttask.h" |
| 22 |
// #include "kptcalendar.h" |
| 23 |
// #include "kptrelation.h" |
| 24 |
// #include "kptresource.h" |
| 25 |
// #include "kptdocuments.h" |
| 26 |
// #include "kptlocale.h" |
| 27 |
#include "kptdebug.h" |
| 28 |
|
| 29 |
#include <QApplication> |
| 30 |
|
| 31 |
using namespace KPlato; |
| 32 |
|
| 33 |
|
| 34 |
//---------------- |
| 35 |
InsertProjectCmd::InsertProjectCmd(Project &fromProject, Node *parent, Node *after, const KUndo2MagicString& name) |
| 36 |
: MacroCommand(name) |
| 37 |
, m_fromProject(&fromProject) |
| 38 |
, m_project(static_cast<Project*>(parent->projectNode())) |
| 39 |
, m_parent(parent) |
| 40 |
, m_after(after) |
| 41 |
{ |
| 42 |
} |
| 43 |
|
| 44 |
void InsertProjectCmd::execute() |
| 45 |
{ |
| 46 |
if (m_fromProject) { |
| 47 |
createCommands(*m_fromProject); |
| 48 |
m_fromProject = nullptr; |
| 49 |
} |
| 50 |
// debugPlanInsertProject<<"before execute:"<<m_project->resourceGroups()<<m_project->resourceList(); |
| 51 |
MacroCommand::execute(); |
| 52 |
// debugPlanInsertProject<<"after execute:"<<m_project->resourceGroups()<<m_project->resourceList(); |
| 53 |
} |
| 54 |
void InsertProjectCmd::unexecute() |
| 55 |
{ |
| 56 |
QApplication::setOverrideCursor(Qt::WaitCursor); |
| 57 |
MacroCommand::unexecute(); |
| 58 |
QApplication::restoreOverrideCursor(); |
| 59 |
} |
| 60 |
|
| 61 |
ResourceGroup *findGroup(const QString &id, const Project *project, const QList<ResourceGroup*> &newGroups) |
| 62 |
{ |
| 63 |
auto group = project->findResourceGroup(id); |
| 64 |
if (group) { |
| 65 |
return group; |
| 66 |
} |
| 67 |
for (const auto g : newGroups) { |
| 68 |
if (g->id() == id) { |
| 69 |
return g; |
| 70 |
} |
| 71 |
} |
| 72 |
return nullptr; |
| 73 |
} |
| 74 |
|
| 75 |
void InsertProjectCmd::createCommands(Project &fromProject) |
| 76 |
{ |
| 77 |
Q_ASSERT(&fromProject != m_project); |
| 78 |
|
| 79 |
if (m_project->defaultCalendar()) { |
| 80 |
fromProject.setDefaultCalendar(nullptr); // or else m_project default calendar may be overwritten |
| 81 |
} |
| 82 |
QString defaultAccount; |
| 83 |
if (! m_project->accounts().defaultAccount() && fromProject.accounts().defaultAccount()) { |
| 84 |
defaultAccount = fromProject.accounts().defaultAccount()->name(); |
| 85 |
} |
| 86 |
QHash<Node*, QString> startupaccountmap; |
| 87 |
QHash<Node*, QString> shutdownaccountmap; |
| 88 |
QHash<Node*, QString> runningaccountmap; |
| 89 |
QHash<Node*, QString> nodecalendarmap; |
| 90 |
|
| 91 |
// remove unhandled info in tasks and get accounts and calendars |
| 92 |
const auto nodes = fromProject.allNodes(); |
| 93 |
for (Node *n : nodes) { |
| 94 |
if (n->type() == Node::Type_Task) { |
| 95 |
Task *t = static_cast<Task*>(n); |
| 96 |
t->workPackage().clear(); |
| 97 |
while (! t->workPackageLog().isEmpty()) { |
| 98 |
WorkPackage *wp = t->workPackageLog().at(0); |
| 99 |
t->removeWorkPackage(wp); |
| 100 |
delete wp; |
| 101 |
} |
| 102 |
} |
| 103 |
while (auto schedule = n->schedules().values().value(0)) { |
| 104 |
n->takeSchedule(schedule); |
| 105 |
if (schedule->resource()) { |
| 106 |
schedule->resource()->takeSchedule(schedule->resource()->findSchedule(schedule->id())); |
| 107 |
} |
| 108 |
delete schedule; |
| 109 |
} |
| 110 |
if (n->startupAccount()) { |
| 111 |
startupaccountmap.insert(n, n->startupAccount()->name()); |
| 112 |
n->setStartupAccount(nullptr); |
| 113 |
} |
| 114 |
if (n->shutdownAccount()) { |
| 115 |
shutdownaccountmap.insert(n, n->shutdownAccount()->name()); |
| 116 |
n->setShutdownAccount(nullptr); |
| 117 |
} |
| 118 |
if (n->runningAccount()) { |
| 119 |
runningaccountmap.insert(n, n->runningAccount()->name()); |
| 120 |
n->setRunningAccount(nullptr); |
| 121 |
} |
| 122 |
if (n->estimate()->calendar()) { |
| 123 |
nodecalendarmap.insert(n, n->estimate()->calendar()->id()); |
| 124 |
n->estimate()->setCalendar(nullptr); |
| 125 |
} |
| 126 |
} |
| 127 |
// get resources pointing to calendars, accounts and parentgroups |
| 128 |
QHash<Resource*, QString> resourceaccountmap; |
| 129 |
QHash<Resource*, QString> resourcecalendarmap; |
| 130 |
QHash<Resource*, QList<AddParentGroupCmd*> > resourceparentgroupcmds; |
| 131 |
const auto resources = fromProject.resourceList(); |
| 132 |
for (Resource *r : resources) { |
| 133 |
if (r->account()) { |
| 134 |
resourceaccountmap.insert(r, r->account()->name()); |
| 135 |
r->setAccount(nullptr); |
| 136 |
} |
| 137 |
if (r->calendar()) { |
| 138 |
resourcecalendarmap.insert(r, r->calendar()->id()); |
| 139 |
r->setCalendar(nullptr); |
| 140 |
} |
| 141 |
if (!m_project->findResource(r->id())) { |
| 142 |
// Need to parentgroups to m_project groups |
| 143 |
// to avoid dangling pointers |
| 144 |
const auto groups = r->parentGroups(); |
| 145 |
if (!groups.isEmpty()) { |
| 146 |
QList<AddParentGroupCmd*> cmds; |
| 147 |
for (const auto g : groups) { |
| 148 |
auto group = m_project->findResourceGroup(g->id()); |
| 149 |
if (group) { |
| 150 |
r->removeParentGroup(g); |
| 151 |
cmds << new AddParentGroupCmd(r, group); |
| 152 |
} |
| 153 |
} |
| 154 |
if (!cmds.isEmpty()) { |
| 155 |
resourceparentgroupcmds.insert(r, cmds); |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
// create add account commands and keep track of used and unused accounts |
| 161 |
QList<Account*> unusedAccounts; |
| 162 |
QMap<QString, Account*> accountsmap; |
| 163 |
const auto accounts = m_project->accounts().allAccounts(); |
| 164 |
for (Account *a : accounts) { |
| 165 |
accountsmap.insert(a->name(), a); |
| 166 |
} |
| 167 |
const auto accounts2 = fromProject.accounts().accountList(); |
| 168 |
for (Account *a : accounts2) { |
| 169 |
addAccounts(a, nullptr, unusedAccounts, accountsmap); |
| 170 |
} |
| 171 |
// create add calendar commands and keep track of used and unused calendars |
| 172 |
QList<Calendar*> unusedCalendars; |
| 173 |
QMap<QString, Calendar*> calendarsmap; |
| 174 |
const auto calendars = m_project->allCalendars(); |
| 175 |
for (Calendar *c : calendars) { |
| 176 |
calendarsmap.insert(c->id(), c); |
| 177 |
} |
| 178 |
const auto calendars2 = fromProject.calendars(); |
| 179 |
for (Calendar *c : calendars2) { |
| 180 |
addCalendars(c, nullptr, unusedCalendars, calendarsmap); |
| 181 |
} |
| 182 |
// get all requests from fromProject before resources are merged |
| 183 |
QMultiHash<Node*, QPair<ResourceRequest*, Resource*> > rreqs; |
| 184 |
const auto nodes2 = fromProject.allNodes(); |
| 185 |
for (Node *n : nodes2 ) { |
| 186 |
if (n->type() != (int)Node::Type_Task || n->requests().isEmpty()) { |
| 187 |
continue; |
| 188 |
} |
| 189 |
while (ResourceRequest *rr = n->requests().resourceRequests(false).value(0)) { |
| 190 |
debugPlanInsertProject<<"Get resource request:"<<rr; |
| 191 |
rreqs.insert(n, QPair<ResourceRequest*, Resource*>(rr, rr->resource())); |
| 192 |
// all resource requests shall be reinserted |
| 193 |
rr->unregisterRequest(); |
| 194 |
n->requests().removeResourceRequest(rr); |
| 195 |
} |
| 196 |
} |
| 197 |
debugPlanInsertProject<<"fromProject resource requests:"<<rreqs; |
| 198 |
// Handle resources. |
| 199 |
// Mainly collects resources in fromProject that does not exist in toProject |
| 200 |
// and collects the parent groups before removing the resource from the parent groups. |
| 201 |
// This makes it possible to add resources and groups and the re-add the parent groups. |
| 202 |
debugPlanInsertProject<<"fromProject resources:"<<fromProject.resourceList(); |
| 203 |
QList<Resource*> allResources; |
| 204 |
QList<Resource*> newResources; // resource in fromProject that does not exist in toProject |
| 205 |
QHash<Resource*, Resource*> existingResources; // hash[toProject resource, fromProject resource] |
| 206 |
QHash<Resource*, QStringList> existingResourceParentGroups; // existing resource with fromProject parentgroup ids |
| 207 |
QHash<Resource*, QStringList> newResourceParentGroups; // new resource with parent group ids |
| 208 |
const auto resources2 = fromProject.resourceList(); |
| 209 |
for (Resource *fromResource : resources2) { |
| 210 |
Resource *toResource = m_project->resource(fromResource->id()); |
| 211 |
if (toResource == nullptr) { |
| 212 |
const auto groups = fromResource->parentGroups(); |
| 213 |
for (auto g : groups) { |
| 214 |
newResourceParentGroups[fromResource].append(g->id()); |
| 215 |
} |
| 216 |
fromResource->setParentGroups(QList<ResourceGroup*>()); |
| 217 |
newResources << fromResource; |
| 218 |
allResources << fromResource; |
| 219 |
fromProject.removeResourceFromProject(fromResource); |
| 220 |
} else { |
| 221 |
const auto groups = fromResource->parentGroups(); |
| 222 |
for (auto g : groups) { |
| 223 |
existingResourceParentGroups[toResource].append(g->id()); |
| 224 |
} |
| 225 |
existingResources[ toResource ] = fromResource; |
| 226 |
allResources << toResource; |
| 227 |
} |
| 228 |
} |
| 229 |
for (auto r1 : std::as_const(allResources)) { |
| 230 |
for (auto r2 : std::as_const(allResources)) { |
| 231 |
if (r1 != r2 && r1->name() == r2->name()) { |
| 232 |
warnPlanInsertProject<<"Two resources with same name!"<<r1<<r2; |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
// Add new groups |
| 237 |
QList<ResourceGroup*> allGroups; |
| 238 |
QHash<ResourceGroup*, ResourceGroup*> existingGroups; // QHash<toProject group, fromProject group> |
| 239 |
QList<ResourceGroup*> newGroups; // all new groups sorted by level: top1, child1..childn, top2, child1..childn ... |
| 240 |
QHash<ResourceGroup*, ResourceGroup*> newGroupsWithParent; // QHash<group, parent group> |
| 241 |
const auto groups = fromProject.allResourceGroups(true /*sorted*/); |
| 242 |
debugPlanInsertProject<<"fromProject resource groups:"<<groups; |
| 243 |
for (ResourceGroup *g : groups) { |
| 244 |
ResourceGroup *gr = m_project->findResourceGroup(g->id()); |
| 245 |
if (gr) { |
| 246 |
existingGroups[gr] = g; |
| 247 |
} else { |
| 248 |
gr = g; |
| 249 |
newGroups << gr; |
| 250 |
if (gr->parentGroup()) { |
| 251 |
// try to map to toProject in case group exists there |
| 252 |
auto parent = m_project->findResourceGroup(gr->parentGroup()->id()); |
| 253 |
if (!parent) { |
| 254 |
parent = gr->parentGroup(); |
| 255 |
} |
| 256 |
newGroupsWithParent[gr] = parent; |
| 257 |
parent->removeChildGroup(gr); |
| 258 |
} |
| 259 |
fromProject.removeResourceGroupFromProject(gr); |
| 260 |
} |
| 261 |
allGroups << gr; |
| 262 |
} |
| 263 |
debugPlanInsertProject<<"New resource groups:"<<newGroups; |
| 264 |
for (auto g : std::as_const(newGroups)) { |
| 265 |
auto parent = newGroupsWithParent.value(g); // if nullptr it is a top level group |
| 266 |
debugPlanInsertProject<<"AddResourceGroupCmd:"<<'f'<<&fromProject<<'t'<<m_project<<g<<parent<<g->resources(); |
| 267 |
addCommand(new AddResourceGroupCmd(m_project, parent, g, kundo2_noi18n(QStringLiteral("ResourceGroup")))); |
| 268 |
} |
| 269 |
// Add new resources |
| 270 |
for (Resource *r : std::as_const(newResources)) { |
| 271 |
addCommand(new AddResourceCmd(m_project, r, kundo2_noi18n(QStringLiteral("Resource")))); |
| 272 |
// add any moved parent groups |
| 273 |
auto cmds = resourceparentgroupcmds.take(r); |
| 274 |
while (!cmds.isEmpty()) { |
| 275 |
addCommand(cmds.takeFirst()); |
| 276 |
} |
| 277 |
debugPlanInsertProject<<"AddResourceCmd:"<<r->name()<<r->parentGroups(); |
| 278 |
// Update resource parent groups |
| 279 |
const auto groupIds = newResourceParentGroups.value(r); |
| 280 |
for (const auto &id : groupIds) { |
| 281 |
auto group = findGroup(id, m_project, newGroups); |
| 282 |
if (group) { |
| 283 |
addCommand(new AddParentGroupCmd(r, group)); |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
// Add the fromProjects parentgroups to existing resources |
| 288 |
{QHash<Resource*, QStringList>::const_iterator it = existingResourceParentGroups.constBegin(); |
| 289 |
QHash<Resource*, QStringList>::const_iterator end = existingResourceParentGroups.constEnd(); |
| 290 |
for (; it != end; ++it) { |
| 291 |
for (const auto &id : it.value()) { |
| 292 |
auto group = findGroup(id, m_project, newGroups); |
| 293 |
if (group) { |
| 294 |
addCommand(new AddParentGroupCmd(it.key(), group)); |
| 295 |
} else { |
| 296 |
warnPlanInsertProject<<"Failed to find parent group for resource:"<<it.key()<<"group id:"<<it.value(); |
| 297 |
} |
| 298 |
} |
| 299 |
}} |
| 300 |
// Update resource account |
| 301 |
{QHash<Resource*, QString>::const_iterator it = resourceaccountmap.constBegin(); |
| 302 |
QHash<Resource*, QString>::const_iterator end = resourceaccountmap.constEnd(); |
| 303 |
for (; it != end; ++it) { |
| 304 |
Resource *r = it.key(); |
| 305 |
if (newResources.contains(r)) { |
| 306 |
Q_ASSERT(allResources.contains(r)); |
| 307 |
addCommand(new ResourceModifyAccountCmd(*r, nullptr, accountsmap.value(it.value()))); |
| 308 |
} |
| 309 |
}} |
| 310 |
// Update resource calendar |
| 311 |
{QHash<Resource*, QString>::const_iterator it = resourcecalendarmap.constBegin(); |
| 312 |
QHash<Resource*, QString>::const_iterator end = resourcecalendarmap.constEnd(); |
| 313 |
for (; it != end; ++it) { |
| 314 |
Resource *r = it.key(); |
| 315 |
if (newResources.contains(r)) { |
| 316 |
Q_ASSERT(allResources.contains(r)); |
| 317 |
addCommand(new ModifyResourceCalendarCmd(r, calendarsmap.value(it.value()))); |
| 318 |
} |
| 319 |
}} |
| 320 |
debugPlanInsertProject<<"Requests: clean up requests to resources already in m_project"; |
| 321 |
debugPlanInsertProject<<"All groups:"<<allGroups; |
| 322 |
debugPlanInsertProject<<"New groups:"<<newGroups; |
| 323 |
debugPlanInsertProject<<"Existing groups:"<<existingGroups; |
| 324 |
debugPlanInsertProject<<"All resources:"<<allResources; |
| 325 |
debugPlanInsertProject<<"New resources:"<<newResources; |
| 326 |
debugPlanInsertProject<<"Existing resources:"<<existingResources; |
| 327 |
debugPlanInsertProject<<"Resource requests:"<<rreqs; |
| 328 |
|
| 329 |
for (auto i = rreqs.constBegin(); i != rreqs.constEnd(); ++i) { |
| 330 |
Node *n = i.key(); |
| 331 |
ResourceRequest *rr = i.value().first; |
| 332 |
rr->setId(0); |
| 333 |
rr->setCollection(nullptr); |
| 334 |
Resource *newRes = i.value().second; |
| 335 |
debugPlanInsertProject<<"Resource exists:"<<newRes<<newRes->id()<<(void*)newRes<<':'<<m_project->resource(newRes->id()); |
| 336 |
if (Resource *nr = existingResources.key(newRes)) { |
| 337 |
newRes = nr; |
| 338 |
debugPlanInsertProject<<"Resource existed:"<<newRes<<(void*)newRes; |
| 339 |
} |
| 340 |
debugPlanInsertProject<<"Add resource request:"<<"task:"<<n->wbsCode()<<n->name()<<"resource:"<<newRes<<"requests:"<<rr; |
| 341 |
if (!rr->requiredResources().isEmpty()) { |
| 342 |
// the resource request may have required resources that needs mapping |
| 343 |
QList<Resource*> required; |
| 344 |
const auto resources = rr->requiredResources(); |
| 345 |
for (Resource *r : resources) { |
| 346 |
if (newResources.contains(r)) { |
| 347 |
required << r; |
| 348 |
debugPlanInsertProject<<"Request: required (new)"<<r->name(); |
| 349 |
continue; |
| 350 |
} |
| 351 |
Resource *r2 = existingResources.key(r); |
| 352 |
Q_ASSERT(allResources.contains(r2)); |
| 353 |
if (r2) { |
| 354 |
debugPlanInsertProject<<"Request: required (existing)"<<r2->name(); |
| 355 |
required << r2; |
| 356 |
} |
| 357 |
} |
| 358 |
rr->setRequiredResources(required); |
| 359 |
} |
| 360 |
if (!rr->alternativeRequests().isEmpty()) { |
| 361 |
const auto alts = rr->alternativeRequests(); |
| 362 |
for (auto alt : alts) { |
| 363 |
auto resource = m_project->findResource(alt->resource()->id()); |
| 364 |
if (resource) { |
| 365 |
debugPlanInsertProject<<"swap alternative"<<alt->resource()<<"->"<<resource; |
| 366 |
alt->setResource(resource); |
| 367 |
} |
| 368 |
} |
| 369 |
} |
| 370 |
Q_ASSERT(allResources.contains(newRes)); |
| 371 |
// all resource requests shall be reinserted |
| 372 |
rr->setResource(newRes); |
| 373 |
addCommand(new AddResourceRequestCmd(&n->requests(), rr)); |
| 374 |
} |
| 375 |
// Add nodes (ids are unique, no need to check) |
| 376 |
Node *node_after = m_after; |
| 377 |
for (int i = 0; i < fromProject.numChildren(); ++i) { |
| 378 |
Node *n = fromProject.childNode(i); |
| 379 |
Q_ASSERT(n); |
| 380 |
Q_ASSERT(n->schedules().isEmpty()); |
| 381 |
n->setParentNode(nullptr); |
| 382 |
if (node_after) { |
| 383 |
addCommand(new TaskAddCmd(m_project, n, node_after, kundo2_noi18n(QStringLiteral("Task")))); |
| 384 |
node_after = n; |
| 385 |
} else { |
| 386 |
addCommand(new SubtaskAddCmd(m_project, n, m_parent, kundo2_noi18n(QStringLiteral("Subtask")))); |
| 387 |
} |
| 388 |
addChildNodes(n); |
| 389 |
} |
| 390 |
// Dependencies: |
| 391 |
const auto nodes3 = fromProject.allNodes(); |
| 392 |
for (Node *n : nodes3) { |
| 393 |
while (n->numDependChildNodes() > 0) { |
| 394 |
Relation *r = n->dependChildNodes().at(0); |
| 395 |
n->takeDependChildNode(r); |
| 396 |
r->child()->takeDependParentNode(r); |
| 397 |
addCommand(new AddRelationCmd(*m_project, r)); |
| 398 |
} |
| 399 |
} |
| 400 |
// node calendar |
| 401 |
{QHash<Node*, QString>::const_iterator it = nodecalendarmap.constBegin(); |
| 402 |
QHash<Node*, QString>::const_iterator end = nodecalendarmap.constEnd(); |
| 403 |
for (; it != end; ++it) { |
| 404 |
addCommand(new ModifyEstimateCalendarCmd(*(it.key()), nullptr, calendarsmap.value(it.value()))); |
| 405 |
}} |
| 406 |
// node startup account |
| 407 |
{QHash<Node*, QString>::const_iterator it = startupaccountmap.constBegin(); |
| 408 |
QHash<Node*, QString>::const_iterator end = startupaccountmap.constEnd(); |
| 409 |
for (; it != end; ++it) { |
| 410 |
addCommand(new NodeModifyStartupAccountCmd(*(it.key()), nullptr, accountsmap.value(it.value()))); |
| 411 |
}} |
| 412 |
// node shutdown account |
| 413 |
{QHash<Node*, QString>::const_iterator it = shutdownaccountmap.constBegin(); |
| 414 |
QHash<Node*, QString>::const_iterator end = shutdownaccountmap.constEnd(); |
| 415 |
for (; it != end; ++it) { |
| 416 |
addCommand(new NodeModifyShutdownAccountCmd(*(it.key()), nullptr, accountsmap.value(it.value()))); |
| 417 |
}} |
| 418 |
// node running account |
| 419 |
{QHash<Node*, QString>::const_iterator it = runningaccountmap.constBegin(); |
| 420 |
QHash<Node*, QString>::const_iterator end = runningaccountmap.constEnd(); |
| 421 |
for (; it != end; ++it) { |
| 422 |
addCommand(new NodeModifyRunningAccountCmd(*(it.key()), nullptr, accountsmap.value(it.value()))); |
| 423 |
}} |
| 424 |
|
| 425 |
if (! defaultAccount.isEmpty()) { |
| 426 |
Account *a = accountsmap.value(defaultAccount); |
| 427 |
if (a && a->list()) { |
| 428 |
addCommand(new ModifyDefaultAccountCmd(m_project->accounts(), nullptr, a)); |
| 429 |
} |
| 430 |
} |
| 431 |
debugPlanInsertProject<<"Cleanup unused stuff from inserted project:"<<&fromProject; |
| 432 |
// Cleanup |
| 433 |
// Remove nodes from fromProject so they are not deleted |
| 434 |
while (Node *ch = fromProject.childNode(0)) { |
| 435 |
fromProject.takeChildNode(ch); |
| 436 |
} |
| 437 |
const auto nodes4 = fromProject.allNodes(); |
| 438 |
for (Node *n : nodes4) { |
| 439 |
fromProject.removeId(n->id()); |
| 440 |
} |
| 441 |
|
| 442 |
// Remove calendars from fromProject |
| 443 |
while (fromProject.calendarCount() > 0) { |
| 444 |
fromProject.takeCalendar(fromProject.calendarAt(0)); |
| 445 |
} |
| 446 |
qDeleteAll(unusedCalendars); |
| 447 |
|
| 448 |
// Remove accounts from fromProject |
| 449 |
while (fromProject.accounts().accountCount() > 0) { |
| 450 |
fromProject.accounts().take(fromProject.accounts().accountAt(0)); |
| 451 |
} |
| 452 |
qDeleteAll(unusedAccounts); |
| 453 |
|
| 454 |
for (Resource *r = fromProject.resourceList().value(0); r; r = fromProject.resourceList().value(0)) { |
| 455 |
debugPlanInsertProject<<"remove all resources from fromProject:"<<r; |
| 456 |
fromProject.removeResourceFromProject(r); |
| 457 |
Q_ASSERT(!fromProject.resourceList().contains(r)); |
| 458 |
} |
| 459 |
while (fromProject.numResourceGroups() > 0) { |
| 460 |
ResourceGroup *g = fromProject.resourceGroupAt(0); |
| 461 |
debugPlanInsertProject<<"Take used group:"<<g; |
| 462 |
fromProject.removeResourceGroupFromProject(g); |
| 463 |
Q_ASSERT(!fromProject.resourceGroups().contains(g)); |
| 464 |
} |
| 465 |
debugPlanInsertProject<<"Delete unused resources:"<<existingResources; |
| 466 |
qDeleteAll(existingResources); // deletes unused resources |
| 467 |
debugPlanInsertProject<<"Delete unused groups:"<<existingGroups.count()<<existingGroups; |
| 468 |
// avoid double delete |
| 469 |
for (auto g : std::as_const(existingGroups)) { |
| 470 |
while (g->numChildGroups() > 0) { |
| 471 |
g->removeChildGroup(g->childGroupAt(0)); |
| 472 |
} |
| 473 |
} |
| 474 |
qDeleteAll(existingGroups); // deletes unused resource groups |
| 475 |
} |
| 476 |
|
| 477 |
void InsertProjectCmd::addCalendars(Calendar *calendar, Calendar *parent, QList<Calendar*> &unused, QMap<QString, Calendar*> &calendarsmap) { |
| 478 |
Calendar *par = nullptr; |
| 479 |
if (parent) { |
| 480 |
par = calendarsmap.value(parent->id()); |
| 481 |
} |
| 482 |
if (par == nullptr) { |
| 483 |
par = parent; |
| 484 |
} |
| 485 |
Calendar *cal = calendarsmap.value(calendar->id()); |
| 486 |
if (cal == nullptr) { |
| 487 |
calendar->project()->takeCalendar(calendar); |
| 488 |
calendarsmap.insert(calendar->id(), calendar); |
| 489 |
addCommand(new CalendarAddCmd(m_project, calendar, -1, par)); |
| 490 |
} else { |
| 491 |
unused << calendar; |
| 492 |
} |
| 493 |
const auto calendars = calendar->calendars(); |
| 494 |
for (Calendar *c : calendars) { |
| 495 |
addCalendars(c, calendar, unused, calendarsmap); |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
void InsertProjectCmd::addAccounts(Account *account, Account *parent, QList<Account*> &unused, QMap<QString, Account*> &accountsmap) { |
| 500 |
Account *par = nullptr; |
| 501 |
if (parent) { |
| 502 |
par = accountsmap.value(parent->name()); |
| 503 |
} |
| 504 |
if (par == nullptr) { |
| 505 |
par = parent; |
| 506 |
} |
| 507 |
Account *acc = accountsmap.value(account->name()); |
| 508 |
if (acc == nullptr) { |
| 509 |
accountsmap.insert(account->name(), account); |
| 510 |
addCommand(new AddAccountCmd(*m_project, account, par, -1, kundo2_noi18n("Add account %1", account->name()))); |
| 511 |
} else { |
| 512 |
unused << account; |
| 513 |
} |
| 514 |
while (! account->accountList().isEmpty()) { |
| 515 |
Account *a = account->accountList().first(); |
| 516 |
account->list()->take(a); |
| 517 |
addAccounts(a, account, unused, accountsmap); |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
void InsertProjectCmd::addChildNodes(Node *node) { |
| 522 |
// schedules not handled |
| 523 |
while (Schedule *s = node->schedules().values().value(0)) { |
| 524 |
node->takeSchedule(s); // schedules not handled |
| 525 |
delete s; |
| 526 |
} |
| 527 |
const auto nodes = node->childNodeIterator(); |
| 528 |
for (Node *n : nodes ) { |
| 529 |
n->setParentNode(nullptr); |
| 530 |
addCommand(new SubtaskAddCmd(m_project, n, node)); |
| 531 |
addChildNodes(n); |
| 532 |
} |
| 533 |
// Remove child nodes so they are not added twice |
| 534 |
while (Node *ch = node->childNode(0)) { |
| 535 |
node->takeChildNode(ch); |
| 536 |
} |
| 537 |
} |