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