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