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