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 cout << "BO::setTmp d="<<dsti->depth();
150 if (dsti->depth()==0)
151 { // new parent is a mapcenter
152 Vector v= ( m - bo->getChildPos());
153 if (v.x()<0) v.setX( v.x()-bbox.width() );
156 move2RelPos (v.toQPointF());
157 cout << " v="<<v<<endl;
163 // new parent is just a branch, link to it
164 QRectF t=bo->getBBoxSizeWithChildren();
165 if (dsti->getLastBranch())
166 y=t.y() + t.height() ;
173 // we want to link above dst
174 y=bo->y() - height() + 5;
176 // we want to link below dst
177 // Bottom of sel should be 5 pixels above
178 // the bottom of the branch _below_ the target:
179 // Don't try to find that branch, guess 12 pixels
180 y=bo->getChildPos().y() -height() + 12;
182 if (bo->getOrientation()==LinkableMapObj::LeftOfCenter)
183 move ( bo->getChildPos().x() - linkwidth, y );
185 move (bo->getChildPos().x() + linkwidth, y );
188 // updateLinkGeometry is called implicitly in move
192 void BranchObj::unsetParObjTmp()
200 setLinkStyle (getDefLinkStyle(treeItem->parent() ) );
201 updateLinkGeometry();
205 void BranchObj::setVisibility(bool v, int toDepth)
207 BranchItem *bi=(BranchItem*)treeItem;
208 if (bi->depth() <= toDepth)
210 frame->setVisibility(v);
211 heading->setVisibility(v);
212 systemFlags->setVisibility(v);
213 standardFlags->setVisibility(v);
214 LinkableMapObj::setVisibility (v);
216 for (i=0; i<treeItem->imageCount(); ++i)
217 treeItem->getImageObjNum(i)->setVisibility (v);
218 for (i=0; i<treeItem->xlinkCount(); ++i)
219 treeItem->getXLinkObjNum(i)->setVisibility ();
221 // Only change children, if I am not scrolled
222 if (! bi->isScrolled() && (bi->depth() < toDepth))
224 // Now go recursivly through all children //FIXME-3 are there multiple calls for lower level items???
225 for (i=0; i<treeItem->branchCount(); ++i)
226 treeItem->getBranchObjNum(i)->setVisibility (v,toDepth);
228 } // depth <= toDepth
232 void BranchObj::setVisibility(bool v)
234 setVisibility (v,MAX_DEPTH);
238 void BranchObj::setLinkColor ()
240 // Overloaded from LinkableMapObj
241 // BranchObj can use color of heading
243 VymModel *model=treeItem->getModel();
246 if (model->getMapLinkColorHint()==HeadingColor)
247 LinkableMapObj::setLinkColor (heading->getColor() );
249 LinkableMapObj::setLinkColor ();
253 void BranchObj::updateContentSize()
260 void BranchObj::positionContents()
262 for (int i=0; i<treeItem->imageCount(); ++i)
263 treeItem->getImageObjNum(i)->reposition();
264 OrnamentedObj::positionContents();
267 void BranchObj::move (double x, double y)
269 OrnamentedObj::move (x,y);
273 void BranchObj::move (QPointF p)
278 void BranchObj::moveBy (double x, double y)
280 OrnamentedObj::moveBy (x,y);
281 for (int i=0; i<treeItem->branchCount(); ++i)
282 treeItem->getBranchObjNum(i)->moveBy (x,y);
286 void BranchObj::moveBy (QPointF p)
288 moveBy (p.x(), p.y());
292 void BranchObj::positionBBox()
294 QPointF ap=getAbsPos();
295 bbox.moveTopLeft (ap);
299 frame->setRect(QRectF(bbox.x(),bbox.y(),bbox.width(),bbox.height() ) );
301 //Update links to other branches
303 for (int i=0; i<treeItem->xlinkCount(); ++i)
305 xlo=treeItem->getXLinkObjNum(i);
306 if (xlo) xlo->updateXLink();
310 void BranchObj::calcBBoxSize()
312 QSizeF heading_r=heading->getSize();
313 qreal heading_w=(qreal) heading_r.width() ;
314 qreal heading_h=(qreal) heading_r.height() ;
315 QSizeF sysflags_r=systemFlags->getSize();
316 qreal sysflags_h=sysflags_r.height();
317 qreal sysflags_w=sysflags_r.width();
318 QSizeF stanflags_r=standardFlags->getSize();
319 qreal stanflags_h=stanflags_r.height();
320 qreal stanflags_w=stanflags_r.width();
324 // set width to sum of all widths
325 w=heading_w + sysflags_w + stanflags_w;
326 // set height to maximum needed height
327 h=max (sysflags_h,stanflags_h);
330 // Save the dimension of flags and heading
331 ornamentsBBox.setSize ( QSizeF(w,h));
333 // clickBox includes Flags and Heading
334 clickBox.setSize (ornamentsBBox.size() );
339 topPad=botPad=leftPad=rightPad=0;
340 bool incV=((BranchItem*)treeItem)->getIncludeImagesVer();
341 bool incH=((BranchItem*)treeItem)->getIncludeImagesHor();
345 for (int i=0; i<treeItem->imageCount(); ++i )
347 fio=treeItem->getImageObjNum(i);
352 topPad=max (topPad,-rp.y()-h);
353 if (rp.y()+fio->height() > 0)
354 botPad=max (botPad,rp.y()+fio->height());
358 if (orientation==LinkableMapObj::RightOfCenter)
361 leftPad=max (leftPad,-rp.x()-w);
362 if (rp.x()+fio->width() > 0)
363 rightPad=max (rightPad,rp.x()+fio->width());
367 leftPad=max (leftPad,-rp.x());
368 if (rp.x()+fio->width() > w)
369 rightPad=max (rightPad,rp.x()+fio->width()-w);
378 w+=frame->getPadding();
379 h+=frame->getPadding();
382 bbox.setSize (QSizeF (w,h));
385 void BranchObj::setDockPos()
387 if (treeItem->getType()==TreeItem::MapCenter)
389 // set childPos to middle of MapCenterObj
390 childPos.setX( clickBox.topLeft().x() + clickBox.width()/2 );
391 childPos.setY( clickBox.topLeft().y() + clickBox.height()/2 );
393 for (int i=0; i<treeItem->branchCount(); ++i)
394 treeItem->getBranchObjNum(i)->updateLinkGeometry();
398 // Sets childpos and parpos depending on orientation
399 if (getOrientation()==LinkableMapObj::LeftOfCenter )
402 ornamentsBBox.bottomLeft().x(),
405 ornamentsBBox.bottomRight().x(),
410 ornamentsBBox.bottomRight().x(),
413 ornamentsBBox.bottomLeft().x(),
419 void BranchObj::updateData()
424 qWarning ("BranchObj::udpateHeading treeItem==NULL");
427 QString s=treeItem->getHeading();
428 if (s!=heading->text())
430 heading->setText (s);
433 QStringList TIactiveFlags=treeItem->activeStandardFlagNames();
435 // Add missing standard flags active in TreeItem
436 for (int i=0;i<=TIactiveFlags.size()-1;i++)
438 if (!standardFlags->isActive (TIactiveFlags.at(i) ))
440 Flag *f=standardFlagsMaster->getFlag(TIactiveFlags.at(i));
441 if (f) standardFlags->activate (f);
445 // Remove standard flags no longer active in TreeItem
446 QStringList BOactiveFlags=standardFlags->activeFlagNames();
447 for (int i=0;i<BOactiveFlags.size();++i)
448 if (!TIactiveFlags.contains (BOactiveFlags.at(i)))
450 standardFlags->deactivate (BOactiveFlags.at(i));
454 // Add missing system flags active in TreeItem
455 TIactiveFlags=treeItem->activeSystemFlagNames();
456 for (int i=0;i<TIactiveFlags.size();++i)
458 if (!systemFlags->isActive (TIactiveFlags.at(i) ))
460 Flag *f=systemFlagsMaster->getFlag(TIactiveFlags.at(i));
461 if (f) systemFlags->activate (f);
465 // Remove system flags no longer active in TreeItem
466 BOactiveFlags=systemFlags->activeFlagNames();
467 for (int i=0;i<BOactiveFlags.size();++i)
469 if (!TIactiveFlags.contains (BOactiveFlags.at(i)))
471 systemFlags->deactivate (BOactiveFlags.at(i));
478 void BranchObj::savePosInAngle ()
480 // Save position in angle
481 for (int i=0; i<treeItem->branchCount(); ++i)
482 treeItem->getBranchObjNum(i)->angle=i;
485 void BranchObj::setDefAttr (BranchModification mod)
488 switch (treeItem->depth())
492 setFrameType (FrameObj::Rectangle);
496 setFrameType (FrameObj::NoFrame);
500 setFrameType (FrameObj::NoFrame);
504 setFrameType (FrameObj::NoFrame);
507 setLinkStyle(getDefLinkStyle(treeItem->parent() ));
509 QFont font("Sans Serif,8,-1,5,50,0,0,0,0,0");
510 font.setPointSize(fontsize);
511 heading->setFont(font );
514 setColor (treeItem->getHeadingColor() );
516 // Also set styles for children
517 for (int i=0; i<treeItem->branchCount(); ++i)
518 treeItem->getBranchObjNum(i)->setDefAttr(MovedBranch);
522 void BranchObj::alignRelativeTo (QPointF ref,bool alignSelf)
524 qreal th = bboxTotal.height();
525 int depth=treeItem->depth(); // FIXME-3 needed to recursively calc depth?
529 QString h=QString (depth,' ');
530 h+=treeItem->getHeading();
533 QPointF pp; if (parObj) pp=parObj->getChildPos();
534 cout << "BO::alignRelTo ";
535 cout<<h.toStdString();
536 cout << " d="<<depth<<
537 //cout<< " ref="<<ref<<
538 " bbox.tL="<<bboxTotal.topLeft()<<
542 // " w="<<bbox.width()<<
543 // " h="<<bbox.height()<<
544 // " orient="<<orientation<<
545 // " alignSelf="<<alignSelf<<
546 // " scrolled="<<((BranchItem*)treeItem)->isScrolled()<<
547 // " pad="<<topPad<<","<<botPad<<","<<leftPad<<","<<rightPad<<
548 // " hidden="<<hidden<<
554 //updateLinkGeometry();
556 if (depth==1 && parObj)
558 // Position relatively, if needed
559 //if (useRelPos) move2RelPos (relPos.x(), relPos.y());
561 // Calc angle to mapCenter if I am a mainbranch
562 // needed for reordering the mainbranches clockwise
564 angle=getAngle (QPointF ((int)(x() - parObj->getChildPos().x() ),
565 (int)(y() - parObj->getChildPos().y() ) ) );
569 move2RelPos (getRelPos() );
573 // Align myself depending on orientation and parent, but
574 // only if I am not a mainbranch or mapcenter itself
576 if (anim.isAnimated())
581 LinkableMapObj::Orientation o;
582 o=parObj->getOrientation();
586 case LinkableMapObj::LeftOfCenter:
587 move (ref.x() - bbox.width(), ref.y() + (th-bbox.height())/2 );
588 //move (ref.x() , ref.y() + (th-bbox.height())/2 );
590 case LinkableMapObj::RightOfCenter:
591 move (ref.x() , ref.y() + (th-bbox.height())/2 );
594 qWarning ("LMO::alignRelativeTo: oops, no orientation given...");
600 if ( ((BranchItem*)treeItem)->isScrolled() ) return;
602 // Set reference point for alignment of children
604 if (orientation==LinkableMapObj::LeftOfCenter)
605 ref2.setX(bbox.topLeft().x() - linkwidth);
607 ref2.setX(bbox.topRight().x() + linkwidth);
610 ref2.setY(absPos.y()-(bboxTotal.height()-bbox.height())/2);
614 // Align the children depending on reference point
615 for (int i=0; i<treeItem->branchCount(); ++i)
617 if (!treeItem->getBranchNum(i)->isHidden())
619 treeItem->getBranchObjNum(i)->alignRelativeTo (ref2,true);
621 // append next branch below current one
622 ref2.setY(ref2.y() + treeItem->getBranchObjNum(i)->getBBoxSizeWithChildren().height() );
628 void BranchObj::reposition()
631 if (!treeItem->getHeading().isEmpty())
632 cout << "BO::reposition "<<qPrintable(treeItem->getHeading())<<endl;
634 cout << "BO::reposition ???"<<endl;
635 // cout << " orient="<<orientation<<endl;
638 if (treeItem->depth()==0)
640 // only calculate the sizes once. If the deepest LMO
641 // changes its height,
642 // all upper LMOs have to change, too.
643 calcBBoxSizeWithChildren();
644 updateLinkGeometry(); // This update is needed if the scene is resized
645 // due to excessive moving of a FIO
647 alignRelativeTo ( QPointF (absPos.x(),
648 absPos.y()-(bboxTotal.height()-bbox.height())/2) );
649 //FIXME-2 qSort (branch.begin(),branch.end(), isAbove);
650 positionBBox(); // Reposition bbox and contents
653 // This is only important for moving branches:
654 // For editing a branch it isn't called...
655 alignRelativeTo ( QPointF (absPos.x(),
656 absPos.y()-(bboxTotal.height()-bbox.height())/2) );
660 void BranchObj::unsetAllRepositionRequests()
662 repositionRequest=false;
663 for (int i=0; i<treeItem->branchCount(); ++i)
664 treeItem->getBranchObjNum(i)->unsetAllRepositionRequests();
668 QRectF BranchObj::getTotalBBox()
672 if ( ((BranchItem*)treeItem)->isScrolled() ) return r;
674 for (int i=0; i<treeItem->branchCount(); ++i)
675 if (!treeItem->getBranchNum(i)->isHidden())
676 r=addBBox(treeItem->getBranchObjNum(i)->getTotalBBox(),r);
678 /* FIXME-3 lots of occurences of treeItem->getBranchObjNum(i) in branchobj.cpp
679 better check if they are not NULL and maybe simplify...
680 (have been NULL at least in calcBBoxSizeWithChilds...)
683 for (int i=0; i<treeItem->imageCount(); ++i)
684 if (!treeItem->isHidden())
685 r=addBBox(treeItem->getImageObjNum(i)->getTotalBBox(),r);
689 QRectF BranchObj::getBBoxSizeWithChildren()
694 ConvexPolygon BranchObj::getBoundingPolygon()
697 if (!pi) //FIXME-3 Testing only
699 pi=scene->addPolygon(MapObj::getBoundingPolygon() );
700 pi->setPen(Qt::NoPen);
701 pi->setBrush( QColor(qrand()%32*8,qrand()%32*8,qrand()%32*8) );
702 pi->setZValue(Z_BBOX);
706 if (treeItem->branchCount()==0 || treeItem->depth()==0)
708 if (pi) pi->setPolygon (MapObj::getBoundingPolygon() );
709 return MapObj::getBoundingPolygon();
712 calcBBoxSizeWithChildren(); //FIXME-3 really needed?
714 p<<bboxTotal.topLeft();
715 p<<bboxTotal.topRight();
716 p<<bboxTotal.bottomRight();
717 p<<bboxTotal.bottomLeft();
718 //cout << "BO::getBP (total) "<<treeItem->getHeadingStd()<<" tL="<<bboxTotal.topLeft()<<" bR="<<bboxTotal.bottomRight()<<endl;
719 //cout << " "<<" tL="<<bbox.topLeft()<<" bR="<<bbox.bottomRight()<<endl;
720 if (pi) pi->setPolygon (p );
724 void BranchObj::calcBBoxSizeWithChildren()
726 // This is initially called only from reposition and
727 // and only for mapcenter. So it won't be
728 // called more than once for a single user
732 // Calculate size of LMO including all children (to align them later)
733 bboxTotal.setX(bbox.x() );
734 bboxTotal.setY(bbox.y() );
736 // if branch is scrolled, ignore children, but still consider floatimages
737 BranchItem *bi=(BranchItem*)treeItem;
738 if ( bi->isScrolled() )
740 bboxTotal.setWidth (bbox.width());
741 bboxTotal.setHeight(bbox.height());
747 bboxTotal.setWidth (0);
748 bboxTotal.setHeight(0);
751 bboxTotal.setX (parObj->x());
752 bboxTotal.setY (parObj->y());
755 bboxTotal.setX (bbox.x());
756 bboxTotal.setY (bbox.y());
763 // Now calculate recursivly
767 for (int i=0; i<treeItem->branchCount(); i++)
769 if (!bi->getBranchNum(i)->isHidden())
771 bi->getBranchObjNum(i)->calcBBoxSizeWithChildren();
772 br=bi->getBranchObjNum(i)->getBBoxSizeWithChildren();
773 r.setWidth( max (br.width(), r.width() ));
774 r.setHeight(br.height() + r.height() );
775 if (br.y()<bboxTotal.y()) bboxTotal.setY(br.y());
776 if (br.x()<bboxTotal.x()) bboxTotal.setX(br.x());
779 // Add myself and also
780 // add width of link to sum if necessary
781 if (bi->branchCount()<1)
782 bboxTotal.setWidth (bbox.width() + r.width() );
784 bboxTotal.setWidth (bbox.width() + r.width() + linkwidth);
786 bboxTotal.setHeight(max (r.height(), bbox.height()));
789 QString BranchObj::getSelectString()
791 VymModel *model=treeItem->getModel();
793 return model->getSelectString (this);
798 void BranchObj::setAnimation(const AnimPoint &ap)
803 bool BranchObj::animate()
806 if ( anim.isAnimated() )
811 parObj->reposition(); // we might have been relinked meanwhile