branchobj.cpp
author insilmaril
Thu, 02 Apr 2009 09:46:29 +0000
changeset 750 ff3b01ce0960
parent 749 9ff332964015
child 751 7fa9b3164cbe
permissions -rw-r--r--
More moving from BranchObj to BranchItem
     1 #include "branchobj.h"
     2 
     3 // #include "texteditor.h"
     4 #include "branchitem.h"
     5 #include "geometry.h"
     6 #include "mapeditor.h"
     7 #include "mainwindow.h"
     8 #include "misc.h"
     9 
    10 class TextEditor;
    11 
    12 extern TextEditor *textEditor;
    13 extern Main *mainWindow;
    14 extern FlagRowObj *standardFlagsDefault;
    15 
    16 
    17 /////////////////////////////////////////////////////////////////
    18 // BranchObj
    19 /////////////////////////////////////////////////////////////////
    20 
    21 BranchObj::BranchObj () :OrnamentedObj()	// FIXME-3 needed at all?
    22 {
    23 //    cout << "Const BranchObj ()\n";
    24     setParObj (this);	
    25     init();
    26     depth=-1;
    27 }
    28 
    29 BranchObj::BranchObj (QGraphicsScene* s):OrnamentedObj (s)
    30 {
    31 //    cout << "Const BranchObj (s)  called from MapCenterObj (s)\n";
    32 	parObj=NULL;
    33     scene=s;
    34 }
    35 
    36 BranchObj::BranchObj (QGraphicsScene* s, LinkableMapObj* p):OrnamentedObj (s)
    37 {
    38 //    cout << "Const BranchObj (s,p)\n";
    39     scene=s;
    40     setParObj (p);	
    41     depth=p->getDepth()+1;
    42 	if (depth==1)
    43 		// Calc angle to mapCenter if I am a mainbranch
    44 		// needed for reordering the mainbranches clockwise 
    45 		// around mapcenter 
    46 		angle=getAngle (QPointF (x() - parObj->getChildPos().x() , 
    47 								(y() - parObj->getChildPos().y() ) ) );
    48     init();
    49 }
    50 
    51 BranchObj::~BranchObj ()
    52 {
    53 	// If I'm animated, I need to un-animate myself first
    54 	if (anim.isAnimated() )
    55 	{
    56 		anim.setAnimated (false);
    57 		model->stopAnimation (this);
    58 	}
    59 
    60 	cout << "Destr BranchObj of "<<this<<" ("<<getHeading().toStdString()<<")"<<endl;
    61 	// Check, if this branch was the last child to be deleted
    62 	// If so, unset the scrolled flags in parent // FIXME-2 better do this in model?
    63 
    64 	/*
    65 	BranchObj *po=(BranchObj*)parObj;
    66 	BranchObj *bo;
    67 	if (po)
    68 	{
    69 		bo=((BranchObj*)parObj)->getLastBranch();
    70 		if (bo) po->unScroll();
    71 	}
    72 	*/
    73 	clear();
    74 }
    75 
    76 bool BranchObj::operator< ( const BranchObj & other )
    77 {
    78     return  angle < other.angle;
    79 }
    80 
    81 bool BranchObj::operator== ( const BranchObj & other )
    82 {
    83     return angle == other.angle;
    84 }
    85 
    86 void BranchObj::init () 
    87 {
    88 	if (parObj)
    89 	{
    90 		absPos=getRandPos();
    91 		absPos+=parObj->getChildPos();
    92 	}
    93 
    94     setChildObj(this);
    95 
    96 	includeImagesVer=false;
    97 	includeImagesHor=false;
    98 }
    99 
   100 void BranchObj::copy (BranchObj* other)
   101 {
   102     OrnamentedObj::copy(other);
   103 
   104 	branch.clear();
   105 	for (int i=0; i<other->treeItem->branchCount(); ++i)
   106 		// Make deep copy of b
   107 		// Because addBranch again calls copy for the children,
   108 		// Those will get a deep copy, too
   109 		addBranch(other->branch.at(i) );	
   110 
   111 	for (int i=0; i<other->floatimage.size(); ++i)
   112 		addFloatImage  (other->floatimage.at(i));
   113 	
   114 	setVisibility (other->visible);
   115 
   116 	angle=other->angle;
   117 
   118     positionBBox();
   119 }
   120 
   121 void BranchObj::clear() 
   122 {
   123 	setVisibility (true);
   124 
   125 	while (!floatimage.isEmpty())
   126 		delete floatimage.takeFirst();
   127 
   128 	while (!xlink.isEmpty())
   129 		delete xlink.takeFirst();
   130 
   131 	while (!branch.isEmpty())
   132 		delete branch.takeFirst();
   133 }
   134 
   135 bool isAbove (BranchObj* a, BranchObj *b)
   136 {
   137 	if (a->angle < b->angle)
   138 		return true;
   139 	else	
   140 		return false;
   141 }
   142 
   143 void BranchObj::setParObjTmp(LinkableMapObj* lmo, QPointF m, int off)
   144 {
   145 	// Temporary link to lmo
   146 	// m is position of mouse pointer 
   147 	// offset 0: default 1: below lmo   -1 above lmo  (if possible)
   148 
   149 
   150 	BranchObj* o=(BranchObj*)(lmo);
   151 	if (!parObjTmpBuf) 
   152 		parObjTmpBuf=parObj;
   153 
   154 	// ignore mapcenter and mainbranch
   155 	if (lmo->getDepth()<2) off=0;
   156 	if (off==0)
   157 		link2ParPos=false;
   158 	else
   159 		link2ParPos=true;
   160 	parObj=o;
   161 
   162 	depth=parObj->getDepth()+1;
   163 
   164 	// setLinkStyle calls updateLink, only set it once
   165 	if (style!=getDefLinkStyle() ) setLinkStyle (getDefLinkStyle());
   166 
   167 	// Move temporary to new position at destination
   168 	// Usually the positioning would be done by reposition(),
   169 	// but then also the destination branch would "Jump" around...
   170 	// Better just do it approximately
   171 	if (depth==1)
   172 	{	// new parent is the mapcenter itself
   173 
   174 		QPointF p= normalise ( QPointF (m.x() - o->getChildPos().x(),
   175 									  m.y() - o->getChildPos().y() ));
   176 		if (p.x()<0) p.setX( p.x()-bbox.width() );
   177 		move2RelPos (p);
   178 	} else
   179 	{	
   180 		qreal y;
   181 		if (off==0)
   182 		{
   183 			// new parent is just a branch, link to it
   184 			QRectF t=o->getBBoxSizeWithChildren();
   185 			if (o->getTreeItem()->getLastBranch())
   186 				y=t.y() + t.height() ;
   187 			else
   188 				y=t.y();
   189 
   190 		} else
   191 		{
   192 			if (off<0)
   193 				// we want to link above lmo
   194 				y=o->y() - height() + 5;
   195 			else	
   196 				// we want to link below lmo
   197 				// Bottom of sel should be 5 pixels above
   198 				// the bottom of the branch _below_ the target:
   199 				// Don't try to find that branch, guess 12 pixels
   200 				y=o->getChildPos().y()  -height() + 12; 
   201 		}	
   202 		if (o->getOrientation()==LinkableMapObj::LeftOfCenter)
   203 			move ( o->getChildPos().x() - linkwidth, y );
   204 		else	
   205 			move (o->getChildPos().x() + linkwidth, y );
   206 	}	
   207 
   208 	// updateLink is called implicitly in move
   209 	requestReposition();	
   210 }
   211 
   212 void BranchObj::unsetParObjTmp()
   213 {
   214 	if (parObjTmpBuf) 
   215 	{
   216 		link2ParPos=false;
   217 		parObj=parObjTmpBuf;
   218 		parObjTmpBuf=NULL;
   219 		depth=parObj->getDepth()+1;
   220 		setLinkStyle (getDefLinkStyle() );
   221 		updateLink();
   222 	}		
   223 }
   224 
   225 void BranchObj::setVisibility(bool v, int toDepth)
   226 {
   227 	BranchItem *bi=(BranchItem*)treeItem;
   228     if (depth <= toDepth)
   229     {
   230 		frame->setVisibility(v);
   231 		heading->setVisibility(v);
   232 		systemFlags->setVisibility(v);
   233 		standardFlags->setVisibility(v);
   234 		LinkableMapObj::setVisibility (v);
   235 		int i;
   236 		for (i=0; i<floatimage.size(); ++i)
   237 			floatimage.at(i)->setVisibility (v);
   238 		for (i=0; i<xlink.size(); ++i)	
   239 			xlink.at(i)->setVisibility ();	
   240 
   241 		// Only change children, if I am not scrolled
   242 		if (! bi->isScrolled() && (bi->depth() < toDepth))
   243 		{
   244 			// Now go recursivly through all children
   245 			for (i=0; i<treeItem->branchCount(); ++i)
   246 				branch.at(i)->setVisibility (v,toDepth);	
   247 		}
   248     } // depth <= toDepth	
   249 	requestReposition();
   250 }	
   251 
   252 void BranchObj::setVisibility(bool v)
   253 {
   254     setVisibility (v,MAX_DEPTH);
   255 }
   256 
   257 
   258 void BranchObj::setLinkColor ()
   259 {
   260 	// Overloaded from LinkableMapObj
   261 	// BranchObj can use color of heading
   262 
   263 	if (model)
   264 	{
   265 		if (model->getMapLinkColorHint()==HeadingColor)
   266 			LinkableMapObj::setLinkColor (heading->getColor() );
   267 		else	
   268 			LinkableMapObj::setLinkColor ();
   269 	}		
   270 }
   271 
   272 void BranchObj::setColorSubtree(QColor col)
   273 {
   274 	setColor (col);
   275 	for (int i=0; i<treeItem->branchCount(); ++i)
   276 		branch.at(i)->setColorSubtree(col);
   277 }
   278 
   279 void BranchObj::positionContents()
   280 {
   281     for (int i=0; i<floatimage.size(); ++i )
   282 		floatimage.at(i)->reposition();
   283 	OrnamentedObj::positionContents();
   284 }
   285 
   286 void BranchObj::move (double x, double y)
   287 {
   288 	OrnamentedObj::move (x,y);
   289     for (int i=0; i<floatimage.size(); ++i )
   290 		floatimage.at(i)->reposition();
   291     positionBBox();
   292 }
   293 
   294 void BranchObj::move (QPointF p)
   295 {
   296 	move (p.x(), p.y());
   297 }
   298 
   299 void BranchObj::moveBy (double x, double y)
   300 {
   301 	OrnamentedObj::moveBy (x,y);
   302 	for (int i=0; i<treeItem->branchCount(); ++i)
   303 		branch.at(i)->moveBy (x,y);
   304     positionBBox();
   305 }
   306 	
   307 void BranchObj::moveBy (QPointF p)
   308 {
   309 	moveBy (p.x(), p.y());
   310 }
   311 
   312 
   313 void BranchObj::positionBBox()
   314 {
   315 	QPointF ap=getAbsPos();
   316 	bbox.moveTopLeft (ap);
   317 	positionContents();
   318 
   319 	// set the frame
   320 	frame->setRect(QRectF(bbox.x(),bbox.y(),bbox.width(),bbox.height() ) );
   321 
   322 	// Update links to other branches
   323 	for (int i=0; i<xlink.size(); ++i)
   324 		xlink.at(i)->updateXLink();
   325 }
   326 
   327 void BranchObj::calcBBoxSize()
   328 {
   329     QSizeF heading_r=heading->getSize();
   330     qreal heading_w=(qreal) heading_r.width() ;
   331     qreal heading_h=(qreal) heading_r.height() ;
   332     QSizeF sysflags_r=systemFlags->getSize();
   333 	qreal sysflags_h=sysflags_r.height();
   334 	qreal sysflags_w=sysflags_r.width();
   335     QSizeF stanflags_r=standardFlags->getSize();
   336 	qreal stanflags_h=stanflags_r.height();
   337 	qreal stanflags_w=stanflags_r.width();
   338     qreal w;
   339     qreal h;
   340 
   341 	// set width to sum of all widths
   342 	w=heading_w + sysflags_w + stanflags_w;
   343 	// set height to maximum needed height
   344 	h=max (sysflags_h,stanflags_h);
   345 	h=max (h,heading_h);
   346 
   347 	// Save the dimension of flags and heading
   348 	ornamentsBBox.setSize ( QSizeF(w,h));
   349 
   350 	// clickBox includes Flags and Heading
   351     clickBox.setSize (ornamentsBBox.size() );
   352 
   353 	// Floatimages 
   354 	QPointF rp;
   355 
   356 	topPad=botPad=leftPad=rightPad=0;
   357 	if (includeImagesVer || includeImagesHor)
   358 	{
   359 		if (treeItem->imageCount()>0)
   360 		{
   361 			for (int i=0; i<floatimage.size(); ++i )
   362 			{
   363 				rp=floatimage.at(i)->getRelPos();
   364 				if (includeImagesVer)
   365 				{
   366 					if (rp.y() < 0) 
   367 						topPad=max (topPad,-rp.y()-h);
   368 					if (rp.y()+floatimage.at(i)->height() > 0)
   369 						botPad=max (botPad,rp.y()+floatimage.at(i)->height());
   370 				}		
   371 				if (includeImagesHor)
   372 				{
   373 					if (orientation==LinkableMapObj::RightOfCenter)
   374 					{
   375 						if (-rp.x()-w > 0) 
   376 							leftPad=max (leftPad,-rp.x()-w);
   377 						if (rp.x()+floatimage.at(i)->width() > 0)
   378 							rightPad=max (rightPad,rp.x()+floatimage.at(i)->width());
   379 					} else
   380 					{
   381 						if (rp.x()< 0) 
   382 							leftPad=max (leftPad,-rp.x());
   383 						if (rp.x()+floatimage.at(i)->width() > w)
   384 							rightPad=max (rightPad,rp.x()+floatimage.at(i)->width()-w);
   385 					}
   386 				}		
   387 			}	
   388 		}	
   389 		h+=topPad+botPad;
   390 		w+=leftPad+rightPad;
   391 	}
   392 
   393 	// Frame thickness
   394     w+=frame->getPadding();
   395     h+=frame->getPadding();
   396 	
   397 	// Finally set size
   398     bbox.setSize (QSizeF (w,h));
   399 }
   400 
   401 void BranchObj::setDockPos()
   402 {
   403 	// Sets childpos and parpos depending on orientation
   404 	if (getOrientation()==LinkableMapObj::LeftOfCenter )
   405     {
   406 		childPos=QPointF (
   407 			ornamentsBBox.bottomLeft().x(), 
   408 			bottomlineY);
   409 		parPos=QPointF (
   410 			ornamentsBBox.bottomRight().x(),
   411 			bottomlineY);
   412     } else
   413     {
   414 		childPos=QPointF (
   415 			ornamentsBBox.bottomRight().x(), 
   416 			bottomlineY);
   417 		parPos=QPointF (
   418 			ornamentsBBox.bottomLeft().x(),
   419 			bottomlineY);
   420     }
   421 }
   422 
   423 LinkableMapObj* BranchObj::findMapObj(QPointF p, LinkableMapObj* excludeLMO)
   424 {
   425 	// Search branches
   426     LinkableMapObj *lmo;
   427 	for (int i=0; i<treeItem->branchCount(); ++i)
   428     {	
   429 		lmo=branch.at(i)->findMapObj(p, excludeLMO);
   430 		if (lmo != NULL) return lmo;
   431     }
   432 	
   433 
   434 	// Search myself
   435     if (inBox (p,clickBox) && (this != excludeLMO) && isVisibleObj() ) 
   436 		return this;
   437 
   438 	// Search float images
   439     for (int i=0; i<floatimage.size(); ++i )
   440 		if (inBox(p,floatimage.at(i)->getClickBox()) && 
   441 			(floatimage.at(i) != excludeLMO) && 
   442 			floatimage.at(i)->getParObj()!= excludeLMO &&
   443 			floatimage.at(i)->isVisibleObj() 
   444 		) return floatimage.at(i);
   445 
   446     return NULL;
   447 }
   448 
   449 LinkableMapObj* BranchObj::findID (QString sid)
   450 {
   451 	// Search branches
   452     LinkableMapObj *lmo;
   453 	for (int i=0; i<treeItem->branchCount(); ++i)
   454     {	
   455 		lmo=branch.at(i)->findID (sid);
   456 		if (lmo != NULL) return lmo;
   457     }
   458 	
   459 	// Search myself
   460 	if (sid==objID) return this;
   461 
   462 
   463 /*
   464 	// Search float images
   465     for (int i=0; i<floatimage.size(); ++i )
   466 		if (floatimage.at(i)->inBox(p) && 
   467 			(floatimage.at(i) != excludeLMO) && 
   468 			floatimage.at(i)->getParObj()!= excludeLMO &&
   469 			floatimage.at(i)->isVisibleObj() 
   470 		) return floatimage.at(i);
   471 */
   472     return NULL;
   473 }
   474 
   475 void BranchObj::setHeading(QString s)
   476 {
   477     heading->setText(s);	// set new heading
   478 	calcBBoxSize();			// recalculate bbox
   479     positionBBox();			// rearrange contents
   480 	requestReposition();
   481 }
   482 
   483 void BranchObj::setHideTmp (HideTmpMode mode)
   484 {
   485 	if (mode==HideExport && (hideExport|| hasHiddenExportParent() ) )
   486 	{
   487 		// Hide stuff according to hideExport flag and parents
   488 		setVisibility (false);
   489 		hidden=true;
   490 	}else
   491 	{
   492 		// Do not hide, but still take care of scrolled status
   493 		if ( ((BranchItem*)treeItem)->hasScrolledParent((BranchItem*)treeItem))
   494 			setVisibility (false);
   495 		else
   496 			setVisibility (true);
   497 		hidden=false;
   498 	}	
   499 
   500 	// And take care of my children
   501 	for (int i=0; i<treeItem->branchCount(); ++i)
   502 		branch.at(i)->setHideTmp (mode);
   503 }
   504 
   505 bool BranchObj::hasHiddenExportParent()
   506 {
   507 	// Calls parents recursivly to
   508 	// find out, if we or parents are temp. hidden
   509 
   510 	if (hidden || hideExport) return true;
   511 
   512 	BranchObj* bo=(BranchObj*)parObj;
   513 	if (bo) 
   514 		return bo->hasHiddenExportParent();
   515 	else
   516 		return false;
   517 }
   518 
   519 QString BranchObj::saveToDir (const QString &tmpdir,const QString &prefix, const QPointF& offset)
   520 {
   521 	// Cloudy stuff can be hidden during exports
   522 	if (hidden) return "";
   523 
   524 	// Update of note is usually done while unselecting a branch
   525 	// if (isNoteInEditor) getNoteFromTextEditor();		//FIXME-2 moved to TreeItem
   526 	
   527     QString s,a;
   528 	QString scrolledAttr;
   529 	if ( ((BranchItem*)treeItem)->isScrolled() ) 
   530 		scrolledAttr=attribut ("scrolled","yes");
   531 	else
   532 		scrolledAttr="";
   533 
   534 	// save area, if not scrolled
   535 	QString areaAttr;
   536 	if (!((BranchItem*) (treeItem->parent()) )->isScrolled() )
   537 	{
   538 		areaAttr=
   539 			attribut("x1",QString().setNum(absPos.x()-offset.x())) +
   540 			attribut("y1",QString().setNum(absPos.y()-offset.y())) +
   541 			attribut("x2",QString().setNum(absPos.x()+width()-offset.x())) +
   542 			attribut("y2",QString().setNum(absPos.y()+height()-offset.y()));
   543 
   544 	} else
   545 		areaAttr="";
   546 	
   547 	// Providing an ID for a branch makes export to XHTML easier
   548 	QString idAttr;
   549 	if (treeItem->xlinkCount()>0)
   550 		idAttr=attribut ("id",model->getSelectString(this)); //TODO directly access model
   551 	else
   552 		idAttr="";
   553 
   554     s=beginElement ("branch" 
   555 		+getOrnXMLAttr() 
   556 		+scrolledAttr 
   557 		+areaAttr 
   558 		+idAttr 
   559 		+getIncludeImageAttr() );
   560     incIndent();
   561 
   562 	// save heading
   563     s+=valueElement("heading", getHeading(),
   564 		attribut ("textColor",QColor(heading->getColor()).name()));
   565 
   566 	// Save frame
   567 	if (frame->getFrameType()!=FrameObj::NoFrame) 
   568 		s+=frame->saveToDir ();
   569 
   570 	// save names of flags set
   571 	s+=standardFlags->saveToDir(tmpdir,prefix,0);
   572 	
   573 	// Save FloatImages
   574 	for (int i=0; i<floatimage.size(); ++i)
   575 		s+=floatimage.at(i)->saveToDir (tmpdir,prefix);
   576 
   577 	// save note
   578 	if (!treeItem->getNoteObj().isEmpty() )
   579 		s+=treeItem->getNoteObj().saveToDir();
   580 	
   581 	// Save branches
   582 	for (int i=0; i<treeItem->branchCount(); ++i)
   583 		s+=branch.at(i)->saveToDir(tmpdir,prefix,offset);
   584 
   585 	// Save XLinks
   586 	QString ol;	// old link
   587 	QString cl;	// current link
   588 	for (int i=0; i<xlink.size(); ++i)
   589 	{
   590 		cl=xlink.at(i)->saveToDir();
   591 		if (cl!=ol)
   592 		{
   593 			s+=cl;
   594 			ol=cl;
   595 		} else
   596 		{
   597 			qWarning (QString("Ignoring of duplicate xLink in %1").arg(getHeading()));
   598 		}
   599 	}	
   600 
   601     decIndent();
   602     s+=endElement   ("branch");
   603     return s;
   604 }
   605 
   606 void BranchObj::addXLink (XLinkObj *xlo)
   607 {
   608 	xlink.append (xlo);
   609 	
   610 }
   611 
   612 void BranchObj::removeXLinkRef (XLinkObj *xlo)
   613 {
   614 	xlink.removeAt (xlink.indexOf(xlo));
   615 }
   616 
   617 void BranchObj::deleteXLink(XLinkObj *xlo)
   618 {
   619 	xlo->deactivate();
   620 	if (!xlo->isUsed()) delete (xlo);
   621 }
   622 
   623 void BranchObj::deleteXLinkAt (int i)
   624 {
   625 	XLinkObj *xlo=xlink.at(i);
   626 	xlo->deactivate();
   627 	if (!xlo->isUsed()) delete(xlo);
   628 }
   629 
   630 XLinkObj* BranchObj::XLinkAt (int i)
   631 {
   632 	return xlink.at(i);
   633 }
   634 
   635 BranchObj* BranchObj::XLinkTargetAt (int i)
   636 {
   637 	if (i>=0 && i<xlink.size())
   638 	{
   639 		if (xlink.at(i))
   640 			return xlink.at(i)->otherBranch (this);
   641 	}
   642 	return NULL;
   643 }
   644 
   645 void BranchObj::setIncludeImagesVer(bool b)
   646 {
   647 	includeImagesVer=b;
   648 	calcBBoxSize();
   649 	positionBBox();
   650 	requestReposition();
   651 }
   652 
   653 bool BranchObj::getIncludeImagesVer()
   654 {
   655 	return includeImagesVer;
   656 }
   657 
   658 void BranchObj::setIncludeImagesHor(bool b)
   659 {
   660 	includeImagesHor=b;
   661 	calcBBoxSize();
   662 	positionBBox();
   663 	requestReposition();
   664 }
   665 
   666 bool BranchObj::getIncludeImagesHor()
   667 {
   668 	return includeImagesHor;
   669 }
   670 
   671 QString BranchObj::getIncludeImageAttr()
   672 {
   673 	QString a;
   674 	if (includeImagesVer)
   675 		a=attribut ("incImgV","true");
   676 	else
   677 		a=attribut ("incImgV","false");
   678 	if (includeImagesHor)
   679 		a+=attribut ("incImgH","true");
   680 	else
   681 		a+=attribut ("incImgH","false");
   682 	return a;	
   683 }
   684 
   685 FloatImageObj* BranchObj::addFloatImage ()
   686 {
   687 	FloatImageObj *newfi=new FloatImageObj (scene,this);
   688 	floatimage.append (newfi);
   689 	if ( ((BranchItem*)treeItem)->hasScrolledParent((BranchItem*)treeItem) )
   690 		newfi->setVisibility (false);
   691 	else	
   692 		newfi->setVisibility(visible);
   693 		/*
   694 	calcBBoxSize();
   695 	positionBBox();
   696 	*/
   697 	requestReposition();
   698 	return newfi;
   699 }
   700 
   701 FloatImageObj* BranchObj::addFloatImage (FloatImageObj *fio)
   702 {
   703 	FloatImageObj *newfi=new FloatImageObj (scene,this);
   704 	floatimage.append (newfi);
   705 	newfi->copy (fio);
   706 	if (((BranchItem*)treeItem)->hasScrolledParent((BranchItem*)treeItem) )
   707 		newfi->setVisibility (false);
   708 	else	
   709 		newfi->setVisibility(visible);
   710 		/*
   711 	calcBBoxSize();
   712 	positionBBox();
   713 	*/
   714 	requestReposition();
   715 	return newfi;
   716 }
   717 
   718 FloatImageObj* BranchObj::getFirstFloatImage ()
   719 {
   720     return floatimage.first();
   721 }
   722 
   723 FloatImageObj* BranchObj::getLastFloatImage ()
   724 {
   725     return floatimage.last();
   726 }
   727 
   728 FloatImageObj* BranchObj::getFloatImageNum (const uint &i)
   729 {
   730     return floatimage.at(i);
   731 }
   732 
   733 void BranchObj::removeFloatImage (FloatImageObj *fio)
   734 {
   735 	int i=floatimage.indexOf (fio);
   736 	if (i>-1) delete (floatimage.takeAt (i));
   737 	calcBBoxSize();
   738 	positionBBox();
   739 	requestReposition();
   740 }
   741 
   742 void BranchObj::savePosInAngle ()
   743 {
   744 	// Save position in angle
   745 	for (int i=0; i<treeItem->branchCount(); ++i)
   746 		branch.at(i)->angle=i;
   747 }
   748 
   749 void BranchObj::setDefAttr (BranchModification mod)
   750 {
   751 	int fontsize;
   752 	switch (depth)
   753 	{
   754 		case 0: fontsize=16; break;
   755 		case 1: fontsize=12; break;
   756 		default: fontsize=10; break;
   757 	}	
   758 
   759 	setLinkColor ();
   760 	setLinkStyle(getDefLinkStyle());
   761 	QFont font("Sans Serif,8,-1,5,50,0,0,0,0,0");
   762 	font.setPointSize(fontsize);
   763 	heading->setFont(font );
   764 
   765 	if (mod==NewBranch)
   766 		setColor (((BranchObj*)(parObj))->getColor());
   767 	
   768 	calcBBoxSize();
   769 }
   770 
   771 BranchObj* BranchObj::addBranch()
   772 {
   773     BranchObj* newbo=new BranchObj(scene,this);
   774     branch.append (newbo);
   775     newbo->setParObj(this);
   776 	newbo->setDefAttr(NewBranch);
   777     newbo->setHeading ("new");
   778 	/* FIXME-2 treeItem not set yet!!!
   779 	if ( ((BranchItem*)treeItem)->isScrolled() )
   780 		newbo->setVisibility (false);
   781 	else	
   782 		newbo->setVisibility(visible);
   783 	*/	
   784 	newbo->updateLink();	
   785 	requestReposition();
   786 	return newbo;
   787 }
   788 
   789 BranchObj* BranchObj::addBranch(BranchObj* bo)
   790 {
   791     BranchObj* newbo=new BranchObj(scene,this);
   792     branch.append (newbo);
   793     newbo->copy(bo);
   794     newbo->setParObj(this);
   795 	newbo->setDefAttr(MovedBranch);
   796 	if ( ((BranchItem*)treeItem)->isScrolled() )
   797 		newbo->setVisibility (false);
   798 	else	
   799 		newbo->setVisibility(bo->visible);
   800 	newbo->updateLink();	
   801 	requestReposition();
   802 	return newbo;
   803 }
   804 
   805 BranchObj* BranchObj::addBranchPtr(BranchObj* bo)
   806 {
   807 	branch.append (bo);
   808 	bo->setParObj (this);
   809 	bo->depth=depth+1;
   810 	bo->setDefAttr(MovedBranch);
   811 	BranchItem *bi=(BranchItem*)treeItem;
   812 	if ( bi->isScrolled() ) bi->tmpUnscroll();
   813 	//setLastSelectedBranch (bo);	//FIXME-3 needed?
   814 	return bo;
   815 }
   816 
   817 BranchObj* BranchObj::insertBranch(int pos)
   818 {
   819 	savePosInAngle();
   820 	// Add new bo and resort branches
   821 	BranchObj *newbo=addBranch ();
   822 	newbo->angle=pos-0.5;
   823 	qSort (branch.begin(),branch.end(), isAbove);
   824 	return newbo;
   825 }
   826 
   827 BranchObj* BranchObj::insertBranch(BranchObj* bo, int pos)
   828 {
   829 	savePosInAngle();
   830 	// Add new bo and resort branches
   831 	bo->angle=pos-0.5;
   832 	BranchObj *newbo=addBranch (bo);
   833 	qSort (branch.begin(),branch.end(), isAbove);
   834 	return newbo;
   835 }
   836 
   837 BranchObj* BranchObj::insertBranchPtr (BranchObj* bo, int pos)
   838 {
   839 	savePosInAngle();
   840 	// Add new bo and resort branches
   841 	bo->angle=pos-0.5;
   842 	branch.append (bo);
   843 	bo->setParObj (this);
   844 	bo->depth=depth+1;
   845 	bo->setDefAttr (MovedBranch);
   846 	BranchItem *bi=(BranchItem*)treeItem;
   847 	if ( bi->isScrolled() ) bi->tmpUnscroll();
   848 	//setLastSelectedBranch (bo); //FIXME-3 needed?
   849 	qSort (branch.begin(),branch.end(), isAbove);
   850 	return bo;
   851 }
   852 
   853 void BranchObj::removeBranchHere(BranchObj* borem)	// FIXME-1 getNum no longer available
   854 {
   855 /*
   856 	// This removes the branch bo from list, but 
   857 	// inserts its children at the place of bo
   858 	BranchObj *bo;
   859 	bo=borem->getLastBranch();
   860 	int pos=borem->getNum();
   861 	while (bo)
   862 	{
   863 		bo->linkTo (this,pos+1);
   864 		bo=borem->getLastBranch();
   865 	}	
   866 	removeBranch (borem);
   867 	*/
   868 }
   869 
   870 void BranchObj::removeChildren()
   871 {
   872 	clear();
   873 }
   874 
   875 void BranchObj::removeBranch(BranchObj* bo)
   876 {
   877     // if bo is not in branch remove returns false, we
   878     // don't care...
   879 	
   880 	int i=branch.indexOf(bo);
   881     if (i>=0)
   882 	{
   883 		delete (bo);
   884 		branch.removeAt (i);
   885 	} else
   886 		qWarning ("BranchObj::removeBranch tried to remove non existing branch?!\n");
   887 	requestReposition();
   888 }
   889 
   890 void BranchObj::removeBranchPtr(BranchObj* bo)
   891 {
   892 	int i=branch.indexOf(bo);
   893 	
   894 	if (i>=0)
   895 		branch.removeAt (i);
   896 	else	
   897 		qWarning ("BranchObj::removeBranchPtr tried to remove non existing branch?!\n");
   898 	requestReposition();
   899 }
   900 
   901 /* FIXME-1 not needed
   902 BranchObj* BranchObj::getFirstBranch ()
   903 {
   904 	if (treeItem->branchCount()>0)
   905 		return branch.first();
   906 	else
   907 		return NULL;
   908 }
   909 
   910 BranchObj* BranchObj::getLastBranch ()
   911 {
   912 	if (treeItem->branchCount()>0)
   913 		return branch.last();
   914 	else
   915 		return NULL;
   916 }
   917 
   918 BranchObj* BranchObj::getBranchNum (int i)
   919 {
   920 	if (i>=0 && i<treeItem->branchCount())
   921 		return branch.at(i);
   922 	else	
   923 		return	NULL;
   924 }
   925 */
   926 
   927 bool BranchObj::canMoveBranchUp() 
   928 {
   929 	/* FIXME-1 move to BranchItem
   930 	if (!parObj || depth==1) return false;
   931 	BranchObj* par=(BranchObj*)parObj;
   932 	if (this==par->getTreeItem()->getFirstBranch())
   933 		return false;
   934 	else
   935 		return true;
   936 		*/
   937 return false;
   938 }
   939 
   940 BranchObj* BranchObj::moveBranchUp(BranchObj* bo1) // modify my childlist
   941 {
   942 	savePosInAngle();
   943     int i=branch.indexOf(bo1);
   944     if (i>0) 
   945 	{	// -1 if bo1 not found 
   946 		branch.at(i)->angle--;
   947 		branch.at(i-1)->angle++;
   948 		qSort (branch.begin(),branch.end(), isAbove);
   949 		return branch.at(i);
   950 	} else
   951 		return NULL;
   952 }
   953 
   954 bool BranchObj::canMoveBranchDown() 
   955 {
   956 	/* FIXME-1 move to BranchItem
   957 	if (!parObj|| depth==1) return false;
   958 	BranchObj* par=(BranchObj*)parObj;
   959 	if (this==par->getTreeItem()->getLastBranch())
   960 		return false;
   961 	else
   962 		return true;
   963 	*/
   964 return false;	
   965 }
   966 
   967 BranchObj* BranchObj::moveBranchDown(BranchObj* bo1)// modify my childlist
   968 {
   969 	savePosInAngle();
   970     int i=branch.indexOf(bo1);
   971 	int j;
   972 	if (i <treeItem->branchCount())
   973 	{
   974 		j = i+1;
   975 		branch.at(i)->angle++;
   976 		branch.at(j)->angle--;
   977 		qSort (branch.begin(),branch.end(), isAbove);
   978 		return branch.at(i);
   979 	} else
   980 		return NULL;
   981 }
   982 
   983 void BranchObj::sortChildren() //FIXME-1  
   984 {
   985 /*
   986 	int childCount=branch.count(); 
   987 	int curChildIndex;
   988 	bool madeChanges=false;
   989 	do
   990 	{
   991 		madeChanges=false;
   992 		for(curChildIndex=1;curChildIndex<childCount;curChildIndex++){
   993 			BranchObj* curChild=(BranchObj*)branch.at(curChildIndex);
   994 			BranchObj* prevChild=(BranchObj*)branch.at(curChildIndex-1);
   995 			if(prevChild->heading->text().compare(curChild->heading->text())>0)
   996 			{
   997 				this->moveBranchUp(curChild);
   998 				madeChanges=true;
   999 			}
  1000 		}
  1001 	}while(madeChanges);
  1002 */
  1003 }
  1004 
  1005 
  1006 BranchObj* BranchObj::linkTo (BranchObj* dst, int pos)
  1007 {
  1008 	// Find current parent and 
  1009 	// remove pointer to myself there
  1010 	if (!dst) return NULL;
  1011 	BranchObj *par=(BranchObj*)parObj;
  1012 	if (par)
  1013 		par->removeBranchPtr (this);
  1014 	else
  1015 		return NULL;
  1016 
  1017 	// Create new pointer to myself at dst
  1018 	if (pos<0||dst->getDepth()==0)
  1019 	{	
  1020 		// links myself as last branch at dst
  1021 		dst->addBranchPtr (this);
  1022 		updateLink();
  1023 		return this;
  1024 	} else
  1025 	{
  1026 		// inserts me at pos in parent of dst
  1027 		if (par)
  1028 		{
  1029 			BranchObj *bo=dst->insertBranchPtr (this,pos);
  1030 			bo->setDefAttr(MovedBranch);
  1031 			updateLink();
  1032 			return bo;
  1033 
  1034 		} else
  1035 			return NULL;
  1036 	}	
  1037 }
  1038 
  1039 void BranchObj::alignRelativeTo (QPointF ref,bool alignSelf)
  1040 {
  1041 	qreal th = bboxTotal.height();	
  1042 // TODO testing
  1043 /*
  1044 	QPointF pp; if (parObj) pp=parObj->getChildPos();
  1045 	cout << "BO::alignRelTo "<<qPrintable (getHeading());
  1046 	cout << "    d="<<depth<<
  1047 		"  ref="<<ref<<
  1048 //		"  bbox.topLeft="<<bboxTotal.topLeft()<<
  1049 		"  absPos="<<absPos<<
  1050 //		"  relPos="<<relPos<<
  1051 //		"  parPos="<<pp<<
  1052 		"  width="<<bbox.width()<<
  1053 		"  orient="<<orientation<<
  1054 		"  alignSelf="<<alignSelf<<
  1055 //		"  pad="<<topPad<<","<<botPad<<","<<leftPad<<","<<rightPad<<
  1056 //		"  hidden="<<hidden<<
  1057 //		"  th="<<th<<
  1058 		endl;
  1059 */
  1060 
  1061 	setOrientation();
  1062 	//updateLink();
  1063 
  1064 	if (depth<2)
  1065 	{
  1066 		if (depth==1)
  1067 		{
  1068 			// Position relatively, if needed
  1069 			//if (useRelPos) move2RelPos (relPos.x(), relPos.y());
  1070 
  1071 			// Calc angle to mapCenter if I am a mainbranch
  1072 			// needed for reordering the mainbranches clockwise 
  1073 			// around mapcenter 
  1074 			angle=getAngle (QPointF ((int)(x() - parObj->getChildPos().x() ), 
  1075 									(int)(y() - parObj->getChildPos().y() ) ) );
  1076 		}							
  1077 	} 
  1078 	else
  1079     {
  1080 		// Align myself depending on orientation and parent, but
  1081 		// only if I am not a mainbranch or mapcenter itself
  1082 
  1083 		if (anim.isAnimated())
  1084 		{
  1085 			move2RelPos(anim);
  1086 		} else
  1087 		{
  1088 			LinkableMapObj::Orientation o;
  1089 			o=parObj->getOrientation();
  1090 			if (alignSelf)
  1091 				switch (orientation) 
  1092 				{
  1093 					case LinkableMapObj::LeftOfCenter:
  1094 						move (ref.x() - bbox.width(), ref.y() + (th-bbox.height())/2 );
  1095 						//move (ref.x() , ref.y() + (th-bbox.height())/2 );
  1096 					break;
  1097 					case LinkableMapObj::RightOfCenter:	
  1098 						move (ref.x() , ref.y() + (th-bbox.height())/2  );
  1099 					break;
  1100 					default:
  1101 						qWarning ("LMO::alignRelativeTo: oops, no orientation given...");
  1102 					break;
  1103 				}
  1104 		}
  1105     }		
  1106 
  1107 	if ( ((BranchItem*)treeItem)->isScrolled() ) return;
  1108 
  1109     // Set reference point for alignment of children
  1110     QPointF ref2;
  1111     if (orientation==LinkableMapObj::LeftOfCenter)
  1112 		ref2.setX(bbox.topLeft().x() - linkwidth);
  1113     else	
  1114 		ref2.setX(bbox.topRight().x() + linkwidth);
  1115 
  1116 	if (depth==1)
  1117 		ref2.setY(absPos.y()-(bboxTotal.height()-bbox.height())/2);
  1118 	else	
  1119 		ref2.setY(ref.y() );	
  1120 
  1121     // Align the children depending on reference point 
  1122 	for (int i=0; i<treeItem->branchCount(); ++i)
  1123     {	
  1124 		if (!branch.at(i)->isHidden())
  1125 		{
  1126 			branch.at(i)->alignRelativeTo (ref2,true);
  1127 
  1128 			// append next branch below current one
  1129 			ref2.setY(ref2.y() + branch.at(i)->getBBoxSizeWithChildren().height() );
  1130 		}
  1131     }
  1132 }
  1133 
  1134 
  1135 void BranchObj::reposition()
  1136 {	
  1137 /* TODO testing only
  1138 	if (!getHeading().isEmpty())
  1139 		cout << "BO::reposition  "<<qPrintable(getHeading())<<endl;
  1140 	else	
  1141 		cout << "BO::reposition  ???"<<endl;
  1142 
  1143 	cout << "  orient="<<orientation<<endl;
  1144 */		
  1145 
  1146 	if (depth==0)
  1147 	{
  1148 		// only calculate the sizes once. If the deepest LMO 
  1149 		// changes its height,
  1150 		// all upper LMOs have to change, too.
  1151 		calcBBoxSizeWithChildren();
  1152 		updateLink();	// This update is needed if the scene is resized 
  1153 						// due to excessive moving of a FIO
  1154 
  1155 	    alignRelativeTo ( QPointF (absPos.x(),
  1156 			absPos.y()-(bboxTotal.height()-bbox.height())/2) );
  1157 		qSort (branch.begin(),branch.end(), isAbove);
  1158 		positionBBox();	// Reposition bbox and contents
  1159 	} else
  1160 	{
  1161 		// This is only important for moving branches:
  1162 		// For editing a branch it isn't called...
  1163 	    alignRelativeTo ( QPointF (absPos.x(),
  1164 							absPos.y()-(bboxTotal.height()-bbox.height())/2) );
  1165 	}
  1166 }
  1167 
  1168 void BranchObj::unsetAllRepositionRequests()
  1169 {
  1170 	repositionRequest=false;
  1171 	for (int i=0; i<treeItem->branchCount(); ++i)
  1172 		branch.at(i)->unsetAllRepositionRequests();
  1173 }
  1174 
  1175 
  1176 QPolygonF BranchObj::shape()
  1177 {
  1178 	QPolygonF p;
  1179 
  1180 	QRectF r=getTotalBBox();
  1181 	if (orientation==LinkableMapObj::LeftOfCenter)
  1182 		p   <<r.bottomLeft()
  1183 			<<r.topLeft()
  1184 			<<QPointF (bbox.topLeft().x(), r.topLeft().y() )
  1185 			<<bbox.topRight()
  1186 			<<bbox.bottomRight()
  1187 			<<QPointF (bbox.bottomLeft().x(), r.bottomLeft().y() ) ;
  1188 	else		
  1189 		p   <<r.bottomRight()
  1190 			<<r.topRight()
  1191 			<<QPointF (bbox.topRight().x(), r.topRight().y() )
  1192 			<<bbox.topLeft()
  1193 			<<bbox.bottomLeft()
  1194 			<<QPointF (bbox.bottomRight().x(), r.bottomRight().y() ) ;
  1195 	return p;
  1196 }
  1197 
  1198 QRectF BranchObj::getTotalBBox()
  1199 {
  1200 	QRectF r=bbox;
  1201 
  1202 	if ( ((BranchItem*)treeItem)->isScrolled() ) return r;
  1203 
  1204 	for (int i=0; i<treeItem->branchCount(); ++i)
  1205 		if (!branch.at(i)->isHidden())
  1206 			r=addBBox(branch.at(i)->getTotalBBox(),r);
  1207 
  1208 	for (int i=0; i<floatimage.size(); ++i)
  1209 		if (!floatimage.at(i)->isHidden())
  1210 			r=addBBox(floatimage.at(i)->getTotalBBox(),r);
  1211 		
  1212 	return r;
  1213 }
  1214 
  1215 QRectF BranchObj::getBBoxSizeWithChildren()
  1216 {
  1217 	return bboxTotal;
  1218 }
  1219 
  1220 void BranchObj::calcBBoxSizeWithChildren()
  1221 {	
  1222 	// This is initially called only from reposition and
  1223 	// and only for mapcenter. So it won't be
  1224 	// called more than once for a single user 
  1225 	// action
  1226 	
  1227 
  1228 	// Calculate size of LMO including all children (to align them later)
  1229 	bboxTotal.setX(bbox.x() );
  1230 	bboxTotal.setY(bbox.y() );
  1231 
  1232 	// if branch is scrolled, ignore children, but still consider floatimages
  1233 	if ( ((BranchItem*)treeItem)->isScrolled() ) 
  1234 	{
  1235 		bboxTotal.setWidth (bbox.width());
  1236 		bboxTotal.setHeight(bbox.height());
  1237 		return;
  1238 	}
  1239 	
  1240 	if (hidden)
  1241 	{
  1242 		bboxTotal.setWidth (0);
  1243 		bboxTotal.setHeight(0);
  1244 		if (parObj)
  1245 		{
  1246 			bboxTotal.setX (parObj->x());
  1247 			bboxTotal.setY (parObj->y());
  1248 		} else
  1249 		{
  1250 			bboxTotal.setX (bbox.x());
  1251 			bboxTotal.setY (bbox.y());
  1252 		}
  1253 		return;
  1254 	}
  1255 	
  1256 	QRectF r(0,0,0,0);
  1257 	QRectF br;
  1258 	// Now calculate recursivly
  1259 	// sum of heights 
  1260 	// maximum of widths 
  1261 	// minimum of y
  1262 	for (int i=0; i<treeItem->branchCount(); ++i)
  1263 	{
  1264 		if (!branch.at(i)->isHidden())
  1265 		{
  1266 			branch.at(i)->calcBBoxSizeWithChildren();
  1267 			br=branch.at(i)->getBBoxSizeWithChildren();
  1268 			r.setWidth( max (br.width(), r.width() ));
  1269 			r.setHeight(br.height() + r.height() );
  1270 			if (br.y()<bboxTotal.y()) bboxTotal.setY(br.y());
  1271 		}
  1272 	}
  1273 	// Add myself and also
  1274 	// add width of link to sum if necessary
  1275 	if (branch.isEmpty())
  1276 		bboxTotal.setWidth (bbox.width() + r.width() );
  1277 	else	
  1278 		bboxTotal.setWidth (bbox.width() + r.width() + linkwidth);
  1279 	
  1280 	bboxTotal.setHeight(max (r.height(),  bbox.height()));
  1281 }
  1282 
  1283 /*
  1284 void BranchObj::select()	// FIXME-2 try to get rid of this in BO completely
  1285 {
  1286 	cout << "BO::select()\n";
  1287 	textEditor->setText(treeItem->getNoteObj().getNote() );
  1288 	QString fnh=treeItem->getNoteObj().getFilenameHint();
  1289 	if (fnh!="")
  1290 		textEditor->setFilenameHint(treeItem->getNoteObj().getFilenameHint() );
  1291 	else	
  1292 		textEditor->setFilenameHint(getHeading() );
  1293 	textEditor->setFontHint (treeItem->getNoteObj().getFontHint() );
  1294 	//isNoteInEditor=true;
  1295 
  1296 	// set selected and visible
  1297     LinkableMapObj::select();
  1298 
  1299     //if (po)	po->setLastSelectedBranch(this);  needed?
  1300 		
  1301 	// temporary unscroll, if we have scrolled parents somewhere
  1302 	if (parObj) ((BranchObj*)(parObj))->tmpUnscroll();
  1303 
  1304 	//moved to vymmodel or vymview...
  1305 	// Show URL and link in statusbar
  1306 	QString status;
  1307 	if (!url.isEmpty()) status+="URL: "+url+"  ";
  1308 	if (!vymLink.isEmpty()) status+="Link: "+vymLink;
  1309 	if (!status.isEmpty()) mainWindow->statusMessage (status);
  1310 
  1311 	// Update Toolbar
  1312 	updateFlagsToolbar();
  1313 
  1314 	// Update actions
  1315 	model->updateActions();
  1316 }
  1317 	*/
  1318 
  1319 /*
  1320 void BranchObj::unselect()	//FIXME-3 should not be needed
  1321 {
  1322 	cout << "BO::unselect()\n";
  1323 	LinkableMapObj::unselect();
  1324 	// Delete any messages like vymLink in StatusBar
  1325 	mainWindow->statusMessage ("");		//this causes segfault, when MainWindow is already gone in global destructor on quitting vym
  1326 
  1327 	// Save current note
  1328 	if (isNoteInEditor) getNoteFromTextEditor();
  1329 	isNoteInEditor=false;
  1330 
  1331 	// reset temporary unscroll, if we have scrolled parents somewhere
  1332 	if (parObj) ((BranchObj*)(parObj))->resetTmpUnscroll();
  1333 
  1334 	// Erase content of editor 
  1335 	textEditor->setInactive();
  1336 
  1337 	// unselect all buttons in toolbar
  1338 	standardFlagsDefault->updateToolbar();
  1339 }
  1340 */	
  1341 
  1342 QString BranchObj::getSelectString()
  1343 {
  1344 	return model->getSelectString (this);
  1345 }
  1346 
  1347 void BranchObj::setAnimation(const AnimPoint &ap)
  1348 {
  1349 	anim=ap;
  1350 }
  1351 
  1352 bool BranchObj::animate()
  1353 {
  1354 	anim.animate ();
  1355 	if ( anim.isAnimated() )
  1356 	{
  1357 		setRelPos (anim);
  1358 		return true;
  1359 	}
  1360 	parObj->reposition();	// we might have been relinked meanwhile
  1361 	return false;
  1362 }
  1363