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