branchobj.cpp
author insilmaril
Tue, 07 Apr 2009 16:15:53 +0000
changeset 753 25a77484ec72
parent 751 7fa9b3164cbe
child 754 db0ec4bcf416
permissions -rw-r--r--
more work on screwing up the data structures
     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 LinkableMapObj* BranchObj::findMapObj(QPointF p, LinkableMapObj* excludeLMO)
   428 {
   429 	// Search branches
   430     LinkableMapObj *lmo;
   431 	for (int i=0; i<treeItem->branchCount(); ++i)
   432     {	
   433 		lmo=treeItem->getBranchObjNum(i)->findMapObj(p, excludeLMO);
   434 		if (lmo != NULL) return lmo;
   435     }
   436 	
   437 
   438 	// Search myself
   439     if (inBox (p,clickBox) && (this != excludeLMO) && isVisibleObj() ) 
   440 		return this;
   441 
   442 	// Search float images
   443     for (int i=0; i<floatimage.size(); ++i )
   444 		if (inBox(p,floatimage.at(i)->getClickBox()) && 
   445 			(floatimage.at(i) != excludeLMO) && 
   446 			floatimage.at(i)->getParObj()!= excludeLMO &&
   447 			floatimage.at(i)->isVisibleObj() 
   448 		) return floatimage.at(i);
   449 
   450     return NULL;
   451 }
   452 
   453 LinkableMapObj* BranchObj::findID (QString sid)
   454 {
   455 	// Search branches
   456     LinkableMapObj *lmo;
   457 	for (int i=0; i<treeItem->branchCount(); ++i)
   458     {	
   459 		lmo=treeItem->getBranchObjNum(i)->findID (sid);
   460 		if (lmo != NULL) return lmo;
   461     }
   462 	
   463 	// Search myself
   464 	if (sid==objID) return this;
   465 
   466 
   467 /*
   468 	// Search float images
   469     for (int i=0; i<floatimage.size(); ++i )
   470 		if (floatimage.at(i)->inBox(p) && 
   471 			(floatimage.at(i) != excludeLMO) && 
   472 			floatimage.at(i)->getParObj()!= excludeLMO &&
   473 			floatimage.at(i)->isVisibleObj() 
   474 		) return floatimage.at(i);
   475 */
   476     return NULL;
   477 }
   478 
   479 void BranchObj::updateHeading()
   480 {
   481 	if (!treeItem)
   482 	{
   483 		qWarning ("BranchObj::udpateHeading treeItem==NULL");
   484 		return;
   485 	}
   486 	cout << "BO::updateHeading: "<<treeItem->getHeading().toStdString()<<endl;
   487 	heading->setText (treeItem->getHeading() );
   488 	updateContentSize();
   489 }
   490 
   491 QString BranchObj::saveToDir (const QString &tmpdir,const QString &prefix, const QPointF& offset)
   492 {
   493 	// Cloudy stuff can be hidden during exports
   494 	// FIXME-1 if (hidden) return "";
   495 
   496 	// Update of note is usually done while unselecting a branch
   497 	// if (isNoteInEditor) getNoteFromTextEditor();		//FIXME-2 moved to TreeItem
   498 	
   499     QString s,a;
   500 	QString scrolledAttr;
   501 	if ( ((BranchItem*)treeItem)->isScrolled() ) 
   502 		scrolledAttr=attribut ("scrolled","yes");
   503 	else
   504 		scrolledAttr="";
   505 
   506 	// save area, if not scrolled
   507 	QString areaAttr;
   508 	if (!((BranchItem*) (treeItem->parent()) )->isScrolled() )
   509 	{
   510 		areaAttr=
   511 			attribut("x1",QString().setNum(absPos.x()-offset.x())) +
   512 			attribut("y1",QString().setNum(absPos.y()-offset.y())) +
   513 			attribut("x2",QString().setNum(absPos.x()+width()-offset.x())) +
   514 			attribut("y2",QString().setNum(absPos.y()+height()-offset.y()));
   515 
   516 	} else
   517 		areaAttr="";
   518 	
   519 	// Providing an ID for a branch makes export to XHTML easier
   520 	QString idAttr;
   521 	if (treeItem->xlinkCount()>0)
   522 		idAttr=attribut ("id",model->getSelectString(this)); //TODO directly access model
   523 	else
   524 		idAttr="";
   525 
   526     s=beginElement ("branch" 
   527 		+getOrnXMLAttr() 
   528 		+scrolledAttr 
   529 		+areaAttr 
   530 		+idAttr 
   531 		+getIncludeImageAttr() );
   532     incIndent();
   533 
   534 	// save heading
   535     s+=valueElement("heading", treeItem->getHeading(),
   536 		attribut ("textColor",QColor(heading->getColor()).name()));
   537 
   538 	// Save frame
   539 	if (frame->getFrameType()!=FrameObj::NoFrame) 
   540 		s+=frame->saveToDir ();
   541 
   542 	// save names of flags set
   543 	s+=standardFlags->saveToDir(tmpdir,prefix,0);
   544 	
   545 	// Save FloatImages
   546 	for (int i=0; i<floatimage.size(); ++i)
   547 		s+=floatimage.at(i)->saveToDir (tmpdir,prefix);
   548 
   549 	// save note
   550 	if (!treeItem->getNoteObj().isEmpty() )
   551 		s+=treeItem->getNoteObj().saveToDir();
   552 	
   553 	// Save branches
   554 	for (int i=0; i<treeItem->branchCount(); ++i)
   555 		s+=treeItem->getBranchObjNum(i)->saveToDir(tmpdir,prefix,offset);
   556 
   557 	// Save XLinks
   558 	QString ol;	// old link
   559 	QString cl;	// current link
   560 	for (int i=0; i<xlink.size(); ++i)
   561 	{
   562 		cl=xlink.at(i)->saveToDir();
   563 		if (cl!=ol)
   564 		{
   565 			s+=cl;
   566 			ol=cl;
   567 		} else
   568 		{
   569 			qWarning (QString("Ignoring of duplicate xLink in %1").arg(treeItem->getHeading()));
   570 		}
   571 	}	
   572 
   573     decIndent();
   574     s+=endElement   ("branch");
   575     return s;
   576 }
   577 
   578 void BranchObj::addXLink (XLinkObj *xlo)
   579 {
   580 	xlink.append (xlo);
   581 	
   582 }
   583 
   584 void BranchObj::removeXLinkRef (XLinkObj *xlo)
   585 {
   586 	xlink.removeAt (xlink.indexOf(xlo));
   587 }
   588 
   589 void BranchObj::deleteXLink(XLinkObj *xlo)
   590 {
   591 	xlo->deactivate();
   592 	if (!xlo->isUsed()) delete (xlo);
   593 }
   594 
   595 void BranchObj::deleteXLinkAt (int i)
   596 {
   597 	XLinkObj *xlo=xlink.at(i);
   598 	xlo->deactivate();
   599 	if (!xlo->isUsed()) delete(xlo);
   600 }
   601 
   602 XLinkObj* BranchObj::XLinkAt (int i)
   603 {
   604 	return xlink.at(i);
   605 }
   606 
   607 BranchObj* BranchObj::XLinkTargetAt (int i)
   608 {
   609 	if (i>=0 && i<xlink.size())
   610 	{
   611 		if (xlink.at(i))
   612 			return xlink.at(i)->otherBranch (this);
   613 	}
   614 	return NULL;
   615 }
   616 
   617 void BranchObj::setIncludeImagesVer(bool b)
   618 {
   619 	includeImagesVer=b;
   620 	calcBBoxSize();
   621 	positionBBox();
   622 	requestReposition();
   623 }
   624 
   625 bool BranchObj::getIncludeImagesVer()
   626 {
   627 	return includeImagesVer;
   628 }
   629 
   630 void BranchObj::setIncludeImagesHor(bool b)
   631 {
   632 	includeImagesHor=b;
   633 	calcBBoxSize();
   634 	positionBBox();
   635 	requestReposition();
   636 }
   637 
   638 bool BranchObj::getIncludeImagesHor()
   639 {
   640 	return includeImagesHor;
   641 }
   642 
   643 QString BranchObj::getIncludeImageAttr()
   644 {
   645 	QString a;
   646 	if (includeImagesVer)
   647 		a=attribut ("incImgV","true");
   648 	else
   649 		a=attribut ("incImgV","false");
   650 	if (includeImagesHor)
   651 		a+=attribut ("incImgH","true");
   652 	else
   653 		a+=attribut ("incImgH","false");
   654 	return a;	
   655 }
   656 
   657 FloatImageObj* BranchObj::addFloatImage ()
   658 {
   659 	FloatImageObj *newfi=new FloatImageObj (scene,this);
   660 	floatimage.append (newfi);
   661 	if ( ((BranchItem*)treeItem)->hasScrolledParent((BranchItem*)treeItem) )
   662 		newfi->setVisibility (false);
   663 	else	
   664 		newfi->setVisibility(visible);
   665 		/*
   666 	calcBBoxSize();
   667 	positionBBox();
   668 	*/
   669 	requestReposition();
   670 	return newfi;
   671 }
   672 
   673 FloatImageObj* BranchObj::addFloatImage (FloatImageObj *fio)
   674 {
   675 	FloatImageObj *newfi=new FloatImageObj (scene,this);
   676 	floatimage.append (newfi);
   677 	newfi->copy (fio);
   678 	if (((BranchItem*)treeItem)->hasScrolledParent((BranchItem*)treeItem) )
   679 		newfi->setVisibility (false);
   680 	else	
   681 		newfi->setVisibility(visible);
   682 		/*
   683 	calcBBoxSize();
   684 	positionBBox();
   685 	*/
   686 	requestReposition();
   687 	return newfi;
   688 }
   689 
   690 FloatImageObj* BranchObj::getFirstFloatImage ()
   691 {
   692     return floatimage.first();
   693 }
   694 
   695 FloatImageObj* BranchObj::getLastFloatImage ()
   696 {
   697     return floatimage.last();
   698 }
   699 
   700 FloatImageObj* BranchObj::getFloatImageNum (const uint &i)
   701 {
   702     return floatimage.at(i);
   703 }
   704 
   705 void BranchObj::removeFloatImage (FloatImageObj *fio)
   706 {
   707 	int i=floatimage.indexOf (fio);
   708 	if (i>-1) delete (floatimage.takeAt (i));
   709 	calcBBoxSize();
   710 	positionBBox();
   711 	requestReposition();
   712 }
   713 
   714 void BranchObj::savePosInAngle ()
   715 {
   716 	// Save position in angle
   717 	for (int i=0; i<treeItem->branchCount(); ++i)
   718 		treeItem->getBranchObjNum(i)->angle=i;
   719 }
   720 
   721 void BranchObj::setDefAttr (BranchModification mod)
   722 {
   723 	int fontsize;
   724 	switch (depth)
   725 	{
   726 		case 0: fontsize=16; break;
   727 		case 1: fontsize=12; break;
   728 		default: fontsize=10; break;
   729 	}	
   730 
   731 	setLinkColor ();
   732 	setLinkStyle(getDefLinkStyle());
   733 	QFont font("Sans Serif,8,-1,5,50,0,0,0,0,0");
   734 	font.setPointSize(fontsize);
   735 	heading->setFont(font );
   736 
   737 	if (mod==NewBranch)
   738 		setColor (((BranchObj*)(parObj))->getColor());
   739 	
   740 	calcBBoxSize();
   741 }
   742 
   743 BranchObj* BranchObj::addBranch()
   744 {
   745     BranchObj* newbo=new BranchObj(scene,this);
   746     //FIXME-1 branch.append (newbo);
   747     newbo->setParObj(this);
   748 	newbo->setDefAttr(NewBranch);
   749 	/* FIXME-2 treeItem not set yet!!!
   750 	if ( ((BranchItem*)treeItem)->isScrolled() )
   751 		newbo->setVisibility (false);
   752 	else	
   753 		newbo->setVisibility(visible);
   754 	*/	
   755 	newbo->updateLink();	
   756 	requestReposition();
   757 	return newbo;
   758 }
   759 
   760 BranchObj* BranchObj::addBranch(BranchObj* bo)
   761 {
   762     BranchObj* newbo=new BranchObj(scene,this);
   763     //FIXME-1 branch.append (newbo);
   764     newbo->copy(bo);
   765     newbo->setParObj(this);
   766 	newbo->setDefAttr(MovedBranch);
   767 	if ( ((BranchItem*)treeItem)->isScrolled() )
   768 		newbo->setVisibility (false);
   769 	else	
   770 		newbo->setVisibility(bo->visible);
   771 	newbo->updateLink();	
   772 	requestReposition();
   773 	return newbo;
   774 }
   775 
   776 BranchObj* BranchObj::addBranchPtr(BranchObj* bo)
   777 {
   778 	//FIXME-1 branch.append (bo);
   779 	bo->setParObj (this);
   780 	bo->depth=depth+1;
   781 	bo->setDefAttr(MovedBranch);
   782 	BranchItem *bi=(BranchItem*)treeItem;
   783 	if ( bi->isScrolled() ) bi->tmpUnscroll();
   784 	//setLastSelectedBranch (bo);	//FIXME-3 needed?
   785 	return bo;
   786 }
   787 
   788 BranchObj* BranchObj::insertBranch(int pos)
   789 {
   790 	savePosInAngle();
   791 	// Add new bo and resort branches
   792 	BranchObj *newbo=addBranch ();
   793 	newbo->angle=pos-0.5;
   794 	//FIXME-1 qSort (branch.begin(),branch.end(), isAbove);
   795 	return newbo;
   796 }
   797 
   798 BranchObj* BranchObj::insertBranch(BranchObj* bo, int pos)
   799 {
   800 	savePosInAngle();
   801 	// Add new bo and resort branches
   802 	bo->angle=pos-0.5;
   803 	BranchObj *newbo=addBranch (bo);
   804 	//FIXME-1 qSort (branch.begin(),branch.end(), isAbove);
   805 	return newbo;
   806 }
   807 
   808 BranchObj* BranchObj::insertBranchPtr (BranchObj* bo, int pos)
   809 {
   810 	savePosInAngle();
   811 	// Add new bo and resort branches
   812 	bo->angle=pos-0.5;
   813 	//FIXME-1 branch.append (bo);
   814 	bo->setParObj (this);
   815 	bo->depth=depth+1;
   816 	bo->setDefAttr (MovedBranch);
   817 	BranchItem *bi=(BranchItem*)treeItem;
   818 	if ( bi->isScrolled() ) bi->tmpUnscroll();
   819 	//setLastSelectedBranch (bo); //FIXME-3 needed?
   820 	//FIXME-1 qSort (branch.begin(),branch.end(), isAbove);
   821 	return bo;
   822 }
   823 
   824 void BranchObj::removeBranchHere(BranchObj* borem)	// FIXME-1 getNum no longer available
   825 {
   826 /*
   827 	// This removes the branch bo from list, but 
   828 	// inserts its children at the place of bo
   829 	BranchObj *bo;
   830 	bo=borem->getLastBranch();
   831 	int pos=borem->getNum();
   832 	while (bo)
   833 	{
   834 		bo->linkTo (this,pos+1);
   835 		bo=borem->getLastBranch();
   836 	}	
   837 	removeBranch (borem);
   838 	*/
   839 }
   840 
   841 void BranchObj::removeChildren()
   842 {
   843 	clear();
   844 }
   845 
   846 void BranchObj::removeBranch(BranchObj* bo)	// FIXME-1 not needed here
   847 {
   848 /*
   849     // if bo is not in branch remove returns false, we
   850     // don't care...
   851 	
   852 	int i=branch.indexOf(bo);
   853     if (i>=0)
   854 	{
   855 		delete (bo);
   856 		branch.removeAt (i);
   857 	} else
   858 		qWarning ("BranchObj::removeBranch tried to remove non existing branch?!\n");
   859 	requestReposition();
   860 	*/
   861 }
   862 
   863 void BranchObj::removeBranchPtr(BranchObj* bo)	// FIXME-1 not needed here
   864 {
   865 /*
   866 	int i=branch.indexOf(bo);
   867 	
   868 	if (i>=0)
   869 		branch.removeAt (i);
   870 	else	
   871 		qWarning ("BranchObj::removeBranchPtr tried to remove non existing branch?!\n");
   872 	requestReposition();
   873 */	
   874 }
   875 
   876 bool BranchObj::canMoveBranchUp() 
   877 {
   878 	/* FIXME-1 move to BranchItem
   879 	if (!parObj || depth==1) return false;
   880 	BranchObj* par=(BranchObj*)parObj;
   881 	if (this==par->getTreeItem()->getFirstBranch())
   882 		return false;
   883 	else
   884 		return true;
   885 		*/
   886 return false;
   887 }
   888 
   889 BranchObj* BranchObj::moveBranchUp(BranchObj* bo1) // FIXME-1
   890 {
   891 /*
   892 	savePosInAngle();
   893     int i=branch.indexOf(bo1);
   894     if (i>0) 
   895 	{	// -1 if bo1 not found 
   896 		treeItem->getBranchObjNum(i)->angle--;
   897 		treeItem->getBranchObjNum(i-1)->angle++;
   898 		qSort (branch.begin(),branch.end(), isAbove);
   899 		return treeItem->getBranchObjNum(i);
   900 	} else
   901 */	
   902 		return NULL;
   903 }
   904 
   905 bool BranchObj::canMoveBranchDown() 
   906 {
   907 	/* FIXME-1 move to BranchItem
   908 	if (!parObj|| depth==1) return false;
   909 	BranchObj* par=(BranchObj*)parObj;
   910 	if (this==par->getTreeItem()->getLastBranch())
   911 		return false;
   912 	else
   913 		return true;
   914 	*/
   915 return false;	
   916 }
   917 
   918 BranchObj* BranchObj::moveBranchDown(BranchObj* bo1)// FIXME-1
   919 {
   920 /*
   921 	savePosInAngle();
   922     int i=branch.indexOf(bo1);
   923 	int j;
   924 	if (i <treeItem->branchCount())
   925 	{
   926 		j = i+1;
   927 		treeItem->getBranchObjNum(i)->angle++;
   928 		treeItem->getBranchObjNum(j)->angle--;
   929 		qSort (branch.begin(),branch.end(), isAbove);
   930 		return treeItem->getBranchObjNum(i);
   931 	} else
   932 */	
   933 		return NULL;
   934 }
   935 
   936 void BranchObj::sortChildren() //FIXME-1  
   937 {
   938 /*
   939 	int childCount=branch.count(); 
   940 	int curChildIndex;
   941 	bool madeChanges=false;
   942 	do
   943 	{
   944 		madeChanges=false;
   945 		for(curChildIndex=1;curChildIndex<childCount;curChildIndex++){
   946 			BranchObj* curChild=(BranchObj*)treeItem->getBranchObjNum(curChildIndex);
   947 			BranchObj* prevChild=(BranchObj*)treeItem->getBranchObjNum(curChildIndex-1);
   948 			if(prevChild->heading->text().compare(curChild->heading->text())>0)
   949 			{
   950 				this->moveBranchUp(curChild);
   951 				madeChanges=true;
   952 			}
   953 		}
   954 	}while(madeChanges);
   955 */
   956 }
   957 
   958 
   959 BranchObj* BranchObj::linkTo (BranchObj* dst, int pos)
   960 {
   961 	// Find current parent and 
   962 	// remove pointer to myself there
   963 	if (!dst) return NULL;
   964 	BranchObj *par=(BranchObj*)parObj;
   965 	if (par)
   966 		par->removeBranchPtr (this);
   967 	else
   968 		return NULL;
   969 
   970 	// Create new pointer to myself at dst
   971 	if (pos<0||dst->getDepth()==0)
   972 	{	
   973 		// links myself as last branch at dst
   974 		dst->addBranchPtr (this);
   975 		updateLink();
   976 		return this;
   977 	} else
   978 	{
   979 		// inserts me at pos in parent of dst
   980 		if (par)
   981 		{
   982 			BranchObj *bo=dst->insertBranchPtr (this,pos);
   983 			bo->setDefAttr(MovedBranch);
   984 			updateLink();
   985 			return bo;
   986 
   987 		} else
   988 			return NULL;
   989 	}	
   990 }
   991 
   992 void BranchObj::alignRelativeTo (QPointF ref,bool alignSelf)
   993 {
   994 	qreal th = bboxTotal.height();	
   995 // TODO testing
   996 /*
   997 	QString h=QString (treeItem->depth(),' ');
   998 	h+=treeItem->getHeading();
   999 	h+=QString (15,' ');
  1000 	h.truncate (15);
  1001 	QPointF pp; if (parObj) pp=parObj->getChildPos();
  1002 	cout << "BO::alignRelTo ";
  1003 	cout<<h.toStdString();
  1004 //	cout << "    d="<<treeItem->depth()<<
  1005 cout<<  "  ref="<<ref<<
  1006       	"  bbox.tL="<<bboxTotal.topLeft()<<
  1007 		"  absPos="<<absPos<<
  1008 //		"  relPos="<<relPos<<
  1009 //		"  parPos="<<pp<<
  1010 		"  w="<<bbox.width()<<
  1011 		"  h="<<bbox.height()<<
  1012 //		"  orient="<<orientation<<
  1013 //		"  alignSelf="<<alignSelf<<
  1014 //		"  scrolled="<<((BranchItem*)treeItem)->isScrolled()<<
  1015 //		"  pad="<<topPad<<","<<botPad<<","<<leftPad<<","<<rightPad<<
  1016 //		"  hidden="<<hidden<<
  1017 		"  th="<<th<<
  1018 		endl;
  1019 */
  1020 
  1021 	setOrientation();
  1022 	//updateLink();
  1023 
  1024 	if (depth==1)
  1025 	{
  1026 		// Position relatively, if needed
  1027 		//if (useRelPos) move2RelPos (relPos.x(), relPos.y());
  1028 
  1029 		// Calc angle to mapCenter if I am a mainbranch
  1030 		// needed for reordering the mainbranches clockwise 
  1031 		// around mapcenter 
  1032 		angle=getAngle (QPointF ((int)(x() - parObj->getChildPos().x() ), 
  1033 								(int)(y() - parObj->getChildPos().y() ) ) );
  1034 	}							
  1035 	if (depth>1)
  1036     {
  1037 		// Align myself depending on orientation and parent, but
  1038 		// only if I am not a mainbranch or mapcenter itself
  1039 
  1040 		if (anim.isAnimated())
  1041 		{
  1042 			move2RelPos(anim);
  1043 		} else
  1044 		{
  1045 			LinkableMapObj::Orientation o;
  1046 			o=parObj->getOrientation();
  1047 			if (alignSelf)
  1048 				switch (orientation) 
  1049 				{
  1050 					case LinkableMapObj::LeftOfCenter:
  1051 						move (ref.x() - bbox.width(), ref.y() + (th-bbox.height())/2 );
  1052 						//move (ref.x() , ref.y() + (th-bbox.height())/2 );
  1053 					break;
  1054 					case LinkableMapObj::RightOfCenter:	
  1055 						move (ref.x() , ref.y() + (th-bbox.height())/2  );
  1056 					break;
  1057 					default:
  1058 						qWarning ("LMO::alignRelativeTo: oops, no orientation given...");
  1059 					break;
  1060 			}
  1061 		}
  1062     }		
  1063 
  1064 	if ( ((BranchItem*)treeItem)->isScrolled() ) return;
  1065 
  1066     // Set reference point for alignment of children
  1067     QPointF ref2;
  1068     if (orientation==LinkableMapObj::LeftOfCenter)
  1069 		ref2.setX(bbox.topLeft().x() - linkwidth);
  1070     else	
  1071 		ref2.setX(bbox.topRight().x() + linkwidth);
  1072 
  1073 	if (depth==1)
  1074 		ref2.setY(absPos.y()-(bboxTotal.height()-bbox.height())/2);
  1075 	else	
  1076 		ref2.setY(ref.y() );	
  1077 
  1078     // Align the children depending on reference point 
  1079 	for (int i=0; i<treeItem->branchCount(); ++i)
  1080     {	
  1081 		if (!treeItem->getBranchNum(i)->isHidden())
  1082 		{
  1083 			treeItem->getBranchObjNum(i)->alignRelativeTo (ref2,true);
  1084 
  1085 			// append next branch below current one
  1086 			ref2.setY(ref2.y() + treeItem->getBranchObjNum(i)->getBBoxSizeWithChildren().height() );
  1087 		}
  1088     }
  1089 }
  1090 
  1091 
  1092 void BranchObj::reposition()
  1093 {	
  1094 /* TODO testing only
  1095 	if (!treeItem->getHeading().isEmpty())
  1096 		cout << "BO::reposition  "<<qPrintable(treeItem->getHeading())<<endl;
  1097 	else	
  1098 		cout << "BO::reposition  ???"<<endl;
  1099 //	cout << "  orient="<<orientation<<endl;
  1100 */		
  1101 
  1102 	if (depth==0)
  1103 	{
  1104 		// only calculate the sizes once. If the deepest LMO 
  1105 		// changes its height,
  1106 		// all upper LMOs have to change, too.
  1107 		calcBBoxSizeWithChildren();
  1108 		updateLink();	// This update is needed if the scene is resized 
  1109 						// due to excessive moving of a FIO
  1110 
  1111 	    alignRelativeTo ( QPointF (absPos.x(),
  1112 			absPos.y()-(bboxTotal.height()-bbox.height())/2) );
  1113 		//FIXME-2 qSort (branch.begin(),branch.end(), isAbove);
  1114 		positionBBox();	// Reposition bbox and contents
  1115 	} else
  1116 	{
  1117 		// This is only important for moving branches:
  1118 		// For editing a branch it isn't called...
  1119 	    alignRelativeTo ( QPointF (absPos.x(),
  1120 							absPos.y()-(bboxTotal.height()-bbox.height())/2) );
  1121 	}
  1122 }
  1123 
  1124 void BranchObj::unsetAllRepositionRequests()
  1125 {
  1126 	repositionRequest=false;
  1127 	for (int i=0; i<treeItem->branchCount(); ++i)
  1128 		treeItem->getBranchObjNum(i)->unsetAllRepositionRequests();
  1129 }
  1130 
  1131 
  1132 QPolygonF BranchObj::shape()
  1133 {
  1134 	QPolygonF p;
  1135 
  1136 	QRectF r=getTotalBBox();
  1137 	if (orientation==LinkableMapObj::LeftOfCenter)
  1138 		p   <<r.bottomLeft()
  1139 			<<r.topLeft()
  1140 			<<QPointF (bbox.topLeft().x(), r.topLeft().y() )
  1141 			<<bbox.topRight()
  1142 			<<bbox.bottomRight()
  1143 			<<QPointF (bbox.bottomLeft().x(), r.bottomLeft().y() ) ;
  1144 	else		
  1145 		p   <<r.bottomRight()
  1146 			<<r.topRight()
  1147 			<<QPointF (bbox.topRight().x(), r.topRight().y() )
  1148 			<<bbox.topLeft()
  1149 			<<bbox.bottomLeft()
  1150 			<<QPointF (bbox.bottomRight().x(), r.bottomRight().y() ) ;
  1151 	return p;
  1152 }
  1153 
  1154 QRectF BranchObj::getTotalBBox()
  1155 {
  1156 	QRectF r=bbox;
  1157 
  1158 	if ( ((BranchItem*)treeItem)->isScrolled() ) return r;
  1159 
  1160 	for (int i=0; i<treeItem->branchCount(); ++i)
  1161 		if (!treeItem->getBranchNum(i)->isHidden())
  1162 			r=addBBox(treeItem->getBranchObjNum(i)->getTotalBBox(),r);
  1163 
  1164 /*
  1165 	FIXME-1 for (int i=0; i<floatimage.size(); ++i)
  1166 		if (!floatimage.at(i)->isHidden())
  1167 			r=addBBox(floatimage.at(i)->getTotalBBox(),r);
  1168 	*/	
  1169 	return r;
  1170 }
  1171 
  1172 QRectF BranchObj::getBBoxSizeWithChildren()
  1173 {
  1174 	return bboxTotal;
  1175 }
  1176 
  1177 void BranchObj::calcBBoxSizeWithChildren()
  1178 {	
  1179 	// This is initially called only from reposition and
  1180 	// and only for mapcenter. So it won't be
  1181 	// called more than once for a single user 
  1182 	// action
  1183 	
  1184 
  1185 	// Calculate size of LMO including all children (to align them later)
  1186 	bboxTotal.setX(bbox.x() );
  1187 	bboxTotal.setY(bbox.y() );
  1188 
  1189 	// if branch is scrolled, ignore children, but still consider floatimages
  1190 	BranchItem *bi=(BranchItem*)treeItem;
  1191 	if ( bi->isScrolled() ) 
  1192 	{
  1193 		bboxTotal.setWidth (bbox.width());
  1194 		bboxTotal.setHeight(bbox.height());
  1195 		return;
  1196 	}
  1197 	
  1198 	if (bi->isHidden())
  1199 	{
  1200 		bboxTotal.setWidth (0);
  1201 		bboxTotal.setHeight(0);
  1202 		if (parObj)
  1203 		{
  1204 			bboxTotal.setX (parObj->x());
  1205 			bboxTotal.setY (parObj->y());
  1206 		} else
  1207 		{
  1208 			bboxTotal.setX (bbox.x());
  1209 			bboxTotal.setY (bbox.y());
  1210 		}
  1211 		return;
  1212 	}
  1213 	
  1214 	QRectF r(0,0,0,0);
  1215 	QRectF br;
  1216 	// Now calculate recursivly
  1217 	// sum of heights 
  1218 	// maximum of widths 
  1219 	// minimum of y
  1220 	for (int i=0; i<treeItem->branchCount(); ++i)
  1221 	{
  1222 		if (!bi->getBranchNum(i)->isHidden())
  1223 		{
  1224 			bi->getBranchObjNum(i)->calcBBoxSizeWithChildren();
  1225 			br=bi->getBranchObjNum(i)->getBBoxSizeWithChildren();
  1226 			r.setWidth( max (br.width(), r.width() ));
  1227 			r.setHeight(br.height() + r.height() );
  1228 			if (br.y()<bboxTotal.y()) bboxTotal.setY(br.y());
  1229 		}
  1230 	}
  1231 	// Add myself and also
  1232 	// add width of link to sum if necessary
  1233 	if (bi->branchCount()<1)
  1234 		bboxTotal.setWidth (bbox.width() + r.width() );
  1235 	else	
  1236 		bboxTotal.setWidth (bbox.width() + r.width() + linkwidth);
  1237 	
  1238 	bboxTotal.setHeight(max (r.height(),  bbox.height()));
  1239 }
  1240 
  1241 /*
  1242 void BranchObj::select()	// FIXME-4 try to get rid of this in BO completely
  1243 {
  1244 	cout << "BO::select()\n";
  1245 	textEditor->setText(treeItem->getNoteObj().getNote() );
  1246 	QString fnh=treeItem->getNoteObj().getFilenameHint();
  1247 	if (fnh!="")
  1248 		textEditor->setFilenameHint(treeItem->getNoteObj().getFilenameHint() );
  1249 	else	
  1250 		textEditor->setFilenameHint(getHeading() );
  1251 	textEditor->setFontHint (treeItem->getNoteObj().getFontHint() );
  1252 	//isNoteInEditor=true;
  1253 
  1254 	// set selected and visible
  1255     LinkableMapObj::select();
  1256 
  1257     //if (po)	po->setLastSelectedBranch(this);  needed?
  1258 		
  1259 	// temporary unscroll, if we have scrolled parents somewhere
  1260 	if (parObj) ((BranchObj*)(parObj))->tmpUnscroll();
  1261 
  1262 	//moved to vymmodel or vymview...
  1263 	// Show URL and link in statusbar
  1264 	QString status;
  1265 	if (!url.isEmpty()) status+="URL: "+url+"  ";
  1266 	if (!vymLink.isEmpty()) status+="Link: "+vymLink;
  1267 	if (!status.isEmpty()) mainWindow->statusMessage (status);
  1268 
  1269 	// Update Toolbar
  1270 	updateFlagsToolbar();
  1271 
  1272 	// Update actions
  1273 	model->updateActions();
  1274 }
  1275 	*/
  1276 
  1277 /*
  1278 void BranchObj::unselect()	//FIXME-4 should not be needed
  1279 {
  1280 	cout << "BO::unselect()\n";
  1281 	LinkableMapObj::unselect();
  1282 	// Delete any messages like vymLink in StatusBar
  1283 	mainWindow->statusMessage ("");		//this causes segfault, when MainWindow is already gone in global destructor on quitting vym
  1284 
  1285 	// Save current note
  1286 	if (isNoteInEditor) getNoteFromTextEditor();
  1287 	isNoteInEditor=false;
  1288 
  1289 	// reset temporary unscroll, if we have scrolled parents somewhere
  1290 	if (parObj) ((BranchObj*)(parObj))->resetTmpUnscroll();
  1291 
  1292 	// Erase content of editor 
  1293 	textEditor->setInactive();
  1294 
  1295 	// unselect all buttons in toolbar
  1296 	standardFlagsDefault->updateToolbar();
  1297 }
  1298 */	
  1299 
  1300 QString BranchObj::getSelectString()
  1301 {
  1302 	return model->getSelectString (this);
  1303 }
  1304 
  1305 void BranchObj::setAnimation(const AnimPoint &ap)
  1306 {
  1307 	anim=ap;
  1308 }
  1309 
  1310 bool BranchObj::animate()
  1311 {
  1312 	anim.animate ();
  1313 	if ( anim.isAnimated() )
  1314 	{
  1315 		setRelPos (anim);
  1316 		return true;
  1317 	}
  1318 	parObj->reposition();	// we might have been relinked meanwhile
  1319 	return false;
  1320 }
  1321