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