branchobj.cpp
author insilmaril
Thu, 23 Apr 2009 12:15:31 +0000
changeset 755 ed5b407975b3
parent 754 db0ec4bcf416
child 756 a8a5c7288f57
permissions -rw-r--r--
more data in Tree, less in Map
     1 #include "branchobj.h"
     2 
     3 #include "branchitem.h"
     4 #include "geometry.h"
     5 #include "mapeditor.h"
     6 #include "mainwindow.h"
     7 #include "misc.h"
     8 
     9 //class TextEditor; //FIXME-3
    10 
    11 //extern TextEditor *textEditor;
    12 //extern Main *mainWindow;
    13 //extern FlagRowObj *standardFlagsDefault;
    14 
    15 
    16 /////////////////////////////////////////////////////////////////
    17 // BranchObj
    18 /////////////////////////////////////////////////////////////////
    19 
    20 BranchObj::BranchObj () :OrnamentedObj()	// FIXME-3 needed at all?
    21 {
    22 //    cout << "Const BranchObj ()\n";
    23     setParObj (this);	
    24     init();
    25 }
    26 
    27 BranchObj::BranchObj (QGraphicsScene* s):OrnamentedObj (s)// FIXME-3 needed at all?
    28 {
    29 //    cout << "Const BranchObj (s)  \n";
    30 	parObj=NULL;
    31     scene=s;
    32 	init();
    33 }
    34 
    35 BranchObj::BranchObj (QGraphicsScene* s, LinkableMapObj* p):OrnamentedObj (s)// FIXME-3 needed at all?
    36 {
    37 //    cout << "Const BranchObj (s,p)\n";
    38     scene=s;
    39     setParObj (p);	
    40 	if (treeItem->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 (QPointF (x() - parObj->getChildPos().x() , 
    45 								(y() - parObj->getChildPos().y() ) ) );
    46     init();
    47 }
    48 
    49 BranchObj::~BranchObj ()
    50 {
    51 	// If I'm animated, I need to un-animate myself first
    52 	if (anim.isAnimated() )
    53 	{
    54 		anim.setAnimated (false);
    55 		model->stopAnimation (this);
    56 	}
    57 
    58 	cout << "Destr BranchObj of "<<this<<" ("<<treeItem->getHeading().toStdString()<<")"<<endl;
    59 	// Check, if this branch was the last child to be deleted
    60 	// If so, unset the scrolled flags in parent // FIXME-2 better do this in model?
    61 
    62 	/*
    63 	BranchObj *po=(BranchObj*)parObj;
    64 	BranchObj *bo;
    65 	if (po)
    66 	{
    67 		bo=((BranchObj*)parObj)->getLastBranch();
    68 		if (bo) po->unScroll();
    69 	}
    70 	*/
    71 	clear();
    72 }
    73 
    74 bool BranchObj::operator< ( const BranchObj & other )
    75 {
    76     return  angle < other.angle;
    77 }
    78 
    79 bool BranchObj::operator== ( const BranchObj & other )
    80 {
    81     return angle == other.angle;
    82 }
    83 
    84 void BranchObj::init () 
    85 {
    86 	if (parObj)
    87 	{
    88 		absPos=getRandPos();
    89 		absPos+=parObj->getChildPos();
    90 	}
    91 
    92     setChildObj(this);
    93 
    94 	includeImagesVer=false;
    95 	includeImagesHor=false;
    96 }
    97 
    98 void BranchObj::copy (BranchObj* other)
    99 {
   100     OrnamentedObj::copy(other);
   101 
   102 /* FIXME-3 not needed
   103 	for (int i=0; i<other->treeItem->branchCount(); ++i)
   104 		// Make deep copy of b
   105 		// Because addBranch again calls copy for the children,
   106 		// Those will get a deep copy, too
   107 		addBranch(other->branch.at(i) );	
   108 */		
   109 
   110 	for (int i=0; i<other->floatimage.size(); ++i)
   111 		addFloatImage  (other->floatimage.at(i));
   112 	
   113 	setVisibility (other->visible);
   114 
   115 	angle=other->angle;
   116 
   117     positionBBox();
   118 }
   119 
   120 void BranchObj::clear() 
   121 {
   122 	//setVisibility (true); //FIXME-4 needed?
   123 
   124 	while (!floatimage.isEmpty())
   125 		delete floatimage.takeFirst();
   126 
   127 	while (!xlink.isEmpty())
   128 		delete xlink.takeFirst();
   129 }
   130 
   131 bool isAbove (BranchObj* a, BranchObj *b)
   132 {
   133 	if (a->angle < b->angle)
   134 		return true;
   135 	else	
   136 		return false;
   137 }
   138 
   139 void BranchObj::setParObjTmp(LinkableMapObj* lmo, QPointF m, int off)
   140 {
   141 	// Temporary link to lmo
   142 	// m is position of mouse pointer 
   143 	// offset 0: default 1: below lmo   -1 above lmo  (if possible)
   144 
   145 
   146 	BranchObj* o=(BranchObj*)(lmo);
   147 	if (!parObjTmpBuf) 
   148 		parObjTmpBuf=parObj;
   149 
   150 	// ignore mapcenter and mainbranch
   151 	if (treeItem->depth()<2) off=0;
   152 	if (off==0)
   153 		link2ParPos=false;
   154 	else
   155 		link2ParPos=true;
   156 	parObj=o;
   157 
   158 	// FIXME-2 depth=parObj->getDepth()+1;
   159 
   160 	// setLinkStyle calls updateLink, only set it once
   161 	if (style!=getDefLinkStyle() ) setLinkStyle (getDefLinkStyle());
   162 
   163 	// Move temporary to new position at destination
   164 	// Usually the positioning would be done by reposition(),
   165 	// but then also the destination branch would "Jump" around...
   166 	// Better just do it approximately
   167 	if (treeItem->depth()==1)
   168 	{	// new parent is the mapcenter itself
   169 
   170 		QPointF p= normalise ( QPointF (m.x() - o->getChildPos().x(),
   171 									  m.y() - o->getChildPos().y() ));
   172 		if (p.x()<0) p.setX( p.x()-bbox.width() );
   173 		move2RelPos (p);
   174 	} else
   175 	{	
   176 		qreal y;
   177 		if (off==0)
   178 		{
   179 			// new parent is just a branch, link to it
   180 			QRectF t=o->getBBoxSizeWithChildren();
   181 			if (o->getTreeItem()->getLastBranch())
   182 				y=t.y() + t.height() ;
   183 			else
   184 				y=t.y();
   185 
   186 		} else
   187 		{
   188 			if (off<0)
   189 				// we want to link above lmo
   190 				y=o->y() - height() + 5;
   191 			else	
   192 				// we want to link below lmo
   193 				// Bottom of sel should be 5 pixels above
   194 				// the bottom of the branch _below_ the target:
   195 				// Don't try to find that branch, guess 12 pixels
   196 				y=o->getChildPos().y()  -height() + 12; 
   197 		}	
   198 		if (o->getOrientation()==LinkableMapObj::LeftOfCenter)
   199 			move ( o->getChildPos().x() - linkwidth, y );
   200 		else	
   201 			move (o->getChildPos().x() + linkwidth, y );
   202 	}	
   203 
   204 	// updateLink is called implicitly in move
   205 	requestReposition();	
   206 }
   207 
   208 void BranchObj::unsetParObjTmp()
   209 {
   210 	if (parObjTmpBuf) 
   211 	{
   212 		link2ParPos=false;
   213 		parObj=parObjTmpBuf;
   214 		parObjTmpBuf=NULL;
   215 		//FIXME-2 depth=parObj->getDepth()+1;
   216 		setLinkStyle (getDefLinkStyle() );
   217 		updateLink();
   218 	}		
   219 }
   220 
   221 void BranchObj::setVisibility(bool v, int toDepth)
   222 {
   223 	BranchItem *bi=(BranchItem*)treeItem;
   224     if (bi->depth() <= toDepth)
   225     {
   226 		frame->setVisibility(v);
   227 		heading->setVisibility(v);
   228 		systemFlags->setVisibility(v);
   229 		standardFlags->setVisibility(v);
   230 		LinkableMapObj::setVisibility (v);
   231 		int i;
   232 		for (i=0; i<floatimage.size(); ++i)
   233 			floatimage.at(i)->setVisibility (v);
   234 		for (i=0; i<xlink.size(); ++i)	
   235 			xlink.at(i)->setVisibility ();	
   236 
   237 		// Only change children, if I am not scrolled
   238 		if (! bi->isScrolled() && (bi->depth() < toDepth))
   239 		{
   240 			// Now go recursivly through all children
   241 			for (i=0; i<treeItem->branchCount(); ++i)
   242 				treeItem->getBranchObjNum(i)->setVisibility (v,toDepth);	
   243 		}
   244     } // depth <= toDepth	
   245 	requestReposition();
   246 }	
   247 
   248 void BranchObj::setVisibility(bool v)
   249 {
   250     setVisibility (v,MAX_DEPTH);
   251 }
   252 
   253 
   254 void BranchObj::setLinkColor ()
   255 {
   256 	// Overloaded from LinkableMapObj
   257 	// BranchObj can use color of heading
   258 
   259 	if (model)
   260 	{
   261 		if (model->getMapLinkColorHint()==HeadingColor)
   262 			LinkableMapObj::setLinkColor (heading->getColor() );
   263 		else	
   264 			LinkableMapObj::setLinkColor ();
   265 	}		
   266 }
   267 
   268 void BranchObj::setColorSubtree(QColor col)
   269 {
   270 	setColor (col);
   271 	for (int i=0; i<treeItem->branchCount(); ++i)
   272 		treeItem->getBranchObjNum(i)->setColorSubtree(col);
   273 }
   274 
   275 void BranchObj::updateContentSize()
   276 {
   277 	calcBBoxSize();
   278 	positionBBox();
   279 	requestReposition();
   280 }
   281 
   282 void BranchObj::positionContents()
   283 {
   284     for (int i=0; i<floatimage.size(); ++i )
   285 		floatimage.at(i)->reposition();
   286 	OrnamentedObj::positionContents();
   287 }
   288 
   289 void BranchObj::move (double x, double y)
   290 {
   291 	OrnamentedObj::move (x,y);
   292     for (int i=0; i<floatimage.size(); ++i )
   293 		floatimage.at(i)->reposition();
   294     positionBBox();
   295 }
   296 
   297 void BranchObj::move (QPointF p)
   298 {
   299 	move (p.x(), p.y());
   300 }
   301 
   302 void BranchObj::moveBy (double x, double y)
   303 {
   304 	OrnamentedObj::moveBy (x,y);
   305 	for (int i=0; i<treeItem->branchCount(); ++i)
   306 		treeItem->getBranchObjNum(i)->moveBy (x,y);
   307     positionBBox();
   308 }
   309 	
   310 void BranchObj::moveBy (QPointF p)
   311 {
   312 	moveBy (p.x(), p.y());
   313 }
   314 
   315 
   316 void BranchObj::positionBBox()
   317 {
   318 	QPointF ap=getAbsPos();
   319 	bbox.moveTopLeft (ap);
   320 	positionContents();
   321 
   322 	// set the frame
   323 	frame->setRect(QRectF(bbox.x(),bbox.y(),bbox.width(),bbox.height() ) );
   324 
   325 	// Update links to other branches
   326 	for (int i=0; i<xlink.size(); ++i)
   327 		xlink.at(i)->updateXLink();
   328 }
   329 
   330 void BranchObj::calcBBoxSize()
   331 {
   332     QSizeF heading_r=heading->getSize();
   333     qreal heading_w=(qreal) heading_r.width() ;
   334     qreal heading_h=(qreal) heading_r.height() ;
   335     QSizeF sysflags_r; //FIXME-1 =systemFlags->getSize();
   336 	qreal sysflags_h=0;//sysflags_r.height();
   337 	qreal sysflags_w=0;//sysflags_r.width();
   338     QSizeF stanflags_r; //FIXME-1 =standardFlags->getSize();
   339 	qreal stanflags_h=0; //stanflags_r.height();
   340 	qreal stanflags_w=0; //stanflags_r.width();
   341     qreal w;
   342     qreal h;
   343 
   344 	// set width to sum of all widths
   345 	w=heading_w + sysflags_w + stanflags_w;
   346 	// set height to maximum needed height
   347 	h=max (sysflags_h,stanflags_h);
   348 	h=max (h,heading_h);
   349 
   350 	// Save the dimension of flags and heading
   351 	ornamentsBBox.setSize ( QSizeF(w,h));
   352 
   353 	// clickBox includes Flags and Heading
   354     clickBox.setSize (ornamentsBBox.size() );
   355 
   356 	// Floatimages 
   357 	QPointF rp;
   358 
   359 	topPad=botPad=leftPad=rightPad=0;
   360 	if (includeImagesVer || includeImagesHor)
   361 	{
   362 		if (treeItem->imageCount()>0)
   363 		{
   364 			for (int i=0; i<floatimage.size(); ++i )
   365 			{
   366 				rp=floatimage.at(i)->getRelPos();
   367 				if (includeImagesVer)
   368 				{
   369 					if (rp.y() < 0) 
   370 						topPad=max (topPad,-rp.y()-h);
   371 					if (rp.y()+floatimage.at(i)->height() > 0)
   372 						botPad=max (botPad,rp.y()+floatimage.at(i)->height());
   373 				}		
   374 				if (includeImagesHor)
   375 				{
   376 					if (orientation==LinkableMapObj::RightOfCenter)
   377 					{
   378 						if (-rp.x()-w > 0) 
   379 							leftPad=max (leftPad,-rp.x()-w);
   380 						if (rp.x()+floatimage.at(i)->width() > 0)
   381 							rightPad=max (rightPad,rp.x()+floatimage.at(i)->width());
   382 					} else
   383 					{
   384 						if (rp.x()< 0) 
   385 							leftPad=max (leftPad,-rp.x());
   386 						if (rp.x()+floatimage.at(i)->width() > w)
   387 							rightPad=max (rightPad,rp.x()+floatimage.at(i)->width()-w);
   388 					}
   389 				}		
   390 			}	
   391 		}	
   392 		h+=topPad+botPad;
   393 		w+=leftPad+rightPad;
   394 	}
   395 
   396 	// Frame thickness
   397     w+=frame->getPadding();
   398     h+=frame->getPadding();
   399 	
   400 	// Finally set size
   401     bbox.setSize (QSizeF (w,h));
   402 }
   403 
   404 void BranchObj::setDockPos()
   405 {
   406 	// Sets childpos and parpos depending on orientation
   407 	if (getOrientation()==LinkableMapObj::LeftOfCenter )
   408     {
   409 		childPos=QPointF (
   410 			ornamentsBBox.bottomLeft().x(), 
   411 			bottomlineY);
   412 		parPos=QPointF (
   413 			ornamentsBBox.bottomRight().x(),
   414 			bottomlineY);
   415     } else
   416     {
   417 		childPos=QPointF (
   418 			ornamentsBBox.bottomRight().x(), 
   419 			bottomlineY);
   420 		parPos=QPointF (
   421 			ornamentsBBox.bottomLeft().x(),
   422 			bottomlineY);
   423     }
   424 }
   425 
   426 void BranchObj::updateHeading()
   427 {
   428 	if (!treeItem)
   429 	{
   430 		qWarning ("BranchObj::udpateHeading treeItem==NULL");
   431 		return;
   432 	}
   433 	cout << "BO::updateHeading: "<<treeItem->getHeading().toStdString()<<endl;
   434 	heading->setText (treeItem->getHeading() );
   435 	updateContentSize();
   436 }
   437 
   438 QString BranchObj::saveToDir (const QString &tmpdir,const QString &prefix, const QPointF& offset)
   439 {
   440 	// Cloudy stuff can be hidden during exports
   441 	// FIXME-1 if (hidden) return "";
   442 
   443 	// Update of note is usually done while unselecting a branch
   444 	// if (isNoteInEditor) getNoteFromTextEditor();		//FIXME-2 moved to TreeItem
   445 	
   446     QString s,a;
   447 	QString scrolledAttr;
   448 	if ( ((BranchItem*)treeItem)->isScrolled() ) 
   449 		scrolledAttr=attribut ("scrolled","yes");
   450 	else
   451 		scrolledAttr="";
   452 
   453 	// save area, if not scrolled
   454 	QString areaAttr;
   455 	if (!((BranchItem*) (treeItem->parent()) )->isScrolled() )
   456 	{
   457 		areaAttr=
   458 			attribut("x1",QString().setNum(absPos.x()-offset.x())) +
   459 			attribut("y1",QString().setNum(absPos.y()-offset.y())) +
   460 			attribut("x2",QString().setNum(absPos.x()+width()-offset.x())) +
   461 			attribut("y2",QString().setNum(absPos.y()+height()-offset.y()));
   462 
   463 	} else
   464 		areaAttr="";
   465 	
   466 	// Providing an ID for a branch makes export to XHTML easier
   467 	QString idAttr;
   468 	if (treeItem->xlinkCount()>0)
   469 		idAttr=attribut ("id",model->getSelectString(this)); //TODO directly access model
   470 	else
   471 		idAttr="";
   472 
   473     s=beginElement ("branch" 
   474 		+getOrnXMLAttr() 
   475 		+scrolledAttr 
   476 		+areaAttr 
   477 		+idAttr 
   478 		+getIncludeImageAttr() );
   479     incIndent();
   480 
   481 	// save heading
   482     s+=valueElement("heading", treeItem->getHeading(),
   483 		attribut ("textColor",QColor(heading->getColor()).name()));
   484 
   485 	// Save frame
   486 	if (frame->getFrameType()!=FrameObj::NoFrame) 
   487 		s+=frame->saveToDir ();
   488 
   489 	// save names of flags set
   490 	s+=standardFlags->saveToDir(tmpdir,prefix,0);
   491 	
   492 	// Save FloatImages
   493 	for (int i=0; i<floatimage.size(); ++i)
   494 		s+=floatimage.at(i)->saveToDir (tmpdir,prefix);
   495 
   496 	// save note
   497 	if (!treeItem->getNoteObj().isEmpty() )
   498 		s+=treeItem->getNoteObj().saveToDir();
   499 	
   500 	// Save branches
   501 	for (int i=0; i<treeItem->branchCount(); ++i)
   502 		s+=treeItem->getBranchObjNum(i)->saveToDir(tmpdir,prefix,offset);
   503 
   504 	// Save XLinks
   505 	QString ol;	// old link
   506 	QString cl;	// current link
   507 	for (int i=0; i<xlink.size(); ++i)
   508 	{
   509 		cl=xlink.at(i)->saveToDir();
   510 		if (cl!=ol)
   511 		{
   512 			s+=cl;
   513 			ol=cl;
   514 		} else
   515 		{
   516 			qWarning (QString("Ignoring of duplicate xLink in %1").arg(treeItem->getHeading()));
   517 		}
   518 	}	
   519 
   520     decIndent();
   521     s+=endElement   ("branch");
   522     return s;
   523 }
   524 
   525 void BranchObj::addXLink (XLinkObj *xlo)
   526 {
   527 	xlink.append (xlo);
   528 	
   529 }
   530 
   531 void BranchObj::removeXLinkRef (XLinkObj *xlo)
   532 {
   533 	xlink.removeAt (xlink.indexOf(xlo));
   534 }
   535 
   536 void BranchObj::deleteXLink(XLinkObj *xlo)
   537 {
   538 	xlo->deactivate();
   539 	if (!xlo->isUsed()) delete (xlo);
   540 }
   541 
   542 void BranchObj::deleteXLinkAt (int i)
   543 {
   544 	XLinkObj *xlo=xlink.at(i);
   545 	xlo->deactivate();
   546 	if (!xlo->isUsed()) delete(xlo);
   547 }
   548 
   549 XLinkObj* BranchObj::XLinkAt (int i)
   550 {
   551 	return xlink.at(i);
   552 }
   553 
   554 BranchObj* BranchObj::XLinkTargetAt (int i)
   555 {
   556 	if (i>=0 && i<xlink.size())
   557 	{
   558 		if (xlink.at(i))
   559 			return xlink.at(i)->otherBranch (this);
   560 	}
   561 	return NULL;
   562 }
   563 
   564 void BranchObj::setIncludeImagesVer(bool b)
   565 {
   566 	includeImagesVer=b;
   567 	calcBBoxSize();
   568 	positionBBox();
   569 	requestReposition();
   570 }
   571 
   572 bool BranchObj::getIncludeImagesVer()
   573 {
   574 	return includeImagesVer;
   575 }
   576 
   577 void BranchObj::setIncludeImagesHor(bool b)
   578 {
   579 	includeImagesHor=b;
   580 	calcBBoxSize();
   581 	positionBBox();
   582 	requestReposition();
   583 }
   584 
   585 bool BranchObj::getIncludeImagesHor()
   586 {
   587 	return includeImagesHor;
   588 }
   589 
   590 QString BranchObj::getIncludeImageAttr()
   591 {
   592 	QString a;
   593 	if (includeImagesVer)
   594 		a=attribut ("incImgV","true");
   595 	else
   596 		a=attribut ("incImgV","false");
   597 	if (includeImagesHor)
   598 		a+=attribut ("incImgH","true");
   599 	else
   600 		a+=attribut ("incImgH","false");
   601 	return a;	
   602 }
   603 
   604 FloatImageObj* BranchObj::addFloatImage ()
   605 {
   606 	FloatImageObj *newfi=new FloatImageObj (scene,this);
   607 	floatimage.append (newfi);
   608 	if ( ((BranchItem*)treeItem)->hasScrolledParent((BranchItem*)treeItem) )
   609 		newfi->setVisibility (false);
   610 	else	
   611 		newfi->setVisibility(visible);
   612 		/*
   613 	calcBBoxSize();
   614 	positionBBox();
   615 	*/
   616 	requestReposition();
   617 	return newfi;
   618 }
   619 
   620 FloatImageObj* BranchObj::addFloatImage (FloatImageObj *fio)
   621 {
   622 	FloatImageObj *newfi=new FloatImageObj (scene,this);
   623 	floatimage.append (newfi);
   624 	newfi->copy (fio);
   625 	if (((BranchItem*)treeItem)->hasScrolledParent((BranchItem*)treeItem) )
   626 		newfi->setVisibility (false);
   627 	else	
   628 		newfi->setVisibility(visible);
   629 		/*
   630 	calcBBoxSize();
   631 	positionBBox();
   632 	*/
   633 	requestReposition();
   634 	return newfi;
   635 }
   636 
   637 FloatImageObj* BranchObj::getFirstFloatImage ()
   638 {
   639     return floatimage.first();
   640 }
   641 
   642 FloatImageObj* BranchObj::getLastFloatImage ()
   643 {
   644     return floatimage.last();
   645 }
   646 
   647 FloatImageObj* BranchObj::getFloatImageNum (const uint &i)
   648 {
   649     return floatimage.at(i);
   650 }
   651 
   652 void BranchObj::removeFloatImage (FloatImageObj *fio)
   653 {
   654 	int i=floatimage.indexOf (fio);
   655 	if (i>-1) delete (floatimage.takeAt (i));
   656 	calcBBoxSize();
   657 	positionBBox();
   658 	requestReposition();
   659 }
   660 
   661 void BranchObj::savePosInAngle ()
   662 {
   663 	// Save position in angle
   664 	for (int i=0; i<treeItem->branchCount(); ++i)
   665 		treeItem->getBranchObjNum(i)->angle=i;
   666 }
   667 
   668 void BranchObj::setDefAttr (BranchModification mod)
   669 {
   670 	int fontsize;
   671 	switch (treeItem->depth())
   672 	{
   673 		case 0: fontsize=16; break;
   674 		case 1: fontsize=12; break;
   675 		default: fontsize=10; break;
   676 	}	
   677 
   678 	setLinkColor ();
   679 	setLinkStyle(getDefLinkStyle());
   680 	QFont font("Sans Serif,8,-1,5,50,0,0,0,0,0");
   681 	font.setPointSize(fontsize);
   682 	heading->setFont(font );
   683 
   684 	if (mod==NewBranch)
   685 		setColor (((BranchObj*)(parObj))->getColor());
   686 	
   687 	calcBBoxSize();
   688 }
   689 
   690 BranchObj* BranchObj::addBranch()	// FIXME-3 still needed?
   691 {
   692     BranchObj* newbo=new BranchObj(scene,this);
   693     newbo->setParObj(this);
   694 	newbo->setDefAttr(NewBranch);
   695 	/* FIXME-2 treeItem not set yet!!!
   696 	if ( ((BranchItem*)treeItem)->isScrolled() )
   697 		newbo->setVisibility (false);
   698 	else	
   699 		newbo->setVisibility(visible);
   700 	*/	
   701 	newbo->updateLink();	
   702 	requestReposition();
   703 	return newbo;
   704 }
   705 
   706 BranchObj* BranchObj::addBranch(BranchObj* bo)
   707 {
   708     BranchObj* newbo=new BranchObj(scene,this);
   709     //FIXME-1 branch.append (newbo);
   710     newbo->copy(bo);
   711     newbo->setParObj(this);
   712 	newbo->setDefAttr(MovedBranch);
   713 	if ( ((BranchItem*)treeItem)->isScrolled() )
   714 		newbo->setVisibility (false);
   715 	else	
   716 		newbo->setVisibility(bo->visible);
   717 	newbo->updateLink();	
   718 	requestReposition();
   719 	return newbo;
   720 }
   721 
   722 BranchObj* BranchObj::addBranchPtr(BranchObj* bo)
   723 {
   724 	//FIXME-1 branch.append (bo);
   725 	bo->setParObj (this);
   726 	//FIXME-2 bo->depth=depth+1;
   727 	bo->setDefAttr(MovedBranch);
   728 	BranchItem *bi=(BranchItem*)treeItem;
   729 	if ( bi->isScrolled() ) bi->tmpUnscroll();
   730 	//setLastSelectedBranch (bo);	//FIXME-3 needed?
   731 	return bo;
   732 }
   733 
   734 BranchObj* BranchObj::insertBranch(int pos)
   735 {
   736 	savePosInAngle();
   737 	// Add new bo and resort branches
   738 	BranchObj *newbo=addBranch ();
   739 	newbo->angle=pos-0.5;
   740 	//FIXME-1 qSort (branch.begin(),branch.end(), isAbove);
   741 	return newbo;
   742 }
   743 
   744 BranchObj* BranchObj::insertBranch(BranchObj* bo, int pos)
   745 {
   746 	savePosInAngle();
   747 	// Add new bo and resort branches
   748 	bo->angle=pos-0.5;
   749 	BranchObj *newbo=addBranch (bo);
   750 	//FIXME-1 qSort (branch.begin(),branch.end(), isAbove);
   751 	return newbo;
   752 }
   753 
   754 BranchObj* BranchObj::insertBranchPtr (BranchObj* bo, int pos)
   755 {
   756 	savePosInAngle();
   757 	// Add new bo and resort branches
   758 	bo->angle=pos-0.5;
   759 	//FIXME-4 branch.append (bo);
   760 	bo->setParObj (this);
   761 	//FIXME-2 bo->depth=depth+1;
   762 	bo->setDefAttr (MovedBranch);
   763 	BranchItem *bi=(BranchItem*)treeItem;
   764 	if ( bi->isScrolled() ) bi->tmpUnscroll();
   765 	//setLastSelectedBranch (bo); //FIXME-3 needed?
   766 	//FIXME-2 qSort (branch.begin(),branch.end(), isAbove);
   767 	return bo;
   768 }
   769 
   770 void BranchObj::removeBranchHere(BranchObj* borem)	// FIXME-1 getNum no longer available
   771 {
   772 /*
   773 	// This removes the branch bo from list, but 
   774 	// inserts its children at the place of bo
   775 	BranchObj *bo;
   776 	bo=borem->getLastBranch();
   777 	int pos=borem->getNum();
   778 	while (bo)
   779 	{
   780 		bo->linkTo (this,pos+1);
   781 		bo=borem->getLastBranch();
   782 	}	
   783 	removeBranch (borem);
   784 	*/
   785 }
   786 
   787 void BranchObj::removeChildren()	// FIXME-3 not needed here
   788 {
   789 	clear();
   790 }
   791 
   792 void BranchObj::removeBranch(BranchObj* bo)	// FIXME-1 not needed here
   793 {
   794 /*
   795     // if bo is not in branch remove returns false, we
   796     // don't care...
   797 	
   798 	int i=branch.indexOf(bo);
   799     if (i>=0)
   800 	{
   801 		delete (bo);
   802 		branch.removeAt (i);
   803 	} else
   804 		qWarning ("BranchObj::removeBranch tried to remove non existing branch?!\n");
   805 	requestReposition();
   806 	*/
   807 }
   808 
   809 void BranchObj::removeBranchPtr(BranchObj* bo)	// FIXME-1 not needed here
   810 {
   811 /*
   812 	int i=branch.indexOf(bo);
   813 	
   814 	if (i>=0)
   815 		branch.removeAt (i);
   816 	else	
   817 		qWarning ("BranchObj::removeBranchPtr tried to remove non existing branch?!\n");
   818 	requestReposition();
   819 */	
   820 }
   821 
   822 bool BranchObj::canMoveBranchUp() 
   823 {
   824 	/* FIXME-1 move to BranchItem
   825 	if (!parObj || depth==1) return false;
   826 	BranchObj* par=(BranchObj*)parObj;
   827 	if (this==par->getTreeItem()->getFirstBranch())
   828 		return false;
   829 	else
   830 		return true;
   831 		*/
   832 return false;
   833 }
   834 
   835 BranchObj* BranchObj::moveBranchUp(BranchObj* bo1) // FIXME-1
   836 {
   837 /*
   838 	savePosInAngle();
   839     int i=branch.indexOf(bo1);
   840     if (i>0) 
   841 	{	// -1 if bo1 not found 
   842 		treeItem->getBranchObjNum(i)->angle--;
   843 		treeItem->getBranchObjNum(i-1)->angle++;
   844 		qSort (branch.begin(),branch.end(), isAbove);
   845 		return treeItem->getBranchObjNum(i);
   846 	} else
   847 */	
   848 		return NULL;
   849 }
   850 
   851 bool BranchObj::canMoveBranchDown() 
   852 {
   853 	/* FIXME-1 move to BranchItem
   854 	if (!parObj|| depth==1) return false;
   855 	BranchObj* par=(BranchObj*)parObj;
   856 	if (this==par->getTreeItem()->getLastBranch())
   857 		return false;
   858 	else
   859 		return true;
   860 	*/
   861 return false;	
   862 }
   863 
   864 BranchObj* BranchObj::moveBranchDown(BranchObj* bo1)// FIXME-1
   865 {
   866 /*
   867 	savePosInAngle();
   868     int i=branch.indexOf(bo1);
   869 	int j;
   870 	if (i <treeItem->branchCount())
   871 	{
   872 		j = i+1;
   873 		treeItem->getBranchObjNum(i)->angle++;
   874 		treeItem->getBranchObjNum(j)->angle--;
   875 		qSort (branch.begin(),branch.end(), isAbove);
   876 		return treeItem->getBranchObjNum(i);
   877 	} else
   878 */	
   879 		return NULL;
   880 }
   881 
   882 void BranchObj::sortChildren() //FIXME-1  
   883 {
   884 /*
   885 	int childCount=branch.count(); 
   886 	int curChildIndex;
   887 	bool madeChanges=false;
   888 	do
   889 	{
   890 		madeChanges=false;
   891 		for(curChildIndex=1;curChildIndex<childCount;curChildIndex++){
   892 			BranchObj* curChild=(BranchObj*)treeItem->getBranchObjNum(curChildIndex);
   893 			BranchObj* prevChild=(BranchObj*)treeItem->getBranchObjNum(curChildIndex-1);
   894 			if(prevChild->heading->text().compare(curChild->heading->text())>0)
   895 			{
   896 				this->moveBranchUp(curChild);
   897 				madeChanges=true;
   898 			}
   899 		}
   900 	}while(madeChanges);
   901 */
   902 }
   903 
   904 
   905 BranchObj* BranchObj::linkTo (BranchObj* dst, int pos)
   906 {
   907 	// Find current parent and 
   908 	// remove pointer to myself there
   909 	if (!dst) return NULL;
   910 	BranchObj *par=(BranchObj*)parObj;
   911 	if (par)
   912 		par->removeBranchPtr (this);
   913 	else
   914 		return NULL;
   915 
   916 /* FIXME-1
   917 	// Create new pointer to myself at dst
   918 	if (pos<0||dst->getDepth()==0)
   919 	{	
   920 		// links myself as last branch at dst
   921 		dst->addBranchPtr (this);
   922 		updateLink();
   923 		return this;
   924 	} else
   925 	{
   926 		// inserts me at pos in parent of dst
   927 		if (par)
   928 		{
   929 			BranchObj *bo=dst->insertBranchPtr (this,pos);
   930 			bo->setDefAttr(MovedBranch);
   931 			updateLink();
   932 			return bo;
   933 
   934 		} else
   935 			return NULL;
   936 	}	
   937 */	
   938 }
   939 
   940 void BranchObj::alignRelativeTo (QPointF ref,bool alignSelf)
   941 {
   942 	qreal th = bboxTotal.height();	
   943 	int depth=treeItem->depth();
   944 // TODO testing
   945 /*
   946 
   947 	QString h=QString (depth,' ');
   948 	h+=treeItem->getHeading();
   949 	h+=QString (15,' ');
   950 	h.truncate (15);
   951 	QPointF pp; if (parObj) pp=parObj->getChildPos();
   952 	cout << "BO::alignRelTo ";
   953 	cout<<h.toStdString();
   954 	cout << "    d="<<depth<<
   955 //cout<<  "  ref="<<ref<<
   956       	"  bbox.tL="<<bboxTotal.topLeft()<<
   957 		"  absPos="<<absPos<<
   958 //		"  relPos="<<relPos<<
   959 //		"  parPos="<<pp<<
   960 		"  w="<<bbox.width()<<
   961 		"  h="<<bbox.height()<<
   962 //		"  orient="<<orientation<<
   963 //		"  alignSelf="<<alignSelf<<
   964 //		"  scrolled="<<((BranchItem*)treeItem)->isScrolled()<<
   965 //		"  pad="<<topPad<<","<<botPad<<","<<leftPad<<","<<rightPad<<
   966 //		"  hidden="<<hidden<<
   967 		"  th="<<th<<
   968 		endl;
   969 */
   970 
   971 	setOrientation();
   972 	//updateLink();
   973 
   974 	if (depth==1 && parObj)
   975 	{
   976 		// Position relatively, if needed
   977 		//if (useRelPos) move2RelPos (relPos.x(), relPos.y());
   978 
   979 		// Calc angle to mapCenter if I am a mainbranch
   980 		// needed for reordering the mainbranches clockwise 
   981 		// around mapcenter 
   982 		angle=getAngle (QPointF ((int)(x() - parObj->getChildPos().x() ), 
   983 								(int)(y() - parObj->getChildPos().y() ) ) );
   984 	}							
   985 	if (depth>1)
   986     {
   987 		// Align myself depending on orientation and parent, but
   988 		// only if I am not a mainbranch or mapcenter itself
   989 
   990 		if (anim.isAnimated())
   991 		{
   992 			move2RelPos(anim);
   993 		} else
   994 		{
   995 			LinkableMapObj::Orientation o;
   996 			o=parObj->getOrientation();
   997 			if (alignSelf)
   998 				switch (orientation) 
   999 				{
  1000 					case LinkableMapObj::LeftOfCenter:
  1001 						move (ref.x() - bbox.width(), ref.y() + (th-bbox.height())/2 );
  1002 						//move (ref.x() , ref.y() + (th-bbox.height())/2 );
  1003 					break;
  1004 					case LinkableMapObj::RightOfCenter:	
  1005 						move (ref.x() , ref.y() + (th-bbox.height())/2  );
  1006 					break;
  1007 					default:
  1008 						qWarning ("LMO::alignRelativeTo: oops, no orientation given...");
  1009 					break;
  1010 			}
  1011 		}
  1012     }		
  1013 
  1014 	if ( ((BranchItem*)treeItem)->isScrolled() ) return;
  1015 
  1016     // Set reference point for alignment of children
  1017     QPointF ref2;
  1018     if (orientation==LinkableMapObj::LeftOfCenter)
  1019 		ref2.setX(bbox.topLeft().x() - linkwidth);
  1020     else	
  1021 		ref2.setX(bbox.topRight().x() + linkwidth);
  1022 
  1023 	if (depth==1)
  1024 		ref2.setY(absPos.y()-(bboxTotal.height()-bbox.height())/2);
  1025 	else	
  1026 		ref2.setY(ref.y() );	
  1027 
  1028     // Align the children depending on reference point 
  1029 	for (int i=0; i<treeItem->branchCount(); ++i)
  1030     {	
  1031 		if (!treeItem->getBranchNum(i)->isHidden())
  1032 		{
  1033 			treeItem->getBranchObjNum(i)->alignRelativeTo (ref2,true);
  1034 
  1035 			// append next branch below current one
  1036 			ref2.setY(ref2.y() + treeItem->getBranchObjNum(i)->getBBoxSizeWithChildren().height() );
  1037 		}
  1038     }
  1039 }
  1040 
  1041 
  1042 void BranchObj::reposition()
  1043 {	
  1044 /* TODO testing only
  1045 	if (!treeItem->getHeading().isEmpty())
  1046 		cout << "BO::reposition  "<<qPrintable(treeItem->getHeading())<<endl;
  1047 	else	
  1048 		cout << "BO::reposition  ???"<<endl;
  1049 //	cout << "  orient="<<orientation<<endl;
  1050 */		
  1051 
  1052 	if (treeItem->depth()==0)
  1053 	{
  1054 		// only calculate the sizes once. If the deepest LMO 
  1055 		// changes its height,
  1056 		// all upper LMOs have to change, too.
  1057 		calcBBoxSizeWithChildren();
  1058 		updateLink();	// This update is needed if the scene is resized 
  1059 						// due to excessive moving of a FIO
  1060 
  1061 	    alignRelativeTo ( QPointF (absPos.x(),
  1062 			absPos.y()-(bboxTotal.height()-bbox.height())/2) );
  1063 		//FIXME-2 qSort (branch.begin(),branch.end(), isAbove);
  1064 		positionBBox();	// Reposition bbox and contents
  1065 	} else
  1066 	{
  1067 		// This is only important for moving branches:
  1068 		// For editing a branch it isn't called...
  1069 	    alignRelativeTo ( QPointF (absPos.x(),
  1070 							absPos.y()-(bboxTotal.height()-bbox.height())/2) );
  1071 	}
  1072 }
  1073 
  1074 void BranchObj::unsetAllRepositionRequests()
  1075 {
  1076 	repositionRequest=false;
  1077 	for (int i=0; i<treeItem->branchCount(); ++i)
  1078 		treeItem->getBranchObjNum(i)->unsetAllRepositionRequests();
  1079 }
  1080 
  1081 
  1082 QPolygonF BranchObj::shape()
  1083 {
  1084 	QPolygonF p;
  1085 
  1086 	QRectF r=getTotalBBox();
  1087 	if (orientation==LinkableMapObj::LeftOfCenter)
  1088 		p   <<r.bottomLeft()
  1089 			<<r.topLeft()
  1090 			<<QPointF (bbox.topLeft().x(), r.topLeft().y() )
  1091 			<<bbox.topRight()
  1092 			<<bbox.bottomRight()
  1093 			<<QPointF (bbox.bottomLeft().x(), r.bottomLeft().y() ) ;
  1094 	else		
  1095 		p   <<r.bottomRight()
  1096 			<<r.topRight()
  1097 			<<QPointF (bbox.topRight().x(), r.topRight().y() )
  1098 			<<bbox.topLeft()
  1099 			<<bbox.bottomLeft()
  1100 			<<QPointF (bbox.bottomRight().x(), r.bottomRight().y() ) ;
  1101 	return p;
  1102 }
  1103 
  1104 QRectF BranchObj::getTotalBBox()
  1105 {
  1106 	QRectF r=bbox;
  1107 
  1108 	if ( ((BranchItem*)treeItem)->isScrolled() ) return r;
  1109 
  1110 	for (int i=0; i<treeItem->branchCount(); ++i)
  1111 		if (!treeItem->getBranchNum(i)->isHidden())
  1112 			r=addBBox(treeItem->getBranchObjNum(i)->getTotalBBox(),r);
  1113 
  1114 /* FIXME-3 lots of occurences of treeItem->getBranchObjNum(i) in branchobj.cpp
  1115             better check if they are not NULL and maybe simplify...
  1116 			(have been NULL at least in calcBBoxSizeWithChilds...)
  1117 */			
  1118 
  1119 /*
  1120 	FIXME-1 for (int i=0; i<floatimage.size(); ++i)
  1121 		if (!floatimage.at(i)->isHidden())
  1122 			r=addBBox(floatimage.at(i)->getTotalBBox(),r);
  1123 	*/	
  1124 	return r;
  1125 }
  1126 
  1127 QRectF BranchObj::getBBoxSizeWithChildren()
  1128 {
  1129 	return bboxTotal;
  1130 }
  1131 
  1132 void BranchObj::calcBBoxSizeWithChildren()
  1133 {	
  1134 	// This is initially called only from reposition and
  1135 	// and only for mapcenter. So it won't be
  1136 	// called more than once for a single user 
  1137 	// action
  1138 	
  1139 
  1140 	// Calculate size of LMO including all children (to align them later)
  1141 	bboxTotal.setX(bbox.x() );
  1142 	bboxTotal.setY(bbox.y() );
  1143 
  1144 	// if branch is scrolled, ignore children, but still consider floatimages
  1145 	BranchItem *bi=(BranchItem*)treeItem;
  1146 	if ( bi->isScrolled() ) 
  1147 	{
  1148 		bboxTotal.setWidth (bbox.width());
  1149 		bboxTotal.setHeight(bbox.height());
  1150 		return;
  1151 	}
  1152 	
  1153 	if (bi->isHidden())
  1154 	{
  1155 		bboxTotal.setWidth (0);
  1156 		bboxTotal.setHeight(0);
  1157 		if (parObj)
  1158 		{
  1159 			bboxTotal.setX (parObj->x());
  1160 			bboxTotal.setY (parObj->y());
  1161 		} else
  1162 		{
  1163 			bboxTotal.setX (bbox.x());
  1164 			bboxTotal.setY (bbox.y());
  1165 		}
  1166 		return;
  1167 	}
  1168 	
  1169 	QRectF r(0,0,0,0);
  1170 	QRectF br;
  1171 	// Now calculate recursivly
  1172 	// sum of heights 
  1173 	// maximum of widths 
  1174 	// minimum of y
  1175 	for (int i=0; i<treeItem->branchCount(); i++)
  1176 	{
  1177 		if (!bi->getBranchNum(i)->isHidden())
  1178 		{
  1179 			bi->getBranchObjNum(i)->calcBBoxSizeWithChildren();
  1180 			br=bi->getBranchObjNum(i)->getBBoxSizeWithChildren();
  1181 			r.setWidth( max (br.width(), r.width() ));
  1182 			r.setHeight(br.height() + r.height() );
  1183 			if (br.y()<bboxTotal.y()) bboxTotal.setY(br.y());
  1184 		}
  1185 	}
  1186 	// Add myself and also
  1187 	// add width of link to sum if necessary
  1188 	if (bi->branchCount()<1)
  1189 		bboxTotal.setWidth (bbox.width() + r.width() );
  1190 	else	
  1191 		bboxTotal.setWidth (bbox.width() + r.width() + linkwidth);
  1192 	
  1193 	bboxTotal.setHeight(max (r.height(),  bbox.height()));
  1194 }
  1195 
  1196 /*
  1197 void BranchObj::select()	// FIXME-4 try to get rid of this in BO completely
  1198 {
  1199 	cout << "BO::select()\n";
  1200 	textEditor->setText(treeItem->getNoteObj().getNote() );
  1201 	QString fnh=treeItem->getNoteObj().getFilenameHint();
  1202 	if (fnh!="")
  1203 		textEditor->setFilenameHint(treeItem->getNoteObj().getFilenameHint() );
  1204 	else	
  1205 		textEditor->setFilenameHint(getHeading() );
  1206 	textEditor->setFontHint (treeItem->getNoteObj().getFontHint() );
  1207 	//isNoteInEditor=true;
  1208 
  1209 	// set selected and visible
  1210     LinkableMapObj::select();
  1211 
  1212     //if (po)	po->setLastSelectedBranch(this);  needed?
  1213 		
  1214 	// temporary unscroll, if we have scrolled parents somewhere
  1215 	if (parObj) ((BranchObj*)(parObj))->tmpUnscroll();
  1216 
  1217 	//moved to vymmodel or vymview...
  1218 	// Show URL and link in statusbar
  1219 	QString status;
  1220 	if (!url.isEmpty()) status+="URL: "+url+"  ";
  1221 	if (!vymLink.isEmpty()) status+="Link: "+vymLink;
  1222 	if (!status.isEmpty()) mainWindow->statusMessage (status);
  1223 
  1224 	// Update Toolbar
  1225 	updateFlagsToolbar();
  1226 
  1227 	// Update actions
  1228 	model->updateActions();
  1229 }
  1230 	*/
  1231 
  1232 /*
  1233 void BranchObj::unselect()	//FIXME-4 should not be needed
  1234 {
  1235 	cout << "BO::unselect()\n";
  1236 	LinkableMapObj::unselect();
  1237 	// Delete any messages like vymLink in StatusBar
  1238 	mainWindow->statusMessage ("");		//this causes segfault, when MainWindow is already gone in global destructor on quitting vym
  1239 
  1240 	// Save current note
  1241 	if (isNoteInEditor) getNoteFromTextEditor();
  1242 	isNoteInEditor=false;
  1243 
  1244 	// reset temporary unscroll, if we have scrolled parents somewhere
  1245 	if (parObj) ((BranchObj*)(parObj))->resetTmpUnscroll();
  1246 
  1247 	// Erase content of editor 
  1248 	textEditor->setInactive();
  1249 
  1250 	// unselect all buttons in toolbar
  1251 	standardFlagsDefault->updateToolbar();
  1252 }
  1253 */	
  1254 
  1255 QString BranchObj::getSelectString()
  1256 {
  1257 	return model->getSelectString (this);
  1258 }
  1259 
  1260 void BranchObj::setAnimation(const AnimPoint &ap)
  1261 {
  1262 	anim=ap;
  1263 }
  1264 
  1265 bool BranchObj::animate()
  1266 {
  1267 	anim.animate ();
  1268 	if ( anim.isAnimated() )
  1269 	{
  1270 		setRelPos (anim);
  1271 		return true;
  1272 	}
  1273 	parObj->reposition();	// we might have been relinked meanwhile
  1274 	return false;
  1275 }
  1276