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