branchobj.cpp
author insilmaril
Mon, 18 Apr 2005 06:17:00 +0000
changeset 95 f688a9913724
parent 94 6783e13bb05d
child 97 0b048b6bb6f4
permissions -rw-r--r--
added basic xLink functions
     1 #include "branchobj.h"
     2 #include "texteditor.h"
     3 #include "mapeditor.h"
     4 #include "mainwindow.h"
     5 
     6 extern TextEditor *textEditor;
     7 extern Main *mainWindow;
     8 extern FlagRowObj *standardFlagsDefault;
     9 extern QAction *actionEditOpenURL;
    10 
    11 
    12 /////////////////////////////////////////////////////////////////
    13 // BranchObj
    14 /////////////////////////////////////////////////////////////////
    15 
    16 BranchObj* BranchObj::itLast=NULL;
    17 
    18 
    19 BranchObj::BranchObj () :OrnamentedObj()
    20 {
    21 //    cout << "Const BranchObj ()\n";
    22     setParObj (this);	
    23     init();
    24     depth=-1;
    25 }
    26 
    27 BranchObj::BranchObj (QCanvas* c):OrnamentedObj (c)
    28 {
    29 //    cout << "Const BranchObj (c)  called from MapCenterObj (c)\n";
    30 	parObj=NULL;
    31     canvas=c;
    32 }
    33 
    34 BranchObj::BranchObj (QCanvas* c, LinkableMapObj* p):OrnamentedObj (c)
    35 {
    36 //    cout << "Const BranchObj (c,p)\n";
    37     canvas=c;
    38     setParObj (p);	
    39     depth=p->getDepth()+1;
    40 	if (depth==1)
    41 		// Calc angle to mapCenter if I am a mainbranch
    42 		// needed for reordering the mainbranches clockwise 
    43 		// around mapcenter 
    44 		angle=getAngle (QPoint ((int)(x() - parObj->getChildPos().x() ), 
    45 								(int)(y() - parObj->getChildPos().y() ) ) );
    46     init();
    47 }
    48 
    49 BranchObj::~BranchObj ()
    50 {
    51 //	cout << "Destr BranchObj of "<<this<<endl;
    52 	// Check, if this branch was the last child to be deleted
    53 	// If so, unset the scrolled flags
    54 
    55 	BranchObj *po=(BranchObj*)(parObj);
    56 	BranchObj *bo;
    57 	if (po)
    58 	{
    59 		bo=((BranchObj*)(parObj))->getLastBranch();
    60 		if (!bo) po->unScroll();
    61 	}
    62 	clear();
    63 }
    64 
    65 bool BranchObj::operator< ( const BranchObj & other )
    66 {
    67     return  angle < other.angle;
    68 }
    69 
    70 bool BranchObj::operator== ( const BranchObj & other )
    71 {
    72     return angle == other.angle;
    73 }
    74 
    75 int BranchObjPtrList::compareItems ( QPtrCollection::Item i, QPtrCollection::Item j)
    76 {
    77 	// Make sure PtrList::find works
    78 	if (i==j) return 0;
    79 
    80 	if ( ((BranchObj*)(i))->angle > ((BranchObj*)(j))->angle )
    81 		return 1;
    82 	else
    83 		return -1;
    84 }
    85 
    86 void BranchObj::init () 
    87 {
    88     branch.setAutoDelete (false);
    89     floatimage.setAutoDelete (true);
    90     xlink.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 (!xlink.isEmpty())
   145 		deleteXLink (xlink.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 	// keep current pointer in branch, 
   167 	// otherwise save might fail
   168 	int cur=branch.at();
   169 	int ind=branch.findRef (bo);
   170 	branch.at(cur);
   171 	return ind;
   172 }
   173 
   174 int BranchObj::getFloatImageNum(FloatImageObj *fio)
   175 {
   176 	return floatimage.findRef (fio);
   177 }
   178 
   179 int BranchObj::countBranches()
   180 {
   181 	return branch.count();
   182 }
   183 
   184 int BranchObj::countFloatImages()
   185 {
   186 	return floatimage.count();
   187 }
   188 
   189 int BranchObj::countXLinks()
   190 {
   191 	return xlink.count();
   192 }
   193 
   194 void BranchObj::setParObjTmp(LinkableMapObj* lmo, QPoint m, int off)
   195 {
   196 	// Temporary link to lmo
   197 	// m is position of mouse pointer 
   198 	// offset 0: default 1: below lmo   -1 above lmo  (if possible)
   199 
   200 
   201 	BranchObj* o=(BranchObj*)(lmo);
   202 	if (!parObjTmpBuf) 
   203 		parObjTmpBuf=parObj;
   204 
   205 	// ignore mapcenter and mainbranch
   206 	if (lmo->getDepth()<2) off=0;
   207 	if (off==0)
   208 	{
   209 		link2ParPos=false;
   210 	//	parObj=o;
   211 	}	
   212 	else
   213 	{	
   214 		link2ParPos=true;
   215 	//	if (off>0)
   216 	//		parObj=o->getParObj();
   217 	//	else	
   218 	//		parObj=o->getParObj();
   219 	//	parObj=o;
   220 	}		
   221 	parObj=o;
   222 
   223 	depth=parObj->getDepth()+1;
   224 
   225 	// setLinkStyle calls updateLink, only set it once
   226 	if (style!=getDefLinkStyle() ) setLinkStyle (getDefLinkStyle());
   227 
   228 	// Move temporary to new position at destination
   229 	// Usually the positioning would be done by reposition(),
   230 	// but then also the destination branch would "Jump" around...
   231 	// Better just do it approximately
   232 	if (depth==1)
   233 	{	// new parent is the mapcenter itself
   234 
   235 		QPoint p= normalise ( QPoint (m.x() - o->getChildPos().x(),
   236 									  m.y() - o->getChildPos().y() ));
   237 		if (p.x()<0) p.setX( p.x()-bbox.width() );
   238 		move2RelPos (p);
   239 	} else
   240 	{	
   241 		int y;
   242 		if (off==0)
   243 		{
   244 			// new parent is just a branch, link to it
   245 			QRect t=o->getBBoxSizeWithChilds();
   246 			if (o->getLastBranch())
   247 				y=t.y() + t.height() ;
   248 			else
   249 				y=t.y();
   250 
   251 		} else
   252 		{
   253 			if (off<0)
   254 				// we want to link above lmo
   255 				y=o->y() - height() + 5;
   256 			else	
   257 				// we want to link below lmo
   258 				// Bottom of sel should be 5 pixels above
   259 				// the bottom of the branch _below_ the target:
   260 				// Don't try to find that branch, guess 12 pixels
   261 				y=o->getChildPos().y()  -height() + 12; 
   262 		}	
   263 		if (o->getOrientation()==OrientLeftOfCenter)
   264 			move ( o->getChildPos().x() - linkwidth, y );
   265 		else	
   266 			move (o->getChildPos().x() + linkwidth, y );
   267 	}	
   268 
   269 	// updateLink is called implicitly in move
   270 	reposition();	// FIXME shouldn't be this a request?
   271 }
   272 
   273 void BranchObj::unsetParObjTmp()
   274 {
   275 	if (parObjTmpBuf) 
   276 	{
   277 		link2ParPos=false;
   278 		parObj=parObjTmpBuf;
   279 		parObjTmpBuf=NULL;
   280 		depth=parObj->getDepth()+1;
   281 		setLinkStyle (getDefLinkStyle() );
   282 		updateLink();
   283 	}		
   284 }
   285 
   286 void BranchObj::unScroll()
   287 {
   288 	if (tmpUnscrolled) resetTmpUnscroll();
   289 	if (scrolled) toggleScroll();
   290 }
   291 
   292 void BranchObj::toggleScroll()
   293 {
   294 	BranchObj *bo;
   295 	if (scrolled)
   296 	{
   297 		scrolled=false;
   298 		systemFlags->deactivate("scrolledright");
   299 		for (bo=branch.first(); bo; bo=branch.next() )
   300 		{
   301 			bo->setVisibility(true);
   302 		}
   303 	} else
   304 	{
   305 		scrolled=true;
   306 		systemFlags->activate("scrolledright");
   307 		for (bo=branch.first(); bo; bo=branch.next() )
   308 		{
   309 			bo->setVisibility(false);
   310 		}
   311 	}
   312 	calcBBoxSize();
   313 	positionBBox();	
   314 	move (absPos.x(), absPos.y() );
   315 	forceReposition();
   316 }
   317 
   318 bool BranchObj::isScrolled()
   319 {
   320 	return scrolled;
   321 }
   322 
   323 bool BranchObj::hasScrolledParent(BranchObj *start)
   324 {
   325 	// Calls parents recursivly to
   326 	// find out, if we are scrolled at all.
   327 	// But ignore myself, just look at parents.
   328 
   329 	if (this !=start && scrolled) return true;
   330 
   331 	BranchObj* bo=(BranchObj*)(parObj);
   332 	if (bo) 
   333 		return bo->hasScrolledParent(start);
   334 	else
   335 		return false;
   336 }
   337 
   338 void BranchObj::tmpUnscroll()
   339 {
   340 	// Unscroll parent (recursivly)
   341 	BranchObj* bo=(BranchObj*)(parObj);
   342 	if (bo) bo->tmpUnscroll();
   343 		
   344 	// Unscroll myself
   345 	if (scrolled)
   346 	{
   347 		tmpUnscrolled=true;
   348 		systemFlags->activate("tmpUnscrolledright");
   349 		toggleScroll();
   350 	}	
   351 }
   352 
   353 void BranchObj::resetTmpUnscroll()
   354 {
   355 	// Unscroll parent (recursivly)
   356 	BranchObj* bo=(BranchObj*)(parObj);
   357 	if (bo)
   358 		bo->resetTmpUnscroll();
   359 		
   360 	// Unscroll myself
   361 	if (tmpUnscrolled)
   362 	{
   363 		tmpUnscrolled=false;
   364 		systemFlags->deactivate("tmpUnscrolledright");
   365 		toggleScroll();
   366 	}	
   367 }
   368 
   369 void BranchObj::setVisibility(bool v, int toDepth)
   370 {
   371     if (depth <= toDepth)
   372     {
   373 		frame->setVisibility(v);
   374 		heading->setVisibility(v);
   375 		systemFlags->setVisibility(v);
   376 		standardFlags->setVisibility(v);
   377 		LinkableMapObj::setVisibility (v);
   378 		
   379 		if (!scrolled && (depth < toDepth))
   380 		{
   381 			// Now go recursivly through all childs
   382 			BranchObj* b;
   383 			for (b=branch.first(); b;b=branch.next() ) 
   384 				b->setVisibility (v,toDepth);	
   385 			FloatImageObj *fio;
   386 			for (fio=floatimage.first(); fio; fio=floatimage.next())
   387 				fio->setVisibility (v);
   388 			XLinkObj* xlo;
   389 			for (xlo=xlink.first(); xlo;xlo=xlink.next() ) 
   390 				xlo->setVisibility ();	
   391 		}
   392     } // depth <= toDepth	
   393 	requestReposition();
   394 }	
   395 
   396 void BranchObj::setVisibility(bool v)
   397 {
   398     setVisibility (v,MAX_DEPTH);
   399 }
   400 
   401 
   402 void BranchObj::setLinkColor ()
   403 {
   404 	// Overloaded from LinkableMapObj
   405 	// BranchObj can use color of heading
   406 
   407 	if (mapEditor->getLinkColorHint()==HeadingColor)
   408 		LinkableMapObj::setLinkColor (heading->getColor() );
   409 	else	
   410 		LinkableMapObj::setLinkColor ();
   411 }
   412 
   413 void BranchObj::setColor (QColor col, bool colorChilds)
   414 {
   415     heading->setColor(col);
   416 	setLinkColor();
   417     if (colorChilds) 
   418     {
   419 		BranchObj *bo;
   420 		for (bo=branch.first(); bo; bo=branch.next() )
   421 			bo->setColor(col,colorChilds);
   422     }	
   423 }
   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     // Adjusting font size
   663     QFont font=heading->getFont();
   664 	if (depth==0)
   665 		font.setPointSize(16);
   666 	else	
   667 		if (depth>1) 
   668 			font.setPointSize(10);
   669 		else
   670 			font.setPointSize(12);
   671     heading->setFont(font);
   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 int BranchObj::countXLink()
   832 {
   833 	return xlink.count();
   834 }
   835 
   836 BranchObj* BranchObj::XLinkTargetAt (int i)
   837 {
   838 	if (xlink.at(i))
   839 		return xlink.at(i)->otherBranch (this);
   840 	else
   841 		return NULL;
   842 }
   843 
   844 LinkableMapObj* BranchObj::addFloatImage ()
   845 {
   846 	FloatImageObj *newfi=new FloatImageObj (canvas,this);
   847 	floatimage.append (newfi);
   848 	if (hasScrolledParent(this) )
   849 		newfi->setVisibility (false);
   850 	else	
   851 		newfi->setVisibility(visible);
   852 	requestReposition();
   853 	return newfi;
   854 }
   855 
   856 LinkableMapObj* BranchObj::addFloatImage (FloatImageObj *fio)
   857 {
   858 	FloatImageObj *newfi=new FloatImageObj (canvas,this);
   859 	floatimage.append (newfi);
   860 	newfi->copy (fio);
   861 	if (hasScrolledParent(this) )
   862 		newfi->setVisibility (false);
   863 	else	
   864 		newfi->setVisibility(visible);
   865 	requestReposition();
   866 	return newfi;
   867 }
   868 
   869 FloatImageObj* BranchObj::getFirstFloatImage ()
   870 {
   871     return floatimage.first();
   872 }
   873 
   874 FloatImageObj* BranchObj::getLastFloatImage ()
   875 {
   876     return floatimage.last();
   877 }
   878 
   879 FloatImageObj* BranchObj::getFloatImageNum (const uint &i)
   880 {
   881     return floatimage.at(i);
   882 }
   883 
   884 void BranchObj::removeFloatImage (FloatImageObj *fio)
   885 {
   886 	floatimage.remove (fio);
   887 	requestReposition();
   888 }
   889 
   890 void BranchObj::savePosInAngle ()
   891 {
   892 	// Save position in angle
   893     BranchObj *b;
   894 	int i=0;
   895     for (b=branch.first(); b; b=branch.next() )
   896 	{
   897 		b->angle=i;
   898 		i++;
   899 	}
   900 }
   901 
   902 BranchObj* BranchObj::addBranch()
   903 {
   904     BranchObj* newbo=new BranchObj(canvas,this);
   905     branch.append (newbo);
   906     newbo->setParObj(this);
   907     newbo->setColor(getColor(),false);	
   908     newbo->setLinkColor();	
   909     newbo->setHeading ("new");
   910 	newbo->setLinkStyle (newbo->getDefLinkStyle());
   911 	if (scrolled)
   912 		newbo->setVisibility (false);
   913 	else	
   914 		newbo->setVisibility(visible);
   915 	newbo->updateLink();	
   916 	requestReposition();
   917 	return newbo;
   918 }
   919 
   920 BranchObj* BranchObj::addBranch(BranchObj* bo)
   921 {
   922     BranchObj* newbo=new BranchObj(canvas,this);
   923     branch.append (newbo);
   924     newbo->copy(bo);
   925     newbo->setParObj(this);
   926 	newbo->setHeading (newbo->getHeading());	// adjust fontsize to depth
   927 	newbo->setLinkStyle (newbo->getDefLinkStyle());
   928 	if (scrolled)
   929 		newbo->setVisibility (false);
   930 	else	
   931 		newbo->setVisibility(bo->visible);
   932 	newbo->updateLink();	
   933 	requestReposition();
   934 	return newbo;
   935 }
   936 
   937 BranchObj* BranchObj::addBranchPtr(BranchObj* bo)
   938 {
   939 	branch.append (bo);
   940 	bo->setParObj (this);
   941 	bo->depth=depth+1;
   942 	bo->setLinkStyle (bo->getDefLinkStyle() );
   943 	if (scrolled) tmpUnscroll();
   944 	setLastSelectedBranch (bo);
   945 	return bo;
   946 }
   947 
   948 BranchObj* BranchObj::insertBranch(int pos)
   949 {
   950 	savePosInAngle();
   951 	// Add new bo and resort branches
   952 	BranchObj *newbo=addBranch ();
   953 	newbo->angle=pos-0.5;
   954 	branch.sort();
   955 	return newbo;
   956 }
   957 
   958 BranchObj* BranchObj::insertBranch(BranchObj* bo, int pos)
   959 {
   960 	savePosInAngle();
   961 	// Add new bo and resort branches
   962 	bo->angle=pos-0.5;
   963 	BranchObj *newbo=addBranch (bo);
   964 	branch.sort();
   965 	return newbo;
   966 }
   967 
   968 BranchObj* BranchObj::insertBranchPtr (BranchObj* bo, int pos)
   969 {
   970 	savePosInAngle();
   971 	// Add new bo and resort branches
   972 	bo->angle=pos-0.5;
   973 	branch.append (bo);
   974 	bo->setParObj (this);
   975 	bo->depth=depth+1;
   976 	bo->setLinkStyle (bo->getDefLinkStyle() );
   977 	if (scrolled) tmpUnscroll();
   978 	setLastSelectedBranch (bo);
   979 	branch.sort();
   980 	return bo;
   981 }
   982 
   983 void BranchObj::removeBranch(BranchObj* bo)
   984 {
   985     // if bo is not in branch remove returns false, we
   986     // don't care...
   987 	
   988     if (branch.remove (bo))
   989 		delete (bo);
   990 	else
   991 		qWarning ("BranchObj::removeBranch tried to remove non existing branch?!\n");
   992 	requestReposition();
   993 }
   994 
   995 void BranchObj::removeBranchPtr(BranchObj* bo)
   996 {
   997 	branch.remove (bo);
   998 	requestReposition();
   999 }
  1000 
  1001 void BranchObj::setLastSelectedBranch (BranchObj* bo)
  1002 {
  1003     lastSelectedBranch=branch.find(bo);
  1004 }
  1005 
  1006 BranchObj* BranchObj::getLastSelectedBranch ()
  1007 {
  1008     if (lastSelectedBranch>=0) 
  1009 	{
  1010 		BranchObj* bo=branch.at(lastSelectedBranch);
  1011 		if (bo) return bo;
  1012     }	
  1013     return branch.first();
  1014 }
  1015 
  1016 BranchObj* BranchObj::getFirstBranch ()
  1017 {
  1018     return branch.first();
  1019 }
  1020 
  1021 BranchObj* BranchObj::getLastBranch ()
  1022 {
  1023     return branch.last();
  1024 }
  1025 
  1026 BranchObj* BranchObj::getBranchNum (const uint &i)
  1027 {
  1028     return branch.at(i);
  1029 }
  1030 
  1031 
  1032 BranchObj* BranchObj::moveBranchUp(BranchObj* bo1) // move a branch up (modify myself)
  1033 {
  1034 	savePosInAngle();
  1035     int i=branch.find(bo1);
  1036     if (i>0) 
  1037 	{	// -1 if bo1 not found 
  1038 		branch.at(i)->angle--;
  1039 		branch.at(i-1)->angle++;
  1040 		branch.sort();
  1041 		return branch.at(i-1);
  1042 	} else
  1043 		return branch.at(i);
  1044 }
  1045 
  1046 BranchObj* BranchObj::moveBranchDown(BranchObj* bo1)
  1047 {
  1048 	savePosInAngle();
  1049     int i=branch.find(bo1);
  1050 	int j;
  1051 	if (branch.next())
  1052 	{
  1053 		j = branch.at();
  1054 		branch.at(i)->angle++;
  1055 		branch.at(j)->angle--;
  1056 		branch.sort();
  1057 		return branch.at(j);
  1058 	} else
  1059 		return branch.at(i);
  1060 }
  1061 
  1062 BranchObj* BranchObj::moveBranchTo (BranchObj* dst, int pos)
  1063 {
  1064 	// Find current parent and 
  1065 	// remove pointer to myself there
  1066 
  1067 	if (!dst) return NULL;
  1068 
  1069 	BranchObj *par=(BranchObj*)(parObj);
  1070 	if (par)
  1071 		par->removeBranchPtr (this);
  1072 	else
  1073 		return NULL;
  1074 
  1075 	if (pos<0  || dst->getDepth()==1)
  1076 	{	
  1077 		// links adds myself as last branch at dst
  1078 		dst->addBranchPtr (this);
  1079 		return this;
  1080 	} else
  1081 	{
  1082 		// inserts me at pos in parent of dst
  1083 		par=(BranchObj*)(dst->getParObj());
  1084 		if (par)
  1085 			return par->insertBranchPtr (this,pos);
  1086 		else
  1087 			return NULL;
  1088 	}	
  1089 }
  1090 
  1091 void BranchObj::alignRelativeTo (QPoint ref)
  1092 {
  1093 /* FIXME testing
  1094 	if (!getHeading().isEmpty())
  1095 		cout << "BO::alignRelTo "<<getHeading()<<endl;
  1096 	else	
  1097 		cout << "BO::alignRelTo  ???"<<endl;
  1098 	cout << "  d="<<depth<<endl;
  1099 */	
  1100 	int th = bboxTotal.height();	
  1101 
  1102 	// If I am the mapcenter or a mainbranch, reposition heading
  1103 	if (depth<2)
  1104 	{
  1105 		move (absPos.x(),absPos.y());
  1106 		if (depth==1)
  1107 		{
  1108 			// Calc angle to mapCenter if I am a mainbranch
  1109 			// needed for reordering the mainbranches clockwise 
  1110 			// around mapcenter 
  1111 			angle=getAngle (QPoint ((int)(x() - parObj->getChildPos().x() ), 
  1112 									(int)(y() - parObj->getChildPos().y() ) ) );
  1113 		}	
  1114 	} 
  1115 	else
  1116     {
  1117 		// Align myself depending on orientation and parent, but
  1118 		// only if I am not the mainbranch or mapcenter itself
  1119 		switch (orientation) 
  1120 		{
  1121 			case OrientLeftOfCenter:
  1122 				move (ref.x()-bbox.width(), ref.y() + (th-bbox.height())/2 );
  1123 			break;
  1124 			case OrientRightOfCenter:	
  1125 				move (ref.x(), ref.y() + (th-bbox.height())/2 );
  1126 			break;
  1127 			default:
  1128 				cout <<"LMO::alignRelativeTo: oops, no orientation given...\n";
  1129 			break;
  1130 		}		
  1131     }		
  1132 
  1133 	FloatImageObj *fio;
  1134     for (fio=floatimage.first(); fio; fio=floatimage.next() )
  1135 		fio->reposition();
  1136 
  1137 	if (scrolled) return;
  1138 
  1139     // Set reference point for alignment of childs
  1140     QPoint ref2;
  1141     if (orientation==OrientLeftOfCenter)
  1142 		ref2.setX(childPos.x() - linkwidth);
  1143     else	
  1144 		ref2.setX(childPos.x() + linkwidth);
  1145 
  1146 	if (depth==1)
  1147 		ref2.setY(absPos.y()-(bboxTotal.height()-bbox.height())/2);
  1148 	else	
  1149 		ref2.setY(ref.y() );	
  1150 
  1151     // Align the childs depending on reference point 
  1152     BranchObj *b;
  1153     for (b=branch.first(); b; b=branch.next() )
  1154     {	
  1155 		b->alignRelativeTo (ref2);
  1156 		ref2.setY(ref2.y() + b->getBBoxSizeWithChilds().height() );
  1157     }
  1158 }
  1159 
  1160 
  1161 void BranchObj::reposition()
  1162 {	
  1163 /* FIXME testing
  1164 	if (!getHeading().isEmpty())
  1165 		cout << "BO::reposition  "<<getHeading()<<endl;
  1166 	else	
  1167 		cout << "BO::reposition  ???"<<endl;
  1168 */		
  1169 	if (depth==0)
  1170 	{
  1171 		// only calculate the sizes once. If the deepest LMO 
  1172 		// changes its height,
  1173 		// all upper LMOs have to change, too.
  1174 		calcBBoxSizeWithChilds();
  1175 	    alignRelativeTo ( QPoint (absPos.x(),
  1176 			absPos.y()-(bboxTotal.height()-bbox.height())/2) );
  1177 		branch.sort();	
  1178 	} else
  1179 	{
  1180 		// This is only important for moving branches:
  1181 		// For editing a branch it isn't called...
  1182 	    alignRelativeTo ( QPoint (absPos.x(),
  1183 							absPos.y()-(bboxTotal.height()-bbox.height())/2) );
  1184 	}
  1185 }
  1186 
  1187 
  1188 QRect BranchObj::getTotalBBox()
  1189 {
  1190 	QRect r=bbox;
  1191 
  1192 	if (scrolled) return r;
  1193 
  1194 	BranchObj* b;
  1195 	for (b=branch.first();b ;b=branch.next() )
  1196 		r=addBBox(b->getTotalBBox(),r);
  1197 
  1198 	FloatImageObj* fio;
  1199 	for (fio=floatimage.first();fio ;fio=floatimage.next() )
  1200 		r=addBBox(fio->getTotalBBox(),r);
  1201 		
  1202 	return r;
  1203 }
  1204 
  1205 QRect BranchObj::getBBoxSizeWithChilds()
  1206 {
  1207 	return bboxTotal;
  1208 }
  1209 
  1210 void BranchObj::calcBBoxSizeWithChilds()
  1211 {
  1212 	// This is called only from reposition and
  1213 	// and only for mapcenter. So it won't be
  1214 	// called more than once for a single user 
  1215 	// action
  1216 	
  1217 	// Calculate size of LMO including all childs (to align them later)
  1218 
  1219 	bboxTotal.setX(bbox.x() );
  1220 	bboxTotal.setY(bbox.y() );
  1221 
  1222 	// if branch is scrolled, ignore childs, but still consider floatimages
  1223 	if (scrolled)
  1224 	{
  1225 		bboxTotal.setWidth (bbox.width());
  1226 		bboxTotal.setHeight(bbox.height());
  1227 		return;
  1228 	}
  1229 	
  1230 	QRect r(0,0,0,0);
  1231 	QRect br;
  1232 	// Now calculate recursivly
  1233 	// sum of heights 
  1234 	// maximum of widths 
  1235 	// minimum of y
  1236 	BranchObj* b;
  1237 	for (b=branch.first();b ;b=branch.next() )
  1238 	{
  1239 		b->calcBBoxSizeWithChilds();
  1240 		br=b->getBBoxSizeWithChilds();
  1241 		r.setWidth( max (br.width(), r.width() ));
  1242 		r.setHeight(br.height() + r.height() );
  1243 		if (br.y()<bboxTotal.y()) bboxTotal.setY(br.y());
  1244 	}
  1245 	// Add myself and also
  1246 	// add width of link to sum if necessary
  1247 	if (branch.isEmpty())
  1248 		bboxTotal.setWidth (bbox.width() + r.width() );
  1249 	else	
  1250 		bboxTotal.setWidth (bbox.width() + r.width() + linkwidth);
  1251 	bboxTotal.setHeight(max (r.height(),  bbox.height() ) );
  1252 //	frame->setRect(QRect(bbox.x(),bbox.y(),bbox.width(),bbox.height() ) );
  1253 }
  1254 
  1255 void BranchObj::select()
  1256 {
  1257     LinkableMapObj::select();
  1258 	// Tell parent that I am selected now:
  1259 	BranchObj* po=(BranchObj*)(parObj);
  1260     if (po)	// TODO	    Try to get rid of this cast...
  1261         po->setLastSelectedBranch(this);
  1262 		
  1263 	// temporary unscroll, if we have scrolled parents somewhere
  1264 	if (parObj) ((BranchObj*)(parObj))->tmpUnscroll();
  1265 
  1266 	// set Text in Editor	
  1267 	textEditor->setText(note.getNote() );
  1268 	textEditor->setFilename(note.getFilenameHint() );
  1269 	textEditor->setFontHint (note.getFontHint() );
  1270 	connect (textEditor, SIGNAL (textHasChanged() ), this, SLOT (updateNoteFlag() ) ); 
  1271 	connect (textEditor, SIGNAL (fontSizeHasChanged() ), this, SLOT (updateNoteFlag() ) ); 
  1272 
  1273 	// Show URL and link in statusbar
  1274 	QString status;
  1275 	if (!url.isEmpty()) status+="URL: "+url+"  ";
  1276 	if (!vymLink.isEmpty()) status+="Link: "+vymLink;
  1277 	if (!status.isEmpty()) mainWindow->statusMessage (status);
  1278 
  1279 	// Update Toolbar
  1280 	standardFlags->updateToolBar();
  1281 
  1282 	// Update Browserbutton
  1283 	if (!url.isEmpty())
  1284 		actionEditOpenURL->setEnabled (true);
  1285 	else	
  1286 		actionEditOpenURL->setEnabled (false);
  1287 
  1288 	// Update actions in mapeditor
  1289 	mapEditor->updateActions();
  1290 }
  1291 
  1292 void BranchObj::unselect()
  1293 {
  1294 	LinkableMapObj::unselect();
  1295 	// Delete any messages like vymLink in StatusBar
  1296 	mainWindow->statusMessage ("");
  1297 
  1298 	// save note from editor and set flag
  1299 	// text is done by updateNoteFlag(), just save
  1300 	// filename here
  1301 	note.setFilenameHint (textEditor->getFilename());
  1302 
  1303 	// reset temporary unscroll, if we have scrolled parents somewhere
  1304 	if (parObj) ((BranchObj*)(parObj))->resetTmpUnscroll();
  1305 
  1306 	// Disconnect textEditor from this LMO
  1307 	disconnect( textEditor, SIGNAL(textHasChanged()), 0, 0 );
  1308 	disconnect( textEditor, SIGNAL (fontSizeHasChanged()),0,0 ); 
  1309 
  1310 	// Erase content of editor 
  1311 	textEditor->setInactive();
  1312 
  1313 	// unselect all buttons in toolbar
  1314 	standardFlagsDefault->updateToolBar();
  1315 }
  1316 
  1317 QString BranchObj::getSelectString()
  1318 {
  1319 	QString s;
  1320 	if (parObj)
  1321 	{
  1322 		if (depth==1)
  1323 			s= "bo:" + QString("%1").arg(getNum());
  1324 		else	
  1325 			s= ((BranchObj*)(parObj))->getSelectString() + ",bo:" + QString("%1").arg(getNum());
  1326 	} else
  1327 		s="mc:";
  1328 	return s;
  1329 }
  1330