1 #include "branchitem.h"
3 #include "attributeitem.h"
11 extern TaskModel *taskModel;
15 BranchItem::BranchItem(const QList<QVariant> &data, TreeItem *parent):MapItem (data,parent)
17 //qDebug()<< "Constr. BranchItem this="<<this;
19 // Set type if parent is known yet
20 // if not, type is set in insertBranch or TreeItem::appendChild
29 includeImagesVer=false;
30 includeImagesHor=false;
31 includeChildren=false;
32 childrenLayout = BranchItem::AutoPositioning;
34 lastSelectedBranchNum=-1;
35 lastSelectedBranchNumAlt=-1;
40 BranchItem::~BranchItem()
42 //qDebug()<< "Destr. BranchItem this="<<this<<" "<<getHeading();
51 void BranchItem::clear()
53 if (task) taskModel->deleteTask (task);
56 void BranchItem::copy (BranchItem *other) // TODO lacks most of data...
58 scrolled=other->scrolled;
59 tmpUnscrolled=other->tmpUnscrolled;
62 BranchItem* BranchItem::parentBranch ()
64 return (BranchItem*) parentItem;
67 void BranchItem::insertBranch (int pos, BranchItem *branch)
70 if (pos>branchCounter) pos=branchCounter;
71 childItems.insert(pos+branchOffset,branch);
72 branch->parentItem=this;
73 branch->rootItem=rootItem;
74 branch->setModel (model);
75 if (parentItem==rootItem)
82 branchOffset=childItems.count()-1;
86 QString BranchItem::saveToDir (const QString &tmpdir,const QString &prefix, const QPointF& offset, QList <Link*> &tmpLinks )
88 // Cloudy stuff can be hidden during exports
89 if (hidden) return QString();
92 QString idAttr=attribut("uuid",uuid.toString());
96 // Update of note is usually done while unselecting a branch
100 scrolledAttr=attribut ("scrolled","yes");
104 // save area, if not scrolled // not needed if HTML is rewritten...
105 // also we could check if _any_ of parents is scrolled
107 if (mo && parentItem->isBranchLikeType() && !((BranchItem*)parentItem)->isScrolled() )
109 qreal x=mo->getAbsPos().x();
110 qreal y=mo->getAbsPos().y();
112 attribut("x1",QString().setNum(x-offset.x())) +
113 attribut("y1",QString().setNum(y-offset.y())) +
114 attribut("x2",QString().setNum(x+mo->width()-offset.x())) +
115 attribut("y2",QString().setNum(y+mo->height()-offset.y()));
121 if (parentItem==rootItem)
122 elementName="mapcenter";
124 elementName="branch";
126 // Free positioning of children
128 if (childrenLayout == BranchItem::FreePositioning)
129 layoutAttr += attribut ("childrenFreePos","true");
133 if (mo && mo->getRotation() !=0 )
134 rotAttr=attribut ("rotation",QString().setNum (mo->getRotation() ) );
136 s=beginElement (elementName
140 + getIncludeImageAttr()
148 s += heading.saveToDir();
150 // Save frame // not saved if there is no MO
153 // Avoid saving NoFrame for objects other than MapCenter
154 if (depth() == 0 || ((OrnamentedObj*)mo)->getFrame()->getFrameType()!=FrameObj::NoFrame)
155 s+=((OrnamentedObj*)mo)->getFrame()->saveToDir ();
158 // save names of flags set
159 s+=standardFlags.saveToDir(tmpdir,prefix,0);
162 for (int i=0; i<imageCount(); ++i)
163 s+=getImageNum(i)->saveToDir (tmpdir,prefix);
166 for (int i=0; i<attributeCount(); ++i)
167 s+=getAttributeNum(i)->getDataXML();
170 if (!note.isEmpty() )
175 s+=task->saveToDir();
179 TreeItem *ti=getBranchNum(i);
182 s+=getBranchNum(i)->saveToDir(tmpdir,prefix,offset,tmpLinks);
187 // Mark Links for save
188 for (int i=0; i<xlinkCount(); ++i)
190 Link *l=getXLinkItemNum (i)->getLink();
191 if (l && !tmpLinks.contains (l)) tmpLinks.append (l);
194 s += endElement (elementName);
198 void BranchItem::updateVisibility()
200 // Needed to hide relinked branch, if parent is scrolled
203 if (hasScrolledParent(this) || hidden)
204 mo->setVisibility (false);
206 mo->setVisibility (true);
210 void BranchItem::setHeadingColor (QColor color)
212 TreeItem::setHeadingColor (color);
213 if (mo) ((BranchObj*)mo)->setColor (color);
216 void BranchItem::updateTaskFlag()
218 systemFlags.deactivateGroup ("system-tasks");
221 QString s="system-" + task->getIconString();
222 systemFlags.activate (s);
223 model->emitDataChanged(this);
227 void BranchItem::setTask(Task *t)
233 Task* BranchItem::getTask()
238 void BranchItem::unScroll()
240 if (tmpUnscrolled) resetTmpUnscroll();
241 if (scrolled) toggleScroll();
244 bool BranchItem::toggleScroll()
246 // MapCenters are not scrollable
247 if (depth()==0) return false;
253 systemFlags.deactivate("system-scrolledright");
255 for (int i=0;i<branchCounter;++i)
257 bo=(BranchObj*)(getBranchNum(i)->getMO());
258 if (bo) bo->setVisibility(true);
263 systemFlags.activate("system-scrolledright");
265 for (int i=0;i<branchCounter;++i)
267 bo=(BranchObj*)(getBranchNum(i)->getMO());
268 if (bo) bo->setVisibility(false);
274 bool BranchItem::isScrolled()
279 bool BranchItem::hasScrolledParent(BranchItem *start)
281 // Calls parents recursivly to
282 // find out, if we are scrolled at all.
283 // But ignore myself, just look at parents.
285 if (!start) start=this;
287 if (this !=start && scrolled) return true;
289 BranchItem* bi=(BranchItem*)parentItem;
290 if (bi && bi!=rootItem )
291 return bi->hasScrolledParent(start);
296 bool BranchItem::tmpUnscroll(BranchItem *start)
300 if (!start) start=this;
302 // Unscroll parent (recursivly)
303 BranchItem * pi=(BranchItem*)parentItem;
304 if (pi && pi->isBranchLikeType() ) result=pi->tmpUnscroll(start);
307 if (start !=this && scrolled)
310 systemFlags.activate("system-tmpUnscrolledRight");
312 model->emitDataChanged (this);
318 bool BranchItem::resetTmpUnscroll()
322 // Unscroll parent (recursivly)
323 BranchItem * pi=(BranchItem*)parentItem;
324 if (pi && pi->isBranchLikeType() ) result=pi->resetTmpUnscroll();
330 systemFlags.deactivate("system-tmpUnscrolledRight");
332 model->emitDataChanged (this);
338 void BranchItem::sortChildren(bool inverse) //FIXME-4 optimize by not using moveUp/Down
340 int childCount=branchCounter;
342 bool madeChanges=false;
346 for(curChildIndex=1;curChildIndex<childCount;curChildIndex++)
348 BranchItem* curChild =getBranchNum(curChildIndex);
349 BranchItem* prevChild=getBranchNum(curChildIndex-1);
352 if (prevChild->getHeadingPlain().compare(curChild->getHeadingPlain())<0)
354 model->moveUp (curChild);
358 if (prevChild->getHeadingPlain().compare(curChild->getHeadingPlain())>0)
360 model->moveUp (curChild);
367 void BranchItem::setChildrenLayout(BranchItem::LayoutHint layoutHint)
369 childrenLayout = layoutHint;
372 BranchItem::LayoutHint BranchItem::getChildrenLayout()
374 return childrenLayout;
377 void BranchItem::setIncludeImagesVer(bool b)
382 bool BranchItem::getIncludeImagesVer()
384 return includeImagesVer;
387 void BranchItem::setIncludeImagesHor(bool b)
392 bool BranchItem::getIncludeImagesHor()
394 return includeImagesHor;
397 QString BranchItem::getIncludeImageAttr()
400 if (includeImagesVer)
401 a=attribut ("incImgV","true");
402 if (includeImagesHor)
403 a+=attribut ("incImgH","true");
407 BranchItem* BranchItem::getFramedParentBranch(BranchItem *start)
409 BranchObj *bo=getBranchObj();
410 if (bo && bo->getFrameType() != FrameObj::NoFrame)
412 if (bo->getFrame()->getFrameIncludeChildren() ) return this;
413 if (this == start) return this;
415 BranchItem* bi=(BranchItem*)parentItem;
416 if (bi && bi!=rootItem )
417 return bi->getFramedParentBranch(start);
422 void BranchItem::setFrameIncludeChildren(bool b)
424 includeChildren=b; // FIXME-4 ugly: same information stored in FrameObj
425 BranchObj *bo=getBranchObj();
426 if (bo) bo->getFrame()->setFrameIncludeChildren(b);
429 bool BranchItem::getFrameIncludeChildren()
431 BranchObj *bo=getBranchObj();
433 return bo->getFrame()->getFrameIncludeChildren();
435 return includeChildren;
438 void BranchItem::setLastSelectedBranch()
444 // Hack to save an additional lastSelected for mapcenters in MapEditor
445 // depending on orientation
446 // this allows to go both left and right from there
447 if (mo && ((BranchObj*)mo)->getOrientation()==LinkableMapObj::LeftOfCenter)
449 ((BranchItem*)parentItem)->lastSelectedBranchNumAlt=parentItem->num(this);
452 ((BranchItem*)parentItem)->lastSelectedBranchNum=parentItem->num(this);
456 void BranchItem::setLastSelectedBranch(int i)
458 lastSelectedBranchNum=i;
461 BranchItem* BranchItem::getLastSelectedBranch()
463 if (lastSelectedBranchNum>=branchCounter)
464 return getBranchNum (branchCounter-1);
466 return getBranchNum (lastSelectedBranchNum);
469 BranchItem* BranchItem::getLastSelectedBranchAlt()
471 return getBranchNum (lastSelectedBranchNumAlt);
477 TreeItem* BranchItem::findMapItem (QPointF p, TreeItem* excludeTI)
481 for (int i=0; i<branchCount(); ++i)
483 ti=getBranchNum(i)->findMapItem(p, excludeTI);
484 if (ti != NULL) return ti;
489 for (int i=0; i<imageCount(); ++i )
492 MapObj *mo=ii->getMO();
493 if (mo && mo->isInClickBox(p) &&
501 if (getBranchObj()->isInClickBox (p) && (this != excludeTI) && getBranchObj()->isVisibleObj() )
507 for (int i=0; i<attributeCount(); ++i )
509 ai=getAttributeNum (i);
510 MapObj *mo=ai->getMO();
511 if (mo && mo->isInClickBox(p) &&
520 void BranchItem::updateStyles(const bool &keepFrame)
522 // Update styles when relinking branches
525 BranchObj *bo=getBranchObj();
526 if ( parentItem != rootItem)
527 bo->setParObj ( (LinkableMapObj*) ( ((MapItem*)parentItem)->getMO() ) );
529 bo->setParObj (NULL);
530 bo->setDefAttr(BranchObj::MovedBranch,keepFrame);
534 BranchObj* BranchItem::getBranchObj()
536 return (BranchObj*)mo;
539 BranchObj* BranchItem::createMapObj(QGraphicsScene *scene)
543 if (parentItem==rootItem)
545 newbo=new BranchObj(NULL,this);
547 scene->addItem (newbo);
550 newbo=new BranchObj( ((MapItem*)parentItem)->getMO(),this);
552 // Set visibility depending on parents
553 if (parentItem!=rootItem &&
554 ( ((BranchItem*)parentItem)->scrolled || !((MapItem*)parentItem)->getLMO()->isVisibleObj() ) )
555 newbo->setVisibility (false);
559 qreal a= -M_PI_4 + M_PI_2 * (num()) + (M_PI_4/2)*(num()/4 % 4);
560 QPointF p (r*cos (a), r*sin (a));
561 newbo->setRelPos (p);
564 newbo->setDefAttr(BranchObj::NewBranch);
567 if (!getHeading().isEmpty() )
570 newbo->setColor (heading.getColor());