branchobj.cpp
author insilmaril
Wed, 04 May 2005 20:35:39 +0000
changeset 98 58adc2d2ed08
parent 97 0b048b6bb6f4
child 100 f79a53ee8cf6
permissions -rw-r--r--
1.6.5 new insert/remove options
     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     // TODO This should be done in TextObj later
    99     QFont font("Sans Serif,8,-1,5,50,0,0,0,0,0");
   100     heading->setFont(font );
   101 
   102     lastSelectedBranch=-1;
   103 
   104     setChildObj(this);
   105 
   106 	scrolled=false;
   107 	tmpUnscrolled=false;
   108 
   109 	url="";
   110 	vymLink="";
   111 }
   112 
   113 void BranchObj::copy (BranchObj* other)
   114 {
   115     OrnamentedObj::copy(other);
   116 
   117 	branch.clear();
   118     BranchObj* b;
   119     for (b=other->branch.first(); b;b=other->branch.next() ) 
   120 		// Make deep copy of b
   121 		// Because addBranch again calls copy for the childs,
   122 		// Those will get a deep copy, too
   123 		addBranch(b);	
   124 
   125 	FloatImageObj *fi;
   126 	for (fi=other->floatimage.first(); fi;fi=other->floatimage.next() )
   127 		addFloatImage (fi);
   128 
   129 	scrolled=other->scrolled;
   130 	tmpUnscrolled=other->tmpUnscrolled;
   131 	setVisibility (other->visible);
   132 
   133 	url=other->url;
   134 	vymLink=other->vymLink;
   135 
   136 	angle=other->angle;
   137 
   138     positionBBox();
   139 }
   140 
   141 void BranchObj::clear() 
   142 {
   143 	floatimage.clear();
   144 	while (!xlink.isEmpty())
   145 		deleteXLink (xlink.first() );
   146 
   147 	BranchObj *bo;
   148 	while (!branch.isEmpty())
   149 	{
   150 		bo=branch.first();
   151 		branch.removeFirst();
   152 		delete (bo);
   153 	}
   154 }
   155 
   156 int BranchObj::getNum()
   157 {
   158 	if (parObj)
   159 		return ((BranchObj*)(parObj))->getNum ((BranchObj*)(this));
   160 	else
   161 		return 0;
   162 }
   163 
   164 int BranchObj::getNum(BranchObj *bo)
   165 {
   166 	// keep current pointer in branch, 
   167 	// otherwise save might fail
   168 	int cur=branch.at();
   169 	int ind=branch.findRef (bo);
   170 	branch.at(cur);
   171 	return ind;
   172 }
   173 
   174 int BranchObj::getFloatImageNum(FloatImageObj *fio)
   175 {
   176 	return floatimage.findRef (fio);
   177 }
   178 
   179 int BranchObj::countBranches()
   180 {
   181 	return branch.count();
   182 }
   183 
   184 int BranchObj::countFloatImages()
   185 {
   186 	return floatimage.count();
   187 }
   188 
   189 int BranchObj::countXLinks()
   190 {
   191 	return xlink.count();
   192 }
   193 
   194 void BranchObj::setParObjTmp(LinkableMapObj* lmo, QPoint m, int off)
   195 {
   196 	// Temporary link to lmo
   197 	// m is position of mouse pointer 
   198 	// offset 0: default 1: below lmo   -1 above lmo  (if possible)
   199 
   200 
   201 	BranchObj* o=(BranchObj*)(lmo);
   202 	if (!parObjTmpBuf) 
   203 		parObjTmpBuf=parObj;
   204 
   205 	// ignore mapcenter and mainbranch
   206 	if (lmo->getDepth()<2) off=0;
   207 	if (off==0)
   208 	{
   209 		link2ParPos=false;
   210 	//	parObj=o;
   211 	}	
   212 	else
   213 	{	
   214 		link2ParPos=true;
   215 	//	if (off>0)
   216 	//		parObj=o->getParObj();
   217 	//	else	
   218 	//		parObj=o->getParObj();
   219 	//	parObj=o;
   220 	}		
   221 	parObj=o;
   222 
   223 	depth=parObj->getDepth()+1;
   224 
   225 	// setLinkStyle calls updateLink, only set it once
   226 	if (style!=getDefLinkStyle() ) setLinkStyle (getDefLinkStyle());
   227 
   228 	// Move temporary to new position at destination
   229 	// Usually the positioning would be done by reposition(),
   230 	// but then also the destination branch would "Jump" around...
   231 	// Better just do it approximately
   232 	if (depth==1)
   233 	{	// new parent is the mapcenter itself
   234 
   235 		QPoint p= normalise ( QPoint (m.x() - o->getChildPos().x(),
   236 									  m.y() - o->getChildPos().y() ));
   237 		if (p.x()<0) p.setX( p.x()-bbox.width() );
   238 		move2RelPos (p);
   239 	} else
   240 	{	
   241 		int y;
   242 		if (off==0)
   243 		{
   244 			// new parent is just a branch, link to it
   245 			QRect t=o->getBBoxSizeWithChilds();
   246 			if (o->getLastBranch())
   247 				y=t.y() + t.height() ;
   248 			else
   249 				y=t.y();
   250 
   251 		} else
   252 		{
   253 			if (off<0)
   254 				// we want to link above lmo
   255 				y=o->y() - height() + 5;
   256 			else	
   257 				// we want to link below lmo
   258 				// Bottom of sel should be 5 pixels above
   259 				// the bottom of the branch _below_ the target:
   260 				// Don't try to find that branch, guess 12 pixels
   261 				y=o->getChildPos().y()  -height() + 12; 
   262 		}	
   263 		if (o->getOrientation()==OrientLeftOfCenter)
   264 			move ( o->getChildPos().x() - linkwidth, y );
   265 		else	
   266 			move (o->getChildPos().x() + linkwidth, y );
   267 	}	
   268 
   269 	// updateLink is called implicitly in move
   270 	reposition();	// FIXME shouldn't be this a request?
   271 }
   272 
   273 void BranchObj::unsetParObjTmp()
   274 {
   275 	if (parObjTmpBuf) 
   276 	{
   277 		link2ParPos=false;
   278 		parObj=parObjTmpBuf;
   279 		parObjTmpBuf=NULL;
   280 		depth=parObj->getDepth()+1;
   281 		setLinkStyle (getDefLinkStyle() );
   282 		updateLink();
   283 	}		
   284 }
   285 
   286 void BranchObj::unScroll()
   287 {
   288 	if (tmpUnscrolled) resetTmpUnscroll();
   289 	if (scrolled) toggleScroll();
   290 }
   291 
   292 void BranchObj::toggleScroll()
   293 {
   294 	BranchObj *bo;
   295 	if (scrolled)
   296 	{
   297 		scrolled=false;
   298 		systemFlags->deactivate("scrolledright");
   299 		for (bo=branch.first(); bo; bo=branch.next() )
   300 		{
   301 			bo->setVisibility(true);
   302 		}
   303 	} else
   304 	{
   305 		scrolled=true;
   306 		systemFlags->activate("scrolledright");
   307 		for (bo=branch.first(); bo; bo=branch.next() )
   308 		{
   309 			bo->setVisibility(false);
   310 		}
   311 	}
   312 	calcBBoxSize();
   313 	positionBBox();	
   314 	move (absPos.x(), absPos.y() );
   315 	forceReposition();
   316 }
   317 
   318 bool BranchObj::isScrolled()
   319 {
   320 	return scrolled;
   321 }
   322 
   323 bool BranchObj::hasScrolledParent(BranchObj *start)
   324 {
   325 	// Calls parents recursivly to
   326 	// find out, if we are scrolled at all.
   327 	// But ignore myself, just look at parents.
   328 
   329 	if (this !=start && scrolled) return true;
   330 
   331 	BranchObj* bo=(BranchObj*)(parObj);
   332 	if (bo) 
   333 		return bo->hasScrolledParent(start);
   334 	else
   335 		return false;
   336 }
   337 
   338 void BranchObj::tmpUnscroll()
   339 {
   340 	// Unscroll parent (recursivly)
   341 	BranchObj* bo=(BranchObj*)(parObj);
   342 	if (bo) bo->tmpUnscroll();
   343 		
   344 	// Unscroll myself
   345 	if (scrolled)
   346 	{
   347 		tmpUnscrolled=true;
   348 		systemFlags->activate("tmpUnscrolledright");
   349 		toggleScroll();
   350 	}	
   351 }
   352 
   353 void BranchObj::resetTmpUnscroll()
   354 {
   355 	// Unscroll parent (recursivly)
   356 	BranchObj* bo=(BranchObj*)(parObj);
   357 	if (bo)
   358 		bo->resetTmpUnscroll();
   359 		
   360 	// Unscroll myself
   361 	if (tmpUnscrolled)
   362 	{
   363 		tmpUnscrolled=false;
   364 		systemFlags->deactivate("tmpUnscrolledright");
   365 		toggleScroll();
   366 	}	
   367 }
   368 
   369 void BranchObj::setVisibility(bool v, int toDepth)
   370 {
   371     if (depth <= toDepth)
   372     {
   373 		frame->setVisibility(v);
   374 		heading->setVisibility(v);
   375 		systemFlags->setVisibility(v);
   376 		standardFlags->setVisibility(v);
   377 		LinkableMapObj::setVisibility (v);
   378 		
   379 		if (!scrolled && (depth < toDepth))
   380 		{
   381 			// Now go recursivly through all childs
   382 			BranchObj* b;
   383 			for (b=branch.first(); b;b=branch.next() ) 
   384 				b->setVisibility (v,toDepth);	
   385 			FloatImageObj *fio;
   386 			for (fio=floatimage.first(); fio; fio=floatimage.next())
   387 				fio->setVisibility (v);
   388 			XLinkObj* xlo;
   389 			for (xlo=xlink.first(); xlo;xlo=xlink.next() ) 
   390 				xlo->setVisibility ();	
   391 		}
   392     } // depth <= toDepth	
   393 	requestReposition();
   394 }	
   395 
   396 void BranchObj::setVisibility(bool v)
   397 {
   398     setVisibility (v,MAX_DEPTH);
   399 }
   400 
   401 
   402 void BranchObj::setLinkColor ()
   403 {
   404 	// Overloaded from LinkableMapObj
   405 	// BranchObj can use color of heading
   406 
   407 	if (mapEditor->getLinkColorHint()==HeadingColor)
   408 		LinkableMapObj::setLinkColor (heading->getColor() );
   409 	else	
   410 		LinkableMapObj::setLinkColor ();
   411 }
   412 
   413 void BranchObj::setColor (QColor col, bool colorChilds)
   414 {
   415     heading->setColor(col);
   416 	setLinkColor();
   417     if (colorChilds) 
   418     {
   419 		BranchObj *bo;
   420 		for (bo=branch.first(); bo; bo=branch.next() )
   421 			bo->setColor(col,colorChilds);
   422     }	
   423 }
   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     // Adjusting font size
   663     QFont font=heading->getFont();
   664 	if (depth==0)
   665 		font.setPointSize(16);
   666 	else	
   667 		if (depth>1) 
   668 			font.setPointSize(10);
   669 		else
   670 			font.setPointSize(12);
   671     heading->setFont(font);
   672     heading->setText(s);	// set new heading
   673 	calcBBoxSize();			// recalculate bbox
   674     positionBBox();			// rearrange contents
   675 	requestReposition();
   676 }
   677 
   678 void BranchObj::setURL(QString s)
   679 {
   680 	url=s;
   681 	if (!url.isEmpty())
   682 		systemFlags->activate("url");
   683 	else	
   684 		systemFlags->deactivate("url");
   685 	calcBBoxSize();			// recalculate bbox
   686     positionBBox();			// rearrange contents
   687 	forceReposition();
   688 }
   689 
   690 QString BranchObj::getURL()
   691 {
   692 	return url;
   693 }
   694 
   695 void BranchObj::setVymLink(QString s)
   696 {
   697 	if (!s.isEmpty())
   698 	{
   699 		// We need the relative (from loading) 
   700 		// or absolute path (from User event)
   701 		// and build the absolute path.
   702 		// Note: If we have relative, use path of
   703 		// current map to build absolute path
   704 		QDir d(s);
   705 		if (!d.path().startsWith ("/"))
   706 		{
   707 			QString p=mapEditor->getDestPath();
   708 			int i=p.findRev("/",-1);
   709 			d.setPath(p.left(i)+"/"+s);
   710 			d.convertToAbs();
   711 		}
   712 		vymLink=d.path();
   713 		systemFlags->activate("vymLink");
   714 	}	
   715 	else	
   716 	{
   717 		systemFlags->deactivate("vymLink");
   718 		vymLink="";
   719 	}	
   720 	calcBBoxSize();			// recalculate bbox
   721     positionBBox();			// rearrange contents
   722 	forceReposition();
   723 }
   724 
   725 QString BranchObj::getVymLink()
   726 {
   727 	return vymLink;
   728 }
   729 
   730 QString BranchObj::saveToDir (const QString &tmpdir,const QString &prefix, const QPoint& offset)
   731 {
   732     QString s,a;
   733 	QString scrolledAttr;
   734 	if (scrolled) 
   735 		scrolledAttr=attribut ("scrolled","yes");
   736 	else
   737 		scrolledAttr="";
   738 
   739 	QString posAttr;
   740 	if (depth<2) posAttr=
   741 		attribut("absPosX",QString().setNum(absPos.x(),10)) +
   742 		attribut("absPosY",QString().setNum(absPos.y(),10)); 
   743 	else
   744 		posAttr="";
   745 
   746 	QString urlAttr;
   747 	if (!url.isEmpty())
   748 		urlAttr=attribut ("url",url);
   749 
   750 	QString vymLinkAttr;
   751 	if (!vymLink.isEmpty())
   752 		vymLinkAttr=attribut ("vymLink",convertToRel(mapEditor->getDestPath(),vymLink) );
   753 
   754 	QString frameAttr;
   755 	if (frame->getFrameType()!=NoFrame)
   756 		frameAttr=attribut ("frameType",frame->getFrameTypeName());
   757 	else
   758 		frameAttr="";
   759 
   760 	// save area, if not scrolled
   761 	QString areaAttr;
   762 	if (!((BranchObj*)(parObj))->isScrolled() )
   763 	{
   764 		areaAttr=
   765 			attribut("x1",QString().setNum(absPos.x()-offset.x(),10)) +
   766 			attribut("y1",QString().setNum(absPos.y()-offset.y(),10)) +
   767 			attribut("x2",QString().setNum(absPos.x()+width()-offset.x(),10)) +
   768 			attribut("y2",QString().setNum(absPos.y()+height()-offset.y(),10));
   769 
   770 	} else
   771 		areaAttr="";
   772 	
   773     s=beginElement ("branch" +scrolledAttr +posAttr +urlAttr +vymLinkAttr +frameAttr +areaAttr );
   774     incIndent();
   775 
   776 	// save heading
   777     s=s+valueElement("heading", getHeading(),
   778 		attribut ("textColor",QColor(heading->getColor()).name()));
   779 
   780 	// save names of flags set
   781 	s+=standardFlags->saveToDir(tmpdir,prefix,0);
   782 	
   783 	// save note
   784 	if (!note.isEmpty() )
   785 		s+=note.saveToDir();
   786 	
   787 	// Save branches
   788     BranchObj *bo;
   789     for (bo=branch.first(); bo; bo=branch.next() )
   790 		s+=bo->saveToDir(tmpdir,prefix,offset);
   791 
   792 	// Save FloatImages
   793 	FloatImageObj *fio;
   794 	for (fio=floatimage.first(); fio; fio=floatimage.next() )
   795 		s+=fio->saveToDir (tmpdir,prefix);
   796 
   797 	// Save XLinks
   798 	XLinkObj *xlo;
   799     for (xlo=xlink.first(); xlo; xlo=xlink.next() )
   800 		s+=xlo->saveToDir();
   801 
   802     decIndent();
   803     s+=endElement   ("branch");
   804     return s;
   805 }
   806 
   807 void BranchObj::addXLink (XLinkObj *xlo)
   808 {
   809 	xlink.append (xlo);
   810 	
   811 }
   812 
   813 void BranchObj::removeXLinkRef (XLinkObj *xlo)
   814 {
   815 	xlink.remove (xlo);
   816 }
   817 
   818 void BranchObj::deleteXLink(XLinkObj *xlo)
   819 {
   820 	xlo->deactivate();
   821 	if (!xlo->isUsed()) delete (xlo);
   822 }
   823 
   824 void BranchObj::deleteXLinkAt (int i)
   825 {
   826 	XLinkObj *xlo=xlink.at(i);
   827 	xlo->deactivate();
   828 	if (!xlo->isUsed()) delete(xlo);
   829 }
   830 
   831 XLinkObj* BranchObj::XLinkAt (int i)
   832 {
   833 	return xlink.at(i);
   834 }
   835 
   836 int BranchObj::countXLink()
   837 {
   838 	return xlink.count();
   839 }
   840 
   841 BranchObj* BranchObj::XLinkTargetAt (int i)
   842 {
   843 	if (xlink.at(i))
   844 		return xlink.at(i)->otherBranch (this);
   845 	else
   846 		return NULL;
   847 }
   848 
   849 LinkableMapObj* BranchObj::addFloatImage ()
   850 {
   851 	FloatImageObj *newfi=new FloatImageObj (canvas,this);
   852 	floatimage.append (newfi);
   853 	if (hasScrolledParent(this) )
   854 		newfi->setVisibility (false);
   855 	else	
   856 		newfi->setVisibility(visible);
   857 	requestReposition();
   858 	return newfi;
   859 }
   860 
   861 LinkableMapObj* BranchObj::addFloatImage (FloatImageObj *fio)
   862 {
   863 	FloatImageObj *newfi=new FloatImageObj (canvas,this);
   864 	floatimage.append (newfi);
   865 	newfi->copy (fio);
   866 	if (hasScrolledParent(this) )
   867 		newfi->setVisibility (false);
   868 	else	
   869 		newfi->setVisibility(visible);
   870 	requestReposition();
   871 	return newfi;
   872 }
   873 
   874 FloatImageObj* BranchObj::getFirstFloatImage ()
   875 {
   876     return floatimage.first();
   877 }
   878 
   879 FloatImageObj* BranchObj::getLastFloatImage ()
   880 {
   881     return floatimage.last();
   882 }
   883 
   884 FloatImageObj* BranchObj::getFloatImageNum (const uint &i)
   885 {
   886     return floatimage.at(i);
   887 }
   888 
   889 void BranchObj::removeFloatImage (FloatImageObj *fio)
   890 {
   891 	floatimage.remove (fio);
   892 	requestReposition();
   893 }
   894 
   895 void BranchObj::savePosInAngle ()
   896 {
   897 	// Save position in angle
   898     BranchObj *b;
   899 	int i=0;
   900     for (b=branch.first(); b; b=branch.next() )
   901 	{
   902 		b->angle=i;
   903 		i++;
   904 	}
   905 }
   906 
   907 BranchObj* BranchObj::addBranch()
   908 {
   909     BranchObj* newbo=new BranchObj(canvas,this);
   910     branch.append (newbo);
   911     newbo->setParObj(this);
   912     newbo->setColor(getColor(),false);	
   913     newbo->setLinkColor();	
   914     newbo->setHeading ("new");
   915 	newbo->setLinkStyle (newbo->getDefLinkStyle());
   916 	if (scrolled)
   917 		newbo->setVisibility (false);
   918 	else	
   919 		newbo->setVisibility(visible);
   920 	newbo->updateLink();	
   921 	requestReposition();
   922 	return newbo;
   923 }
   924 
   925 BranchObj* BranchObj::addBranch(BranchObj* bo)
   926 {
   927     BranchObj* newbo=new BranchObj(canvas,this);
   928     branch.append (newbo);
   929     newbo->copy(bo);
   930     newbo->setParObj(this);
   931 	newbo->setHeading (newbo->getHeading());	// adjust fontsize to depth
   932 	newbo->setLinkStyle (newbo->getDefLinkStyle());
   933 	if (scrolled)
   934 		newbo->setVisibility (false);
   935 	else	
   936 		newbo->setVisibility(bo->visible);
   937 	newbo->updateLink();	
   938 	requestReposition();
   939 	return newbo;
   940 }
   941 
   942 BranchObj* BranchObj::addBranchPtr(BranchObj* bo)
   943 {
   944 	branch.append (bo);
   945 	bo->setParObj (this);
   946 	bo->depth=depth+1;
   947 	bo->setLinkStyle (bo->getDefLinkStyle() );
   948 	if (scrolled) tmpUnscroll();
   949 	setLastSelectedBranch (bo);
   950 	return bo;
   951 }
   952 
   953 BranchObj* BranchObj::insertBranch(int pos)
   954 {
   955 	savePosInAngle();
   956 	// Add new bo and resort branches
   957 	BranchObj *newbo=addBranch ();
   958 	newbo->angle=pos-0.5;
   959 	branch.sort();
   960 	return newbo;
   961 }
   962 
   963 BranchObj* BranchObj::insertBranch(BranchObj* bo, int pos)
   964 {
   965 	savePosInAngle();
   966 	// Add new bo and resort branches
   967 	bo->angle=pos-0.5;
   968 	BranchObj *newbo=addBranch (bo);
   969 	branch.sort();
   970 	return newbo;
   971 }
   972 
   973 BranchObj* BranchObj::insertBranchPtr (BranchObj* bo, int pos)
   974 {
   975 	savePosInAngle();
   976 	// Add new bo and resort branches
   977 	bo->angle=pos-0.5;
   978 	branch.append (bo);
   979 	bo->setParObj (this);
   980 	bo->depth=depth+1;
   981 	bo->setLinkStyle (bo->getDefLinkStyle() );
   982 	if (scrolled) tmpUnscroll();
   983 	setLastSelectedBranch (bo);
   984 	branch.sort();
   985 	return bo;
   986 }
   987 
   988 void BranchObj::removeBranchHere(BranchObj* borem)
   989 {
   990 	// This removes the branch bo from list, but 
   991 	// inserts its childs at the place of bo
   992 	BranchObj *bo;
   993 	bo=borem->getLastBranch();
   994 	while (bo)
   995 	{
   996 		bo->moveBranchTo (borem,1);
   997 		bo=borem->getLastBranch();
   998 	}	
   999 	removeBranch (borem);
  1000 }
  1001 
  1002 void BranchObj::removeChilds()
  1003 {
  1004 	clear();
  1005 }
  1006 
  1007 void BranchObj::removeBranch(BranchObj* bo)
  1008 {
  1009     // if bo is not in branch remove returns false, we
  1010     // don't care...
  1011 	
  1012     if (branch.remove (bo))
  1013 		delete (bo);
  1014 	else
  1015 		qWarning ("BranchObj::removeBranch tried to remove non existing branch?!\n");
  1016 	requestReposition();
  1017 }
  1018 
  1019 void BranchObj::removeBranchPtr(BranchObj* bo)
  1020 {
  1021 	branch.remove (bo);
  1022 	requestReposition();
  1023 }
  1024 
  1025 void BranchObj::setLastSelectedBranch (BranchObj* bo)
  1026 {
  1027     lastSelectedBranch=branch.find(bo);
  1028 }
  1029 
  1030 BranchObj* BranchObj::getLastSelectedBranch ()
  1031 {
  1032     if (lastSelectedBranch>=0) 
  1033 	{
  1034 		BranchObj* bo=branch.at(lastSelectedBranch);
  1035 		if (bo) return bo;
  1036     }	
  1037     return branch.first();
  1038 }
  1039 
  1040 BranchObj* BranchObj::getFirstBranch ()
  1041 {
  1042     return branch.first();
  1043 }
  1044 
  1045 BranchObj* BranchObj::getLastBranch ()
  1046 {
  1047     return branch.last();
  1048 }
  1049 
  1050 BranchObj* BranchObj::getBranchNum (const uint &i)
  1051 {
  1052     return branch.at(i);
  1053 }
  1054 
  1055 
  1056 BranchObj* BranchObj::moveBranchUp(BranchObj* bo1) // move a branch up (modify myself)
  1057 {
  1058 	savePosInAngle();
  1059     int i=branch.find(bo1);
  1060     if (i>0) 
  1061 	{	// -1 if bo1 not found 
  1062 		branch.at(i)->angle--;
  1063 		branch.at(i-1)->angle++;
  1064 		branch.sort();
  1065 		return branch.at(i-1);
  1066 	} else
  1067 		return branch.at(i);
  1068 }
  1069 
  1070 BranchObj* BranchObj::moveBranchDown(BranchObj* bo1)
  1071 {
  1072 	savePosInAngle();
  1073     int i=branch.find(bo1);
  1074 	int j;
  1075 	if (branch.next())
  1076 	{
  1077 		j = branch.at();
  1078 		branch.at(i)->angle++;
  1079 		branch.at(j)->angle--;
  1080 		branch.sort();
  1081 		return branch.at(j);
  1082 	} else
  1083 		return branch.at(i);
  1084 }
  1085 
  1086 BranchObj* BranchObj::moveBranchTo (BranchObj* dst, int pos)
  1087 {
  1088 	// Find current parent and 
  1089 	// remove pointer to myself there
  1090 
  1091 	if (!dst) return NULL;
  1092 
  1093 	BranchObj *par=(BranchObj*)(parObj);
  1094 	if (par)
  1095 		par->removeBranchPtr (this);
  1096 	else
  1097 		return NULL;
  1098 
  1099 	if (pos<0||dst->getDepth()==0)
  1100 	{	
  1101 		// links myself as last branch at dst
  1102 		dst->addBranchPtr (this);
  1103 		return this;
  1104 	} else
  1105 	{
  1106 		// inserts me at pos in parent of dst
  1107 		par=(BranchObj*)(dst->getParObj());
  1108 		if (par)
  1109 			return par->insertBranchPtr (this,pos);
  1110 		else
  1111 			return NULL;
  1112 	}	
  1113 }
  1114 
  1115 void BranchObj::alignRelativeTo (QPoint ref)
  1116 {
  1117 /* FIXME testing
  1118 	if (!getHeading().isEmpty())
  1119 		cout << "BO::alignRelTo "<<getHeading()<<endl;
  1120 	else	
  1121 		cout << "BO::alignRelTo  ???"<<endl;
  1122 	cout << "  d="<<depth<<endl;
  1123 */	
  1124 	int th = bboxTotal.height();	
  1125 
  1126 	// If I am the mapcenter or a mainbranch, reposition heading
  1127 	if (depth<2)
  1128 	{
  1129 		move (absPos.x(),absPos.y());
  1130 		if (depth==1)
  1131 		{
  1132 			// Calc angle to mapCenter if I am a mainbranch
  1133 			// needed for reordering the mainbranches clockwise 
  1134 			// around mapcenter 
  1135 			angle=getAngle (QPoint ((int)(x() - parObj->getChildPos().x() ), 
  1136 									(int)(y() - parObj->getChildPos().y() ) ) );
  1137 		}	
  1138 	} 
  1139 	else
  1140     {
  1141 		// Align myself depending on orientation and parent, but
  1142 		// only if I am not the mainbranch or mapcenter itself
  1143 		switch (orientation) 
  1144 		{
  1145 			case OrientLeftOfCenter:
  1146 				move (ref.x()-bbox.width(), ref.y() + (th-bbox.height())/2 );
  1147 			break;
  1148 			case OrientRightOfCenter:	
  1149 				move (ref.x(), ref.y() + (th-bbox.height())/2 );
  1150 			break;
  1151 			default:
  1152 				cout <<"LMO::alignRelativeTo: oops, no orientation given...\n";
  1153 			break;
  1154 		}		
  1155     }		
  1156 
  1157 	FloatImageObj *fio;
  1158     for (fio=floatimage.first(); fio; fio=floatimage.next() )
  1159 		fio->reposition();
  1160 
  1161 	if (scrolled) return;
  1162 
  1163     // Set reference point for alignment of childs
  1164     QPoint ref2;
  1165     if (orientation==OrientLeftOfCenter)
  1166 		ref2.setX(childPos.x() - linkwidth);
  1167     else	
  1168 		ref2.setX(childPos.x() + linkwidth);
  1169 
  1170 	if (depth==1)
  1171 		ref2.setY(absPos.y()-(bboxTotal.height()-bbox.height())/2);
  1172 	else	
  1173 		ref2.setY(ref.y() );	
  1174 
  1175     // Align the childs depending on reference point 
  1176     BranchObj *b;
  1177     for (b=branch.first(); b; b=branch.next() )
  1178     {	
  1179 		b->alignRelativeTo (ref2);
  1180 		ref2.setY(ref2.y() + b->getBBoxSizeWithChilds().height() );
  1181     }
  1182 }
  1183 
  1184 
  1185 void BranchObj::reposition()
  1186 {	
  1187 /* FIXME testing
  1188 	if (!getHeading().isEmpty())
  1189 		cout << "BO::reposition  "<<getHeading()<<endl;
  1190 	else	
  1191 		cout << "BO::reposition  ???"<<endl;
  1192 */		
  1193 	if (depth==0)
  1194 	{
  1195 		// only calculate the sizes once. If the deepest LMO 
  1196 		// changes its height,
  1197 		// all upper LMOs have to change, too.
  1198 		calcBBoxSizeWithChilds();
  1199 	    alignRelativeTo ( QPoint (absPos.x(),
  1200 			absPos.y()-(bboxTotal.height()-bbox.height())/2) );
  1201 		branch.sort();	
  1202 	} else
  1203 	{
  1204 		// This is only important for moving branches:
  1205 		// For editing a branch it isn't called...
  1206 	    alignRelativeTo ( QPoint (absPos.x(),
  1207 							absPos.y()-(bboxTotal.height()-bbox.height())/2) );
  1208 	}
  1209 }
  1210 
  1211 
  1212 QRect BranchObj::getTotalBBox()
  1213 {
  1214 	QRect r=bbox;
  1215 
  1216 	if (scrolled) return r;
  1217 
  1218 	BranchObj* b;
  1219 	for (b=branch.first();b ;b=branch.next() )
  1220 		r=addBBox(b->getTotalBBox(),r);
  1221 
  1222 	FloatImageObj* fio;
  1223 	for (fio=floatimage.first();fio ;fio=floatimage.next() )
  1224 		r=addBBox(fio->getTotalBBox(),r);
  1225 		
  1226 	return r;
  1227 }
  1228 
  1229 QRect BranchObj::getBBoxSizeWithChilds()
  1230 {
  1231 	return bboxTotal;
  1232 }
  1233 
  1234 void BranchObj::calcBBoxSizeWithChilds()
  1235 {
  1236 	// This is called only from reposition and
  1237 	// and only for mapcenter. So it won't be
  1238 	// called more than once for a single user 
  1239 	// action
  1240 	
  1241 	// Calculate size of LMO including all childs (to align them later)
  1242 
  1243 	bboxTotal.setX(bbox.x() );
  1244 	bboxTotal.setY(bbox.y() );
  1245 
  1246 	// if branch is scrolled, ignore childs, but still consider floatimages
  1247 	if (scrolled)
  1248 	{
  1249 		bboxTotal.setWidth (bbox.width());
  1250 		bboxTotal.setHeight(bbox.height());
  1251 		return;
  1252 	}
  1253 	
  1254 	QRect r(0,0,0,0);
  1255 	QRect br;
  1256 	// Now calculate recursivly
  1257 	// sum of heights 
  1258 	// maximum of widths 
  1259 	// minimum of y
  1260 	BranchObj* b;
  1261 	for (b=branch.first();b ;b=branch.next() )
  1262 	{
  1263 		b->calcBBoxSizeWithChilds();
  1264 		br=b->getBBoxSizeWithChilds();
  1265 		r.setWidth( max (br.width(), r.width() ));
  1266 		r.setHeight(br.height() + r.height() );
  1267 		if (br.y()<bboxTotal.y()) bboxTotal.setY(br.y());
  1268 	}
  1269 	// Add myself and also
  1270 	// add width of link to sum if necessary
  1271 	if (branch.isEmpty())
  1272 		bboxTotal.setWidth (bbox.width() + r.width() );
  1273 	else	
  1274 		bboxTotal.setWidth (bbox.width() + r.width() + linkwidth);
  1275 	bboxTotal.setHeight(max (r.height(),  bbox.height() ) );
  1276 //	frame->setRect(QRect(bbox.x(),bbox.y(),bbox.width(),bbox.height() ) );
  1277 }
  1278 
  1279 void BranchObj::select()
  1280 {
  1281     LinkableMapObj::select();
  1282 	// Tell parent that I am selected now:
  1283 	BranchObj* po=(BranchObj*)(parObj);
  1284     if (po)	// TODO	    Try to get rid of this cast...
  1285         po->setLastSelectedBranch(this);
  1286 		
  1287 	// temporary unscroll, if we have scrolled parents somewhere
  1288 	if (parObj) ((BranchObj*)(parObj))->tmpUnscroll();
  1289 
  1290 	// set Text in Editor	
  1291 	textEditor->setText(note.getNote() );
  1292 	textEditor->setFilename(note.getFilenameHint() );
  1293 	textEditor->setFontHint (note.getFontHint() );
  1294 	connect (textEditor, SIGNAL (textHasChanged() ), this, SLOT (updateNoteFlag() ) ); 
  1295 	connect (textEditor, SIGNAL (fontSizeHasChanged() ), this, SLOT (updateNoteFlag() ) ); 
  1296 
  1297 	// Show URL and link in statusbar
  1298 	QString status;
  1299 	if (!url.isEmpty()) status+="URL: "+url+"  ";
  1300 	if (!vymLink.isEmpty()) status+="Link: "+vymLink;
  1301 	if (!status.isEmpty()) mainWindow->statusMessage (status);
  1302 
  1303 	// Update Toolbar
  1304 	standardFlags->updateToolBar();
  1305 
  1306 	// Update Browserbutton
  1307 	if (!url.isEmpty())
  1308 		actionEditOpenURL->setEnabled (true);
  1309 	else	
  1310 		actionEditOpenURL->setEnabled (false);
  1311 
  1312 	// Update actions in mapeditor
  1313 	mapEditor->updateActions();
  1314 }
  1315 
  1316 void BranchObj::unselect()
  1317 {
  1318 	LinkableMapObj::unselect();
  1319 	// Delete any messages like vymLink in StatusBar
  1320 	mainWindow->statusMessage ("");
  1321 
  1322 	// save note from editor and set flag
  1323 	// text is done by updateNoteFlag(), just save
  1324 	// filename here
  1325 	note.setFilenameHint (textEditor->getFilename());
  1326 
  1327 	// reset temporary unscroll, if we have scrolled parents somewhere
  1328 	if (parObj) ((BranchObj*)(parObj))->resetTmpUnscroll();
  1329 
  1330 	// Disconnect textEditor from this LMO
  1331 	disconnect( textEditor, SIGNAL(textHasChanged()), 0, 0 );
  1332 	disconnect( textEditor, SIGNAL (fontSizeHasChanged()),0,0 ); 
  1333 
  1334 	// Erase content of editor 
  1335 	textEditor->setInactive();
  1336 
  1337 	// unselect all buttons in toolbar
  1338 	standardFlagsDefault->updateToolBar();
  1339 }
  1340 
  1341 QString BranchObj::getSelectString()
  1342 {
  1343 	QString s;
  1344 	if (parObj)
  1345 	{
  1346 		if (depth==1)
  1347 			s= "bo:" + QString("%1").arg(getNum());
  1348 		else	
  1349 			s= ((BranchObj*)(parObj))->getSelectString() + ",bo:" + QString("%1").arg(getNum());
  1350 	} else
  1351 		s="mc:";
  1352 	return s;
  1353 }
  1354