3 #include "branchitem.h"
6 #include "mainwindow.h"
9 extern FlagRow *standardFlagsMaster;
10 extern FlagRow *systemFlagsMaster;
12 /////////////////////////////////////////////////////////////////
14 /////////////////////////////////////////////////////////////////
16 BranchObj::BranchObj (QGraphicsScene* s,TreeItem *ti):OrnamentedObj (s)
18 // cout << "Const BranchObj (s) \n";
21 BranchItem *pi=(BranchItem*)(ti->parent());
22 if (pi && pi!=ti->getModel()->getRootItem() )
30 BranchObj::BranchObj (QGraphicsScene* s, LinkableMapObj* p):OrnamentedObj (s)// FIXME-3 needed at all?
32 // cout << "Const BranchObj (s,p)\n";
35 if (treeItem->depth()==1) // FIXME-3 needed to recursively calc depth?
36 // Calc angle to mapCenter if I am a mainbranch
37 // needed for reordering the mainbranches clockwise
39 angle=getAngle (QPointF (x() - parObj->getChildPos().x() ,
40 (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* dst, QPointF m, int off) //FIXME-1 when moving a mainbranch to a branch it still has relPos, not moved as child
120 // Temporary link to dst
121 // m is position of mouse pointer
122 // offset 0: default 1: below dst -1 above dst (if possible)
124 BranchItem *dsti=(BranchItem*)(dst->getTreeItem());
126 BranchItem *pi=(BranchItem*)(dsti->parent());
127 int pi_depth=pi->depth();
128 BranchObj* bo=(BranchObj*)dst;
136 if (pi_depth<2) off=0;
143 setLinkStyle (dst->getDefLinkStyle (dsti));
145 // Move temporary to new position at destination
146 // Usually the positioning would be done by reposition(),
147 // but then also the destination branch would "Jump" around...
148 // Better just do it approximately
149 if (parObj->getTreeItem()->depth()==0)
150 { // new parent is a mapcenter
151 Vector v= ( m - bo->getChildPos());
152 if (v.x()<0) v.setX( v.x()-bbox.width() );
155 move2RelPos (v.toQPointF());
161 // new parent is just a branch, link to it
162 QRectF t=bo->getBBoxSizeWithChildren();
163 if (dsti->getLastBranch())
164 y=t.y() + t.height() ;
171 // we want to link above dst
172 y=bo->y() - height() + 5;
174 // we want to link below dst
175 // Bottom of sel should be 5 pixels above
176 // the bottom of the branch _below_ the target:
177 // Don't try to find that branch, guess 12 pixels
178 y=bo->getChildPos().y() -height() + 12;
180 if (bo->getOrientation()==LinkableMapObj::LeftOfCenter)
181 move ( bo->getChildPos().x() - linkwidth, y );
183 move (bo->getChildPos().x() + linkwidth, y );
186 // updateLinkGeometry is called implicitly in move
190 void BranchObj::unsetParObjTmp()
198 setLinkStyle (getDefLinkStyle(treeItem->parent() ) );
199 updateLinkGeometry();
203 void BranchObj::setVisibility(bool v, int toDepth)
205 BranchItem *bi=(BranchItem*)treeItem;
206 if (bi->depth() <= toDepth)
208 frame->setVisibility(v);
209 heading->setVisibility(v);
210 systemFlags->setVisibility(v);
211 standardFlags->setVisibility(v);
212 LinkableMapObj::setVisibility (v);
214 for (i=0; i<treeItem->imageCount(); ++i)
215 treeItem->getImageObjNum(i)->setVisibility (v);
216 for (i=0; i<treeItem->xlinkCount(); ++i)
217 treeItem->getXLinkObjNum(i)->setVisibility ();
219 // Only change children, if I am not scrolled
220 if (! bi->isScrolled() && (bi->depth() < toDepth))
222 // Now go recursivly through all children //FIXME-3 are there multiple calls for lower level items???
223 for (i=0; i<treeItem->branchCount(); ++i)
224 treeItem->getBranchObjNum(i)->setVisibility (v,toDepth);
226 } // depth <= toDepth
230 void BranchObj::setVisibility(bool v)
232 setVisibility (v,MAX_DEPTH);
236 void BranchObj::setLinkColor ()
238 // Overloaded from LinkableMapObj
239 // BranchObj can use color of heading
241 VymModel *model=treeItem->getModel();
244 if (model->getMapLinkColorHint()==HeadingColor)
245 LinkableMapObj::setLinkColor (heading->getColor() );
247 LinkableMapObj::setLinkColor ();
251 void BranchObj::updateContentSize()
258 void BranchObj::positionContents()
260 for (int i=0; i<treeItem->imageCount(); ++i)
261 treeItem->getImageObjNum(i)->reposition();
262 OrnamentedObj::positionContents();
265 void BranchObj::move (double x, double y)
267 OrnamentedObj::move (x,y);
271 void BranchObj::move (QPointF p)
276 void BranchObj::moveBy (double x, double y)
278 OrnamentedObj::moveBy (x,y);
279 for (int i=0; i<treeItem->branchCount(); ++i)
280 treeItem->getBranchObjNum(i)->moveBy (x,y);
284 void BranchObj::moveBy (QPointF p)
286 moveBy (p.x(), p.y());
290 void BranchObj::positionBBox()
292 QPointF ap=getAbsPos();
293 bbox.moveTopLeft (ap);
297 frame->setRect(QRectF(bbox.x(),bbox.y(),bbox.width(),bbox.height() ) );
299 //Update links to other branches
301 for (int i=0; i<treeItem->xlinkCount(); ++i)
303 xlo=treeItem->getXLinkObjNum(i);
304 if (xlo) xlo->updateXLink();
308 void BranchObj::calcBBoxSize()
310 QSizeF heading_r=heading->getSize();
311 qreal heading_w=(qreal) heading_r.width() ;
312 qreal heading_h=(qreal) heading_r.height() ;
313 QSizeF sysflags_r=systemFlags->getSize();
314 qreal sysflags_h=sysflags_r.height();
315 qreal sysflags_w=sysflags_r.width();
316 QSizeF stanflags_r=standardFlags->getSize();
317 qreal stanflags_h=stanflags_r.height();
318 qreal stanflags_w=stanflags_r.width();
322 // set width to sum of all widths
323 w=heading_w + sysflags_w + stanflags_w;
324 // set height to maximum needed height
325 h=max (sysflags_h,stanflags_h);
328 // Save the dimension of flags and heading
329 ornamentsBBox.setSize ( QSizeF(w,h));
331 // clickBox includes Flags and Heading
332 clickBox.setSize (ornamentsBBox.size() );
337 topPad=botPad=leftPad=rightPad=0;
338 bool incV=((BranchItem*)treeItem)->getIncludeImagesVer();
339 bool incH=((BranchItem*)treeItem)->getIncludeImagesHor();
343 for (int i=0; i<treeItem->imageCount(); ++i )
345 fio=treeItem->getImageObjNum(i);
350 topPad=max (topPad,-rp.y()-h);
351 if (rp.y()+fio->height() > 0)
352 botPad=max (botPad,rp.y()+fio->height());
356 if (orientation==LinkableMapObj::RightOfCenter)
359 leftPad=max (leftPad,-rp.x()-w);
360 if (rp.x()+fio->width() > 0)
361 rightPad=max (rightPad,rp.x()+fio->width());
365 leftPad=max (leftPad,-rp.x());
366 if (rp.x()+fio->width() > w)
367 rightPad=max (rightPad,rp.x()+fio->width()-w);
376 w+=frame->getPadding();
377 h+=frame->getPadding();
380 bbox.setSize (QSizeF (w,h));
383 void BranchObj::setDockPos()
385 if (treeItem->getType()==TreeItem::MapCenter)
387 // set childPos to middle of MapCenterObj
388 childPos.setX( clickBox.topLeft().x() + clickBox.width()/2 );
389 childPos.setY( clickBox.topLeft().y() + clickBox.height()/2 );
391 for (int i=0; i<treeItem->branchCount(); ++i)
392 treeItem->getBranchObjNum(i)->updateLinkGeometry();
396 // Sets childpos and parpos depending on orientation
397 if (getOrientation()==LinkableMapObj::LeftOfCenter )
400 ornamentsBBox.bottomLeft().x(),
403 ornamentsBBox.bottomRight().x(),
408 ornamentsBBox.bottomRight().x(),
411 ornamentsBBox.bottomLeft().x(),
417 void BranchObj::updateData()
422 qWarning ("BranchObj::udpateHeading treeItem==NULL");
425 QString s=treeItem->getHeading();
426 if (s!=heading->text())
428 heading->setText (s);
431 QStringList TIactiveFlags=treeItem->activeStandardFlagNames();
433 // Add missing standard flags active in TreeItem
434 for (int i=0;i<=TIactiveFlags.size()-1;i++)
436 if (!standardFlags->isActive (TIactiveFlags.at(i) ))
438 Flag *f=standardFlagsMaster->getFlag(TIactiveFlags.at(i));
439 if (f) standardFlags->activate (f);
443 // Remove standard flags no longer active in TreeItem
444 QStringList BOactiveFlags=standardFlags->activeFlagNames();
445 for (int i=0;i<BOactiveFlags.size();++i)
446 if (!TIactiveFlags.contains (BOactiveFlags.at(i)))
448 standardFlags->deactivate (BOactiveFlags.at(i));
452 // Add missing system flags active in TreeItem
453 TIactiveFlags=treeItem->activeSystemFlagNames();
454 for (int i=0;i<TIactiveFlags.size();++i)
456 if (!systemFlags->isActive (TIactiveFlags.at(i) ))
458 Flag *f=systemFlagsMaster->getFlag(TIactiveFlags.at(i));
459 if (f) systemFlags->activate (f);
463 // Remove system flags no longer active in TreeItem
464 BOactiveFlags=systemFlags->activeFlagNames();
465 for (int i=0;i<BOactiveFlags.size();++i)
467 if (!TIactiveFlags.contains (BOactiveFlags.at(i)))
469 systemFlags->deactivate (BOactiveFlags.at(i));
476 void BranchObj::savePosInAngle ()
478 // Save position in angle
479 for (int i=0; i<treeItem->branchCount(); ++i)
480 treeItem->getBranchObjNum(i)->angle=i;
483 void BranchObj::setDefAttr (BranchModification mod)
486 switch (treeItem->depth())
490 setFrameType (FrameObj::Rectangle);
494 setFrameType (FrameObj::NoFrame);
498 setFrameType (FrameObj::NoFrame);
502 setFrameType (FrameObj::NoFrame);
505 setLinkStyle(getDefLinkStyle(treeItem->parent() ));
507 QFont font("Sans Serif,8,-1,5,50,0,0,0,0,0");
508 font.setPointSize(fontsize);
509 heading->setFont(font );
512 setColor (treeItem->getHeadingColor() );
514 // Also set styles for children
515 for (int i=0; i<treeItem->branchCount(); ++i)
516 treeItem->getBranchObjNum(i)->setDefAttr(MovedBranch);
520 void BranchObj::alignRelativeTo (QPointF ref,bool alignSelf)
522 qreal th = bboxTotal.height();
523 int depth=treeItem->depth(); // FIXME-3 needed to recursively calc depth?
527 QString h=QString (depth,' ');
528 h+=treeItem->getHeading();
531 QPointF pp; if (parObj) pp=parObj->getChildPos();
532 cout << "BO::alignRelTo ";
533 cout<<h.toStdString();
534 cout << " d="<<depth<<
535 //cout<< " ref="<<ref<<
536 " bbox.tL="<<bboxTotal.topLeft()<<
540 // " w="<<bbox.width()<<
541 // " h="<<bbox.height()<<
542 // " orient="<<orientation<<
543 // " alignSelf="<<alignSelf<<
544 // " scrolled="<<((BranchItem*)treeItem)->isScrolled()<<
545 // " pad="<<topPad<<","<<botPad<<","<<leftPad<<","<<rightPad<<
546 // " hidden="<<hidden<<
552 //updateLinkGeometry();
554 if (depth==1 && parObj)
556 // Position relatively, if needed
557 //if (useRelPos) move2RelPos (relPos.x(), relPos.y());
559 // Calc angle to mapCenter if I am a mainbranch
560 // needed for reordering the mainbranches clockwise
562 angle=getAngle (QPointF ((int)(x() - parObj->getChildPos().x() ),
563 (int)(y() - parObj->getChildPos().y() ) ) );
567 move2RelPos (getRelPos() );
571 // Align myself depending on orientation and parent, but
572 // only if I am not a mainbranch or mapcenter itself
574 if (anim.isAnimated())
579 LinkableMapObj::Orientation o;
580 o=parObj->getOrientation();
584 case LinkableMapObj::LeftOfCenter:
585 move (ref.x() - bbox.width(), ref.y() + (th-bbox.height())/2 );
586 //move (ref.x() , ref.y() + (th-bbox.height())/2 );
588 case LinkableMapObj::RightOfCenter:
589 move (ref.x() , ref.y() + (th-bbox.height())/2 );
592 qWarning ("LMO::alignRelativeTo: oops, no orientation given...");
598 if ( ((BranchItem*)treeItem)->isScrolled() ) return;
600 // Set reference point for alignment of children
602 if (orientation==LinkableMapObj::LeftOfCenter)
603 ref2.setX(bbox.topLeft().x() - linkwidth);
605 ref2.setX(bbox.topRight().x() + linkwidth);
608 ref2.setY(absPos.y()-(bboxTotal.height()-bbox.height())/2);
612 // Align the children depending on reference point
613 for (int i=0; i<treeItem->branchCount(); ++i)
615 if (!treeItem->getBranchNum(i)->isHidden())
617 treeItem->getBranchObjNum(i)->alignRelativeTo (ref2,true);
619 // append next branch below current one
620 ref2.setY(ref2.y() + treeItem->getBranchObjNum(i)->getBBoxSizeWithChildren().height() );
626 void BranchObj::reposition()
629 if (!treeItem->getHeading().isEmpty())
630 cout << "BO::reposition "<<qPrintable(treeItem->getHeading())<<endl;
632 cout << "BO::reposition ???"<<endl;
633 // cout << " orient="<<orientation<<endl;
636 if (treeItem->depth()==0)
638 // only calculate the sizes once. If the deepest LMO
639 // changes its height,
640 // all upper LMOs have to change, too.
641 calcBBoxSizeWithChildren();
642 updateLinkGeometry(); // This update is needed if the scene is resized
643 // due to excessive moving of a FIO
645 alignRelativeTo ( QPointF (absPos.x(),
646 absPos.y()-(bboxTotal.height()-bbox.height())/2) );
647 //FIXME-2 qSort (branch.begin(),branch.end(), isAbove);
648 positionBBox(); // Reposition bbox and contents
651 // This is only important for moving branches:
652 // For editing a branch it isn't called...
653 alignRelativeTo ( QPointF (absPos.x(),
654 absPos.y()-(bboxTotal.height()-bbox.height())/2) );
658 void BranchObj::unsetAllRepositionRequests()
660 repositionRequest=false;
661 for (int i=0; i<treeItem->branchCount(); ++i)
662 treeItem->getBranchObjNum(i)->unsetAllRepositionRequests();
666 QRectF BranchObj::getTotalBBox()
670 if ( ((BranchItem*)treeItem)->isScrolled() ) return r;
672 for (int i=0; i<treeItem->branchCount(); ++i)
673 if (!treeItem->getBranchNum(i)->isHidden())
674 r=addBBox(treeItem->getBranchObjNum(i)->getTotalBBox(),r);
676 /* FIXME-3 lots of occurences of treeItem->getBranchObjNum(i) in branchobj.cpp
677 better check if they are not NULL and maybe simplify...
678 (have been NULL at least in calcBBoxSizeWithChilds...)
681 for (int i=0; i<treeItem->imageCount(); ++i)
682 if (!treeItem->isHidden())
683 r=addBBox(treeItem->getImageObjNum(i)->getTotalBBox(),r);
687 QRectF BranchObj::getBBoxSizeWithChildren()
692 ConvexPolygon BranchObj::getBoundingPolygon()
695 if (!pi) //FIXME-3 Testing only
697 pi=scene->addPolygon(MapObj::getBoundingPolygon() );
698 pi->setPen(Qt::NoPen);
699 pi->setBrush( QColor(qrand()%32*8,qrand()%32*8,qrand()%32*8) );
700 pi->setZValue(Z_BBOX);
704 if (treeItem->branchCount()==0 || treeItem->depth()==0)
706 if (pi) pi->setPolygon (MapObj::getBoundingPolygon() );
707 return MapObj::getBoundingPolygon();
710 calcBBoxSizeWithChildren(); //FIXME-3 really needed?
712 p<<bboxTotal.topLeft();
713 p<<bboxTotal.topRight();
714 p<<bboxTotal.bottomRight();
715 p<<bboxTotal.bottomLeft();
716 //cout << "BO::getBP (total) "<<treeItem->getHeadingStd()<<" tL="<<bboxTotal.topLeft()<<" bR="<<bboxTotal.bottomRight()<<endl;
717 //cout << " "<<" tL="<<bbox.topLeft()<<" bR="<<bbox.bottomRight()<<endl;
718 if (pi) pi->setPolygon (p );
722 void BranchObj::calcBBoxSizeWithChildren()
724 // This is initially called only from reposition and
725 // and only for mapcenter. So it won't be
726 // called more than once for a single user
730 // Calculate size of LMO including all children (to align them later)
731 bboxTotal.setX(bbox.x() );
732 bboxTotal.setY(bbox.y() );
734 // if branch is scrolled, ignore children, but still consider floatimages
735 BranchItem *bi=(BranchItem*)treeItem;
736 if ( bi->isScrolled() )
738 bboxTotal.setWidth (bbox.width());
739 bboxTotal.setHeight(bbox.height());
745 bboxTotal.setWidth (0);
746 bboxTotal.setHeight(0);
749 bboxTotal.setX (parObj->x());
750 bboxTotal.setY (parObj->y());
753 bboxTotal.setX (bbox.x());
754 bboxTotal.setY (bbox.y());
761 // Now calculate recursivly
765 for (int i=0; i<treeItem->branchCount(); i++)
767 if (!bi->getBranchNum(i)->isHidden())
769 bi->getBranchObjNum(i)->calcBBoxSizeWithChildren();
770 br=bi->getBranchObjNum(i)->getBBoxSizeWithChildren();
771 r.setWidth( max (br.width(), r.width() ));
772 r.setHeight(br.height() + r.height() );
773 if (br.y()<bboxTotal.y()) bboxTotal.setY(br.y());
774 if (br.x()<bboxTotal.x()) bboxTotal.setX(br.x());
777 // Add myself and also
778 // add width of link to sum if necessary
779 if (bi->branchCount()<1)
780 bboxTotal.setWidth (bbox.width() + r.width() );
782 bboxTotal.setWidth (bbox.width() + r.width() + linkwidth);
784 bboxTotal.setHeight(max (r.height(), bbox.height()));
787 QString BranchObj::getSelectString()
789 VymModel *model=treeItem->getModel();
791 return model->getSelectString (this);
796 void BranchObj::setAnimation(const AnimPoint &ap)
801 bool BranchObj::animate()
804 if ( anim.isAnimated() )
809 parObj->reposition(); // we might have been relinked meanwhile