3 #include "branchitem.h"
6 #include "mainwindow.h"
9 extern FlagRow *standardFlagsMaster;
10 extern FlagRow *systemFlagsMaster;
12 /////////////////////////////////////////////////////////////////
14 /////////////////////////////////////////////////////////////////
16 BranchObj::BranchObj () :OrnamentedObj() // FIXME-3 needed at all?
18 // cout << "Const BranchObj ()\n";
23 BranchObj::BranchObj (QGraphicsScene* s):OrnamentedObj (s)// FIXME-3 needed at all?
25 // cout << "Const BranchObj (s) \n";
31 BranchObj::BranchObj (QGraphicsScene* s, LinkableMapObj* p):OrnamentedObj (s)// FIXME-3 needed at all?
33 // cout << "Const BranchObj (s,p)\n";
36 if (treeItem->depth()==1) // FIXME-3 needed to recursively calc depth?
37 // Calc angle to mapCenter if I am a mainbranch
38 // needed for reordering the mainbranches clockwise
40 angle=getAngle (QPointF (x() - parObj->getChildPos().x() ,
41 (y() - parObj->getChildPos().y() ) ) );
45 BranchObj::~BranchObj ()
47 // cout << "Destr BranchObj of "<<this<<" ("<<treeItem->getHeading().toStdString()<<")"<<endl;
49 // If I'm animated, I need to un-animate myself first
50 if (anim.isAnimated() )
52 anim.setAnimated (false);
53 VymModel *model=treeItem->getModel();
54 model->stopAnimation (this);
57 // Check, if this branch was the last child to be deleted
58 // If so, unset the scrolled flags in parent // FIXME-2 better do this in model?
61 BranchObj *po=(BranchObj*)parObj;
65 bo=((BranchObj*)parObj)->getLastBranch();
66 if (bo) po->unScroll();
72 bool BranchObj::operator< ( const BranchObj & other )
74 return angle < other.angle;
77 bool BranchObj::operator== ( const BranchObj & other )
79 return angle == other.angle;
82 void BranchObj::init ()
87 absPos+=parObj->getChildPos();
90 includeImagesVer=false;
91 includeImagesHor=false;
94 void BranchObj::copy (BranchObj* other)
96 OrnamentedObj::copy(other);
98 for (int i=0; i<other->floatimage.size(); ++i)
99 addFloatImage (other->floatimage.at(i));
101 setVisibility (other->visible);
108 void BranchObj::clear()
110 //setVisibility (true); //FIXME-4 needed?
112 while (!floatimage.isEmpty())
113 delete floatimage.takeFirst();
115 while (!xlink.isEmpty())
116 delete xlink.takeFirst();
119 bool isAbove (BranchObj* a, BranchObj *b)
121 if (a->angle < b->angle)
127 void BranchObj::setParObjTmp(LinkableMapObj* lmo, QPointF m, int off)
129 // Temporary link to lmo
130 // m is position of mouse pointer
131 // offset 0: default 1: below lmo -1 above lmo (if possible)
133 BranchItem *pi=(BranchItem*)(lmo->getTreeItem()->parent());
134 int pi_depth=pi->depth();
135 BranchObj* o=(BranchObj*)(lmo);
139 // ignore mapcenter and mainbranch
140 if (pi_depth<2) off=0;
147 // FIXME-2 depth=parObj->getDepth()+1;
149 // setLinkStyle calls updateLink, only set it once
150 if (style!=getDefLinkStyle() ) setLinkStyle (getDefLinkStyle());
152 // Move temporary to new position at destination
153 // Usually the positioning would be done by reposition(),
154 // but then also the destination branch would "Jump" around...
155 // Better just do it approximately
156 if (treeItem->depth()==1) // FIXME-3 needed to recursively calc depth?
157 { // new parent is the mapcenter itself
159 QPointF p= normalise ( QPointF (m.x() - o->getChildPos().x(),
160 m.y() - o->getChildPos().y() ));
161 if (p.x()<0) p.setX( p.x()-bbox.width() );
168 // new parent is just a branch, link to it
169 QRectF t=o->getBBoxSizeWithChildren();
170 if (o->getTreeItem()->getLastBranch())
171 y=t.y() + t.height() ;
178 // we want to link above lmo
179 y=o->y() - height() + 5;
181 // we want to link below lmo
182 // Bottom of sel should be 5 pixels above
183 // the bottom of the branch _below_ the target:
184 // Don't try to find that branch, guess 12 pixels
185 y=o->getChildPos().y() -height() + 12;
187 if (o->getOrientation()==LinkableMapObj::LeftOfCenter)
188 move ( o->getChildPos().x() - linkwidth, y );
190 move (o->getChildPos().x() + linkwidth, y );
193 // updateLink is called implicitly in move
197 void BranchObj::unsetParObjTmp()
204 //FIXME-2 depth=parObj->getDepth()+1;
205 setLinkStyle (getDefLinkStyle() );
210 void BranchObj::setVisibility(bool v, int toDepth)
212 BranchItem *bi=(BranchItem*)treeItem;
213 if (bi->depth() <= toDepth)
215 frame->setVisibility(v);
216 heading->setVisibility(v);
217 systemFlags->setVisibility(v);
218 standardFlags->setVisibility(v);
219 LinkableMapObj::setVisibility (v);
221 for (i=0; i<floatimage.size(); ++i)
222 floatimage.at(i)->setVisibility (v);
223 for (i=0; i<xlink.size(); ++i)
224 xlink.at(i)->setVisibility ();
226 // Only change children, if I am not scrolled
227 if (! bi->isScrolled() && (bi->depth() < toDepth))
229 // Now go recursivly through all children
230 for (i=0; i<treeItem->branchCount(); ++i)
231 treeItem->getBranchObjNum(i)->setVisibility (v,toDepth);
233 } // depth <= toDepth
237 void BranchObj::setVisibility(bool v)
239 setVisibility (v,MAX_DEPTH);
243 void BranchObj::setLinkColor ()
245 // Overloaded from LinkableMapObj
246 // BranchObj can use color of heading
248 VymModel *model=treeItem->getModel();
251 if (model->getMapLinkColorHint()==HeadingColor)
252 LinkableMapObj::setLinkColor (heading->getColor() );
254 LinkableMapObj::setLinkColor ();
258 void BranchObj::updateContentSize()
265 void BranchObj::positionContents()
267 for (int i=0; i<floatimage.size(); ++i )
268 floatimage.at(i)->reposition();
269 OrnamentedObj::positionContents();
272 void BranchObj::move (double x, double y)
274 OrnamentedObj::move (x,y);
275 for (int i=0; i<floatimage.size(); ++i )
276 floatimage.at(i)->reposition();
280 void BranchObj::move (QPointF p)
285 void BranchObj::moveBy (double x, double y)
287 OrnamentedObj::moveBy (x,y);
288 for (int i=0; i<treeItem->branchCount(); ++i)
289 treeItem->getBranchObjNum(i)->moveBy (x,y);
293 void BranchObj::moveBy (QPointF p)
295 moveBy (p.x(), p.y());
299 void BranchObj::positionBBox()
301 QPointF ap=getAbsPos();
302 bbox.moveTopLeft (ap);
306 frame->setRect(QRectF(bbox.x(),bbox.y(),bbox.width(),bbox.height() ) );
308 // Update links to other branches
309 for (int i=0; i<xlink.size(); ++i)
310 xlink.at(i)->updateXLink();
313 void BranchObj::calcBBoxSize()
315 QSizeF heading_r=heading->getSize();
316 qreal heading_w=(qreal) heading_r.width() ;
317 qreal heading_h=(qreal) heading_r.height() ;
318 QSizeF sysflags_r=systemFlags->getSize();
319 qreal sysflags_h=sysflags_r.height();
320 qreal sysflags_w=sysflags_r.width();
321 QSizeF stanflags_r=standardFlags->getSize();
322 qreal stanflags_h=stanflags_r.height();
323 qreal stanflags_w=stanflags_r.width();
327 // set width to sum of all widths
328 w=heading_w + sysflags_w + stanflags_w;
329 // set height to maximum needed height
330 h=max (sysflags_h,stanflags_h);
333 // Save the dimension of flags and heading
334 ornamentsBBox.setSize ( QSizeF(w,h));
336 // clickBox includes Flags and Heading
337 clickBox.setSize (ornamentsBBox.size() );
342 topPad=botPad=leftPad=rightPad=0;
343 if (includeImagesVer || includeImagesHor)
345 if (treeItem->imageCount()>0)
347 for (int i=0; i<floatimage.size(); ++i )
349 rp=floatimage.at(i)->getRelPos();
350 if (includeImagesVer)
353 topPad=max (topPad,-rp.y()-h);
354 if (rp.y()+floatimage.at(i)->height() > 0)
355 botPad=max (botPad,rp.y()+floatimage.at(i)->height());
357 if (includeImagesHor)
359 if (orientation==LinkableMapObj::RightOfCenter)
362 leftPad=max (leftPad,-rp.x()-w);
363 if (rp.x()+floatimage.at(i)->width() > 0)
364 rightPad=max (rightPad,rp.x()+floatimage.at(i)->width());
368 leftPad=max (leftPad,-rp.x());
369 if (rp.x()+floatimage.at(i)->width() > w)
370 rightPad=max (rightPad,rp.x()+floatimage.at(i)->width()-w);
380 w+=frame->getPadding();
381 h+=frame->getPadding();
384 bbox.setSize (QSizeF (w,h));
387 void BranchObj::setDockPos()
389 if (treeItem->getType()==TreeItem::MapCenter)
391 // set childPos to middle of MapCenterObj
392 childPos.setX( clickBox.topLeft().x() + clickBox.width()/2 );
393 childPos.setY( clickBox.topLeft().y() + clickBox.height()/2 );
395 for (int i=0; i<treeItem->branchCount(); ++i)
396 treeItem->getBranchObjNum(i)->updateLink();
400 // Sets childpos and parpos depending on orientation
401 if (getOrientation()==LinkableMapObj::LeftOfCenter )
404 ornamentsBBox.bottomLeft().x(),
407 ornamentsBBox.bottomRight().x(),
412 ornamentsBBox.bottomRight().x(),
415 ornamentsBBox.bottomLeft().x(),
421 void BranchObj::updateData()
426 qWarning ("BranchObj::udpateHeading treeItem==NULL");
429 QString s=treeItem->getHeading();
430 if (s!=heading->text())
432 heading->setText (s);
435 QStringList TIactiveFlags=treeItem->activeStandardFlagNames();
437 // Add missing standard flags active in TreeItem
438 for (int i=0;i<=TIactiveFlags.size()-1;i++)
440 if (!standardFlags->isActive (TIactiveFlags.at(i) ))
442 Flag *f=standardFlagsMaster->getFlag(TIactiveFlags.at(i));
443 if (f) standardFlags->activate (f);
447 // Remove standard flags no longer active in TreeItem
448 QStringList BOactiveFlags=standardFlags->activeFlagNames();
449 for (int i=0;i<BOactiveFlags.size();++i)
450 if (!TIactiveFlags.contains (BOactiveFlags.at(i)))
452 standardFlags->deactivate (BOactiveFlags.at(i));
456 // Add missing system flags active in TreeItem
457 TIactiveFlags=treeItem->activeSystemFlagNames();
458 for (int i=0;i<TIactiveFlags.size();++i)
460 if (!systemFlags->isActive (TIactiveFlags.at(i) ))
462 Flag *f=systemFlagsMaster->getFlag(TIactiveFlags.at(i));
463 if (f) systemFlags->activate (f);
467 // Remove system flags no longer active in TreeItem
468 BOactiveFlags=systemFlags->activeFlagNames();
469 for (int i=0;i<BOactiveFlags.size();++i)
471 if (!TIactiveFlags.contains (BOactiveFlags.at(i)))
473 systemFlags->deactivate (BOactiveFlags.at(i));
483 void BranchObj::addXLink (XLinkObj *xlo)
489 void BranchObj::removeXLinkRef (XLinkObj *xlo)
491 xlink.removeAt (xlink.indexOf(xlo));
494 void BranchObj::deleteXLink(XLinkObj *xlo)
497 if (!xlo->isUsed()) delete (xlo);
500 void BranchObj::deleteXLinkAt (int i)
502 XLinkObj *xlo=xlink.at(i);
504 if (!xlo->isUsed()) delete(xlo);
507 XLinkObj* BranchObj::XLinkAt (int i)
512 BranchObj* BranchObj::XLinkTargetAt (int i)
514 if (i>=0 && i<xlink.size())
517 return xlink.at(i)->otherBranch (this);
522 void BranchObj::setIncludeImagesVer(bool b)
530 bool BranchObj::getIncludeImagesVer()
532 return includeImagesVer;
535 void BranchObj::setIncludeImagesHor(bool b)
543 bool BranchObj::getIncludeImagesHor()
545 return includeImagesHor;
548 QString BranchObj::getIncludeImageAttr()
551 if (includeImagesVer)
552 a=attribut ("incImgV","true");
554 a=attribut ("incImgV","false");
555 if (includeImagesHor)
556 a+=attribut ("incImgH","true");
558 a+=attribut ("incImgH","false");
562 FloatImageObj* BranchObj::addFloatImage ()
564 FloatImageObj *newfi=new FloatImageObj (scene,this);
565 floatimage.append (newfi);
566 if ( ((BranchItem*)treeItem)->hasScrolledParent((BranchItem*)treeItem) )
567 newfi->setVisibility (false);
569 newfi->setVisibility(visible);
578 FloatImageObj* BranchObj::addFloatImage (FloatImageObj *fio)
580 FloatImageObj *newfi=new FloatImageObj (scene,this);
581 floatimage.append (newfi);
583 if (((BranchItem*)treeItem)->hasScrolledParent((BranchItem*)treeItem) )
584 newfi->setVisibility (false);
586 newfi->setVisibility(visible);
595 FloatImageObj* BranchObj::getFirstFloatImage ()
597 return floatimage.first();
600 FloatImageObj* BranchObj::getLastFloatImage ()
602 return floatimage.last();
605 FloatImageObj* BranchObj::getFloatImageNum (const uint &i)
607 return floatimage.at(i);
610 void BranchObj::removeFloatImage (FloatImageObj *fio)
612 int i=floatimage.indexOf (fio);
613 if (i>-1) delete (floatimage.takeAt (i));
619 void BranchObj::savePosInAngle ()
621 // Save position in angle
622 for (int i=0; i<treeItem->branchCount(); ++i)
623 treeItem->getBranchObjNum(i)->angle=i;
626 void BranchObj::setDefAttr (BranchModification mod)
629 switch (treeItem->depth())
631 case 0: fontsize=16; break;
632 case 1: fontsize=12; break;
633 default: fontsize=10; break;
637 setLinkStyle(getDefLinkStyle());
638 QFont font("Sans Serif,8,-1,5,50,0,0,0,0,0");
639 font.setPointSize(fontsize);
640 heading->setFont(font );
643 setColor (treeItem->getHeadingColor() );
647 void BranchObj::sortChildren() //FIXME-2
650 int childCount=branch.count();
652 bool madeChanges=false;
656 for(curChildIndex=1;curChildIndex<childCount;curChildIndex++){
657 BranchObj* curChild=(BranchObj*)treeItem->getBranchObjNum(curChildIndex);
658 BranchObj* prevChild=(BranchObj*)treeItem->getBranchObjNum(curChildIndex-1);
659 if(prevChild->heading->text().compare(curChild->heading->text())>0)
661 this->moveBranchUp(curChild);
669 void BranchObj::alignRelativeTo (QPointF ref,bool alignSelf)
671 qreal th = bboxTotal.height();
672 int depth=treeItem->depth(); // FIXME-3 needed to recursively calc depth?
676 QString h=QString (depth,' ');
677 h+=treeItem->getHeading();
680 QPointF pp; if (parObj) pp=parObj->getChildPos();
681 cout << "BO::alignRelTo ";
682 cout<<h.toStdString();
683 cout << " d="<<depth<<
684 //cout<< " ref="<<ref<<
685 " bbox.tL="<<bboxTotal.topLeft()<<
687 // " relPos="<<relPos<<
689 " w="<<bbox.width()<<
690 " h="<<bbox.height()<<
691 // " orient="<<orientation<<
692 // " alignSelf="<<alignSelf<<
693 // " scrolled="<<((BranchItem*)treeItem)->isScrolled()<<
694 // " pad="<<topPad<<","<<botPad<<","<<leftPad<<","<<rightPad<<
695 // " hidden="<<hidden<<
703 if (depth==1 && parObj)
705 // Position relatively, if needed
706 //if (useRelPos) move2RelPos (relPos.x(), relPos.y());
708 // Calc angle to mapCenter if I am a mainbranch
709 // needed for reordering the mainbranches clockwise
711 angle=getAngle (QPointF ((int)(x() - parObj->getChildPos().x() ),
712 (int)(y() - parObj->getChildPos().y() ) ) );
716 // Align myself depending on orientation and parent, but
717 // only if I am not a mainbranch or mapcenter itself
719 if (anim.isAnimated())
724 LinkableMapObj::Orientation o;
725 o=parObj->getOrientation();
729 case LinkableMapObj::LeftOfCenter:
730 move (ref.x() - bbox.width(), ref.y() + (th-bbox.height())/2 );
731 //move (ref.x() , ref.y() + (th-bbox.height())/2 );
733 case LinkableMapObj::RightOfCenter:
734 move (ref.x() , ref.y() + (th-bbox.height())/2 );
737 qWarning ("LMO::alignRelativeTo: oops, no orientation given...");
743 if ( ((BranchItem*)treeItem)->isScrolled() ) return;
745 // Set reference point for alignment of children
747 if (orientation==LinkableMapObj::LeftOfCenter)
748 ref2.setX(bbox.topLeft().x() - linkwidth);
750 ref2.setX(bbox.topRight().x() + linkwidth);
753 ref2.setY(absPos.y()-(bboxTotal.height()-bbox.height())/2);
757 // Align the children depending on reference point
758 for (int i=0; i<treeItem->branchCount(); ++i)
760 if (!treeItem->getBranchNum(i)->isHidden())
762 treeItem->getBranchObjNum(i)->alignRelativeTo (ref2,true);
764 // append next branch below current one
765 ref2.setY(ref2.y() + treeItem->getBranchObjNum(i)->getBBoxSizeWithChildren().height() );
771 void BranchObj::reposition()
774 if (!treeItem->getHeading().isEmpty())
775 cout << "BO::reposition "<<qPrintable(treeItem->getHeading())<<endl;
777 cout << "BO::reposition ???"<<endl;
778 // cout << " orient="<<orientation<<endl;
781 if (treeItem->depth()==0)
783 // only calculate the sizes once. If the deepest LMO
784 // changes its height,
785 // all upper LMOs have to change, too.
786 calcBBoxSizeWithChildren();
787 updateLink(); // This update is needed if the scene is resized
788 // due to excessive moving of a FIO
790 alignRelativeTo ( QPointF (absPos.x(),
791 absPos.y()-(bboxTotal.height()-bbox.height())/2) );
792 //FIXME-2 qSort (branch.begin(),branch.end(), isAbove);
793 positionBBox(); // Reposition bbox and contents
796 // This is only important for moving branches:
797 // For editing a branch it isn't called...
798 alignRelativeTo ( QPointF (absPos.x(),
799 absPos.y()-(bboxTotal.height()-bbox.height())/2) );
803 void BranchObj::unsetAllRepositionRequests()
805 repositionRequest=false;
806 for (int i=0; i<treeItem->branchCount(); ++i)
807 treeItem->getBranchObjNum(i)->unsetAllRepositionRequests();
811 QPolygonF BranchObj::shape()
815 QRectF r=getTotalBBox();
816 if (orientation==LinkableMapObj::LeftOfCenter)
819 <<QPointF (bbox.topLeft().x(), r.topLeft().y() )
822 <<QPointF (bbox.bottomLeft().x(), r.bottomLeft().y() ) ;
826 <<QPointF (bbox.topRight().x(), r.topRight().y() )
829 <<QPointF (bbox.bottomRight().x(), r.bottomRight().y() ) ;
833 QRectF BranchObj::getTotalBBox()
837 if ( ((BranchItem*)treeItem)->isScrolled() ) return r;
839 for (int i=0; i<treeItem->branchCount(); ++i)
840 if (!treeItem->getBranchNum(i)->isHidden())
841 r=addBBox(treeItem->getBranchObjNum(i)->getTotalBBox(),r);
843 /* FIXME-3 lots of occurences of treeItem->getBranchObjNum(i) in branchobj.cpp
844 better check if they are not NULL and maybe simplify...
845 (have been NULL at least in calcBBoxSizeWithChilds...)
849 FIXME-3 for (int i=0; i<floatimage.size(); ++i)
850 if (!floatimage.at(i)->isHidden())
851 r=addBBox(floatimage.at(i)->getTotalBBox(),r);
856 QRectF BranchObj::getBBoxSizeWithChildren()
861 void BranchObj::calcBBoxSizeWithChildren()
863 // This is initially called only from reposition and
864 // and only for mapcenter. So it won't be
865 // called more than once for a single user
869 // Calculate size of LMO including all children (to align them later)
870 bboxTotal.setX(bbox.x() );
871 bboxTotal.setY(bbox.y() );
873 // if branch is scrolled, ignore children, but still consider floatimages
874 BranchItem *bi=(BranchItem*)treeItem;
875 if ( bi->isScrolled() )
877 bboxTotal.setWidth (bbox.width());
878 bboxTotal.setHeight(bbox.height());
884 bboxTotal.setWidth (0);
885 bboxTotal.setHeight(0);
888 bboxTotal.setX (parObj->x());
889 bboxTotal.setY (parObj->y());
892 bboxTotal.setX (bbox.x());
893 bboxTotal.setY (bbox.y());
900 // Now calculate recursivly
904 for (int i=0; i<treeItem->branchCount(); i++)
906 if (!bi->getBranchNum(i)->isHidden())
908 bi->getBranchObjNum(i)->calcBBoxSizeWithChildren();
909 br=bi->getBranchObjNum(i)->getBBoxSizeWithChildren();
910 r.setWidth( max (br.width(), r.width() ));
911 r.setHeight(br.height() + r.height() );
912 if (br.y()<bboxTotal.y()) bboxTotal.setY(br.y());
915 // Add myself and also
916 // add width of link to sum if necessary
917 if (bi->branchCount()<1)
918 bboxTotal.setWidth (bbox.width() + r.width() );
920 bboxTotal.setWidth (bbox.width() + r.width() + linkwidth);
922 bboxTotal.setHeight(max (r.height(), bbox.height()));
925 QString BranchObj::getSelectString()
927 VymModel *model=treeItem->getModel();
929 return model->getSelectString (this);
934 void BranchObj::setAnimation(const AnimPoint &ap)
939 bool BranchObj::animate()
942 if ( anim.isAnimated() )
947 parObj->reposition(); // we might have been relinked meanwhile