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