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 updateLinkGeometry, 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 // updateLinkGeometry is called implicitly in move
197 void BranchObj::unsetParObjTmp()
204 //FIXME-2 depth=parObj->getDepth()+1;
205 setLinkStyle (getDefLinkStyle() );
206 updateLinkGeometry();
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);
276 for (int i=0; i<treeItem->imageCount(); ++i )
278 fio=treeItem->getImageObjNum(i);
279 if (fio) fio->reposition();
284 void BranchObj::move (QPointF p)
289 void BranchObj::moveBy (double x, double y)
291 OrnamentedObj::moveBy (x,y);
292 for (int i=0; i<treeItem->branchCount(); ++i)
293 treeItem->getBranchObjNum(i)->moveBy (x,y);
297 void BranchObj::moveBy (QPointF p)
299 moveBy (p.x(), p.y());
303 void BranchObj::positionBBox()
305 QPointF ap=getAbsPos();
306 bbox.moveTopLeft (ap);
310 frame->setRect(QRectF(bbox.x(),bbox.y(),bbox.width(),bbox.height() ) );
312 // Update links to other branches
313 for (int i=0; i<xlink.size(); ++i)
314 xlink.at(i)->updateXLink();
317 void BranchObj::calcBBoxSize()
319 QSizeF heading_r=heading->getSize();
320 qreal heading_w=(qreal) heading_r.width() ;
321 qreal heading_h=(qreal) heading_r.height() ;
322 QSizeF sysflags_r=systemFlags->getSize();
323 qreal sysflags_h=sysflags_r.height();
324 qreal sysflags_w=sysflags_r.width();
325 QSizeF stanflags_r=standardFlags->getSize();
326 qreal stanflags_h=stanflags_r.height();
327 qreal stanflags_w=stanflags_r.width();
331 // set width to sum of all widths
332 w=heading_w + sysflags_w + stanflags_w;
333 // set height to maximum needed height
334 h=max (sysflags_h,stanflags_h);
337 // Save the dimension of flags and heading
338 ornamentsBBox.setSize ( QSizeF(w,h));
340 // clickBox includes Flags and Heading
341 clickBox.setSize (ornamentsBBox.size() );
346 topPad=botPad=leftPad=rightPad=0;
347 if (includeImagesVer || includeImagesHor)
349 if (treeItem->imageCount()>0)
351 for (int i=0; i<floatimage.size(); ++i )
353 rp=floatimage.at(i)->getRelPos();
354 if (includeImagesVer)
357 topPad=max (topPad,-rp.y()-h);
358 if (rp.y()+floatimage.at(i)->height() > 0)
359 botPad=max (botPad,rp.y()+floatimage.at(i)->height());
361 if (includeImagesHor)
363 if (orientation==LinkableMapObj::RightOfCenter)
366 leftPad=max (leftPad,-rp.x()-w);
367 if (rp.x()+floatimage.at(i)->width() > 0)
368 rightPad=max (rightPad,rp.x()+floatimage.at(i)->width());
372 leftPad=max (leftPad,-rp.x());
373 if (rp.x()+floatimage.at(i)->width() > w)
374 rightPad=max (rightPad,rp.x()+floatimage.at(i)->width()-w);
384 w+=frame->getPadding();
385 h+=frame->getPadding();
388 bbox.setSize (QSizeF (w,h));
391 void BranchObj::setDockPos()
393 if (treeItem->getType()==TreeItem::MapCenter)
395 // set childPos to middle of MapCenterObj
396 childPos.setX( clickBox.topLeft().x() + clickBox.width()/2 );
397 childPos.setY( clickBox.topLeft().y() + clickBox.height()/2 );
399 for (int i=0; i<treeItem->branchCount(); ++i)
400 treeItem->getBranchObjNum(i)->updateLinkGeometry();
404 // Sets childpos and parpos depending on orientation
405 if (getOrientation()==LinkableMapObj::LeftOfCenter )
408 ornamentsBBox.bottomLeft().x(),
411 ornamentsBBox.bottomRight().x(),
416 ornamentsBBox.bottomRight().x(),
419 ornamentsBBox.bottomLeft().x(),
425 void BranchObj::updateData()
430 qWarning ("BranchObj::udpateHeading treeItem==NULL");
433 QString s=treeItem->getHeading();
434 if (s!=heading->text())
436 heading->setText (s);
439 QStringList TIactiveFlags=treeItem->activeStandardFlagNames();
441 // Add missing standard flags active in TreeItem
442 for (int i=0;i<=TIactiveFlags.size()-1;i++)
444 if (!standardFlags->isActive (TIactiveFlags.at(i) ))
446 Flag *f=standardFlagsMaster->getFlag(TIactiveFlags.at(i));
447 if (f) standardFlags->activate (f);
451 // Remove standard flags no longer active in TreeItem
452 QStringList BOactiveFlags=standardFlags->activeFlagNames();
453 for (int i=0;i<BOactiveFlags.size();++i)
454 if (!TIactiveFlags.contains (BOactiveFlags.at(i)))
456 standardFlags->deactivate (BOactiveFlags.at(i));
460 // Add missing system flags active in TreeItem
461 TIactiveFlags=treeItem->activeSystemFlagNames();
462 for (int i=0;i<TIactiveFlags.size();++i)
464 if (!systemFlags->isActive (TIactiveFlags.at(i) ))
466 Flag *f=systemFlagsMaster->getFlag(TIactiveFlags.at(i));
467 if (f) systemFlags->activate (f);
471 // Remove system flags no longer active in TreeItem
472 BOactiveFlags=systemFlags->activeFlagNames();
473 for (int i=0;i<BOactiveFlags.size();++i)
475 if (!TIactiveFlags.contains (BOactiveFlags.at(i)))
477 systemFlags->deactivate (BOactiveFlags.at(i));
487 void BranchObj::addXLink (XLinkObj *xlo)
493 void BranchObj::removeXLinkRef (XLinkObj *xlo)
495 xlink.removeAt (xlink.indexOf(xlo));
498 void BranchObj::deleteXLink(XLinkObj *xlo)
501 if (!xlo->isUsed()) delete (xlo);
504 void BranchObj::deleteXLinkAt (int i)
506 XLinkObj *xlo=xlink.at(i);
508 if (!xlo->isUsed()) delete(xlo);
511 XLinkObj* BranchObj::XLinkAt (int i)
516 BranchObj* BranchObj::XLinkTargetAt (int i)
518 if (i>=0 && i<xlink.size())
521 return xlink.at(i)->otherBranch (this);
526 void BranchObj::setIncludeImagesVer(bool b)
534 bool BranchObj::getIncludeImagesVer()
536 return includeImagesVer;
539 void BranchObj::setIncludeImagesHor(bool b)
547 bool BranchObj::getIncludeImagesHor()
549 return includeImagesHor;
552 QString BranchObj::getIncludeImageAttr()
555 if (includeImagesVer)
556 a=attribut ("incImgV","true");
558 a=attribut ("incImgV","false");
559 if (includeImagesHor)
560 a+=attribut ("incImgH","true");
562 a+=attribut ("incImgH","false");
566 FloatImageObj* BranchObj::addFloatImage ()
568 FloatImageObj *newfi=new FloatImageObj (scene,this);
569 floatimage.append (newfi);
570 if ( ((BranchItem*)treeItem)->hasScrolledParent((BranchItem*)treeItem) )
571 newfi->setVisibility (false);
573 newfi->setVisibility(visible);
582 FloatImageObj* BranchObj::addFloatImage (FloatImageObj *fio)
584 FloatImageObj *newfi=new FloatImageObj (scene,this);
585 floatimage.append (newfi);
587 if (((BranchItem*)treeItem)->hasScrolledParent((BranchItem*)treeItem) )
588 newfi->setVisibility (false);
590 newfi->setVisibility(visible);
599 FloatImageObj* BranchObj::getFirstFloatImage ()
601 return floatimage.first();
604 FloatImageObj* BranchObj::getLastFloatImage ()
606 return floatimage.last();
609 FloatImageObj* BranchObj::getFloatImageNum (const uint &i)
611 return floatimage.at(i);
614 void BranchObj::removeFloatImage (FloatImageObj *fio)
616 int i=floatimage.indexOf (fio);
617 if (i>-1) delete (floatimage.takeAt (i));
623 void BranchObj::savePosInAngle ()
625 // Save position in angle
626 for (int i=0; i<treeItem->branchCount(); ++i)
627 treeItem->getBranchObjNum(i)->angle=i;
630 void BranchObj::setDefAttr (BranchModification mod)
633 switch (treeItem->depth())
635 case 0: fontsize=16; break;
636 case 1: fontsize=12; break;
637 default: fontsize=10; break;
641 setLinkStyle(getDefLinkStyle());
642 QFont font("Sans Serif,8,-1,5,50,0,0,0,0,0");
643 font.setPointSize(fontsize);
644 heading->setFont(font );
647 setColor (treeItem->getHeadingColor() );
651 void BranchObj::sortChildren() //FIXME-2
654 int childCount=branch.count();
656 bool madeChanges=false;
660 for(curChildIndex=1;curChildIndex<childCount;curChildIndex++){
661 BranchObj* curChild=(BranchObj*)treeItem->getBranchObjNum(curChildIndex);
662 BranchObj* prevChild=(BranchObj*)treeItem->getBranchObjNum(curChildIndex-1);
663 if(prevChild->heading->text().compare(curChild->heading->text())>0)
665 this->moveBranchUp(curChild);
673 void BranchObj::alignRelativeTo (QPointF ref,bool alignSelf)
675 qreal th = bboxTotal.height();
676 int depth=treeItem->depth(); // FIXME-3 needed to recursively calc depth?
680 QString h=QString (depth,' ');
681 h+=treeItem->getHeading();
684 QPointF pp; if (parObj) pp=parObj->getChildPos();
685 cout << "BO::alignRelTo ";
686 cout<<h.toStdString();
687 cout << " d="<<depth<<
688 //cout<< " ref="<<ref<<
689 " bbox.tL="<<bboxTotal.topLeft()<<
691 // " relPos="<<relPos<<
693 " w="<<bbox.width()<<
694 " h="<<bbox.height()<<
695 // " orient="<<orientation<<
696 // " alignSelf="<<alignSelf<<
697 // " scrolled="<<((BranchItem*)treeItem)->isScrolled()<<
698 // " pad="<<topPad<<","<<botPad<<","<<leftPad<<","<<rightPad<<
699 // " hidden="<<hidden<<
705 //updateLinkGeometry();
707 if (depth==1 && parObj)
709 // Position relatively, if needed
710 //if (useRelPos) move2RelPos (relPos.x(), relPos.y());
712 // Calc angle to mapCenter if I am a mainbranch
713 // needed for reordering the mainbranches clockwise
715 angle=getAngle (QPointF ((int)(x() - parObj->getChildPos().x() ),
716 (int)(y() - parObj->getChildPos().y() ) ) );
720 // Align myself depending on orientation and parent, but
721 // only if I am not a mainbranch or mapcenter itself
723 if (anim.isAnimated())
728 LinkableMapObj::Orientation o;
729 o=parObj->getOrientation();
733 case LinkableMapObj::LeftOfCenter:
734 move (ref.x() - bbox.width(), ref.y() + (th-bbox.height())/2 );
735 //move (ref.x() , ref.y() + (th-bbox.height())/2 );
737 case LinkableMapObj::RightOfCenter:
738 move (ref.x() , ref.y() + (th-bbox.height())/2 );
741 qWarning ("LMO::alignRelativeTo: oops, no orientation given...");
747 if ( ((BranchItem*)treeItem)->isScrolled() ) return;
749 // Set reference point for alignment of children
751 if (orientation==LinkableMapObj::LeftOfCenter)
752 ref2.setX(bbox.topLeft().x() - linkwidth);
754 ref2.setX(bbox.topRight().x() + linkwidth);
757 ref2.setY(absPos.y()-(bboxTotal.height()-bbox.height())/2);
761 // Align the children depending on reference point
762 for (int i=0; i<treeItem->branchCount(); ++i)
764 if (!treeItem->getBranchNum(i)->isHidden())
766 treeItem->getBranchObjNum(i)->alignRelativeTo (ref2,true);
768 // append next branch below current one
769 ref2.setY(ref2.y() + treeItem->getBranchObjNum(i)->getBBoxSizeWithChildren().height() );
775 void BranchObj::reposition()
778 if (!treeItem->getHeading().isEmpty())
779 cout << "BO::reposition "<<qPrintable(treeItem->getHeading())<<endl;
781 cout << "BO::reposition ???"<<endl;
782 // cout << " orient="<<orientation<<endl;
785 if (treeItem->depth()==0)
787 // only calculate the sizes once. If the deepest LMO
788 // changes its height,
789 // all upper LMOs have to change, too.
790 calcBBoxSizeWithChildren();
791 updateLinkGeometry(); // This update is needed if the scene is resized
792 // due to excessive moving of a FIO
794 alignRelativeTo ( QPointF (absPos.x(),
795 absPos.y()-(bboxTotal.height()-bbox.height())/2) );
796 //FIXME-2 qSort (branch.begin(),branch.end(), isAbove);
797 positionBBox(); // Reposition bbox and contents
800 // This is only important for moving branches:
801 // For editing a branch it isn't called...
802 alignRelativeTo ( QPointF (absPos.x(),
803 absPos.y()-(bboxTotal.height()-bbox.height())/2) );
807 void BranchObj::unsetAllRepositionRequests()
809 repositionRequest=false;
810 for (int i=0; i<treeItem->branchCount(); ++i)
811 treeItem->getBranchObjNum(i)->unsetAllRepositionRequests();
815 QPolygonF BranchObj::shape()
819 QRectF r=getTotalBBox();
820 if (orientation==LinkableMapObj::LeftOfCenter)
823 <<QPointF (bbox.topLeft().x(), r.topLeft().y() )
826 <<QPointF (bbox.bottomLeft().x(), r.bottomLeft().y() ) ;
830 <<QPointF (bbox.topRight().x(), r.topRight().y() )
833 <<QPointF (bbox.bottomRight().x(), r.bottomRight().y() ) ;
837 QRectF BranchObj::getTotalBBox()
841 if ( ((BranchItem*)treeItem)->isScrolled() ) return r;
843 for (int i=0; i<treeItem->branchCount(); ++i)
844 if (!treeItem->getBranchNum(i)->isHidden())
845 r=addBBox(treeItem->getBranchObjNum(i)->getTotalBBox(),r);
847 /* FIXME-3 lots of occurences of treeItem->getBranchObjNum(i) in branchobj.cpp
848 better check if they are not NULL and maybe simplify...
849 (have been NULL at least in calcBBoxSizeWithChilds...)
853 FIXME-3 for (int i=0; i<floatimage.size(); ++i)
854 if (!floatimage.at(i)->isHidden())
855 r=addBBox(floatimage.at(i)->getTotalBBox(),r);
860 QRectF BranchObj::getBBoxSizeWithChildren()
865 void BranchObj::calcBBoxSizeWithChildren()
867 // This is initially called only from reposition and
868 // and only for mapcenter. So it won't be
869 // called more than once for a single user
873 // Calculate size of LMO including all children (to align them later)
874 bboxTotal.setX(bbox.x() );
875 bboxTotal.setY(bbox.y() );
877 // if branch is scrolled, ignore children, but still consider floatimages
878 BranchItem *bi=(BranchItem*)treeItem;
879 if ( bi->isScrolled() )
881 bboxTotal.setWidth (bbox.width());
882 bboxTotal.setHeight(bbox.height());
888 bboxTotal.setWidth (0);
889 bboxTotal.setHeight(0);
892 bboxTotal.setX (parObj->x());
893 bboxTotal.setY (parObj->y());
896 bboxTotal.setX (bbox.x());
897 bboxTotal.setY (bbox.y());
904 // Now calculate recursivly
908 for (int i=0; i<treeItem->branchCount(); i++)
910 if (!bi->getBranchNum(i)->isHidden())
912 bi->getBranchObjNum(i)->calcBBoxSizeWithChildren();
913 br=bi->getBranchObjNum(i)->getBBoxSizeWithChildren();
914 r.setWidth( max (br.width(), r.width() ));
915 r.setHeight(br.height() + r.height() );
916 if (br.y()<bboxTotal.y()) bboxTotal.setY(br.y());
919 // Add myself and also
920 // add width of link to sum if necessary
921 if (bi->branchCount()<1)
922 bboxTotal.setWidth (bbox.width() + r.width() );
924 bboxTotal.setWidth (bbox.width() + r.width() + linkwidth);
926 bboxTotal.setHeight(max (r.height(), bbox.height()));
929 QString BranchObj::getSelectString()
931 VymModel *model=treeItem->getModel();
933 return model->getSelectString (this);
938 void BranchObj::setAnimation(const AnimPoint &ap)
943 bool BranchObj::animate()
946 if ( anim.isAnimated() )
951 parObj->reposition(); // we might have been relinked meanwhile