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