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