branchobj.cpp
author jhilmer
Tue Aug 16 17:27:34 2005 +0000 (2005-08-16)
changeset 153 47cd4ae30000
parent 146 1b52ff19aad5
child 158 a33a2bdd8299
permissions -rw-r--r--
Fixed link handling in aboutdialog
     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) && (foi != excludeLMO) && foi->getParObj()!= excludeLMO) return foi;
   656 
   657     return NULL;
   658 }
   659 
   660 void BranchObj::setHeading(QString s)
   661 {
   662 	/*FIXME
   663     // Adjusting font size
   664     QFont font=heading->getFont();
   665 	font.setPointSize(getDefHeadingSize() );
   666     heading->setFont(font);
   667 	*/
   668     heading->setText(s);	// set new heading
   669 	calcBBoxSize();			// recalculate bbox
   670     positionBBox();			// rearrange contents
   671 	requestReposition();
   672 }
   673 
   674 void BranchObj::setURL(QString s)
   675 {
   676 	url=s;
   677 	if (!url.isEmpty())
   678 		systemFlags->activate("url");
   679 	else	
   680 		systemFlags->deactivate("url");
   681 	calcBBoxSize();			// recalculate bbox
   682     positionBBox();			// rearrange contents
   683 	forceReposition();
   684 }
   685 
   686 QString BranchObj::getURL()
   687 {
   688 	return url;
   689 }
   690 
   691 void BranchObj::setVymLink(QString s)
   692 {
   693 	if (!s.isEmpty())
   694 	{
   695 		// We need the relative (from loading) 
   696 		// or absolute path (from User event)
   697 		// and build the absolute path.
   698 		// Note: If we have relative, use path of
   699 		// current map to build absolute path
   700 		QDir d(s);
   701 		if (!d.path().startsWith ("/"))
   702 		{
   703 			QString p=mapEditor->getDestPath();
   704 			int i=p.findRev("/",-1);
   705 			d.setPath(p.left(i)+"/"+s);
   706 			d.convertToAbs();
   707 		}
   708 		vymLink=d.path();
   709 		systemFlags->activate("vymLink");
   710 	}	
   711 	else	
   712 	{
   713 		systemFlags->deactivate("vymLink");
   714 		vymLink="";
   715 	}	
   716 	calcBBoxSize();			// recalculate bbox
   717     positionBBox();			// rearrange contents
   718 	forceReposition();
   719 }
   720 
   721 QString BranchObj::getVymLink()
   722 {
   723 	return vymLink;
   724 }
   725 
   726 QString BranchObj::saveToDir (const QString &tmpdir,const QString &prefix, const QPoint& offset)
   727 {
   728     QString s,a;
   729 	QString scrolledAttr;
   730 	if (scrolled) 
   731 		scrolledAttr=attribut ("scrolled","yes");
   732 	else
   733 		scrolledAttr="";
   734 
   735 	QString posAttr;
   736 	if (depth<2) posAttr=
   737 		attribut("absPosX",QString().setNum(absPos.x(),10)) +
   738 		attribut("absPosY",QString().setNum(absPos.y(),10)); 
   739 	else
   740 		posAttr="";
   741 
   742 	QString urlAttr;
   743 	if (!url.isEmpty())
   744 		urlAttr=attribut ("url",url);
   745 
   746 	QString vymLinkAttr;
   747 	if (!vymLink.isEmpty())
   748 		vymLinkAttr=attribut ("vymLink",convertToRel(mapEditor->getDestPath(),vymLink) );
   749 
   750 	QString frameAttr;
   751 	if (frame->getFrameType()!=NoFrame)
   752 		frameAttr=attribut ("frameType",frame->getFrameTypeName());
   753 	else
   754 		frameAttr="";
   755 
   756 	// save area, if not scrolled
   757 	QString areaAttr;
   758 	if (!((BranchObj*)(parObj))->isScrolled() )
   759 	{
   760 		areaAttr=
   761 			attribut("x1",QString().setNum(absPos.x()-offset.x(),10)) +
   762 			attribut("y1",QString().setNum(absPos.y()-offset.y(),10)) +
   763 			attribut("x2",QString().setNum(absPos.x()+width()-offset.x(),10)) +
   764 			attribut("y2",QString().setNum(absPos.y()+height()-offset.y(),10));
   765 
   766 	} else
   767 		areaAttr="";
   768 	
   769     s=beginElement ("branch" +scrolledAttr +posAttr +urlAttr +vymLinkAttr +frameAttr +areaAttr );
   770     incIndent();
   771 
   772 	// save heading
   773     s=s+valueElement("heading", getHeading(),
   774 		attribut ("textColor",QColor(heading->getColor()).name()));
   775 
   776 	// save names of flags set
   777 	s+=standardFlags->saveToDir(tmpdir,prefix,0);
   778 	
   779 	// save note
   780 	if (!note.isEmpty() )
   781 		s+=note.saveToDir();
   782 	
   783 	// Save branches
   784     BranchObj *bo;
   785     for (bo=branch.first(); bo; bo=branch.next() )
   786 		s+=bo->saveToDir(tmpdir,prefix,offset);
   787 
   788 	// Save FloatImages
   789 	FloatImageObj *fio;
   790 	for (fio=floatimage.first(); fio; fio=floatimage.next() )
   791 		s+=fio->saveToDir (tmpdir,prefix);
   792 
   793 	// Save XLinks
   794 	XLinkObj *xlo;
   795     for (xlo=xlink.first(); xlo; xlo=xlink.next() )
   796 		s+=xlo->saveToDir();
   797 
   798     decIndent();
   799     s+=endElement   ("branch");
   800     return s;
   801 }
   802 
   803 void BranchObj::addXLink (XLinkObj *xlo)
   804 {
   805 	xlink.append (xlo);
   806 	
   807 }
   808 
   809 void BranchObj::removeXLinkRef (XLinkObj *xlo)
   810 {
   811 	xlink.remove (xlo);
   812 }
   813 
   814 void BranchObj::deleteXLink(XLinkObj *xlo)
   815 {
   816 	xlo->deactivate();
   817 	if (!xlo->isUsed()) delete (xlo);
   818 }
   819 
   820 void BranchObj::deleteXLinkAt (int i)
   821 {
   822 	XLinkObj *xlo=xlink.at(i);
   823 	xlo->deactivate();
   824 	if (!xlo->isUsed()) delete(xlo);
   825 }
   826 
   827 XLinkObj* BranchObj::XLinkAt (int i)
   828 {
   829 	return xlink.at(i);
   830 }
   831 
   832 int BranchObj::countXLink()
   833 {
   834 	return xlink.count();
   835 }
   836 
   837 BranchObj* BranchObj::XLinkTargetAt (int i)
   838 {
   839 	if (xlink.at(i))
   840 		return xlink.at(i)->otherBranch (this);
   841 	else
   842 		return NULL;
   843 }
   844 
   845 LinkableMapObj* BranchObj::addFloatImage ()
   846 {
   847 	FloatImageObj *newfi=new FloatImageObj (canvas,this);
   848 	floatimage.append (newfi);
   849 	if (hasScrolledParent(this) )
   850 		newfi->setVisibility (false);
   851 	else	
   852 		newfi->setVisibility(visible);
   853 	requestReposition();
   854 	return newfi;
   855 }
   856 
   857 LinkableMapObj* BranchObj::addFloatImage (FloatImageObj *fio)
   858 {
   859 	FloatImageObj *newfi=new FloatImageObj (canvas,this);
   860 	floatimage.append (newfi);
   861 	newfi->copy (fio);
   862 	if (hasScrolledParent(this) )
   863 		newfi->setVisibility (false);
   864 	else	
   865 		newfi->setVisibility(visible);
   866 	requestReposition();
   867 	return newfi;
   868 }
   869 
   870 FloatImageObj* BranchObj::getFirstFloatImage ()
   871 {
   872     return floatimage.first();
   873 }
   874 
   875 FloatImageObj* BranchObj::getLastFloatImage ()
   876 {
   877     return floatimage.last();
   878 }
   879 
   880 FloatImageObj* BranchObj::getFloatImageNum (const uint &i)
   881 {
   882     return floatimage.at(i);
   883 }
   884 
   885 void BranchObj::removeFloatImage (FloatImageObj *fio)
   886 {
   887 	floatimage.remove (fio);
   888 	requestReposition();
   889 }
   890 
   891 void BranchObj::savePosInAngle ()
   892 {
   893 	// Save position in angle
   894     BranchObj *b;
   895 	int i=0;
   896     for (b=branch.first(); b; b=branch.next() )
   897 	{
   898 		b->angle=i;
   899 		i++;
   900 	}
   901 }
   902 
   903 void BranchObj::setDefAttr (BranchModification mod)
   904 {
   905 	int fontsize;
   906 	switch (depth)
   907 	{
   908 		case 0: fontsize=16; break;
   909 		case 1: fontsize=12; break;
   910 		default: fontsize=10; break;
   911 	}	
   912 
   913 	setLinkColor ();
   914 	setLinkStyle(getDefLinkStyle());
   915 	QFont font("Sans Serif,8,-1,5,50,0,0,0,0,0");
   916 	font.setPointSize(fontsize);
   917 	heading->setFont(font );
   918 
   919 	if (mod==NewBranch)
   920 		setColor (((BranchObj*)(parObj))->getColor(),false);
   921 	
   922 	calcBBoxSize();
   923 }
   924 
   925 BranchObj* BranchObj::addBranch()
   926 {
   927     BranchObj* newbo=new BranchObj(canvas,this);
   928     branch.append (newbo);
   929     newbo->setParObj(this);
   930 	newbo->setDefAttr(NewBranch);
   931     newbo->setHeading ("new");
   932 	if (scrolled)
   933 		newbo->setVisibility (false);
   934 	else	
   935 		newbo->setVisibility(visible);
   936 	newbo->updateLink();	
   937 	requestReposition();
   938 	return newbo;
   939 }
   940 
   941 BranchObj* BranchObj::addBranch(BranchObj* bo)
   942 {
   943     BranchObj* newbo=new BranchObj(canvas,this);
   944     branch.append (newbo);
   945     newbo->copy(bo);
   946     newbo->setParObj(this);
   947 	newbo->setDefAttr(NewBranch);
   948 	if (scrolled)
   949 		newbo->setVisibility (false);
   950 	else	
   951 		newbo->setVisibility(bo->visible);
   952 	newbo->updateLink();	
   953 	requestReposition();
   954 	return newbo;
   955 }
   956 
   957 BranchObj* BranchObj::addBranchPtr(BranchObj* bo)
   958 {
   959 	branch.append (bo);
   960 	bo->setParObj (this);
   961 	bo->depth=depth+1;
   962 	bo->setDefAttr(MovedBranch);
   963 	if (scrolled) tmpUnscroll();
   964 	setLastSelectedBranch (bo);
   965 	return bo;
   966 }
   967 
   968 BranchObj* BranchObj::insertBranch(int pos)
   969 {
   970 	savePosInAngle();
   971 	// Add new bo and resort branches
   972 	BranchObj *newbo=addBranch ();
   973 	newbo->angle=pos-0.5;
   974 	branch.sort();
   975 	return newbo;
   976 }
   977 
   978 BranchObj* BranchObj::insertBranch(BranchObj* bo, int pos)
   979 {
   980 	savePosInAngle();
   981 	// Add new bo and resort branches
   982 	bo->angle=pos-0.5;
   983 	BranchObj *newbo=addBranch (bo);
   984 	branch.sort();
   985 	return newbo;
   986 }
   987 
   988 BranchObj* BranchObj::insertBranchPtr (BranchObj* bo, int pos)
   989 {
   990 	savePosInAngle();
   991 	// Add new bo and resort branches
   992 	bo->angle=pos-0.5;
   993 	branch.append (bo);
   994 	bo->setParObj (this);
   995 	bo->depth=depth+1;
   996 	bo->setDefAttr (MovedBranch);
   997 	if (scrolled) tmpUnscroll();
   998 	setLastSelectedBranch (bo);
   999 	branch.sort();
  1000 	return bo;
  1001 }
  1002 
  1003 void BranchObj::removeBranchHere(BranchObj* borem)
  1004 {
  1005 	// This removes the branch bo from list, but 
  1006 	// inserts its childs at the place of bo
  1007 	BranchObj *bo;
  1008 	bo=borem->getLastBranch();
  1009 	int pos=borem->getNum();
  1010 	while (bo)
  1011 	{
  1012 		bo->moveBranchTo (this,pos+1);
  1013 		bo=borem->getLastBranch();
  1014 	}	
  1015 	removeBranch (borem);
  1016 }
  1017 
  1018 void BranchObj::removeChilds()
  1019 {
  1020 	clear();
  1021 }
  1022 
  1023 void BranchObj::removeBranch(BranchObj* bo)
  1024 {
  1025     // if bo is not in branch remove returns false, we
  1026     // don't care...
  1027 	
  1028     if (branch.remove (bo))
  1029 		delete (bo);
  1030 	else
  1031 		qWarning ("BranchObj::removeBranch tried to remove non existing branch?!\n");
  1032 	requestReposition();
  1033 }
  1034 
  1035 void BranchObj::removeBranchPtr(BranchObj* bo)
  1036 {
  1037 	branch.remove (bo);
  1038 	requestReposition();
  1039 }
  1040 
  1041 void BranchObj::setLastSelectedBranch (BranchObj* bo)
  1042 {
  1043     lastSelectedBranch=branch.find(bo);
  1044 }
  1045 
  1046 BranchObj* BranchObj::getLastSelectedBranch ()
  1047 {
  1048     if (lastSelectedBranch>=0) 
  1049 	{
  1050 		BranchObj* bo=branch.at(lastSelectedBranch);
  1051 		if (bo) return bo;
  1052     }	
  1053     return branch.first();
  1054 }
  1055 
  1056 BranchObj* BranchObj::getFirstBranch ()
  1057 {
  1058     return branch.first();
  1059 }
  1060 
  1061 BranchObj* BranchObj::getLastBranch ()
  1062 {
  1063     return branch.last();
  1064 }
  1065 
  1066 BranchObj* BranchObj::getBranchNum (const uint &i)
  1067 {
  1068     return branch.at(i);
  1069 }
  1070 
  1071 
  1072 BranchObj* BranchObj::moveBranchUp(BranchObj* bo1) // move a branch up (modify myself)
  1073 {
  1074 	savePosInAngle();
  1075     int i=branch.find(bo1);
  1076     if (i>0) 
  1077 	{	// -1 if bo1 not found 
  1078 		branch.at(i)->angle--;
  1079 		branch.at(i-1)->angle++;
  1080 		branch.sort();
  1081 		return branch.at(i-1);
  1082 	} else
  1083 		return branch.at(i);
  1084 }
  1085 
  1086 BranchObj* BranchObj::moveBranchDown(BranchObj* bo1)
  1087 {
  1088 	savePosInAngle();
  1089     int i=branch.find(bo1);
  1090 	int j;
  1091 	if (branch.next())
  1092 	{
  1093 		j = branch.at();
  1094 		branch.at(i)->angle++;
  1095 		branch.at(j)->angle--;
  1096 		branch.sort();
  1097 		return branch.at(j);
  1098 	} else
  1099 		return branch.at(i);
  1100 }
  1101 
  1102 BranchObj* BranchObj::moveBranchTo (BranchObj* dst, int pos)
  1103 {
  1104 	// Find current parent and 
  1105 	// remove pointer to myself there
  1106 	if (!dst) return NULL;
  1107 	BranchObj *par=(BranchObj*)(parObj);
  1108 	if (par)
  1109 		par->removeBranchPtr (this);
  1110 	else
  1111 		return NULL;
  1112 
  1113 	// Create new pointer to myself at dst
  1114 	if (pos<0||dst->getDepth()==0)
  1115 	{	
  1116 		// links myself as last branch at dst
  1117 		dst->addBranchPtr (this);
  1118 		updateLink();
  1119 		return this;
  1120 	} else
  1121 	{
  1122 		// inserts me at pos in parent of dst
  1123 		if (par)
  1124 		{
  1125 			BranchObj *bo=dst->insertBranchPtr (this,pos);
  1126 			bo->setDefAttr(MovedBranch);
  1127 			updateLink();
  1128 			return bo;
  1129 
  1130 		} else
  1131 			return NULL;
  1132 	}	
  1133 }
  1134 
  1135 void BranchObj::alignRelativeTo (QPoint ref)
  1136 {
  1137 /* FIXME testing
  1138 	if (!getHeading().isEmpty())
  1139 		cout << "BO::alignRelTo "<<getHeading()<<endl;
  1140 	else	
  1141 		cout << "BO::alignRelTo  ???"<<endl;
  1142 	cout << "  d="<<depth<<endl;
  1143 */	
  1144 	int th = bboxTotal.height();	
  1145 
  1146 	// If I am the mapcenter or a mainbranch, reposition heading
  1147 	if (depth<2)
  1148 	{
  1149 		move (absPos.x(),absPos.y());
  1150 		if (depth==1)
  1151 		{
  1152 			// Calc angle to mapCenter if I am a mainbranch
  1153 			// needed for reordering the mainbranches clockwise 
  1154 			// around mapcenter 
  1155 			angle=getAngle (QPoint ((int)(x() - parObj->getChildPos().x() ), 
  1156 									(int)(y() - parObj->getChildPos().y() ) ) );
  1157 		}	
  1158 	} 
  1159 	else
  1160     {
  1161 		// Align myself depending on orientation and parent, but
  1162 		// only if I am not the mainbranch or mapcenter itself
  1163 		switch (orientation) 
  1164 		{
  1165 			case OrientLeftOfCenter:
  1166 				move (ref.x()-bbox.width(), ref.y() + (th-bbox.height())/2 );
  1167 			break;
  1168 			case OrientRightOfCenter:	
  1169 				move (ref.x(), ref.y() + (th-bbox.height())/2 );
  1170 			break;
  1171 			default:
  1172 				cout <<"LMO::alignRelativeTo: oops, no orientation given...\n";
  1173 			break;
  1174 		}		
  1175     }		
  1176 
  1177 	FloatImageObj *fio;
  1178     for (fio=floatimage.first(); fio; fio=floatimage.next() )
  1179 		fio->reposition();
  1180 
  1181 	if (scrolled) return;
  1182 
  1183     // Set reference point for alignment of childs
  1184     QPoint ref2;
  1185     if (orientation==OrientLeftOfCenter)
  1186 		ref2.setX(childPos.x() - linkwidth);
  1187     else	
  1188 		ref2.setX(childPos.x() + linkwidth);
  1189 
  1190 	if (depth==1)
  1191 		ref2.setY(absPos.y()-(bboxTotal.height()-bbox.height())/2);
  1192 	else	
  1193 		ref2.setY(ref.y() );	
  1194 
  1195     // Align the childs depending on reference point 
  1196     BranchObj *b;
  1197     for (b=branch.first(); b; b=branch.next() )
  1198     {	
  1199 		b->alignRelativeTo (ref2);
  1200 		ref2.setY(ref2.y() + b->getBBoxSizeWithChilds().height() );
  1201     }
  1202 }
  1203 
  1204 
  1205 void BranchObj::reposition()
  1206 {	
  1207 /* FIXME testing
  1208 	if (!getHeading().isEmpty())
  1209 		cout << "BO::reposition  "<<getHeading()<<endl;
  1210 	else	
  1211 		cout << "BO::reposition  ???"<<endl;
  1212 */		
  1213 	if (depth==0)
  1214 	{
  1215 		// only calculate the sizes once. If the deepest LMO 
  1216 		// changes its height,
  1217 		// all upper LMOs have to change, too.
  1218 		calcBBoxSizeWithChilds();
  1219 	    alignRelativeTo ( QPoint (absPos.x(),
  1220 			absPos.y()-(bboxTotal.height()-bbox.height())/2) );
  1221 		branch.sort();	
  1222 	} else
  1223 	{
  1224 		// This is only important for moving branches:
  1225 		// For editing a branch it isn't called...
  1226 	    alignRelativeTo ( QPoint (absPos.x(),
  1227 							absPos.y()-(bboxTotal.height()-bbox.height())/2) );
  1228 	}
  1229 }
  1230 
  1231 
  1232 QRect BranchObj::getTotalBBox()
  1233 {
  1234 	QRect r=bbox;
  1235 
  1236 	if (scrolled) return r;
  1237 
  1238 	BranchObj* b;
  1239 	for (b=branch.first();b ;b=branch.next() )
  1240 		r=addBBox(b->getTotalBBox(),r);
  1241 
  1242 	FloatImageObj* fio;
  1243 	for (fio=floatimage.first();fio ;fio=floatimage.next() )
  1244 		r=addBBox(fio->getTotalBBox(),r);
  1245 		
  1246 	return r;
  1247 }
  1248 
  1249 QRect BranchObj::getBBoxSizeWithChilds()
  1250 {
  1251 	return bboxTotal;
  1252 }
  1253 
  1254 void BranchObj::calcBBoxSizeWithChilds()
  1255 {
  1256 	// This is called only from reposition and
  1257 	// and only for mapcenter. So it won't be
  1258 	// called more than once for a single user 
  1259 	// action
  1260 	
  1261 	// Calculate size of LMO including all childs (to align them later)
  1262 
  1263 	bboxTotal.setX(bbox.x() );
  1264 	bboxTotal.setY(bbox.y() );
  1265 
  1266 	// if branch is scrolled, ignore childs, but still consider floatimages
  1267 	if (scrolled)
  1268 	{
  1269 		bboxTotal.setWidth (bbox.width());
  1270 		bboxTotal.setHeight(bbox.height());
  1271 		return;
  1272 	}
  1273 	
  1274 	QRect r(0,0,0,0);
  1275 	QRect br;
  1276 	// Now calculate recursivly
  1277 	// sum of heights 
  1278 	// maximum of widths 
  1279 	// minimum of y
  1280 	BranchObj* b;
  1281 	for (b=branch.first();b ;b=branch.next() )
  1282 	{
  1283 		b->calcBBoxSizeWithChilds();
  1284 		br=b->getBBoxSizeWithChilds();
  1285 		r.setWidth( max (br.width(), r.width() ));
  1286 		r.setHeight(br.height() + r.height() );
  1287 		if (br.y()<bboxTotal.y()) bboxTotal.setY(br.y());
  1288 	}
  1289 	// Add myself and also
  1290 	// add width of link to sum if necessary
  1291 	if (branch.isEmpty())
  1292 		bboxTotal.setWidth (bbox.width() + r.width() );
  1293 	else	
  1294 		bboxTotal.setWidth (bbox.width() + r.width() + linkwidth);
  1295 	bboxTotal.setHeight(max (r.height(),  bbox.height() ) );
  1296 //	frame->setRect(QRect(bbox.x(),bbox.y(),bbox.width(),bbox.height() ) );
  1297 }
  1298 
  1299 void BranchObj::select()
  1300 {
  1301     LinkableMapObj::select();
  1302 	// Tell parent that I am selected now:
  1303 	BranchObj* po=(BranchObj*)(parObj);
  1304     if (po)	// TODO	    Try to get rid of this cast...
  1305         po->setLastSelectedBranch(this);
  1306 		
  1307 	// temporary unscroll, if we have scrolled parents somewhere
  1308 	if (parObj) ((BranchObj*)(parObj))->tmpUnscroll();
  1309 
  1310 	// set Text in Editor	
  1311 	textEditor->setText(note.getNote() );
  1312 	QString fnh=note.getFilenameHint();
  1313 	if (fnh!="")
  1314 		textEditor->setFilenameHint(note.getFilenameHint() );
  1315 	else	
  1316 		textEditor->setFilenameHint(getHeading() );
  1317 	textEditor->setFontHint (note.getFontHint() );
  1318 	connect (textEditor, SIGNAL (textHasChanged() ), this, SLOT (updateNoteFlag() ) ); 
  1319 	connect (textEditor, SIGNAL (fontSizeHasChanged() ), this, SLOT (updateNoteFlag() ) ); 
  1320 
  1321 	// Show URL and link in statusbar
  1322 	QString status;
  1323 	if (!url.isEmpty()) status+="URL: "+url+"  ";
  1324 	if (!vymLink.isEmpty()) status+="Link: "+vymLink;
  1325 	if (!status.isEmpty()) mainWindow->statusMessage (status);
  1326 
  1327 	// Update Toolbar
  1328 	standardFlags->updateToolbar();
  1329 
  1330 	// Update Browserbutton
  1331 	if (!url.isEmpty())
  1332 		actionEditOpenURL->setEnabled (true);
  1333 	else	
  1334 		actionEditOpenURL->setEnabled (false);
  1335 
  1336 	// Update actions in mapeditor
  1337 	mapEditor->updateActions();
  1338 }
  1339 
  1340 void BranchObj::unselect()
  1341 {
  1342 	LinkableMapObj::unselect();
  1343 	// Delete any messages like vymLink in StatusBar
  1344 	mainWindow->statusMessage ("");
  1345 
  1346 	// save note from editor and set flag
  1347 	// text is done by updateNoteFlag(), just save
  1348 	// filename here
  1349 	note.setFilenameHint (textEditor->getFilename());
  1350 
  1351 	// reset temporary unscroll, if we have scrolled parents somewhere
  1352 	if (parObj) ((BranchObj*)(parObj))->resetTmpUnscroll();
  1353 
  1354 	// Disconnect textEditor from this LMO
  1355 	disconnect( textEditor, SIGNAL(textHasChanged()), 0, 0 );
  1356 	disconnect( textEditor, SIGNAL (fontSizeHasChanged()),0,0 ); 
  1357 
  1358 	// Erase content of editor 
  1359 	textEditor->setInactive();
  1360 
  1361 	// unselect all buttons in toolbar
  1362 	standardFlagsDefault->updateToolbar();
  1363 }
  1364 
  1365 QString BranchObj::getSelectString()
  1366 {
  1367 	QString s;
  1368 	if (parObj)
  1369 	{
  1370 		if (depth==1)
  1371 			s= "bo:" + QString("%1").arg(getNum());
  1372 		else	
  1373 			s= ((BranchObj*)(parObj))->getSelectString() + ",bo:" + QString("%1").arg(getNum());
  1374 	} else
  1375 		s="mc:";
  1376 	return s;
  1377 }
  1378