Menu

[efa3d9]: / src / branchitem.cpp  Maximize  Restore  History

Download this file

576 lines (467 with data), 16.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
#include "branchitem.h"
#include "attributeitem.h"
#include "branch-container.h"
#include "branch-wrapper.h"
#include "frame-container.h"
#include "heading-container.h"
#include "image-container.h"
#include "task.h"
#include "taskmodel.h"
#include "vymmodel.h"
#include "xlink.h"
#include "xlinkitem.h"
extern TaskModel *taskModel;
BranchItem::BranchItem(TreeItem *parent)
: MapItem(parent)
{
//qDebug()<< "Constr. BranchItem this=" << this << "parent:" << parent;
// Set type if parent is known yet
// if not, type is set in insertBranch or TreeItem::appendChild
if (parent == rootItem)
setType(MapCenter);
else
setType(Branch);
scrolled = false;
tmpUnscrolled = false;
lastSelectedBranchNum = 0;
lastSelectedBranchNumAlt = 0;
task = nullptr;
branchWrapperInt = nullptr;
branchContainer = nullptr;
}
BranchItem::~BranchItem()
{
// std::cout << "Destr. BranchItem: this=" << this << " " << headingPlain().toStdString() << " branchContainer=" << branchContainer << std::endl;
if (task)
taskModel->deleteTask(task);
if (branchContainer) {
// This deletes only the first container here.
// All other containers deeper down in tree will unlink themselves
// by calling BranchItem::unlinkBranchContainer, which will set
// the branchContainer == nullptr;
//
// QGraphicsItems such as BranchContainer will delete all their children
// themselves
delete branchContainer;
}
if (branchWrapperInt) {
delete branchWrapperInt;
branchWrapperInt = nullptr;
}
}
void BranchItem::copy(BranchItem *other) // TODO lacks most of data...
{
scrolled = other->scrolled;
tmpUnscrolled = other->tmpUnscrolled;
}
BranchItem *BranchItem::parentBranch()
{
// For MapCenters this will return rootItem
return (BranchItem *)parentItem;
}
BranchWrapper *BranchItem::branchWrapper()
{
if (!branchWrapperInt)
branchWrapperInt = new BranchWrapper(this);
return branchWrapperInt;
}
void BranchItem::insertBranch(int pos, BranchItem *branch)
{
if (pos < 0)
pos = 0;
if (pos > branchCounter)
pos = branchCounter;
childItems.insert(pos + branchOffsetInt, branch);
branch->parentItem = this;
branch->rootItem = rootItem;
branch->setModel(model);
if (this == rootItem)
branch->setType(MapCenter);
else
branch->setType(Branch);
if (branchCounter == 0)
branchOffsetInt = childItems.count() - 1;
branchCounter++;
}
void BranchItem::insertImage(int pos, ImageItem *image)
{
if (pos < 0)
pos = 0;
if (pos > imageCounter)
pos = imageCounter;
childItems.insert(pos + imageOffsetInt, image);
// Set parentItem, rootItem and model
image->setParentBranch(this);
imageCounter++;
branchOffsetInt++;
}
QString BranchItem::saveToDir(const QString &tmpdir, const QString &prefix,
const QPointF &offset, QList<XLink *> &tmpXLinks, const bool &exportBoundingBoxes)
{
// Cloudy stuff can be hidden during exports
if (hidden)
return QString();
QString attr;
// Save uuid
attr += attribute("uuid", uuid.toString());
// vymLink, Url, hideExport, localTarget
attr += getGeneralAttr();
// Linkable attributes
attr += getLinkableAttr();
// Update of note is usually done while unselecting a branch
if (scrolled)
attr += attribute("scrolled", "yes");
// save area, if not scrolled
// FIXME-5 we could check if _any_ of parents is scrolled
if (exportBoundingBoxes && branchContainer && parentItem->hasTypeBranch() &&
!((BranchItem *)parentItem)->isScrolled()) {
QRectF r_bc = branchContainer->mapToScene(branchContainer->rect()).boundingRect();
attr +=
attribute("x1", QString().setNum(r_bc.topLeft().x() - offset.x())) +
attribute("y1", QString().setNum(r_bc.topLeft().y() - offset.y())) +
attribute("x2", QString().setNum(r_bc.bottomRight().x() - offset.x())) +
attribute("y2", QString().setNum(r_bc.bottomRight().y() - offset.y()));
}
QString elementName;
if (parentItem == rootItem)
elementName = "mapcenter";
else
elementName = "branch";
// Free positioning of children
if (!branchContainer->branchesContainerAutoLayout)
// Save the manually set layout for children branches
attr += attribute("branchesLayout", branchContainer->layoutString(branchContainer->branchesContainerLayout()));
if (!branchContainer->imagesContainerAutoLayout)
// Save the manually set layout for children Images
attr += attribute("imagesLayout", branchContainer->Container::layoutString(branchContainer->imagesContainerLayout()));
if (!branchContainer->rotationsAutoDesign()) {
attr += attribute("rotHeading", QString("%1").arg(branchContainer->rotationHeading()));
attr += attribute("rotSubtree", QString("%1").arg(branchContainer->rotationSubtree()));
}
if (!branchContainer->scaleAutoDesign()) {
attr += attribute("scaleHeading", QString("%1").arg(branchContainer->scaleHeading()));
attr += attribute("scaleSubtree", QString("%1").arg(branchContainer->scaleSubtree()));
}
// width of heading
if (!branchContainer->columnWidthAutoDesign())
attr += attribute("colWidth", QString("%1").arg(branchContainer->getHeadingContainer()->columnWidth()));
if (parentItem == rootItem || branchContainer->isFloating())
attr += getPosAttr();
QString s = beginElement(elementName + " " + attr);
incIndent();
// save heading
s += headingInt.saveToDir();
// save note
if (!note.isEmpty())
s += note.saveToDir();
// Save frame
if (branchContainer->frameType(true) != FrameContainer::NoFrame ||
branchContainer->frameType(false) != FrameContainer::NoFrame)
s += branchContainer->saveFrame();
// save names of flag set
s += standardFlags.saveState();
s += userFlags.saveState();
// Save Images
for (int i = 0; i < imageCount(); ++i)
s += getImageNum(i)->saveToDir(tmpdir);
// save attributes
for (int i = 0; i < attributeCount(); ++i)
s += getAttributeNum(i)->getDataXML();
// save task
if (task)
s += task->saveToDir();
// Save branches
int i = 0;
TreeItem *ti = getBranchNum(i);
while (ti) {
s += getBranchNum(i)->saveToDir(tmpdir, prefix, offset, tmpXLinks, exportBoundingBoxes);
i++;
ti = getBranchNum(i);
}
// Mark Links for save
for (int i = 0; i < xlinkCount(); ++i) {
XLink *xl = getXLinkItemNum(i)->getXLink();
if (xl && !tmpXLinks.contains(xl))
tmpXLinks.append(xl);
}
decIndent();
s += endElement(elementName);
return s;
}
void BranchItem::setHeadingColor(QColor color)
{
TreeItem::setHeadingColor(color);
branchContainer->setColor(color);
}
void BranchItem::updateTaskFlag()
{
systemFlags.deactivateGroup("system-tasks");
if (task) {
QString s = "system-" + task->getIconString();
systemFlags.activate(s);
model->emitDataChanged(this);
}
// else: During initialization the task is not yet attached to branch,
// so ignore it for now
}
void BranchItem::setTask(Task *t)
{
task = t;
updateTaskFlag();
}
Task *BranchItem::getTask() { return task; }
void BranchItem::scroll()
{
if (tmpUnscrolled)
resetTmpUnscroll();
if (!scrolled)
toggleScroll();
}
void BranchItem::unScroll()
{
if (tmpUnscrolled)
resetTmpUnscroll();
if (scrolled)
toggleScroll();
}
bool BranchItem::toggleScroll()
{
// MapCenters are not scrollable
if (depth() == 0)
return false;
if (scrolled) {
scrolled = false;
systemFlags.deactivate(QString("system-scrolledright"));
}
else {
scrolled = true;
systemFlags.activate(QString("system-scrolledright"));
}
branchContainer->updateChildrenStructure(); // needed to insert linkSpaceContainer
branchContainer->updateVisibility();
return true;
}
bool BranchItem::isScrolled() { return scrolled; }
bool BranchItem::hasScrolledParent(BranchItem *start)
{
// Calls parents recursivly to
// find out, if we are scrolled at all.
// But ignore myself, just look at parents.
if (!start)
start = this;
if (this != start && scrolled)
return true;
BranchItem *bi = (BranchItem *)parentItem;
if (bi && bi != rootItem)
return bi->hasScrolledParent(start);
else
return false;
}
bool BranchItem::tmpUnscroll(BranchItem *start)
{
bool result = false;
if (!start)
start = this;
// Unscroll parent (recursivly)
BranchItem *pi = (BranchItem *)parentItem;
if (pi && pi->hasTypeBranch())
result = pi->tmpUnscroll(start);
// Unscroll myself
if (start != this && scrolled) {
tmpUnscrolled = true;
systemFlags.activate(QString("system-tmpUnscrolledRight"));
toggleScroll();
model->emitDataChanged(this);
result = true;
}
return result;
}
bool BranchItem::resetTmpUnscroll()
{
bool result = false;
// Unscroll parent (recursivly)
BranchItem *pi = (BranchItem *)parentItem;
if (pi && pi->hasTypeBranch())
result = pi->resetTmpUnscroll();
// Unscroll myself
if (tmpUnscrolled) {
tmpUnscrolled = false;
systemFlags.deactivate(QString("system-tmpUnscrolledRight"));
toggleScroll();
model->emitDataChanged(this);
result = true;
}
return result;
}
void BranchItem::setBranchesLayout(const QString &s)
{
branchContainer->setBranchesContainerLayout(Container::layoutFromString(s));
}
void BranchItem::setImagesLayout(const QString &s)
{
branchContainer->setImagesContainerLayout(Container::layoutFromString(s));
}
QColor BranchItem::getBackgroundColor(BranchItem *start, bool checkInnerFrame)
{
if (!branchContainer)
return QColor();
// Determine background color in taskEditor, first try inner frame
if (checkInnerFrame && branchContainer->frameType(true) != FrameContainer::NoFrame)
return branchContainer->frameBrushColor(true);
// Outer frame
if (branchContainer->frameType(false) != FrameContainer::NoFrame)
return branchContainer->frameBrushColor(false);
BranchItem *pb = parentBranch();
if (pb && pb != rootItem)
// Recursively try parents and check for frames there
return pb->getBackgroundColor(start, false);
else
// No frame found
return model->mapDesign()->backgroundColor();
}
void BranchItem::setLastSelectedBranch()
{
int d = depth();
if (d >= 0) {
if (d == 1)
// Hack to save an additional lastSelected for mapcenters in
// MapEditor depending on orientation this allows to go both left
// and right from there
if (branchContainer->getOrientation() ==
BranchContainer::LeftOfParent) {
((BranchItem *)parentItem)->lastSelectedBranchNumAlt =
parentItem->num(this);
return;
}
((BranchItem *)parentItem)->lastSelectedBranchNum =
parentItem->num(this);
}
}
void BranchItem::setLastSelectedBranch(int i) { lastSelectedBranchNum = i; }
BranchItem *BranchItem::getLastSelectedBranch()
{
if (lastSelectedBranchNum >= branchCounter)
return getBranchNum(branchCounter - 1);
else
return getBranchNum(lastSelectedBranchNum);
}
BranchItem *BranchItem::getLastSelectedBranchAlt()
{
return getBranchNum(lastSelectedBranchNumAlt);
}
TreeItem *BranchItem::findMapItem(QPointF p, QList <TreeItem*> excludedItems)
{
// Search branches
if (!isScrolled()) {
TreeItem *ti;
for (int i = 0; i < branchCounter; ++i) {
ti = getBranchNum(i)->findMapItem(p, excludedItems);
if (ti != nullptr)
return ti;
}
}
// Search images
ImageItem *ii;
ImageContainer *ic;
for (int i = 0; i < imageCount(); ++i) {
ii = getImageNum(i);
ic = ii->getImageContainer();
if (!excludedItems.contains(ii) && ic->isVisible() && ic->mapToScene(ic->rect()).containsPoint(p, Qt::OddEvenFill)) return ii;
}
// Search my container
if (branchContainer->isVisible() && branchContainer->isInClickBox(p) && !excludedItems.contains(this) ) // &&
//getBranchObj()->isVisibleObj())
return this;
return nullptr;
}
void BranchItem::setHideMode(HideTmpMode mode)
{
TreeItem::setHideMode(mode);
branchContainer->updateVisibility();
}
void BranchItem::updateVisuals()
{
branchContainer->updateVisuals();
}
BranchContainer *BranchItem::createBranchContainer(QGraphicsScene *scene)
{
branchContainer = new BranchContainer(scene, this);
if (parentBranch() != rootItem) {
// For floating branches get a position hint
parentBranch()->addToBranchesContainer(branchContainer);
BranchContainer *pbc = branchContainer->parentBranchContainer();
if (pbc->hasFloatingBranchesLayout())
branchContainer->setPos(pbc->getPositionHintNewChild(branchContainer));
// Link to parent branch visually by
// adding my upLink to parents linkContainer
branchContainer->linkTo(parentBranch()->getBranchContainer());
}
return branchContainer;
}
BranchContainer* BranchItem::getBranchContainer()
{
return branchContainer;
}
void BranchItem::unlinkBranchContainer()
{
//qDebug() << "BI::unlinkBC in " << this << headingPlain();
// Called from destructor of containers to
// avoid double deletion
branchContainer = nullptr;
}
Container* BranchItem::getBranchesContainer()
{
return branchContainer->getBranchesContainer();
}
Container* BranchItem::getImagesContainer()
{
return branchContainer->getImagesContainer();
}
void BranchItem::updateContainerStackingOrder()
{
// After relinking branches (also moving up/down), the order of the
// BranchContainers does not match the order of BranchItems any longer and
// needs to be adjusted. Or the BranchContainer has (temporarily) been linked to
// a completely different parent.
//
// It seems the QGraphicsItem::stackBefore only works, if an item is moved up.
// For moving below (or into another subtree), we have to reparent first :-(
// For simplicity we always reparent. The absolute position will not be changed here
int n = num();
QPointF sp = branchContainer->scenePos();
branchContainer->setParentItem(nullptr);
if (parentBranch() == rootItem) {
// I am a MapCenter
branchContainer->setPos(sp);
return;
}
parentBranch()->addToBranchesContainer(branchContainer);
while (n < parentBranch()->branchCount() - 1) {
// Insert container of this branch above others
// The next sibling container might currently still be temporarily
// linked to tmpParentContainer, in that case it is not a sibling and
// cannot be inserted using QGraphicsItem::stackBefore
//
// We try the next sibling then, if this fails, just append at the end.
if ( (parentBranch()->getBranchNum(n + 1))->getContainer()->parentItem() != parentBranch()->getBranchesContainer() )
n++;
else {
branchContainer->stackBefore( (parentBranch()->getBranchNum(n + 1))->getContainer() );
break;
}
}
branchContainer->setPos(branchContainer->parentItem()->sceneTransform().inverted().map(sp));
}
void BranchItem::addToBranchesContainer(BranchContainer *bc)
{
branchContainer->addToBranchesContainer(bc);
}
void BranchItem::addToImagesContainer(ImageContainer *ic)
{
// Keep scene position while relinking image container
branchContainer->addToImagesContainer(ic);
}
void BranchItem::repositionContainers()
{
branchContainer->reposition();
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.