4 #include "attributeitem.h"
5 #include "branchitem.h"
10 #include "xlinkitem.h"
15 extern ulong itemLastID;
16 extern FlagRowMaster *standardFlagsMaster;
17 extern FlagRowMaster *systemFlagsMaster;
18 extern FlagRowMaster *userFlagsMaster;
20 extern QTextStream vout;
22 TreeItem::TreeItem(TreeItem *parent)
24 // qDebug() << "Constructor TreeItem this=" << this << " parent=" << parent;
30 rootItem = parentItem->rootItem;
35 // qDebug()<<"Destr TreeItem this="<<this<<"
36 // childcount="<<childItems.count();
38 while (!childItems.isEmpty()) {
39 ti = childItems.takeFirst();
51 uuid = QUuid::createUuid();
77 backgroundColor = Qt::transparent;
79 standardFlags.setMasterRow(standardFlagsMaster);
80 userFlags.setMasterRow(userFlagsMaster);
81 systemFlags.setMasterRow(systemFlagsMaster);
84 void TreeItem::setModel(VymModel *m) { model = m; }
86 VymModel *TreeItem::getModel() { return model; }
88 int TreeItem::getRowNumAppend(TreeItem *item)
92 return attributeOffset + attributeCounter;
94 return xlinkOffset + xlinkCounter;
96 return imageOffset + imageCounter;
98 return branchOffset + branchCounter;
100 return branchOffset + branchCounter;
106 void TreeItem::appendChild(TreeItem *item)
108 item->parentItem = this;
109 item->rootItem = rootItem;
110 item->setModel(model);
112 if (item->type == Attribute) {
113 // attribute are on top of list
114 childItems.insert(attributeCounter, item);
121 if (item->type == XLink) {
122 childItems.insert(xlinkCounter + xlinkOffset, item);
128 if (item->type == Image) {
129 childItems.insert(imageCounter + imageOffset, item);
134 if (item->isBranchLikeType()) {
135 // branches are on bottom of list
136 childItems.append(item);
140 if (this == rootItem)
141 item->setType(MapCenter);
143 item->setType(Branch);
147 void TreeItem::removeChild(int row)
149 if (row < 0 || row > childItems.size() - 1)
150 qWarning("TreeItem::removeChild tried to remove non existing item?!");
152 if (childItems.at(row)->type == Attribute) {
158 if (childItems.at(row)->type == XLink) {
163 if (childItems.at(row)->type == Image) {
167 if (childItems.at(row)->isBranchLikeType())
170 childItems.removeAt(row);
174 TreeItem *TreeItem::child(int row) { return childItems.value(row); }
176 int TreeItem::childCount() const { return childItems.count(); }
178 int TreeItem::childNumber() const
181 return parentItem->childItems.indexOf(const_cast<TreeItem *>(this));
186 int TreeItem::columnCount() const { return 1; }
188 int TreeItem::branchCount() const { return branchCounter; }
190 int TreeItem::imageCount() const { return imageCounter; }
192 int TreeItem::xlinkCount() const { return xlinkCounter; }
194 int TreeItem::attributeCount() const { return attributeCounter; }
196 int TreeItem::row() const
199 return parentItem->childItems.indexOf(const_cast<TreeItem *>(this));
201 qDebug() << "TI::row() pI=NULL this=" << this << " ***************";
205 int TreeItem::depth()
218 TreeItem *TreeItem::parent()
220 // qDebug() << "TI::parent of "<<getHeadingStd()<<" is "<<parentItem;
224 bool TreeItem::isChildOf(TreeItem *ti)
226 if (this == rootItem)
228 if (parentItem == ti)
230 if (parentItem == rootItem)
232 return parentItem->isChildOf(ti);
235 int TreeItem::childNum() { return parentItem->childItems.indexOf(this); }
241 return parentItem->num(this);
244 int TreeItem::num(TreeItem *item)
248 if (!childItems.contains(item))
250 switch (item->getType()) {
252 return childItems.indexOf(item) - branchOffset;
254 return childItems.indexOf(item) - branchOffset;
256 return childItems.indexOf(item) - imageOffset;
258 return childItems.indexOf(item) - attributeOffset;
260 return childItems.indexOf(item) - xlinkOffset;
265 void TreeItem::setType(const Type t)
270 TreeItem::Type TreeItem::getType()
272 if (type == Branch && depth() == 0)
273 return MapCenter; // should not be necesssary
277 bool TreeItem::isBranchLikeType() const
279 if (type == Branch || type == MapCenter)
285 QString TreeItem::getTypeName()
289 return QString("Undefined");
291 return QString("MapCenter");
293 return QString("Branch");
295 return QString("Image");
297 return QString("Attribute");
299 return QString("XLink");
301 return QString("TreeItem::getTypeName no typename defined?!");
305 QVariant TreeItem::data(int column) const { return itemData.value(column); }
307 void TreeItem::setHeading(const VymText &vt)
310 itemData[0] = getHeadingPlain().replace("\n"," "); // used in TreeEditor
313 void TreeItem::setHeadingPlainText(const QString &s)
319 if (!heading.isRichText())
320 // Keep current color
321 vt.setColor(heading.getColor());
325 Heading TreeItem::getHeading() const { return heading; }
327 QString TreeItem::getHeadingText() { return heading.getText(); }
329 std::string TreeItem::getHeadingStd() const
331 return getHeadingPlain().toStdString();
334 QString TreeItem::getHeadingPlain() const
336 // strip beginning and tailing WS
337 return heading.getTextASCII().trimmed();
340 QString TreeItem::getHeadingPlainWithParents(uint numberOfParents = 0)
342 QString s = getHeadingPlain();
343 if (numberOfParents > 0) {
345 int l = numberOfParents;
346 while (l > 0 && ti->depth() > 0) {
349 s = ti->getHeadingPlain() + " -> " + s;
358 QString TreeItem::getHeadingDepth() // Indent by depth for debugging
361 for (int i = 0; i < depth(); i++)
363 return ds + getHeadingPlain();
366 void TreeItem::setHeadingColor(QColor color) { heading.setColor(color); }
368 QColor TreeItem::getHeadingColor() { return heading.getColor(); }
370 void TreeItem::setBackgroundColor(QColor color) { backgroundColor = color; }
372 void TreeItem::setURL(const QString &u)
376 systemFlags.activate(QString("system-url"));
378 systemFlags.deactivate(QString("system-url"));
381 QString TreeItem::getURL() { return url; }
383 void TreeItem::setVymLink(const QString &vl)
386 // We need the relative (from loading)
387 // or absolute path (from User event)
388 // and build the absolute path.
394 // If we have relative, use path of
395 // current map to build absolute path
396 // based on path of current map and relative
397 // path to linked map
398 QString p = dirname(model->getDestPath());
399 vymLink = convertToAbs(p, vl);
401 systemFlags.activate(QString("system-vymLink"));
405 systemFlags.deactivate(QString("system-vymLink"));
409 QString TreeItem::getVymLink() { return vymLink; }
411 void TreeItem::toggleTarget()
413 systemFlags.toggle(QString("system-target"));
414 target = systemFlags.isActive(QString("system-target"));
415 model->emitDataChanged(this); // FIXME-4 better call from VM?
418 bool TreeItem::isTarget() { return target; }
420 bool TreeItem::isNoteEmpty() { return note.isEmpty(); }
422 bool TreeItem::clearNote()
425 return systemFlags.deactivate(QString("system-note"));
428 bool TreeItem::setNote(const VymText &vt)
432 if (note.isEmpty()) {
433 if (systemFlags.isActive(QString("system-note")))
434 return systemFlags.deactivate(QString("system-note"));
437 if (!systemFlags.isActive(QString("system-note")))
438 return systemFlags.activate(QString("system-note"));
440 return false; // No need to update flag and reposition later
443 bool TreeItem::setNote(const VymNote &vn) { return setNote((VymText)vn); }
445 bool TreeItem::hasEmptyNote() { return note.isEmpty(); }
447 VymNote TreeItem::getNote() { return note; }
449 QString TreeItem::getNoteASCII(const QString &indent, const int &width)
451 return note.getTextASCII(indent, width);
454 QString TreeItem::getNoteASCII() { return note.getTextASCII(); }
456 void TreeItem::activateStandardFlagByName(const QString &name)
458 standardFlags.activate(name);
459 // model->emitDataChanged(this);
462 void TreeItem::deactivateStandardFlagByName(const QString &name)
464 standardFlags.deactivate(name);
465 // model->emitDataChanged(this);
468 void TreeItem::deactivateAllStandardFlags()
470 standardFlags.deactivateAll();
471 userFlags.deactivateAll();
472 // model->emitDataChanged(this);
475 Flag *TreeItem::findFlagByUid(const QUuid &uid)
477 Flag *f = standardFlagsMaster->findFlagByUid(uid);
479 f = userFlagsMaster->findFlagByUid(uid);
483 Flag *TreeItem::toggleFlagByUid(const QUuid &uid, bool useGroups)
485 Flag *f = standardFlagsMaster->findFlagByUid(uid);
487 standardFlags.toggle(uid, useGroups);
490 f = userFlagsMaster->findFlagByUid(uid);
492 userFlags.toggle(uid, useGroups);
495 qWarning() << "TI::toggleFlag failed for flag " << uid;
503 void TreeItem::toggleSystemFlag(const QString &name, FlagRow *master)
505 systemFlags.toggle(name, master);
506 model->emitDataChanged(this);
509 bool TreeItem::hasActiveFlag(const QString &name)
511 return standardFlags.isActive(name);
514 bool TreeItem::hasActiveSystemFlag(const QString &name)
516 return systemFlags.isActive(name);
519 QList<QUuid> TreeItem::activeFlagUids()
521 return standardFlags.activeFlagUids() + userFlags.activeFlagUids();
524 QList<QUuid> TreeItem::activeSystemFlagUids()
526 return systemFlags.activeFlagUids();
529 bool TreeItem::canMoveDown()
538 if (parentItem->num(this) < parentItem->branchCount() - 1)
550 bool TreeItem::canMoveUp()
557 if (parentItem->num(this) > 0)
567 ulong TreeItem::getID() { return itemID; }
569 void TreeItem::setUuid(const QString &id) { uuid = QUuid(id); }
571 QUuid TreeItem::getUuid() { return uuid; }
573 TreeItem *TreeItem::getChildNum(const int &n)
575 if (n >= 0 && n < childItems.count())
576 return childItems.at(n);
581 BranchItem *TreeItem::getFirstBranch()
583 if (branchCounter > 0)
584 return getBranchNum(0);
589 BranchItem *TreeItem::getLastBranch()
591 if (branchCounter > 0)
592 return getBranchNum(branchCounter - 1);
597 ImageItem *TreeItem::getFirstImage()
599 if (imageCounter > 0)
600 return getImageNum(imageCounter - 1);
605 ImageItem *TreeItem::getLastImage()
607 if (imageCounter > 0)
608 return getImageNum(imageCounter - 1);
613 BranchItem *TreeItem::getNextBranch(BranchItem *currentBranch)
617 int n = num(currentBranch) + 1;
618 if (n < branchCounter)
619 return getBranchNum(branchOffset + n);
624 BranchItem *TreeItem::getBranchNum(const int &n)
626 if (n >= 0 && n < branchCounter)
627 return (BranchItem *)getChildNum(branchOffset + n);
632 BranchObj *TreeItem::getBranchObjNum(const int &n)
634 if (n >= 0 && n < branchCounter) {
635 BranchItem *bi = getBranchNum(n);
637 BranchObj *bo = (BranchObj *)(bi->getLMO());
641 qDebug() << "TI::getBONum bo=NULL";
647 ImageItem *TreeItem::getImageNum(const int &n)
649 if (n >= 0 && n < imageCounter)
650 return (ImageItem *)getChildNum(imageOffset + n);
655 FloatImageObj *TreeItem::getImageObjNum(const int &n)
657 if (imageCounter > 0)
658 return (FloatImageObj *)(getImageNum(n)->getLMO());
663 AttributeItem *TreeItem::getAttributeNum(const int &n)
665 if (n >= 0 && n < attributeCounter)
666 return (AttributeItem *)getChildNum(attributeOffset + n);
671 AttributeItem *TreeItem::getAttributeByKey(const QString &k)
674 for (int i = 0; i < attributeCount(); i++) {
675 ai = getAttributeNum(i);
676 if (ai->getKey() == k) return ai;
681 XLinkItem *TreeItem::getXLinkItemNum(const int &n)
683 if (n >= 0 && n < xlinkCounter)
684 return (XLinkItem *)getChildNum(xlinkOffset + n);
689 XLinkObj *TreeItem::getXLinkObjNum(const int &n)
691 if (xlinkCounter > 0) {
692 XLinkItem *xli = getXLinkItemNum(n);
694 Link *l = xli->getLink();
696 return l->getXLinkObj();
702 void TreeItem::setHideTmp(HideTmpMode mode)
704 if (type == Image || type == Branch || type == MapCenter)
705 // ((ImageItem*)this)->updateVisibility();
707 // LinkableMapObj* lmo=((MapItem*)this)->getLMO();
709 if (mode == HideExport &&
711 hasHiddenExportParent())) // FIXME-4 try to avoid calling
712 // hasScrolledParent repeatedly
714 // Hide stuff according to hideExport flag and parents
717 // Do not hide, but still take care of scrolled status
720 // And take care of my children
721 for (int i = 0; i < branchCount(); ++i)
722 getBranchNum(i)->setHideTmp(mode);
726 bool TreeItem::hasHiddenExportParent()
728 // Calls parents recursivly to
729 // find out, if we or parents are temp. hidden
731 if (hidden || hideExport)
735 return parentItem->hasHiddenExportParent();
740 void TreeItem::setHideInExport(bool b)
742 if (type == MapCenter || type == Branch || type == Image) {
745 systemFlags.activate(QString("system-hideInExport"));
747 systemFlags.deactivate(QString("system-hideInExport"));
751 bool TreeItem::hideInExport() { return hideExport; }
753 void TreeItem::updateVisibility()
755 // overloaded in derived objects
758 bool TreeItem::isHidden() { return hidden; }
760 QString TreeItem::getGeneralAttr()
764 s += attribut("hideInExport", "true");
766 s += attribut("url", url);
767 if (!vymLink.isEmpty())
768 s += attribut("vymLink", convertToRel(model->getDestPath(), vymLink));
771 s += attribut("localTarget", "true");