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