branchobj.cpp
author jhilmer
Mon, 01 Aug 2005 19:33:16 +0000
changeset 144 f4bbdc809fec
parent 139 087e60400acc
child 146 1b52ff19aad5
permissions -rw-r--r--
Fix: closing of editor when text are modified
     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 QColor BranchObj::getColor()
   426 {
   427 	return heading->getColor();
   428 }
   429 
   430 BranchObj* BranchObj::first()
   431 {
   432 	itLast=NULL;	
   433 	return this; 
   434 }
   435 	
   436 BranchObj* BranchObj::next()
   437 {
   438 	BranchObj *lmo;
   439 	BranchObj *bo=branch.first();
   440 	BranchObj *po=(BranchObj*)(parObj);
   441 
   442 	if (!itLast)
   443 	{	// We are just beginning at the mapCenter
   444 		if (bo) 
   445 		{
   446 			itLast=this;
   447 			return bo;
   448 		}	
   449 		else
   450 		{
   451 			itLast=NULL;
   452 			return NULL;
   453 		}	
   454 	}
   455 
   456 	if (itLast==parObj)
   457 	{	// We come from above
   458 		if (bo)
   459 		{
   460 			// there are childs, go there
   461 			itLast=this;
   462 			return bo;
   463 		}	
   464 		else
   465 		{	// no childs, try to go up again
   466 			if (po)
   467 			{
   468 				// go up
   469 				itLast=this;
   470 				lmo=po->next();
   471 				itLast=this;
   472 				return lmo;
   473 
   474 			}	
   475 			else
   476 			{
   477 				// can't go up, I am mapCenter
   478 				itLast=NULL;
   479 				return NULL;
   480 			}	
   481 		}
   482 	}
   483 
   484 	// Try to find last child, we came from, in my own childs
   485 	bool searching=true;
   486 	while (bo && searching)
   487 	{
   488 		if (itLast==bo) searching=false;
   489 		bo=branch.next();
   490 	}
   491 	if (!searching)
   492 	{	// found lastLMO in my childs
   493 		if (bo)
   494 		{
   495 			// found a brother of lastLMO 
   496 			itLast=this;
   497 			return bo;
   498 		}	
   499 		else
   500 		{
   501 			if (po)
   502 			{
   503 				// go up
   504 				itLast=this;
   505 				lmo=po->next();
   506 				itLast=this;
   507 				return lmo;
   508 			}
   509 			else
   510 			{
   511 				// can't go up, I am mapCenter
   512 				itLast=NULL;
   513 				return NULL;
   514 			}	
   515 		}
   516 	}
   517 
   518 	// couldn't find last child, it must be a nephew of mine
   519 	bo=branch.first();
   520 	if (bo)
   521 	{
   522 		// proceed with my first child
   523 		itLast=this;	
   524 		return bo;
   525 	}	
   526 	else
   527 	{
   528 		// or go back to my parents
   529 		if (po)
   530 		{
   531 			// go up
   532 			itLast=this;
   533 			lmo=po->next();
   534 			itLast=this;
   535 			return lmo;
   536 		}	
   537 		else
   538 		{
   539 			// can't go up, I am mapCenter
   540 			itLast=NULL;
   541 			return NULL;
   542 		}	
   543 	}	
   544 }
   545 
   546 BranchObj* BranchObj::getLastIterator()
   547 {
   548 	return itLast;
   549 }
   550 
   551 void BranchObj::setLastIterator(BranchObj* it)
   552 {
   553 	itLast=it;
   554 }
   555 
   556 
   557 void BranchObj::move (double x, double y)
   558 {
   559 	OrnamentedObj::move (x,y);
   560     positionBBox();
   561 }
   562 
   563 void BranchObj::move (QPoint p)
   564 {
   565 	move (p.x(), p.y());
   566 }
   567 
   568 void BranchObj::moveBy (double x, double y)
   569 {
   570 	OrnamentedObj::moveBy (x,y);
   571     positionBBox();
   572     BranchObj* b;
   573     for (b=branch.first(); b;b=branch.next() ) 
   574 		b->moveBy (x,y);
   575 }
   576 	
   577 void BranchObj::moveBy (QPoint p)
   578 {
   579 	moveBy (p.x(), p.y());
   580 }
   581 
   582 
   583 void BranchObj::positionBBox()
   584 {
   585 	// FIXME testing (optimization)
   586 	/*
   587 	QString h=getHeading();
   588 	if (!h.isEmpty())
   589 		cout << "BO::positionBBox("<<h<<")\n";
   590 	else	
   591 		cout << "BO::positionBBox (noHeading)\n";
   592 	*/	
   593 
   594     heading->positionBBox();
   595 	systemFlags->positionBBox();
   596 	standardFlags->positionBBox();
   597 	// It seems that setting x,y also affects width,height
   598 	int w_old=bbox.width();
   599 	int h_old=bbox.height();
   600     bbox.setX (absPos.x() );
   601 	bbox.setY (absPos.y() );
   602 	bbox.setWidth(w_old);
   603 	bbox.setHeight(h_old);
   604 	
   605 	setSelBox();
   606 
   607 	// set the frame
   608 	frame->setRect(QRect(bbox.x(),bbox.y(),bbox.width(),bbox.height() ) );
   609 
   610 	// Update links to other branches
   611 	XLinkObj *xlo;
   612     for (xlo=xlink.first(); xlo; xlo=xlink.next() )
   613 		xlo->updateXLink();
   614 }
   615 
   616 void BranchObj::calcBBoxSize()
   617 {
   618     QSize heading_r=heading->getSize();
   619     int heading_w=static_cast <int> (heading_r.width() );
   620     int heading_h=static_cast <int> (heading_r.height() );
   621     QSize sysflags_r=systemFlags->getSize();
   622 	int sysflags_h=sysflags_r.height();
   623 	int sysflags_w=sysflags_r.width();
   624     QSize stanflags_r=standardFlags->getSize();
   625 	int stanflags_h=stanflags_r.height();
   626 	int stanflags_w=stanflags_r.width();
   627     int w;
   628     int h;
   629 
   630 	// set width to sum of all widths
   631 	w=heading_w + sysflags_w + stanflags_w;
   632 	// set height to maximum needed height
   633 	h=max (sysflags_h,stanflags_h);
   634 	h=max (h,heading_h);
   635 
   636     w+=frame->getBorder();
   637     h+=frame->getBorder();
   638     bbox.setSize (QSize (w,h));
   639 }
   640 
   641 LinkableMapObj* BranchObj::findMapObj(QPoint p, LinkableMapObj* excludeLMO)
   642 {
   643 	// Search branches
   644     BranchObj *b;
   645     LinkableMapObj *lmo;
   646     for (b=branch.first(); b; b=branch.next() )
   647     {	
   648 		lmo=b->findMapObj(p, excludeLMO);
   649 		if (lmo != NULL) return lmo;
   650     }
   651 	
   652 	// Search myself
   653     if (inBBox (p) && (this != excludeLMO) && isVisibleObj() ) 
   654 		return this;
   655 
   656 	// Search float images
   657 	FloatImageObj *foi;
   658     for (foi=floatimage.first(); foi; foi=floatimage.next() )
   659 		if (foi->inBBox(p) && (foi != excludeLMO) && foi->getParObj()!= excludeLMO) return foi;
   660 
   661     return NULL;
   662 }
   663 
   664 int BranchObj::getDefHeadingSize()
   665 {
   666 	if (depth==0)
   667 		return 16;
   668 	else	
   669 		if (depth==1) 
   670 			return 12;
   671 		else
   672 			return 10;
   673 }
   674 
   675 void BranchObj::setHeading(QString s)
   676 {
   677     // Adjusting font size
   678     QFont font=heading->getFont();
   679 	font.setPointSize(getDefHeadingSize() );
   680     heading->setFont(font);
   681     heading->setText(s);	// set new heading
   682 	calcBBoxSize();			// recalculate bbox
   683     positionBBox();			// rearrange contents
   684 	requestReposition();
   685 }
   686 
   687 void BranchObj::setURL(QString s)
   688 {
   689 	url=s;
   690 	if (!url.isEmpty())
   691 		systemFlags->activate("url");
   692 	else	
   693 		systemFlags->deactivate("url");
   694 	calcBBoxSize();			// recalculate bbox
   695     positionBBox();			// rearrange contents
   696 	forceReposition();
   697 }
   698 
   699 QString BranchObj::getURL()
   700 {
   701 	return url;
   702 }
   703 
   704 void BranchObj::setVymLink(QString s)
   705 {
   706 	if (!s.isEmpty())
   707 	{
   708 		// We need the relative (from loading) 
   709 		// or absolute path (from User event)
   710 		// and build the absolute path.
   711 		// Note: If we have relative, use path of
   712 		// current map to build absolute path
   713 		QDir d(s);
   714 		if (!d.path().startsWith ("/"))
   715 		{
   716 			QString p=mapEditor->getDestPath();
   717 			int i=p.findRev("/",-1);
   718 			d.setPath(p.left(i)+"/"+s);
   719 			d.convertToAbs();
   720 		}
   721 		vymLink=d.path();
   722 		systemFlags->activate("vymLink");
   723 	}	
   724 	else	
   725 	{
   726 		systemFlags->deactivate("vymLink");
   727 		vymLink="";
   728 	}	
   729 	calcBBoxSize();			// recalculate bbox
   730     positionBBox();			// rearrange contents
   731 	forceReposition();
   732 }
   733 
   734 QString BranchObj::getVymLink()
   735 {
   736 	return vymLink;
   737 }
   738 
   739 QString BranchObj::saveToDir (const QString &tmpdir,const QString &prefix, const QPoint& offset)
   740 {
   741     QString s,a;
   742 	QString scrolledAttr;
   743 	if (scrolled) 
   744 		scrolledAttr=attribut ("scrolled","yes");
   745 	else
   746 		scrolledAttr="";
   747 
   748 	QString posAttr;
   749 	if (depth<2) posAttr=
   750 		attribut("absPosX",QString().setNum(absPos.x(),10)) +
   751 		attribut("absPosY",QString().setNum(absPos.y(),10)); 
   752 	else
   753 		posAttr="";
   754 
   755 	QString urlAttr;
   756 	if (!url.isEmpty())
   757 		urlAttr=attribut ("url",url);
   758 
   759 	QString vymLinkAttr;
   760 	if (!vymLink.isEmpty())
   761 		vymLinkAttr=attribut ("vymLink",convertToRel(mapEditor->getDestPath(),vymLink) );
   762 
   763 	QString frameAttr;
   764 	if (frame->getFrameType()!=NoFrame)
   765 		frameAttr=attribut ("frameType",frame->getFrameTypeName());
   766 	else
   767 		frameAttr="";
   768 
   769 	// save area, if not scrolled
   770 	QString areaAttr;
   771 	if (!((BranchObj*)(parObj))->isScrolled() )
   772 	{
   773 		areaAttr=
   774 			attribut("x1",QString().setNum(absPos.x()-offset.x(),10)) +
   775 			attribut("y1",QString().setNum(absPos.y()-offset.y(),10)) +
   776 			attribut("x2",QString().setNum(absPos.x()+width()-offset.x(),10)) +
   777 			attribut("y2",QString().setNum(absPos.y()+height()-offset.y(),10));
   778 
   779 	} else
   780 		areaAttr="";
   781 	
   782     s=beginElement ("branch" +scrolledAttr +posAttr +urlAttr +vymLinkAttr +frameAttr +areaAttr );
   783     incIndent();
   784 
   785 	// save heading
   786     s=s+valueElement("heading", getHeading(),
   787 		attribut ("textColor",QColor(heading->getColor()).name()));
   788 
   789 	// save names of flags set
   790 	s+=standardFlags->saveToDir(tmpdir,prefix,0);
   791 	
   792 	// save note
   793 	if (!note.isEmpty() )
   794 		s+=note.saveToDir();
   795 	
   796 	// Save branches
   797     BranchObj *bo;
   798     for (bo=branch.first(); bo; bo=branch.next() )
   799 		s+=bo->saveToDir(tmpdir,prefix,offset);
   800 
   801 	// Save FloatImages
   802 	FloatImageObj *fio;
   803 	for (fio=floatimage.first(); fio; fio=floatimage.next() )
   804 		s+=fio->saveToDir (tmpdir,prefix);
   805 
   806 	// Save XLinks
   807 	XLinkObj *xlo;
   808     for (xlo=xlink.first(); xlo; xlo=xlink.next() )
   809 		s+=xlo->saveToDir();
   810 
   811     decIndent();
   812     s+=endElement   ("branch");
   813     return s;
   814 }
   815 
   816 void BranchObj::addXLink (XLinkObj *xlo)
   817 {
   818 	xlink.append (xlo);
   819 	
   820 }
   821 
   822 void BranchObj::removeXLinkRef (XLinkObj *xlo)
   823 {
   824 	xlink.remove (xlo);
   825 }
   826 
   827 void BranchObj::deleteXLink(XLinkObj *xlo)
   828 {
   829 	xlo->deactivate();
   830 	if (!xlo->isUsed()) delete (xlo);
   831 }
   832 
   833 void BranchObj::deleteXLinkAt (int i)
   834 {
   835 	XLinkObj *xlo=xlink.at(i);
   836 	xlo->deactivate();
   837 	if (!xlo->isUsed()) delete(xlo);
   838 }
   839 
   840 XLinkObj* BranchObj::XLinkAt (int i)
   841 {
   842 	return xlink.at(i);
   843 }
   844 
   845 int BranchObj::countXLink()
   846 {
   847 	return xlink.count();
   848 }
   849 
   850 BranchObj* BranchObj::XLinkTargetAt (int i)
   851 {
   852 	if (xlink.at(i))
   853 		return xlink.at(i)->otherBranch (this);
   854 	else
   855 		return NULL;
   856 }
   857 
   858 LinkableMapObj* BranchObj::addFloatImage ()
   859 {
   860 	FloatImageObj *newfi=new FloatImageObj (canvas,this);
   861 	floatimage.append (newfi);
   862 	if (hasScrolledParent(this) )
   863 		newfi->setVisibility (false);
   864 	else	
   865 		newfi->setVisibility(visible);
   866 	requestReposition();
   867 	return newfi;
   868 }
   869 
   870 LinkableMapObj* BranchObj::addFloatImage (FloatImageObj *fio)
   871 {
   872 	FloatImageObj *newfi=new FloatImageObj (canvas,this);
   873 	floatimage.append (newfi);
   874 	newfi->copy (fio);
   875 	if (hasScrolledParent(this) )
   876 		newfi->setVisibility (false);
   877 	else	
   878 		newfi->setVisibility(visible);
   879 	requestReposition();
   880 	return newfi;
   881 }
   882 
   883 FloatImageObj* BranchObj::getFirstFloatImage ()
   884 {
   885     return floatimage.first();
   886 }
   887 
   888 FloatImageObj* BranchObj::getLastFloatImage ()
   889 {
   890     return floatimage.last();
   891 }
   892 
   893 FloatImageObj* BranchObj::getFloatImageNum (const uint &i)
   894 {
   895     return floatimage.at(i);
   896 }
   897 
   898 void BranchObj::removeFloatImage (FloatImageObj *fio)
   899 {
   900 	floatimage.remove (fio);
   901 	requestReposition();
   902 }
   903 
   904 void BranchObj::savePosInAngle ()
   905 {
   906 	// Save position in angle
   907     BranchObj *b;
   908 	int i=0;
   909     for (b=branch.first(); b; b=branch.next() )
   910 	{
   911 		b->angle=i;
   912 		i++;
   913 	}
   914 }
   915 
   916 BranchObj* BranchObj::addBranch()
   917 {
   918     BranchObj* newbo=new BranchObj(canvas,this);
   919     branch.append (newbo);
   920     newbo->setParObj(this);
   921     newbo->setColor(getColor(),false);	
   922     newbo->setLinkColor();	
   923     newbo->setHeading ("new");
   924 	newbo->setLinkStyle (newbo->getDefLinkStyle());
   925 	if (scrolled)
   926 		newbo->setVisibility (false);
   927 	else	
   928 		newbo->setVisibility(visible);
   929 	newbo->updateLink();	
   930 	requestReposition();
   931 	return newbo;
   932 }
   933 
   934 BranchObj* BranchObj::addBranch(BranchObj* bo)
   935 {
   936     BranchObj* newbo=new BranchObj(canvas,this);
   937     branch.append (newbo);
   938     newbo->copy(bo);
   939     newbo->setParObj(this);
   940 	newbo->setHeading (newbo->getHeading());	// adjust fontsize to depth
   941 	newbo->setLinkStyle (newbo->getDefLinkStyle());
   942 	if (scrolled)
   943 		newbo->setVisibility (false);
   944 	else	
   945 		newbo->setVisibility(bo->visible);
   946 	newbo->updateLink();	
   947 	requestReposition();
   948 	return newbo;
   949 }
   950 
   951 BranchObj* BranchObj::addBranchPtr(BranchObj* bo)
   952 {
   953 	branch.append (bo);
   954 	bo->setParObj (this);
   955 	bo->depth=depth+1;
   956 	bo->setLinkStyle (bo->getDefLinkStyle() );
   957 	if (scrolled) tmpUnscroll();
   958 	setLastSelectedBranch (bo);
   959 	return bo;
   960 }
   961 
   962 BranchObj* BranchObj::insertBranch(int pos)
   963 {
   964 	savePosInAngle();
   965 	// Add new bo and resort branches
   966 	BranchObj *newbo=addBranch ();
   967 	newbo->angle=pos-0.5;
   968 	branch.sort();
   969 	return newbo;
   970 }
   971 
   972 BranchObj* BranchObj::insertBranch(BranchObj* bo, int pos)
   973 {
   974 	savePosInAngle();
   975 	// Add new bo and resort branches
   976 	bo->angle=pos-0.5;
   977 	BranchObj *newbo=addBranch (bo);
   978 	branch.sort();
   979 	return newbo;
   980 }
   981 
   982 BranchObj* BranchObj::insertBranchPtr (BranchObj* bo, int pos)
   983 {
   984 	savePosInAngle();
   985 	// Add new bo and resort branches
   986 	bo->angle=pos-0.5;
   987 	branch.append (bo);
   988 	bo->setParObj (this);
   989 	bo->depth=depth+1;
   990 	bo->setLinkStyle (bo->getDefLinkStyle() );
   991 	if (scrolled) tmpUnscroll();
   992 	setLastSelectedBranch (bo);
   993 	branch.sort();
   994 	return bo;
   995 }
   996 
   997 void BranchObj::removeBranchHere(BranchObj* borem)
   998 {
   999 	// This removes the branch bo from list, but 
  1000 	// inserts its childs at the place of bo
  1001 	BranchObj *bo;
  1002 	bo=borem->getLastBranch();
  1003 	int pos=borem->getNum();
  1004 	while (bo)
  1005 	{
  1006 		bo->moveBranchTo (this,pos+1);
  1007 		bo=borem->getLastBranch();
  1008 	}	
  1009 	removeBranch (borem);
  1010 }
  1011 
  1012 void BranchObj::removeChilds()
  1013 {
  1014 	clear();
  1015 }
  1016 
  1017 void BranchObj::removeBranch(BranchObj* bo)
  1018 {
  1019     // if bo is not in branch remove returns false, we
  1020     // don't care...
  1021 	
  1022     if (branch.remove (bo))
  1023 		delete (bo);
  1024 	else
  1025 		qWarning ("BranchObj::removeBranch tried to remove non existing branch?!\n");
  1026 	requestReposition();
  1027 }
  1028 
  1029 void BranchObj::removeBranchPtr(BranchObj* bo)
  1030 {
  1031 	branch.remove (bo);
  1032 	requestReposition();
  1033 }
  1034 
  1035 void BranchObj::setLastSelectedBranch (BranchObj* bo)
  1036 {
  1037     lastSelectedBranch=branch.find(bo);
  1038 }
  1039 
  1040 BranchObj* BranchObj::getLastSelectedBranch ()
  1041 {
  1042     if (lastSelectedBranch>=0) 
  1043 	{
  1044 		BranchObj* bo=branch.at(lastSelectedBranch);
  1045 		if (bo) return bo;
  1046     }	
  1047     return branch.first();
  1048 }
  1049 
  1050 BranchObj* BranchObj::getFirstBranch ()
  1051 {
  1052     return branch.first();
  1053 }
  1054 
  1055 BranchObj* BranchObj::getLastBranch ()
  1056 {
  1057     return branch.last();
  1058 }
  1059 
  1060 BranchObj* BranchObj::getBranchNum (const uint &i)
  1061 {
  1062     return branch.at(i);
  1063 }
  1064 
  1065 
  1066 BranchObj* BranchObj::moveBranchUp(BranchObj* bo1) // move a branch up (modify myself)
  1067 {
  1068 	savePosInAngle();
  1069     int i=branch.find(bo1);
  1070     if (i>0) 
  1071 	{	// -1 if bo1 not found 
  1072 		branch.at(i)->angle--;
  1073 		branch.at(i-1)->angle++;
  1074 		branch.sort();
  1075 		return branch.at(i-1);
  1076 	} else
  1077 		return branch.at(i);
  1078 }
  1079 
  1080 BranchObj* BranchObj::moveBranchDown(BranchObj* bo1)
  1081 {
  1082 	savePosInAngle();
  1083     int i=branch.find(bo1);
  1084 	int j;
  1085 	if (branch.next())
  1086 	{
  1087 		j = branch.at();
  1088 		branch.at(i)->angle++;
  1089 		branch.at(j)->angle--;
  1090 		branch.sort();
  1091 		return branch.at(j);
  1092 	} else
  1093 		return branch.at(i);
  1094 }
  1095 
  1096 BranchObj* BranchObj::moveBranchTo (BranchObj* dst, int pos)
  1097 {
  1098 	// Find current parent and 
  1099 	// remove pointer to myself there
  1100 	if (!dst) return NULL;
  1101 	BranchObj *par=(BranchObj*)(parObj);
  1102 	if (par)
  1103 		par->removeBranchPtr (this);
  1104 	else
  1105 		return NULL;
  1106 
  1107 	// Create new pointer to myself at dst
  1108 	if (pos<0||dst->getDepth()==0)
  1109 	{	
  1110 		// links myself as last branch at dst
  1111 		dst->addBranchPtr (this);
  1112 		return this;
  1113 	} else
  1114 	{
  1115 		// inserts me at pos in parent of dst
  1116 		if (par)
  1117 		{
  1118 			BranchObj *bo=dst->insertBranchPtr (this,pos);
  1119 			bo->setHeading (bo->getHeading());
  1120 			return bo;
  1121 
  1122 		} else
  1123 			return NULL;
  1124 	}	
  1125 }
  1126 
  1127 void BranchObj::alignRelativeTo (QPoint ref)
  1128 {
  1129 /* FIXME testing
  1130 	if (!getHeading().isEmpty())
  1131 		cout << "BO::alignRelTo "<<getHeading()<<endl;
  1132 	else	
  1133 		cout << "BO::alignRelTo  ???"<<endl;
  1134 	cout << "  d="<<depth<<endl;
  1135 */	
  1136 	int th = bboxTotal.height();	
  1137 
  1138 	// If I am the mapcenter or a mainbranch, reposition heading
  1139 	if (depth<2)
  1140 	{
  1141 		move (absPos.x(),absPos.y());
  1142 		if (depth==1)
  1143 		{
  1144 			// Calc angle to mapCenter if I am a mainbranch
  1145 			// needed for reordering the mainbranches clockwise 
  1146 			// around mapcenter 
  1147 			angle=getAngle (QPoint ((int)(x() - parObj->getChildPos().x() ), 
  1148 									(int)(y() - parObj->getChildPos().y() ) ) );
  1149 		}	
  1150 	} 
  1151 	else
  1152     {
  1153 		// Align myself depending on orientation and parent, but
  1154 		// only if I am not the mainbranch or mapcenter itself
  1155 		switch (orientation) 
  1156 		{
  1157 			case OrientLeftOfCenter:
  1158 				move (ref.x()-bbox.width(), ref.y() + (th-bbox.height())/2 );
  1159 			break;
  1160 			case OrientRightOfCenter:	
  1161 				move (ref.x(), ref.y() + (th-bbox.height())/2 );
  1162 			break;
  1163 			default:
  1164 				cout <<"LMO::alignRelativeTo: oops, no orientation given...\n";
  1165 			break;
  1166 		}		
  1167     }		
  1168 
  1169 	FloatImageObj *fio;
  1170     for (fio=floatimage.first(); fio; fio=floatimage.next() )
  1171 		fio->reposition();
  1172 
  1173 	if (scrolled) return;
  1174 
  1175     // Set reference point for alignment of childs
  1176     QPoint ref2;
  1177     if (orientation==OrientLeftOfCenter)
  1178 		ref2.setX(childPos.x() - linkwidth);
  1179     else	
  1180 		ref2.setX(childPos.x() + linkwidth);
  1181 
  1182 	if (depth==1)
  1183 		ref2.setY(absPos.y()-(bboxTotal.height()-bbox.height())/2);
  1184 	else	
  1185 		ref2.setY(ref.y() );	
  1186 
  1187     // Align the childs depending on reference point 
  1188     BranchObj *b;
  1189     for (b=branch.first(); b; b=branch.next() )
  1190     {	
  1191 		b->alignRelativeTo (ref2);
  1192 		ref2.setY(ref2.y() + b->getBBoxSizeWithChilds().height() );
  1193     }
  1194 }
  1195 
  1196 
  1197 void BranchObj::reposition()
  1198 {	
  1199 /* FIXME testing
  1200 	if (!getHeading().isEmpty())
  1201 		cout << "BO::reposition  "<<getHeading()<<endl;
  1202 	else	
  1203 		cout << "BO::reposition  ???"<<endl;
  1204 */		
  1205 	if (depth==0)
  1206 	{
  1207 		// only calculate the sizes once. If the deepest LMO 
  1208 		// changes its height,
  1209 		// all upper LMOs have to change, too.
  1210 		calcBBoxSizeWithChilds();
  1211 	    alignRelativeTo ( QPoint (absPos.x(),
  1212 			absPos.y()-(bboxTotal.height()-bbox.height())/2) );
  1213 		branch.sort();	
  1214 	} else
  1215 	{
  1216 		// This is only important for moving branches:
  1217 		// For editing a branch it isn't called...
  1218 	    alignRelativeTo ( QPoint (absPos.x(),
  1219 							absPos.y()-(bboxTotal.height()-bbox.height())/2) );
  1220 	}
  1221 }
  1222 
  1223 
  1224 QRect BranchObj::getTotalBBox()
  1225 {
  1226 	QRect r=bbox;
  1227 
  1228 	if (scrolled) return r;
  1229 
  1230 	BranchObj* b;
  1231 	for (b=branch.first();b ;b=branch.next() )
  1232 		r=addBBox(b->getTotalBBox(),r);
  1233 
  1234 	FloatImageObj* fio;
  1235 	for (fio=floatimage.first();fio ;fio=floatimage.next() )
  1236 		r=addBBox(fio->getTotalBBox(),r);
  1237 		
  1238 	return r;
  1239 }
  1240 
  1241 QRect BranchObj::getBBoxSizeWithChilds()
  1242 {
  1243 	return bboxTotal;
  1244 }
  1245 
  1246 void BranchObj::calcBBoxSizeWithChilds()
  1247 {
  1248 	// This is called only from reposition and
  1249 	// and only for mapcenter. So it won't be
  1250 	// called more than once for a single user 
  1251 	// action
  1252 	
  1253 	// Calculate size of LMO including all childs (to align them later)
  1254 
  1255 	bboxTotal.setX(bbox.x() );
  1256 	bboxTotal.setY(bbox.y() );
  1257 
  1258 	// if branch is scrolled, ignore childs, but still consider floatimages
  1259 	if (scrolled)
  1260 	{
  1261 		bboxTotal.setWidth (bbox.width());
  1262 		bboxTotal.setHeight(bbox.height());
  1263 		return;
  1264 	}
  1265 	
  1266 	QRect r(0,0,0,0);
  1267 	QRect br;
  1268 	// Now calculate recursivly
  1269 	// sum of heights 
  1270 	// maximum of widths 
  1271 	// minimum of y
  1272 	BranchObj* b;
  1273 	for (b=branch.first();b ;b=branch.next() )
  1274 	{
  1275 		b->calcBBoxSizeWithChilds();
  1276 		br=b->getBBoxSizeWithChilds();
  1277 		r.setWidth( max (br.width(), r.width() ));
  1278 		r.setHeight(br.height() + r.height() );
  1279 		if (br.y()<bboxTotal.y()) bboxTotal.setY(br.y());
  1280 	}
  1281 	// Add myself and also
  1282 	// add width of link to sum if necessary
  1283 	if (branch.isEmpty())
  1284 		bboxTotal.setWidth (bbox.width() + r.width() );
  1285 	else	
  1286 		bboxTotal.setWidth (bbox.width() + r.width() + linkwidth);
  1287 	bboxTotal.setHeight(max (r.height(),  bbox.height() ) );
  1288 //	frame->setRect(QRect(bbox.x(),bbox.y(),bbox.width(),bbox.height() ) );
  1289 }
  1290 
  1291 void BranchObj::select()
  1292 {
  1293     LinkableMapObj::select();
  1294 	// Tell parent that I am selected now:
  1295 	BranchObj* po=(BranchObj*)(parObj);
  1296     if (po)	// TODO	    Try to get rid of this cast...
  1297         po->setLastSelectedBranch(this);
  1298 		
  1299 	// temporary unscroll, if we have scrolled parents somewhere
  1300 	if (parObj) ((BranchObj*)(parObj))->tmpUnscroll();
  1301 
  1302 	// set Text in Editor	
  1303 	textEditor->setText(note.getNote() );
  1304 	QString fnh=note.getFilenameHint();
  1305 	if (fnh!="")
  1306 		textEditor->setFilenameHint(note.getFilenameHint() );
  1307 	else	
  1308 		textEditor->setFilenameHint(getHeading() );
  1309 	textEditor->setFontHint (note.getFontHint() );
  1310 	connect (textEditor, SIGNAL (textHasChanged() ), this, SLOT (updateNoteFlag() ) ); 
  1311 	connect (textEditor, SIGNAL (fontSizeHasChanged() ), this, SLOT (updateNoteFlag() ) ); 
  1312 
  1313 	// Show URL and link in statusbar
  1314 	QString status;
  1315 	if (!url.isEmpty()) status+="URL: "+url+"  ";
  1316 	if (!vymLink.isEmpty()) status+="Link: "+vymLink;
  1317 	if (!status.isEmpty()) mainWindow->statusMessage (status);
  1318 
  1319 	// Update Toolbar
  1320 	standardFlags->updateToolbar();
  1321 
  1322 	// Update Browserbutton
  1323 	if (!url.isEmpty())
  1324 		actionEditOpenURL->setEnabled (true);
  1325 	else	
  1326 		actionEditOpenURL->setEnabled (false);
  1327 
  1328 	// Update actions in mapeditor
  1329 	mapEditor->updateActions();
  1330 }
  1331 
  1332 void BranchObj::unselect()
  1333 {
  1334 	LinkableMapObj::unselect();
  1335 	// Delete any messages like vymLink in StatusBar
  1336 	mainWindow->statusMessage ("");
  1337 
  1338 	// save note from editor and set flag
  1339 	// text is done by updateNoteFlag(), just save
  1340 	// filename here
  1341 	note.setFilenameHint (textEditor->getFilename());
  1342 
  1343 	// reset temporary unscroll, if we have scrolled parents somewhere
  1344 	if (parObj) ((BranchObj*)(parObj))->resetTmpUnscroll();
  1345 
  1346 	// Disconnect textEditor from this LMO
  1347 	disconnect( textEditor, SIGNAL(textHasChanged()), 0, 0 );
  1348 	disconnect( textEditor, SIGNAL (fontSizeHasChanged()),0,0 ); 
  1349 
  1350 	// Erase content of editor 
  1351 	textEditor->setInactive();
  1352 
  1353 	// unselect all buttons in toolbar
  1354 	standardFlagsDefault->updateToolbar();
  1355 }
  1356 
  1357 QString BranchObj::getSelectString()
  1358 {
  1359 	QString s;
  1360 	if (parObj)
  1361 	{
  1362 		if (depth==1)
  1363 			s= "bo:" + QString("%1").arg(getNum());
  1364 		else	
  1365 			s= ((BranchObj*)(parObj))->getSelectString() + ",bo:" + QString("%1").arg(getNum());
  1366 	} else
  1367 		s="mc:";
  1368 	return s;
  1369 }
  1370