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)
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);
130 if (!parObjTmpBuf) parObjTmpBuf=parObj;
132 // ignore mapcenter and mainbranch //FIXME-1 MCO meanwhile also could be relinked
133 if (pi_depth<2) off=0;
140 // setLinkStyle calls updateLinkGeometry, only set it once
141 //LinkableMapObj::Style ls=getDefLinkStyle (dst->getTreeItem() );
142 //if (style!=ls ) setLinkStyle (ls);
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
152 //FIXME-2 rewrite to us new normalize QPointF p= normalize ( QPointF (m.x() - o->getChildPos().x(),
153 // m.y() - o->getChildPos().y() ));
154 QPointF p= ( m - bo->getChildPos());
155 if (p.x()<0) p.setX( p.x()-bbox.width() );
162 // new parent is just a branch, link to it
163 QRectF t=bo->getBBoxSizeWithChildren();
164 if (dsti->getLastBranch())
165 y=t.y() + t.height() ;
172 // we want to link above dst
173 y=bo->y() - height() + 5;
175 // we want to link below dst
176 // Bottom of sel should be 5 pixels above
177 // the bottom of the branch _below_ the target:
178 // Don't try to find that branch, guess 12 pixels
179 y=bo->getChildPos().y() -height() + 12;
181 if (bo->getOrientation()==LinkableMapObj::LeftOfCenter)
182 move ( bo->getChildPos().x() - linkwidth, y );
184 move (bo->getChildPos().x() + linkwidth, y );
187 // updateLinkGeometry is called implicitly in move
191 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())
488 case 0: fontsize=16; break;
489 case 1: fontsize=12; break;
490 default: fontsize=10; break;
492 setLinkStyle(getDefLinkStyle(treeItem->parent() ));
494 QFont font("Sans Serif,8,-1,5,50,0,0,0,0,0");
495 font.setPointSize(fontsize);
496 heading->setFont(font );
499 setColor (treeItem->getHeadingColor() );
503 void BranchObj::alignRelativeTo (QPointF ref,bool alignSelf)
505 qreal th = bboxTotal.height();
506 int depth=treeItem->depth(); // FIXME-3 needed to recursively calc depth?
510 QString h=QString (depth,' ');
511 h+=treeItem->getHeading();
514 QPointF pp; if (parObj) pp=parObj->getChildPos();
515 cout << "BO::alignRelTo ";
516 cout<<h.toStdString();
517 cout << " d="<<depth<<
518 //cout<< " ref="<<ref<<
519 " bbox.tL="<<bboxTotal.topLeft()<<
523 // " w="<<bbox.width()<<
524 // " h="<<bbox.height()<<
525 // " orient="<<orientation<<
526 // " alignSelf="<<alignSelf<<
527 // " scrolled="<<((BranchItem*)treeItem)->isScrolled()<<
528 // " pad="<<topPad<<","<<botPad<<","<<leftPad<<","<<rightPad<<
529 // " hidden="<<hidden<<
535 //updateLinkGeometry();
537 if (depth==1 && parObj)
539 // Position relatively, if needed
540 //if (useRelPos) move2RelPos (relPos.x(), relPos.y());
542 // Calc angle to mapCenter if I am a mainbranch
543 // needed for reordering the mainbranches clockwise
545 angle=getAngle (QPointF ((int)(x() - parObj->getChildPos().x() ),
546 (int)(y() - parObj->getChildPos().y() ) ) );
550 move2RelPos (getRelPos() );
554 // Align myself depending on orientation and parent, but
555 // only if I am not a mainbranch or mapcenter itself
557 if (anim.isAnimated())
562 LinkableMapObj::Orientation o;
563 o=parObj->getOrientation();
567 case LinkableMapObj::LeftOfCenter:
568 move (ref.x() - bbox.width(), ref.y() + (th-bbox.height())/2 );
569 //move (ref.x() , ref.y() + (th-bbox.height())/2 );
571 case LinkableMapObj::RightOfCenter:
572 move (ref.x() , ref.y() + (th-bbox.height())/2 );
575 qWarning ("LMO::alignRelativeTo: oops, no orientation given...");
581 if ( ((BranchItem*)treeItem)->isScrolled() ) return;
583 // Set reference point for alignment of children
585 if (orientation==LinkableMapObj::LeftOfCenter)
586 ref2.setX(bbox.topLeft().x() - linkwidth);
588 ref2.setX(bbox.topRight().x() + linkwidth);
591 ref2.setY(absPos.y()-(bboxTotal.height()-bbox.height())/2);
595 // Align the children depending on reference point
596 for (int i=0; i<treeItem->branchCount(); ++i)
598 if (!treeItem->getBranchNum(i)->isHidden())
600 treeItem->getBranchObjNum(i)->alignRelativeTo (ref2,true);
602 // append next branch below current one
603 ref2.setY(ref2.y() + treeItem->getBranchObjNum(i)->getBBoxSizeWithChildren().height() );
609 void BranchObj::reposition()
612 if (!treeItem->getHeading().isEmpty())
613 cout << "BO::reposition "<<qPrintable(treeItem->getHeading())<<endl;
615 cout << "BO::reposition ???"<<endl;
616 // cout << " orient="<<orientation<<endl;
619 if (treeItem->depth()==0)
621 // only calculate the sizes once. If the deepest LMO
622 // changes its height,
623 // all upper LMOs have to change, too.
624 calcBBoxSizeWithChildren();
625 updateLinkGeometry(); // This update is needed if the scene is resized
626 // due to excessive moving of a FIO
628 alignRelativeTo ( QPointF (absPos.x(),
629 absPos.y()-(bboxTotal.height()-bbox.height())/2) );
630 //FIXME-2 qSort (branch.begin(),branch.end(), isAbove);
631 positionBBox(); // Reposition bbox and contents
634 // This is only important for moving branches:
635 // For editing a branch it isn't called...
636 alignRelativeTo ( QPointF (absPos.x(),
637 absPos.y()-(bboxTotal.height()-bbox.height())/2) );
641 void BranchObj::unsetAllRepositionRequests()
643 repositionRequest=false;
644 for (int i=0; i<treeItem->branchCount(); ++i)
645 treeItem->getBranchObjNum(i)->unsetAllRepositionRequests();
649 QRectF BranchObj::getTotalBBox()
653 if ( ((BranchItem*)treeItem)->isScrolled() ) return r;
655 for (int i=0; i<treeItem->branchCount(); ++i)
656 if (!treeItem->getBranchNum(i)->isHidden())
657 r=addBBox(treeItem->getBranchObjNum(i)->getTotalBBox(),r);
659 /* FIXME-3 lots of occurences of treeItem->getBranchObjNum(i) in branchobj.cpp
660 better check if they are not NULL and maybe simplify...
661 (have been NULL at least in calcBBoxSizeWithChilds...)
664 for (int i=0; i<treeItem->imageCount(); ++i)
665 if (!treeItem->isHidden())
666 r=addBBox(treeItem->getImageObjNum(i)->getTotalBBox(),r);
670 QRectF BranchObj::getBBoxSizeWithChildren()
675 ConvexPolygon BranchObj::getBoundingPolygon()
678 if (!pi) //FIXME-3 Testing only
680 pi=scene->addPolygon(MapObj::getBoundingPolygon() );
681 pi->setPen(Qt::NoPen);
682 pi->setBrush( QColor(qrand()%32*8,qrand()%32*8,qrand()%32*8) );
683 pi->setZValue(Z_BBOX);
686 if (treeItem->branchCount()==0 || treeItem->depth()==0)
688 if (pi) pi->setPolygon (MapObj::getBoundingPolygon() );
689 return MapObj::getBoundingPolygon();
692 calcBBoxSizeWithChildren(); //FIXME-3 really needed?
694 p<<bboxTotal.topLeft();
695 p<<bboxTotal.topRight();
696 p<<bboxTotal.bottomRight();
697 p<<bboxTotal.bottomLeft();
698 if (pi) pi->setPolygon (p );
702 void BranchObj::calcBBoxSizeWithChildren()
704 // This is initially called only from reposition and
705 // and only for mapcenter. So it won't be
706 // called more than once for a single user
710 // Calculate size of LMO including all children (to align them later)
711 bboxTotal.setX(bbox.x() );
712 bboxTotal.setY(bbox.y() );
714 // if branch is scrolled, ignore children, but still consider floatimages
715 BranchItem *bi=(BranchItem*)treeItem;
716 if ( bi->isScrolled() )
718 bboxTotal.setWidth (bbox.width());
719 bboxTotal.setHeight(bbox.height());
725 bboxTotal.setWidth (0);
726 bboxTotal.setHeight(0);
729 bboxTotal.setX (parObj->x());
730 bboxTotal.setY (parObj->y());
733 bboxTotal.setX (bbox.x());
734 bboxTotal.setY (bbox.y());
741 // Now calculate recursivly
745 for (int i=0; i<treeItem->branchCount(); i++)
747 if (!bi->getBranchNum(i)->isHidden())
749 bi->getBranchObjNum(i)->calcBBoxSizeWithChildren();
750 br=bi->getBranchObjNum(i)->getBBoxSizeWithChildren();
751 r.setWidth( max (br.width(), r.width() ));
752 r.setHeight(br.height() + r.height() );
753 if (br.y()<bboxTotal.y()) bboxTotal.setY(br.y());
756 // Add myself and also
757 // add width of link to sum if necessary
758 if (bi->branchCount()<1)
759 bboxTotal.setWidth (bbox.width() + r.width() );
761 bboxTotal.setWidth (bbox.width() + r.width() + linkwidth);
763 bboxTotal.setHeight(max (r.height(), bbox.height()));
766 QString BranchObj::getSelectString()
768 VymModel *model=treeItem->getModel();
770 return model->getSelectString (this);
775 void BranchObj::setAnimation(const AnimPoint &ap)
780 bool BranchObj::animate()
783 if ( anim.isAnimated() )
788 parObj->reposition(); // we might have been relinked meanwhile