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