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