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();
91 void BranchObj::copy (BranchObj* other)
93 OrnamentedObj::copy(other);
95 setVisibility (other->visible);
102 void BranchObj::clear()
104 //setVisibility (true); //FIXME-4 needed?
106 while (!xlink.isEmpty())
107 delete xlink.takeFirst();
110 bool isAbove (BranchObj* a, BranchObj *b)
112 if (a->angle < b->angle)
118 void BranchObj::setParObjTmp(LinkableMapObj* lmo, QPointF m, int off)
120 // Temporary link to lmo
121 // m is position of mouse pointer
122 // offset 0: default 1: below lmo -1 above lmo (if possible)
124 BranchItem *pi=(BranchItem*)(lmo->getTreeItem()->parent());
125 int pi_depth=pi->depth();
126 BranchObj* o=(BranchObj*)(lmo);
130 // ignore mapcenter and mainbranch //FIXME-3 MCO meanwhile also could be relinked
131 if (pi_depth<2) off=0;
138 // setLinkStyle calls updateLinkGeometry, only set it once
139 //LinkableMapObj::Style ls=getDefLinkStyle (lmo->getTreeItem() );
140 //if (style!=ls ) setLinkStyle (ls);
141 setLinkStyle (lmo->getDefLinkStyle (lmo->getTreeItem()));
143 // Move temporary to new position at destination
144 // Usually the positioning would be done by reposition(),
145 // but then also the destination branch would "Jump" around...
146 // Better just do it approximately
147 if (parObj->getTreeItem()->depth()==0)
148 { // new parent is a mapcenter
150 QPointF p= normalize ( QPointF (m.x() - o->getChildPos().x(),
151 m.y() - o->getChildPos().y() ));
152 if (p.x()<0) p.setX( p.x()-bbox.width() );
159 // new parent is just a branch, link to it
160 QRectF t=o->getBBoxSizeWithChildren();
161 if (o->getTreeItem()->getLastBranch())
162 y=t.y() + t.height() ;
169 // we want to link above lmo
170 y=o->y() - height() + 5;
172 // we want to link below lmo
173 // Bottom of sel should be 5 pixels above
174 // the bottom of the branch _below_ the target:
175 // Don't try to find that branch, guess 12 pixels
176 y=o->getChildPos().y() -height() + 12;
178 if (o->getOrientation()==LinkableMapObj::LeftOfCenter)
179 move ( o->getChildPos().x() - linkwidth, y );
181 move (o->getChildPos().x() + linkwidth, y );
184 // updateLinkGeometry is called implicitly in move
188 void BranchObj::unsetParObjTmp()
195 setLinkStyle (getDefLinkStyle(treeItem->parent() ) );
196 updateLinkGeometry();
200 void BranchObj::setVisibility(bool v, int toDepth)
202 BranchItem *bi=(BranchItem*)treeItem;
203 if (bi->depth() <= toDepth)
205 frame->setVisibility(v);
206 heading->setVisibility(v);
207 systemFlags->setVisibility(v);
208 standardFlags->setVisibility(v);
209 LinkableMapObj::setVisibility (v);
211 for (i=0; i<treeItem->imageCount(); ++i)
212 treeItem->getImageObjNum(i)->setVisibility (v);
213 for (i=0; i<xlink.size(); ++i)
214 xlink.at(i)->setVisibility ();
216 // Only change children, if I am not scrolled
217 if (! bi->isScrolled() && (bi->depth() < toDepth))
219 // Now go recursivly through all children //FIXME-3 are there multiple calls for lower level items???
220 for (i=0; i<treeItem->branchCount(); ++i)
221 treeItem->getBranchObjNum(i)->setVisibility (v,toDepth);
223 } // depth <= toDepth
227 void BranchObj::setVisibility(bool v)
229 setVisibility (v,MAX_DEPTH);
233 void BranchObj::setLinkColor ()
235 // Overloaded from LinkableMapObj
236 // BranchObj can use color of heading
238 VymModel *model=treeItem->getModel();
241 if (model->getMapLinkColorHint()==HeadingColor)
242 LinkableMapObj::setLinkColor (heading->getColor() );
244 LinkableMapObj::setLinkColor ();
248 void BranchObj::updateContentSize()
255 void BranchObj::positionContents()
257 for (int i=0; i<treeItem->imageCount(); ++i)
258 treeItem->getImageObjNum(i)->reposition();
259 OrnamentedObj::positionContents();
262 void BranchObj::move (double x, double y)
264 OrnamentedObj::move (x,y);
266 for (int i=0; i<treeItem->imageCount(); ++i )
268 fio=treeItem->getImageObjNum(i);
269 if (fio) fio->reposition();
274 void BranchObj::move (QPointF p)
279 void BranchObj::moveBy (double x, double y)
281 OrnamentedObj::moveBy (x,y);
282 for (int i=0; i<treeItem->branchCount(); ++i)
283 treeItem->getBranchObjNum(i)->moveBy (x,y);
287 void BranchObj::moveBy (QPointF p)
289 moveBy (p.x(), p.y());
293 void BranchObj::positionBBox()
295 QPointF ap=getAbsPos();
296 bbox.moveTopLeft (ap);
300 frame->setRect(QRectF(bbox.x(),bbox.y(),bbox.width(),bbox.height() ) );
302 // Update links to other branches
303 for (int i=0; i<xlink.size(); ++i)
304 xlink.at(i)->updateXLink();
307 void BranchObj::calcBBoxSize()
309 QSizeF heading_r=heading->getSize();
310 qreal heading_w=(qreal) heading_r.width() ;
311 qreal heading_h=(qreal) heading_r.height() ;
312 QSizeF sysflags_r=systemFlags->getSize();
313 qreal sysflags_h=sysflags_r.height();
314 qreal sysflags_w=sysflags_r.width();
315 QSizeF stanflags_r=standardFlags->getSize();
316 qreal stanflags_h=stanflags_r.height();
317 qreal stanflags_w=stanflags_r.width();
321 // set width to sum of all widths
322 w=heading_w + sysflags_w + stanflags_w;
323 // set height to maximum needed height
324 h=max (sysflags_h,stanflags_h);
327 // Save the dimension of flags and heading
328 ornamentsBBox.setSize ( QSizeF(w,h));
330 // clickBox includes Flags and Heading
331 clickBox.setSize (ornamentsBBox.size() );
336 topPad=botPad=leftPad=rightPad=0;
337 bool incV=((BranchItem*)treeItem)->getIncludeImagesVer();
338 bool incH=((BranchItem*)treeItem)->getIncludeImagesHor();
342 for (int i=0; i<treeItem->imageCount(); ++i )
344 fio=treeItem->getImageObjNum(i);
349 topPad=max (topPad,-rp.y()-h);
350 if (rp.y()+fio->height() > 0)
351 botPad=max (botPad,rp.y()+fio->height());
355 if (orientation==LinkableMapObj::RightOfCenter)
358 leftPad=max (leftPad,-rp.x()-w);
359 if (rp.x()+fio->width() > 0)
360 rightPad=max (rightPad,rp.x()+fio->width());
364 leftPad=max (leftPad,-rp.x());
365 if (rp.x()+fio->width() > w)
366 rightPad=max (rightPad,rp.x()+fio->width()-w);
375 w+=frame->getPadding();
376 h+=frame->getPadding();
379 bbox.setSize (QSizeF (w,h));
382 void BranchObj::setDockPos()
384 if (treeItem->getType()==TreeItem::MapCenter)
386 // set childPos to middle of MapCenterObj
387 childPos.setX( clickBox.topLeft().x() + clickBox.width()/2 );
388 childPos.setY( clickBox.topLeft().y() + clickBox.height()/2 );
390 for (int i=0; i<treeItem->branchCount(); ++i)
391 treeItem->getBranchObjNum(i)->updateLinkGeometry();
395 // Sets childpos and parpos depending on orientation
396 if (getOrientation()==LinkableMapObj::LeftOfCenter )
399 ornamentsBBox.bottomLeft().x(),
402 ornamentsBBox.bottomRight().x(),
407 ornamentsBBox.bottomRight().x(),
410 ornamentsBBox.bottomLeft().x(),
416 void BranchObj::updateData()
421 qWarning ("BranchObj::udpateHeading treeItem==NULL");
424 QString s=treeItem->getHeading();
425 if (s!=heading->text())
427 heading->setText (s);
430 QStringList TIactiveFlags=treeItem->activeStandardFlagNames();
432 // Add missing standard flags active in TreeItem
433 for (int i=0;i<=TIactiveFlags.size()-1;i++)
435 if (!standardFlags->isActive (TIactiveFlags.at(i) ))
437 Flag *f=standardFlagsMaster->getFlag(TIactiveFlags.at(i));
438 if (f) standardFlags->activate (f);
442 // Remove standard flags no longer active in TreeItem
443 QStringList BOactiveFlags=standardFlags->activeFlagNames();
444 for (int i=0;i<BOactiveFlags.size();++i)
445 if (!TIactiveFlags.contains (BOactiveFlags.at(i)))
447 standardFlags->deactivate (BOactiveFlags.at(i));
451 // Add missing system flags active in TreeItem
452 TIactiveFlags=treeItem->activeSystemFlagNames();
453 for (int i=0;i<TIactiveFlags.size();++i)
455 if (!systemFlags->isActive (TIactiveFlags.at(i) ))
457 Flag *f=systemFlagsMaster->getFlag(TIactiveFlags.at(i));
458 if (f) systemFlags->activate (f);
462 // Remove system flags no longer active in TreeItem
463 BOactiveFlags=systemFlags->activeFlagNames();
464 for (int i=0;i<BOactiveFlags.size();++i)
466 if (!TIactiveFlags.contains (BOactiveFlags.at(i)))
468 systemFlags->deactivate (BOactiveFlags.at(i));
476 void BranchObj::addXLink (XLinkObj *xlo)
482 void BranchObj::removeXLinkRef (XLinkObj *xlo)
484 xlink.removeAt (xlink.indexOf(xlo));
487 void BranchObj::deleteXLink(XLinkObj *xlo)
490 if (!xlo->isUsed()) delete (xlo);
493 void BranchObj::deleteXLinkAt (int i)
495 XLinkObj *xlo=xlink.at(i);
497 if (!xlo->isUsed()) delete(xlo);
500 XLinkObj* BranchObj::XLinkAt (int i)
505 BranchObj* BranchObj::XLinkTargetAt (int i)
507 if (i>=0 && i<xlink.size())
510 return xlink.at(i)->otherBranch (this);
516 // FIXME-3 FloatImageObj* BranchObj::addFloatImage ()
519 FloatImageObj *newfi=new FloatImageObj (scene,this);
520 floatimage.append (newfi);
521 if ( ((BranchItem*)treeItem)->hasScrolledParent((BranchItem*)treeItem) )
522 newfi->setVisibility (false);
524 newfi->setVisibility(visible);
532 //FIXME-3 FloatImageObj* BranchObj::addFloatImage (FloatImageObj *fio)
535 FloatImageObj *newfi=new FloatImageObj (scene,this);
536 floatimage.append (newfi);
538 if (((BranchItem*)treeItem)->hasScrolledParent((BranchItem*)treeItem) )
539 newfi->setVisibility (false);
541 newfi->setVisibility(visible);
549 /* FIXME-3 FloatImageObj* BranchObj::getFirstFloatImage ()
551 return floatimage.first();
554 FloatImageObj* BranchObj::getLastFloatImage ()
556 return floatimage.last();
559 FIXME -3 FloatImageObj* BranchObj::getFloatImageNum (const uint &i)
561 return floatimage.at(i);
564 void BranchObj::removeFloatImage (FloatImageObj *fio)
566 int i=floatimage.indexOf (fio);
567 if (i>-1) delete (floatimage.takeAt (i));
574 void BranchObj::savePosInAngle ()
576 // Save position in angle
577 for (int i=0; i<treeItem->branchCount(); ++i)
578 treeItem->getBranchObjNum(i)->angle=i;
581 void BranchObj::setDefAttr (BranchModification mod)
584 switch (treeItem->depth())
586 case 0: fontsize=16; break;
587 case 1: fontsize=12; break;
588 default: fontsize=10; break;
590 setLinkStyle(getDefLinkStyle(treeItem->parent() ));
592 QFont font("Sans Serif,8,-1,5,50,0,0,0,0,0");
593 font.setPointSize(fontsize);
594 heading->setFont(font );
597 setColor (treeItem->getHeadingColor() );
601 void BranchObj::sortChildren() //FIXME-3 not moved to model yet
604 int childCount=branch.count();
606 bool madeChanges=false;
610 for(curChildIndex=1;curChildIndex<childCount;curChildIndex++){
611 BranchObj* curChild=(BranchObj*)treeItem->getBranchObjNum(curChildIndex);
612 BranchObj* prevChild=(BranchObj*)treeItem->getBranchObjNum(curChildIndex-1);
613 if(prevChild->heading->text().compare(curChild->heading->text())>0)
615 this->moveBranchUp(curChild);
623 void BranchObj::alignRelativeTo (QPointF ref,bool alignSelf)
625 qreal th = bboxTotal.height();
626 int depth=treeItem->depth(); // FIXME-3 needed to recursively calc depth?
630 QString h=QString (depth,' ');
631 h+=treeItem->getHeading();
634 QPointF pp; if (parObj) pp=parObj->getChildPos();
635 cout << "BO::alignRelTo ";
636 cout<<h.toStdString();
637 cout << " d="<<depth<<
638 //cout<< " ref="<<ref<<
639 " bbox.tL="<<bboxTotal.topLeft()<<
641 // " relPos="<<relPos<<
643 " w="<<bbox.width()<<
644 " h="<<bbox.height()<<
645 // " orient="<<orientation<<
646 // " alignSelf="<<alignSelf<<
647 // " scrolled="<<((BranchItem*)treeItem)->isScrolled()<<
648 // " pad="<<topPad<<","<<botPad<<","<<leftPad<<","<<rightPad<<
649 // " hidden="<<hidden<<
655 //updateLinkGeometry();
657 if (depth==1 && parObj)
659 // Position relatively, if needed
660 //if (useRelPos) move2RelPos (relPos.x(), relPos.y());
662 // Calc angle to mapCenter if I am a mainbranch
663 // needed for reordering the mainbranches clockwise
665 angle=getAngle (QPointF ((int)(x() - parObj->getChildPos().x() ),
666 (int)(y() - parObj->getChildPos().y() ) ) );
670 // Align myself depending on orientation and parent, but
671 // only if I am not a mainbranch or mapcenter itself
673 if (anim.isAnimated())
678 LinkableMapObj::Orientation o;
679 o=parObj->getOrientation();
683 case LinkableMapObj::LeftOfCenter:
684 move (ref.x() - bbox.width(), ref.y() + (th-bbox.height())/2 );
685 //move (ref.x() , ref.y() + (th-bbox.height())/2 );
687 case LinkableMapObj::RightOfCenter:
688 move (ref.x() , ref.y() + (th-bbox.height())/2 );
691 qWarning ("LMO::alignRelativeTo: oops, no orientation given...");
697 if ( ((BranchItem*)treeItem)->isScrolled() ) return;
699 // Set reference point for alignment of children
701 if (orientation==LinkableMapObj::LeftOfCenter)
702 ref2.setX(bbox.topLeft().x() - linkwidth);
704 ref2.setX(bbox.topRight().x() + linkwidth);
707 ref2.setY(absPos.y()-(bboxTotal.height()-bbox.height())/2);
711 // Align the children depending on reference point
712 for (int i=0; i<treeItem->branchCount(); ++i)
714 if (!treeItem->getBranchNum(i)->isHidden())
716 treeItem->getBranchObjNum(i)->alignRelativeTo (ref2,true);
718 // append next branch below current one
719 ref2.setY(ref2.y() + treeItem->getBranchObjNum(i)->getBBoxSizeWithChildren().height() );
725 void BranchObj::reposition()
728 if (!treeItem->getHeading().isEmpty())
729 cout << "BO::reposition "<<qPrintable(treeItem->getHeading())<<endl;
731 cout << "BO::reposition ???"<<endl;
732 // cout << " orient="<<orientation<<endl;
735 if (treeItem->depth()==0)
737 // only calculate the sizes once. If the deepest LMO
738 // changes its height,
739 // all upper LMOs have to change, too.
740 calcBBoxSizeWithChildren();
741 updateLinkGeometry(); // This update is needed if the scene is resized
742 // due to excessive moving of a FIO
744 alignRelativeTo ( QPointF (absPos.x(),
745 absPos.y()-(bboxTotal.height()-bbox.height())/2) );
746 //FIXME-2 qSort (branch.begin(),branch.end(), isAbove);
747 positionBBox(); // Reposition bbox and contents
750 // This is only important for moving branches:
751 // For editing a branch it isn't called...
752 alignRelativeTo ( QPointF (absPos.x(),
753 absPos.y()-(bboxTotal.height()-bbox.height())/2) );
757 void BranchObj::unsetAllRepositionRequests()
759 repositionRequest=false;
760 for (int i=0; i<treeItem->branchCount(); ++i)
761 treeItem->getBranchObjNum(i)->unsetAllRepositionRequests();
765 QPolygonF BranchObj::shape()
769 QRectF r=getTotalBBox();
770 if (orientation==LinkableMapObj::LeftOfCenter)
773 <<QPointF (bbox.topLeft().x(), r.topLeft().y() )
776 <<QPointF (bbox.bottomLeft().x(), r.bottomLeft().y() ) ;
780 <<QPointF (bbox.topRight().x(), r.topRight().y() )
783 <<QPointF (bbox.bottomRight().x(), r.bottomRight().y() ) ;
787 QRectF BranchObj::getTotalBBox()
791 if ( ((BranchItem*)treeItem)->isScrolled() ) return r;
793 for (int i=0; i<treeItem->branchCount(); ++i)
794 if (!treeItem->getBranchNum(i)->isHidden())
795 r=addBBox(treeItem->getBranchObjNum(i)->getTotalBBox(),r);
797 /* FIXME-3 lots of occurences of treeItem->getBranchObjNum(i) in branchobj.cpp
798 better check if they are not NULL and maybe simplify...
799 (have been NULL at least in calcBBoxSizeWithChilds...)
802 for (int i=0; i<treeItem->imageCount(); ++i)
803 if (!treeItem->isHidden())
804 r=addBBox(treeItem->getImageObjNum(i)->getTotalBBox(),r);
808 QRectF BranchObj::getBBoxSizeWithChildren()
813 void BranchObj::calcBBoxSizeWithChildren()
815 // This is initially called only from reposition and
816 // and only for mapcenter. So it won't be
817 // called more than once for a single user
821 // Calculate size of LMO including all children (to align them later)
822 bboxTotal.setX(bbox.x() );
823 bboxTotal.setY(bbox.y() );
825 // if branch is scrolled, ignore children, but still consider floatimages
826 BranchItem *bi=(BranchItem*)treeItem;
827 if ( bi->isScrolled() )
829 bboxTotal.setWidth (bbox.width());
830 bboxTotal.setHeight(bbox.height());
836 bboxTotal.setWidth (0);
837 bboxTotal.setHeight(0);
840 bboxTotal.setX (parObj->x());
841 bboxTotal.setY (parObj->y());
844 bboxTotal.setX (bbox.x());
845 bboxTotal.setY (bbox.y());
852 // Now calculate recursivly
856 for (int i=0; i<treeItem->branchCount(); i++)
858 if (!bi->getBranchNum(i)->isHidden())
860 bi->getBranchObjNum(i)->calcBBoxSizeWithChildren();
861 br=bi->getBranchObjNum(i)->getBBoxSizeWithChildren();
862 r.setWidth( max (br.width(), r.width() ));
863 r.setHeight(br.height() + r.height() );
864 if (br.y()<bboxTotal.y()) bboxTotal.setY(br.y());
867 // Add myself and also
868 // add width of link to sum if necessary
869 if (bi->branchCount()<1)
870 bboxTotal.setWidth (bbox.width() + r.width() );
872 bboxTotal.setWidth (bbox.width() + r.width() + linkwidth);
874 bboxTotal.setHeight(max (r.height(), bbox.height()));
877 QString BranchObj::getSelectString()
879 VymModel *model=treeItem->getModel();
881 return model->getSelectString (this);
886 void BranchObj::setAnimation(const AnimPoint &ap)
891 bool BranchObj::animate()
894 if ( anim.isAnimated() )
899 parObj->reposition(); // we might have been relinked meanwhile