branchobj.cpp
author insilmaril
Mon May 08 13:25:46 2006 +0000 (2006-05-08)
changeset 316 25f100aa30b2
parent 301 4258723b534c
child 333 94cd6a451593
permissions -rw-r--r--
1.7.16 Small bugfixes
     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 		if (hasScrolledParent(this))
   709 			setVisibility (false);
   710 		else
   711 			setVisibility (true);
   712 		hidden=false;
   713 	}	
   714 
   715     BranchObj *bo;
   716     for (bo=branch.first(); bo; bo=branch.next() )
   717 		bo->setHideTmp (mode);
   718 }
   719 
   720 bool BranchObj::hasHiddenExportParent(BranchObj *start)
   721 {
   722 	// Calls parents recursivly to
   723 	// find out, if we are temp. hidden
   724 
   725 	if (hideExport) return true;
   726 
   727 	BranchObj* bo=(BranchObj*)(parObj);
   728 	if (bo) 
   729 		return bo->hasHiddenExportParent(start);
   730 	else
   731 		return false;
   732 }
   733 
   734 QString BranchObj::saveToDir (const QString &tmpdir,const QString &prefix, const QPoint& offset)
   735 {
   736 	if (hidden) return "";
   737 
   738     QString s,a;
   739 	QString scrolledAttr;
   740 	if (scrolled) 
   741 		scrolledAttr=attribut ("scrolled","yes");
   742 	else
   743 		scrolledAttr="";
   744 
   745 	QString frameAttr;
   746 	if (frame->getFrameType()!=NoFrame)
   747 		frameAttr=attribut ("frameType",frame->getFrameTypeName());
   748 	else
   749 		frameAttr="";
   750 
   751 	// save area, if not scrolled
   752 	QString areaAttr;
   753 	if (!((BranchObj*)(parObj))->isScrolled() )
   754 	{
   755 		areaAttr=
   756 			attribut("x1",QString().setNum(absPos.x()-offset.x(),10)) +
   757 			attribut("y1",QString().setNum(absPos.y()-offset.y(),10)) +
   758 			attribut("x2",QString().setNum(absPos.x()+width()-offset.x(),10)) +
   759 			attribut("y2",QString().setNum(absPos.y()+height()-offset.y(),10));
   760 
   761 	} else
   762 		areaAttr="";
   763 	
   764 	// Providing an ID for a branch makes export to XHTML easier
   765 	QString idAttr;
   766 	if (countXLinks()>0)
   767 		idAttr=attribut ("id",getSelectString());
   768 	else
   769 		idAttr="";
   770 
   771     s=beginElement ("branch" 
   772 		+getOrnAttr() 
   773 		+scrolledAttr 
   774 		+frameAttr 
   775 		+areaAttr 
   776 		+idAttr 
   777 		+getIncludeImageAttr() );
   778     incIndent();
   779 
   780 	// save heading
   781     s+=valueElement("heading", getHeading(),
   782 		attribut ("textColor",QColor(heading->getColor()).name()));
   783 
   784 	// save names of flags set
   785 	s+=standardFlags->saveToDir(tmpdir,prefix,0);
   786 	
   787 	// save note
   788 	if (!note.isEmpty() )
   789 		s+=note.saveToDir();
   790 	
   791 	// Save branches
   792     BranchObj *bo;
   793     for (bo=branch.first(); bo; bo=branch.next() )
   794 		s+=bo->saveToDir(tmpdir,prefix,offset);
   795 
   796 	// Save FloatImages
   797 	FloatImageObj *fio;
   798 	for (fio=floatimage.first(); fio; fio=floatimage.next() )
   799 		s+=fio->saveToDir (tmpdir,prefix,offset);
   800 
   801 	// Save XLinks
   802 	XLinkObj *xlo;
   803     for (xlo=xlink.first(); xlo; xlo=xlink.next() )
   804 		s+=xlo->saveToDir();
   805 
   806     decIndent();
   807     s+=endElement   ("branch");
   808     return s;
   809 }
   810 
   811 void BranchObj::addXLink (XLinkObj *xlo)
   812 {
   813 	xlink.append (xlo);
   814 	
   815 }
   816 
   817 void BranchObj::removeXLinkRef (XLinkObj *xlo)
   818 {
   819 	xlink.remove (xlo);
   820 }
   821 
   822 void BranchObj::deleteXLink(XLinkObj *xlo)
   823 {
   824 	xlo->deactivate();
   825 	if (!xlo->isUsed()) delete (xlo);
   826 }
   827 
   828 void BranchObj::deleteXLinkAt (int i)
   829 {
   830 	XLinkObj *xlo=xlink.at(i);
   831 	xlo->deactivate();
   832 	if (!xlo->isUsed()) delete(xlo);
   833 }
   834 
   835 XLinkObj* BranchObj::XLinkAt (int i)
   836 {
   837 	return xlink.at(i);
   838 }
   839 
   840 int BranchObj::countXLink()
   841 {
   842 	return xlink.count();
   843 }
   844 
   845 
   846 BranchObj* BranchObj::XLinkTargetAt (int i)
   847 {
   848 	if (xlink.at(i))
   849 		return xlink.at(i)->otherBranch (this);
   850 	else
   851 		return NULL;
   852 }
   853 
   854 void BranchObj::setIncludeImagesVer(bool b)
   855 {
   856 	includeImagesVer=b;
   857 	calcBBoxSize();
   858 	positionBBox();
   859 	requestReposition();
   860 	//FIXME undo needed
   861 }
   862 
   863 bool BranchObj::getIncludeImagesVer()
   864 {
   865 	return includeImagesVer;
   866 }
   867 
   868 void BranchObj::setIncludeImagesHor(bool b)
   869 {
   870 	includeImagesHor=b;
   871 	calcBBoxSize();
   872 	positionBBox();
   873 	requestReposition();
   874 	//FIXME undo needed
   875 }
   876 
   877 bool BranchObj::getIncludeImagesHor()
   878 {
   879 	return includeImagesHor;
   880 }
   881 
   882 QString BranchObj::getIncludeImageAttr()
   883 {
   884 	QString a;
   885 	if (includeImagesVer)
   886 		a=attribut ("incImgV","true");
   887 	else
   888 		a=attribut ("incImgV","false");
   889 	if (includeImagesHor)
   890 		a+=attribut ("incImgH","true");
   891 	else
   892 		a+=attribut ("incImgH","false");
   893 	return a;	
   894 }
   895 
   896 LinkableMapObj* BranchObj::addFloatImage ()
   897 {
   898 	FloatImageObj *newfi=new FloatImageObj (canvas,this);
   899 	floatimage.append (newfi);
   900 	if (hasScrolledParent(this) )
   901 		newfi->setVisibility (false);
   902 	else	
   903 		newfi->setVisibility(visible);
   904 	calcBBoxSize();
   905 	positionBBox();
   906 	requestReposition();
   907 	return newfi;
   908 	//FIXME undo needed
   909 }
   910 
   911 LinkableMapObj* BranchObj::addFloatImage (FloatImageObj *fio)
   912 {
   913 	FloatImageObj *newfi=new FloatImageObj (canvas,this);
   914 	floatimage.append (newfi);
   915 	newfi->copy (fio);
   916 	if (hasScrolledParent(this) )
   917 		newfi->setVisibility (false);
   918 	else	
   919 		newfi->setVisibility(visible);
   920 	calcBBoxSize();
   921 	positionBBox();
   922 	requestReposition();
   923 	return newfi;
   924 	// FIMXE undo needed
   925 }
   926 
   927 FloatImageObj* BranchObj::getFirstFloatImage ()
   928 {
   929     return floatimage.first();
   930 }
   931 
   932 FloatImageObj* BranchObj::getLastFloatImage ()
   933 {
   934     return floatimage.last();
   935 }
   936 
   937 FloatImageObj* BranchObj::getFloatImageNum (const uint &i)
   938 {
   939     return floatimage.at(i);
   940 }
   941 
   942 void BranchObj::removeFloatImage (FloatImageObj *fio)
   943 {
   944 	floatimage.remove (fio);
   945 	calcBBoxSize();
   946 	positionBBox();
   947 	requestReposition();
   948 	// FIMXE undo needed
   949 }
   950 
   951 void BranchObj::savePosInAngle ()
   952 {
   953 	// Save position in angle
   954     BranchObj *b;
   955 	int i=0;
   956     for (b=branch.first(); b; b=branch.next() )
   957 	{
   958 		b->angle=i;
   959 		i++;
   960 	}
   961 }
   962 
   963 void BranchObj::setDefAttr (BranchModification mod)
   964 {
   965 	int fontsize;
   966 	switch (depth)
   967 	{
   968 		case 0: fontsize=16; break;
   969 		case 1: fontsize=12; break;
   970 		default: fontsize=10; break;
   971 	}	
   972 
   973 	setLinkColor ();
   974 	setLinkStyle(getDefLinkStyle());
   975 	QFont font("Sans Serif,8,-1,5,50,0,0,0,0,0");
   976 	font.setPointSize(fontsize);
   977 	heading->setFont(font );
   978 
   979 	if (mod==NewBranch)
   980 		setColor (((BranchObj*)(parObj))->getColor());
   981 	
   982 	calcBBoxSize();
   983 }
   984 
   985 BranchObj* BranchObj::addBranch()
   986 {
   987     BranchObj* newbo=new BranchObj(canvas,this);
   988     branch.append (newbo);
   989     newbo->setParObj(this);
   990 	newbo->setDefAttr(NewBranch);
   991     newbo->setHeading ("new");
   992 	if (scrolled)
   993 		newbo->setVisibility (false);
   994 	else	
   995 		newbo->setVisibility(visible);
   996 	newbo->updateLink();	
   997 	requestReposition();
   998 	return newbo;
   999 }
  1000 
  1001 BranchObj* BranchObj::addBranch(BranchObj* bo)
  1002 {
  1003     BranchObj* newbo=new BranchObj(canvas,this);
  1004     branch.append (newbo);
  1005     newbo->copy(bo);
  1006     newbo->setParObj(this);
  1007 	newbo->setDefAttr(MovedBranch);
  1008 	if (scrolled)
  1009 		newbo->setVisibility (false);
  1010 	else	
  1011 		newbo->setVisibility(bo->visible);
  1012 	newbo->updateLink();	
  1013 	requestReposition();
  1014 	return newbo;
  1015 }
  1016 
  1017 BranchObj* BranchObj::addBranchPtr(BranchObj* bo)
  1018 {
  1019 	branch.append (bo);
  1020 	bo->setParObj (this);
  1021 	bo->depth=depth+1;
  1022 	bo->setDefAttr(MovedBranch);
  1023 	if (scrolled) tmpUnscroll();
  1024 	setLastSelectedBranch (bo);
  1025 	return bo;
  1026 }
  1027 
  1028 BranchObj* BranchObj::insertBranch(int pos)
  1029 {
  1030 	savePosInAngle();
  1031 	// Add new bo and resort branches
  1032 	BranchObj *newbo=addBranch ();
  1033 	newbo->angle=pos-0.5;
  1034 	branch.sort();
  1035 	return newbo;
  1036 }
  1037 
  1038 BranchObj* BranchObj::insertBranch(BranchObj* bo, int pos)
  1039 {
  1040 	savePosInAngle();
  1041 	// Add new bo and resort branches
  1042 	bo->angle=pos-0.5;
  1043 	BranchObj *newbo=addBranch (bo);
  1044 	branch.sort();
  1045 	return newbo;
  1046 }
  1047 
  1048 BranchObj* BranchObj::insertBranchPtr (BranchObj* bo, int pos)
  1049 {
  1050 	savePosInAngle();
  1051 	// Add new bo and resort branches
  1052 	bo->angle=pos-0.5;
  1053 	branch.append (bo);
  1054 	bo->setParObj (this);
  1055 	bo->depth=depth+1;
  1056 	bo->setDefAttr (MovedBranch);
  1057 	if (scrolled) tmpUnscroll();
  1058 	setLastSelectedBranch (bo);
  1059 	branch.sort();
  1060 	return bo;
  1061 }
  1062 
  1063 void BranchObj::removeBranchHere(BranchObj* borem)
  1064 {
  1065 	// This removes the branch bo from list, but 
  1066 	// inserts its childs at the place of bo
  1067 	BranchObj *bo;
  1068 	bo=borem->getLastBranch();
  1069 	int pos=borem->getNum();
  1070 	while (bo)
  1071 	{
  1072 		bo->moveBranchTo (this,pos+1);
  1073 		bo=borem->getLastBranch();
  1074 	}	
  1075 	removeBranch (borem);
  1076 }
  1077 
  1078 void BranchObj::removeChilds()
  1079 {
  1080 	clear();
  1081 }
  1082 
  1083 void BranchObj::removeBranch(BranchObj* bo)
  1084 {
  1085     // if bo is not in branch remove returns false, we
  1086     // don't care...
  1087 	
  1088     if (branch.remove (bo))
  1089 		delete (bo);
  1090 	else
  1091 		qWarning ("BranchObj::removeBranch tried to remove non existing branch?!\n");
  1092 	requestReposition();
  1093 }
  1094 
  1095 void BranchObj::removeBranchPtr(BranchObj* bo)
  1096 {
  1097 	branch.remove (bo);
  1098 	requestReposition();
  1099 }
  1100 
  1101 void BranchObj::setLastSelectedBranch (BranchObj* bo)
  1102 {
  1103     lastSelectedBranch=branch.find(bo);
  1104 }
  1105 
  1106 BranchObj* BranchObj::getLastSelectedBranch ()
  1107 {
  1108     if (lastSelectedBranch>=0) 
  1109 	{
  1110 		BranchObj* bo=branch.at(lastSelectedBranch);
  1111 		if (bo) return bo;
  1112     }	
  1113     return branch.first();
  1114 }
  1115 
  1116 BranchObj* BranchObj::getFirstBranch ()
  1117 {
  1118     return branch.first();
  1119 }
  1120 
  1121 BranchObj* BranchObj::getLastBranch ()
  1122 {
  1123     return branch.last();
  1124 }
  1125 
  1126 BranchObj* BranchObj::getBranchNum (const uint &i)
  1127 {
  1128     return branch.at(i);
  1129 }
  1130 
  1131 bool BranchObj::canMoveBranchUp() 
  1132 {
  1133 	if (!parObj) return false;
  1134 	BranchObj* par=(BranchObj*)parObj;
  1135 	if (this==par->getFirstBranch())
  1136 		return false;
  1137 	else
  1138 		return true;
  1139 }
  1140 
  1141 BranchObj* BranchObj::moveBranchUp(BranchObj* bo1) // move a branch up (modify myself)
  1142 {
  1143 	savePosInAngle();
  1144     int i=branch.find(bo1);
  1145     if (i>0) 
  1146 	{	// -1 if bo1 not found 
  1147 		branch.at(i)->angle--;
  1148 		branch.at(i-1)->angle++;
  1149 		branch.sort();
  1150 		return branch.at(i-1);
  1151 	} else
  1152 		return branch.at(i);
  1153 }
  1154 
  1155 bool BranchObj::canMoveBranchDown() 
  1156 {
  1157 	if (!parObj) return false;
  1158 	BranchObj* par=(BranchObj*)parObj;
  1159 	if (this==par->getLastBranch())
  1160 		return false;
  1161 	else
  1162 		return true;
  1163 }
  1164 
  1165 BranchObj* BranchObj::moveBranchDown(BranchObj* bo1)
  1166 {
  1167 	savePosInAngle();
  1168     int i=branch.find(bo1);
  1169 	int j;
  1170 	if (branch.next())
  1171 	{
  1172 		j = branch.at();
  1173 		branch.at(i)->angle++;
  1174 		branch.at(j)->angle--;
  1175 		branch.sort();
  1176 		return branch.at(j);
  1177 	} else
  1178 		return branch.at(i);
  1179 }
  1180 
  1181 BranchObj* BranchObj::moveBranchTo (BranchObj* dst, int pos)
  1182 {
  1183 	// Find current parent and 
  1184 	// remove pointer to myself there
  1185 	if (!dst) return NULL;
  1186 	BranchObj *par=(BranchObj*)(parObj);
  1187 	if (par)
  1188 		par->removeBranchPtr (this);
  1189 	else
  1190 		return NULL;
  1191 
  1192 	// Create new pointer to myself at dst
  1193 	if (pos<0||dst->getDepth()==0)
  1194 	{	
  1195 		// links myself as last branch at dst
  1196 		dst->addBranchPtr (this);
  1197 		updateLink();
  1198 		return this;
  1199 	} else
  1200 	{
  1201 		// inserts me at pos in parent of dst
  1202 		if (par)
  1203 		{
  1204 			BranchObj *bo=dst->insertBranchPtr (this,pos);
  1205 			bo->setDefAttr(MovedBranch);
  1206 			updateLink();
  1207 			return bo;
  1208 
  1209 		} else
  1210 			return NULL;
  1211 	}	
  1212 }
  1213 
  1214 void BranchObj::alignRelativeTo (QPoint ref)
  1215 {
  1216 	int th = bboxTotal.height();	
  1217 // TODO testing
  1218 /*
  1219 	cout << "BO::alignRelTo "<<getHeading()<<endl;
  1220 	cout << "  d="<<depth<<
  1221 		"  ref="<<ref<<
  1222 //		"  bbox.topLeft="<<bboxTotal.topLeft()<<
  1223 		"  absPos="<<absPos<<
  1224 //		"  pad="<<topPad<<","<<botPad<<","<<leftPad<<","<<rightPad<<
  1225 		"  hidden="<<hidden<<
  1226 		"  th="<<th<<endl;
  1227 */
  1228 
  1229 	// If I am the mapcenter or a mainbranch, reposition heading
  1230 	if (depth<2)
  1231 	{	//FIXME optimize this   move for MCO needed to initially position text in box...
  1232 		if (depth==1)
  1233 		{
  1234 			move (absPos.x(),absPos.y());
  1235 			// Calc angle to mapCenter if I am a mainbranch
  1236 			// needed for reordering the mainbranches clockwise 
  1237 			// around mapcenter 
  1238 			angle=getAngle (QPoint ((int)(x() - parObj->getChildPos().x() ), 
  1239 									(int)(y() - parObj->getChildPos().y() ) ) );
  1240 		} 
  1241 	} 
  1242 	else
  1243     {
  1244 		// Align myself depending on orientation and parent, but
  1245 		// only if I am not the mainbranch or mapcenter itself
  1246 		switch (orientation) 
  1247 		{
  1248 			case OrientLeftOfCenter:
  1249 				move (ref.x() - bbox.width(), ref.y() + (th-bbox.height())/2 );
  1250 			break;
  1251 			case OrientRightOfCenter:	
  1252 				move (ref.x() , ref.y() + (th-bbox.height())/2  );
  1253 			break;
  1254 			default:
  1255 				qWarning ("LMO::alignRelativeTo: oops, no orientation given...");
  1256 			break;
  1257 		}		
  1258     }		
  1259 
  1260 	if (scrolled) return;
  1261 
  1262     // Set reference point for alignment of childs
  1263     QPoint ref2;
  1264     if (orientation==OrientLeftOfCenter)
  1265 		ref2.setX(bbox.topLeft().x() - linkwidth);
  1266     else	
  1267 		ref2.setX(bbox.topRight().x() + linkwidth);
  1268 
  1269 	if (depth==1)
  1270 		ref2.setY(absPos.y()-(bboxTotal.height()-bbox.height())/2);
  1271 	else	
  1272 		ref2.setY(ref.y() );	
  1273 
  1274     // Align the childs depending on reference point 
  1275     BranchObj *b;
  1276     for (b=branch.first(); b; b=branch.next() )
  1277     {	
  1278 		if (!b->isHidden())
  1279 		{
  1280 			b->alignRelativeTo (ref2);
  1281 			ref2.setY(ref2.y() + b->getBBoxSizeWithChilds().height() );
  1282 		}
  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 		updateLink();	// This update is needed if the canvas is resized 
  1305 						// due to excessive moving of a FIO
  1306 
  1307 		// After load, the floats might be at wrong position, force
  1308 		// them to move, too
  1309 		//move (absPos);	// FIXME really still needed to position floats?
  1310 	} else
  1311 	{
  1312 		// This is only important for moving branches:
  1313 		// For editing a branch it isn't called...
  1314 	    alignRelativeTo ( QPoint (absPos.x(),
  1315 							absPos.y()-(bboxTotal.height()-bbox.height())/2) );
  1316 	}
  1317 }
  1318 
  1319 
  1320 QRect BranchObj::getTotalBBox()
  1321 {
  1322 	QRect r=bbox;
  1323 
  1324 	if (scrolled) return r;
  1325 
  1326 	BranchObj* b;
  1327 	for (b=branch.first();b ;b=branch.next() )
  1328 		if (!b->isHidden())
  1329 			r=addBBox(b->getTotalBBox(),r);
  1330 
  1331 	FloatImageObj* fio;
  1332 	for (fio=floatimage.first();fio ;fio=floatimage.next() )
  1333 		if (!fio->isHidden())
  1334 			r=addBBox(fio->getTotalBBox(),r);
  1335 		
  1336 	return r;
  1337 }
  1338 
  1339 QRect BranchObj::getBBoxSizeWithChilds()
  1340 {
  1341 	return bboxTotal;
  1342 }
  1343 
  1344 void BranchObj::calcBBoxSizeWithChilds()
  1345 {	
  1346 	// This is initially called only from reposition and
  1347 	// and only for mapcenter. So it won't be
  1348 	// called more than once for a single user 
  1349 	// action
  1350 	
  1351 
  1352 	// Calculate size of LMO including all childs (to align them later)
  1353 	bboxTotal.setX(bbox.x() );
  1354 	bboxTotal.setY(bbox.y() );
  1355 
  1356 	// if branch is scrolled, ignore childs, but still consider floatimages
  1357 	if (scrolled)
  1358 	{
  1359 		bboxTotal.setWidth (bbox.width());
  1360 		bboxTotal.setHeight(bbox.height());
  1361 		return;
  1362 	}
  1363 	
  1364 	if (hidden)
  1365 	{
  1366 		bboxTotal.setWidth (0);
  1367 		bboxTotal.setHeight(0);
  1368 		if (parObj)
  1369 		{
  1370 			bboxTotal.setX (parObj->x());
  1371 			bboxTotal.setY (parObj->y());
  1372 		} else
  1373 		{
  1374 			bboxTotal.setX (bbox.x());
  1375 			bboxTotal.setY (bbox.y());
  1376 		}
  1377 		return;
  1378 	}
  1379 	
  1380 	QRect r(0,0,0,0);
  1381 	QRect br;
  1382 	// Now calculate recursivly
  1383 	// sum of heights 
  1384 	// maximum of widths 
  1385 	// minimum of y
  1386 	BranchObj* b;
  1387 	for (b=branch.first();b ;b=branch.next() )
  1388 	{
  1389 		if (!b->isHidden())
  1390 		{
  1391 			b->calcBBoxSizeWithChilds();
  1392 			br=b->getBBoxSizeWithChilds();
  1393 			r.setWidth( max (br.width(), r.width() ));
  1394 			r.setHeight(br.height() + r.height() );
  1395 			if (br.y()<bboxTotal.y()) bboxTotal.setY(br.y());
  1396 		}
  1397 	}
  1398 	// Add myself and also
  1399 	// add width of link to sum if necessary
  1400 	if (branch.isEmpty())
  1401 		bboxTotal.setWidth (bbox.width() + r.width() );
  1402 	else	
  1403 		bboxTotal.setWidth (bbox.width() + r.width() + linkwidth);
  1404 	
  1405 	bboxTotal.setHeight(max (r.height(),  bbox.height()));
  1406 }
  1407 
  1408 void BranchObj::select()
  1409 {
  1410 	// set Text in Editor	
  1411 	textEditor->setText(note.getNote() );
  1412 	QString fnh=note.getFilenameHint();
  1413 	if (fnh!="")
  1414 		textEditor->setFilenameHint(note.getFilenameHint() );
  1415 	else	
  1416 		textEditor->setFilenameHint(getHeading() );
  1417 	textEditor->setFontHint (note.getFontHint() );
  1418 
  1419     LinkableMapObj::select();
  1420 	// Tell parent that I am selected now:
  1421 	BranchObj* po=(BranchObj*)(parObj);
  1422     if (po)	// TODO	    Try to get rid of this cast...
  1423         po->setLastSelectedBranch(this);
  1424 		
  1425 	// temporary unscroll, if we have scrolled parents somewhere
  1426 	if (parObj) ((BranchObj*)(parObj))->tmpUnscroll();
  1427 
  1428 	// Show URL and link in statusbar
  1429 	QString status;
  1430 	if (!url.isEmpty()) status+="URL: "+url+"  ";
  1431 	if (!vymLink.isEmpty()) status+="Link: "+vymLink;
  1432 	if (!status.isEmpty()) mainWindow->statusMessage (status);
  1433 
  1434 	// Update Toolbar
  1435 	standardFlags->updateToolbar();
  1436 
  1437 	// Update actions in mapeditor
  1438 	mapEditor->updateActions();
  1439 }
  1440 
  1441 void BranchObj::unselect()
  1442 {
  1443 	LinkableMapObj::unselect();
  1444 	// Delete any messages like vymLink in StatusBar
  1445 	mainWindow->statusMessage ("");
  1446 
  1447 	// save note from editor and set flag
  1448 	// text is done by updateNoteFlag(), just save
  1449 	// filename here
  1450 	note.setFilenameHint (textEditor->getFilename());
  1451 
  1452 	// reset temporary unscroll, if we have scrolled parents somewhere
  1453 	if (parObj) ((BranchObj*)(parObj))->resetTmpUnscroll();
  1454 
  1455 	// Erase content of editor 
  1456 	textEditor->setInactive();
  1457 
  1458 	// unselect all buttons in toolbar
  1459 	standardFlagsDefault->updateToolbar();
  1460 }
  1461 
  1462 QString BranchObj::getSelectString()
  1463 {
  1464 	QString s;
  1465 	if (parObj)
  1466 	{
  1467 		if (depth==1)
  1468 			s= "bo:" + QString("%1").arg(getNum());
  1469 		else	
  1470 			s= ((BranchObj*)(parObj))->getSelectString() + ",bo:" + QString("%1").arg(getNum());
  1471 	} else
  1472 		s="mc:";
  1473 	return s;
  1474 }
  1475