branchobj.cpp
author insilmaril
Mon, 24 Apr 2006 10:05:09 +0000
changeset 301 4258723b534c
parent 254 d3080e02b13a
child 310 00ac7b2ac016
permissions -rw-r--r--
Fixed missing Icon for flag-url in XHTML export
     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 
  1132 BranchObj* BranchObj::moveBranchUp(BranchObj* bo1) // move a branch up (modify myself)
  1133 {
  1134 	savePosInAngle();
  1135     int i=branch.find(bo1);
  1136     if (i>0) 
  1137 	{	// -1 if bo1 not found 
  1138 		branch.at(i)->angle--;
  1139 		branch.at(i-1)->angle++;
  1140 		branch.sort();
  1141 		return branch.at(i-1);
  1142 	} else
  1143 		return branch.at(i);
  1144 }
  1145 
  1146 BranchObj* BranchObj::moveBranchDown(BranchObj* bo1)
  1147 {
  1148 	savePosInAngle();
  1149     int i=branch.find(bo1);
  1150 	int j;
  1151 	if (branch.next())
  1152 	{
  1153 		j = branch.at();
  1154 		branch.at(i)->angle++;
  1155 		branch.at(j)->angle--;
  1156 		branch.sort();
  1157 		return branch.at(j);
  1158 	} else
  1159 		return branch.at(i);
  1160 }
  1161 
  1162 BranchObj* BranchObj::moveBranchTo (BranchObj* dst, int pos)
  1163 {
  1164 	// Find current parent and 
  1165 	// remove pointer to myself there
  1166 	if (!dst) return NULL;
  1167 	BranchObj *par=(BranchObj*)(parObj);
  1168 	if (par)
  1169 		par->removeBranchPtr (this);
  1170 	else
  1171 		return NULL;
  1172 
  1173 	// Create new pointer to myself at dst
  1174 	if (pos<0||dst->getDepth()==0)
  1175 	{	
  1176 		// links myself as last branch at dst
  1177 		dst->addBranchPtr (this);
  1178 		updateLink();
  1179 		return this;
  1180 	} else
  1181 	{
  1182 		// inserts me at pos in parent of dst
  1183 		if (par)
  1184 		{
  1185 			BranchObj *bo=dst->insertBranchPtr (this,pos);
  1186 			bo->setDefAttr(MovedBranch);
  1187 			updateLink();
  1188 			return bo;
  1189 
  1190 		} else
  1191 			return NULL;
  1192 	}	
  1193 }
  1194 
  1195 void BranchObj::alignRelativeTo (QPoint ref)
  1196 {
  1197 	int th = bboxTotal.height();	
  1198 // TODO testing
  1199 /*
  1200 	cout << "BO::alignRelTo "<<getHeading()<<endl;
  1201 	cout << "  d="<<depth<<
  1202 		"  ref="<<ref<<
  1203 //		"  bbox.topLeft="<<bboxTotal.topLeft()<<
  1204 		"  absPos="<<absPos<<
  1205 //		"  pad="<<topPad<<","<<botPad<<","<<leftPad<<","<<rightPad<<
  1206 		"  hidden="<<hidden<<
  1207 		"  th="<<th<<endl;
  1208 */
  1209 
  1210 	// If I am the mapcenter or a mainbranch, reposition heading
  1211 	if (depth<2)
  1212 	{	//FIXME optimize this   move for MCO needed to initially position text in box...
  1213 		if (depth==1)
  1214 		{
  1215 			move (absPos.x(),absPos.y());
  1216 			// Calc angle to mapCenter if I am a mainbranch
  1217 			// needed for reordering the mainbranches clockwise 
  1218 			// around mapcenter 
  1219 			angle=getAngle (QPoint ((int)(x() - parObj->getChildPos().x() ), 
  1220 									(int)(y() - parObj->getChildPos().y() ) ) );
  1221 		} 
  1222 	} 
  1223 	else
  1224     {
  1225 		// Align myself depending on orientation and parent, but
  1226 		// only if I am not the mainbranch or mapcenter itself
  1227 		switch (orientation) 
  1228 		{
  1229 			case OrientLeftOfCenter:
  1230 				move (ref.x() - bbox.width(), ref.y() + (th-bbox.height())/2 );
  1231 			break;
  1232 			case OrientRightOfCenter:	
  1233 				move (ref.x() , ref.y() + (th-bbox.height())/2  );
  1234 			break;
  1235 			default:
  1236 				qWarning ("LMO::alignRelativeTo: oops, no orientation given...");
  1237 			break;
  1238 		}		
  1239     }		
  1240 
  1241 	if (scrolled) return;
  1242 
  1243     // Set reference point for alignment of childs
  1244     QPoint ref2;
  1245     if (orientation==OrientLeftOfCenter)
  1246 		ref2.setX(bbox.topLeft().x() - linkwidth);
  1247     else	
  1248 		ref2.setX(bbox.topRight().x() + linkwidth);
  1249 
  1250 	if (depth==1)
  1251 		ref2.setY(absPos.y()-(bboxTotal.height()-bbox.height())/2);
  1252 	else	
  1253 		ref2.setY(ref.y() );	
  1254 
  1255     // Align the childs depending on reference point 
  1256     BranchObj *b;
  1257     for (b=branch.first(); b; b=branch.next() )
  1258     {	
  1259 		if (!b->isHidden())
  1260 		{
  1261 			b->alignRelativeTo (ref2);
  1262 			ref2.setY(ref2.y() + b->getBBoxSizeWithChilds().height() );
  1263 		}
  1264     }
  1265 }
  1266 
  1267 
  1268 void BranchObj::reposition()
  1269 {	
  1270 /* TODO testing only
  1271 	if (!getHeading().isEmpty())
  1272 		cout << "BO::reposition  "<<getHeading()<<endl;
  1273 	else	
  1274 		cout << "BO::reposition  ???"<<endl;
  1275 */		
  1276 	if (depth==0)
  1277 	{
  1278 		// only calculate the sizes once. If the deepest LMO 
  1279 		// changes its height,
  1280 		// all upper LMOs have to change, too.
  1281 		calcBBoxSizeWithChilds();
  1282 	    alignRelativeTo ( QPoint (absPos.x(),
  1283 			absPos.y()-(bboxTotal.height()-bbox.height())/2) );
  1284 		branch.sort();	
  1285 		updateLink();	// This update is needed if the canvas is resized 
  1286 						// due to excessive moving of a FIO
  1287 
  1288 		// After load, the floats might be at wrong position, force
  1289 		// them to move, too
  1290 		//move (absPos);	// FIXME really still needed to position floats?
  1291 	} else
  1292 	{
  1293 		// This is only important for moving branches:
  1294 		// For editing a branch it isn't called...
  1295 	    alignRelativeTo ( QPoint (absPos.x(),
  1296 							absPos.y()-(bboxTotal.height()-bbox.height())/2) );
  1297 	}
  1298 }
  1299 
  1300 
  1301 QRect BranchObj::getTotalBBox()
  1302 {
  1303 	QRect r=bbox;
  1304 
  1305 	if (scrolled) return r;
  1306 
  1307 	BranchObj* b;
  1308 	for (b=branch.first();b ;b=branch.next() )
  1309 		if (!b->isHidden())
  1310 			r=addBBox(b->getTotalBBox(),r);
  1311 
  1312 	FloatImageObj* fio;
  1313 	for (fio=floatimage.first();fio ;fio=floatimage.next() )
  1314 		if (!fio->isHidden())
  1315 			r=addBBox(fio->getTotalBBox(),r);
  1316 		
  1317 	return r;
  1318 }
  1319 
  1320 QRect BranchObj::getBBoxSizeWithChilds()
  1321 {
  1322 	return bboxTotal;
  1323 }
  1324 
  1325 void BranchObj::calcBBoxSizeWithChilds()
  1326 {	
  1327 	// This is initially called only from reposition and
  1328 	// and only for mapcenter. So it won't be
  1329 	// called more than once for a single user 
  1330 	// action
  1331 	
  1332 
  1333 	// Calculate size of LMO including all childs (to align them later)
  1334 	bboxTotal.setX(bbox.x() );
  1335 	bboxTotal.setY(bbox.y() );
  1336 
  1337 	// if branch is scrolled, ignore childs, but still consider floatimages
  1338 	if (scrolled)
  1339 	{
  1340 		bboxTotal.setWidth (bbox.width());
  1341 		bboxTotal.setHeight(bbox.height());
  1342 		return;
  1343 	}
  1344 	
  1345 	if (hidden)
  1346 	{
  1347 		bboxTotal.setWidth (0);
  1348 		bboxTotal.setHeight(0);
  1349 		if (parObj)
  1350 		{
  1351 			bboxTotal.setX (parObj->x());
  1352 			bboxTotal.setY (parObj->y());
  1353 		} else
  1354 		{
  1355 			bboxTotal.setX (bbox.x());
  1356 			bboxTotal.setY (bbox.y());
  1357 		}
  1358 		return;
  1359 	}
  1360 	
  1361 	QRect r(0,0,0,0);
  1362 	QRect br;
  1363 	// Now calculate recursivly
  1364 	// sum of heights 
  1365 	// maximum of widths 
  1366 	// minimum of y
  1367 	BranchObj* b;
  1368 	for (b=branch.first();b ;b=branch.next() )
  1369 	{
  1370 		if (!b->isHidden())
  1371 		{
  1372 			b->calcBBoxSizeWithChilds();
  1373 			br=b->getBBoxSizeWithChilds();
  1374 			r.setWidth( max (br.width(), r.width() ));
  1375 			r.setHeight(br.height() + r.height() );
  1376 			if (br.y()<bboxTotal.y()) bboxTotal.setY(br.y());
  1377 		}
  1378 	}
  1379 	// Add myself and also
  1380 	// add width of link to sum if necessary
  1381 	if (branch.isEmpty())
  1382 		bboxTotal.setWidth (bbox.width() + r.width() );
  1383 	else	
  1384 		bboxTotal.setWidth (bbox.width() + r.width() + linkwidth);
  1385 	
  1386 	bboxTotal.setHeight(max (r.height(),  bbox.height()));
  1387 }
  1388 
  1389 void BranchObj::select()
  1390 {
  1391 	// set Text in Editor	
  1392 	textEditor->setText(note.getNote() );
  1393 	QString fnh=note.getFilenameHint();
  1394 	if (fnh!="")
  1395 		textEditor->setFilenameHint(note.getFilenameHint() );
  1396 	else	
  1397 		textEditor->setFilenameHint(getHeading() );
  1398 	textEditor->setFontHint (note.getFontHint() );
  1399 
  1400     LinkableMapObj::select();
  1401 	// Tell parent that I am selected now:
  1402 	BranchObj* po=(BranchObj*)(parObj);
  1403     if (po)	// TODO	    Try to get rid of this cast...
  1404         po->setLastSelectedBranch(this);
  1405 		
  1406 	// temporary unscroll, if we have scrolled parents somewhere
  1407 	if (parObj) ((BranchObj*)(parObj))->tmpUnscroll();
  1408 
  1409 	// Show URL and link in statusbar
  1410 	QString status;
  1411 	if (!url.isEmpty()) status+="URL: "+url+"  ";
  1412 	if (!vymLink.isEmpty()) status+="Link: "+vymLink;
  1413 	if (!status.isEmpty()) mainWindow->statusMessage (status);
  1414 
  1415 	// Update Toolbar
  1416 	standardFlags->updateToolbar();
  1417 
  1418 	// Update actions in mapeditor
  1419 	mapEditor->updateActions();
  1420 }
  1421 
  1422 void BranchObj::unselect()
  1423 {
  1424 	LinkableMapObj::unselect();
  1425 	// Delete any messages like vymLink in StatusBar
  1426 	mainWindow->statusMessage ("");
  1427 
  1428 	// save note from editor and set flag
  1429 	// text is done by updateNoteFlag(), just save
  1430 	// filename here
  1431 	note.setFilenameHint (textEditor->getFilename());
  1432 
  1433 	// reset temporary unscroll, if we have scrolled parents somewhere
  1434 	if (parObj) ((BranchObj*)(parObj))->resetTmpUnscroll();
  1435 
  1436 	// Erase content of editor 
  1437 	textEditor->setInactive();
  1438 
  1439 	// unselect all buttons in toolbar
  1440 	standardFlagsDefault->updateToolbar();
  1441 }
  1442 
  1443 QString BranchObj::getSelectString()
  1444 {
  1445 	QString s;
  1446 	if (parObj)
  1447 	{
  1448 		if (depth==1)
  1449 			s= "bo:" + QString("%1").arg(getNum());
  1450 		else	
  1451 			s= ((BranchObj*)(parObj))->getSelectString() + ",bo:" + QString("%1").arg(getNum());
  1452 	} else
  1453 		s="mc:";
  1454 	return s;
  1455 }
  1456