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