linkablemapobj.cpp
author insilmaril
Mon, 15 Mar 2010 11:49:42 +0000
changeset 835 31841b366d5e
parent 823 0bba81dde1bc
child 836 62d621e3e550
permissions -rw-r--r--
Fixes for autoLayout (later)
     1 #include <iostream>
     2 #include <math.h>
     3 
     4 #include "linkablemapobj.h"
     5 #include "branchobj.h"
     6 #include "vymmodel.h"
     7 
     8 using namespace std;
     9 
    10 /////////////////////////////////////////////////////////////////
    11 // LinkableMapObj
    12 /////////////////////////////////////////////////////////////////
    13 
    14 /* FIXME-3
    15 LinkableMapObj::LinkableMapObj():MapObj()
    16 {
    17   //  cout << "Const LinkableMapObj ()\n";
    18     init ();
    19 }
    20 */
    21 
    22 LinkableMapObj::LinkableMapObj(QGraphicsScene* s, TreeItem *ti) :MapObj(s,ti)
    23 {
    24 //    cout << "Const LinkableMapObj s="<<s<<"  ti="<<ti<<"  treeItem="<<treeItem<<endl;
    25     init ();
    26 }
    27 
    28 LinkableMapObj::LinkableMapObj (LinkableMapObj* lmo) : MapObj (lmo->scene)
    29 {
    30     copy (lmo);
    31 }
    32 
    33 LinkableMapObj::~LinkableMapObj()
    34 {
    35 	//cout << "Destructor LMO\n";
    36     delete (bottomline);
    37 	delLink();
    38 }
    39 
    40 void LinkableMapObj::delLink()
    41 {
    42 	//bottomline->hide();
    43 	switch (style)
    44 	{
    45 		case Line:
    46 			delete (l);
    47 			break;
    48 		case Parabel:
    49 			while (!segment.isEmpty()) delete segment.takeFirst();
    50 			break;
    51 		case PolyLine:
    52 			delete (p);
    53 			break;
    54 		case PolyParabel:
    55 			delete (p);
    56 			break;
    57 		default:
    58 			break;
    59 	}		
    60 }
    61 
    62 void LinkableMapObj::init ()
    63 {
    64     parObj=NULL;
    65     parObjTmpBuf=NULL;
    66 	tmpParent=false;
    67     parPos=QPointF(0,0);
    68     childPos=QPointF(0,0);
    69 	link2ParPos=false;
    70     l=NULL;
    71 	p=NULL;
    72     orientation=UndefinedOrientation;
    73     linkwidth=20;		
    74 	thickness_start=8;
    75     style=UndefinedStyle;
    76 	linkpos=Bottom;
    77     arcsegs=13;
    78     
    79 // TODO instead of linkcolor pen.color() could be used	all around
    80 	pen.setWidth (1);
    81 	pen.setColor (linkcolor);
    82 	pen.setCapStyle ( Qt::RoundCap );
    83 
    84 	useBottomline=true;
    85 	bottomline=scene->addLine(QLineF(1,1,1,1),pen);
    86     bottomline->setZValue(Z_LINK);
    87     bottomline->show();
    88 
    89 	topPad=botPad=leftPad=rightPad=0;
    90 
    91 	repositionRequest=false;
    92 
    93 	// Rel Positions
    94 	relPos=QPointF(0,0);
    95 	useRelPos=false;
    96 }
    97 
    98 void LinkableMapObj::copy (LinkableMapObj* other)
    99 {
   100     MapObj::copy(other);
   101 	bboxTotal=other->bboxTotal;
   102     setLinkStyle(other->style);
   103     setLinkColor (other->linkcolor);
   104 	relPos=other->relPos;
   105 	treeItem=other->treeItem;
   106 }
   107 
   108 void LinkableMapObj::setParObj(LinkableMapObj* o)
   109 {
   110     parObj=o;
   111 }
   112 
   113 void LinkableMapObj::setParObjTmp(LinkableMapObj*,QPointF,int)	// FIXME-3 make pure virtual
   114 {
   115 }
   116 
   117 void LinkableMapObj::unsetParObjTmp()	// FIXME-3 make pure virtual
   118 {
   119 }
   120 
   121 bool LinkableMapObj::hasParObjTmp()
   122 {
   123 	return tmpParent;
   124 }
   125 
   126 void LinkableMapObj::setUseRelPos (const bool &b)
   127 {
   128 	useRelPos=b;
   129 }
   130 
   131 bool LinkableMapObj::getUseRelPos ()
   132 {
   133 	return useRelPos;
   134 }
   135 
   136 void LinkableMapObj::setRelPos()
   137 {
   138 	if (parObj)
   139 	{	
   140 		relPos.setX (absPos.x() - parObj->getChildPos().x() );
   141 		relPos.setY (absPos.y() - parObj->getChildPos().y() );
   142 		parObj->calcBBoxSize();
   143 	}	
   144 }
   145 
   146 void LinkableMapObj::setRelPos(const QPointF &p)
   147 {
   148 	relPos=p;
   149 	if (parObj)
   150 	{		
   151 		parObj->calcBBoxSize();
   152 		requestReposition();
   153 	}
   154 }
   155 
   156 QPointF LinkableMapObj::getRelPos()
   157 {
   158 	if (!parObj) 
   159 	{
   160 		qWarning("LMO::getRelPos parObj==0");
   161 		return QPointF();
   162 	}
   163 	return relPos;
   164 }
   165 
   166 qreal LinkableMapObj::getTopPad()
   167 {
   168 	return topPad;
   169 }
   170 
   171 qreal LinkableMapObj::getLeftPad()
   172 {
   173 	return leftPad;
   174 }
   175 
   176 qreal LinkableMapObj::getRightPad()
   177 {
   178 	return rightPad;
   179 }
   180 
   181 LinkableMapObj::Style LinkableMapObj::getDefLinkStyle (TreeItem *parent)
   182 {
   183 	VymModel *model=treeItem->getModel();
   184 	if (!model)
   185 	{
   186 		qWarning ("LMO::getDefLinkStyle   model=NULL");
   187 		//return UndefinedStyle;
   188 	}
   189 	Style ls=model->getMapLinkStyle();
   190 	int depth=1+parent->depth();
   191 	if (depth==0) return UndefinedStyle;
   192 	switch (ls)
   193 	{
   194 		case Line: 
   195 			return ls;
   196 			break;
   197 		case Parabel:
   198 			return ls;
   199 			break;
   200 		case PolyLine:	
   201 			if (depth>1)
   202 				return Line;
   203 			else	
   204 				return ls;
   205 			break;
   206 		case PolyParabel:	
   207 			if (depth>1)
   208 				return Parabel;
   209 			else	
   210 				return ls;
   211 			break;
   212 		default: 
   213 			break;	
   214 	}	
   215 	return UndefinedStyle;
   216 }
   217 
   218 void LinkableMapObj::setLinkStyle(Style newstyle)
   219 {
   220 	delLink();
   221 		
   222 	style=newstyle;
   223 
   224     if (parObj != NULL)
   225     {
   226 		QGraphicsLineItem *cl;
   227 		switch (style)
   228 		{
   229 			case UndefinedStyle:
   230 				bottomline->hide();
   231 				break;
   232 			case Line: 
   233 				l = scene->addLine(QLineF(1,1,1,1),pen);
   234 				l->setZValue(Z_LINK);
   235 				if (visible)
   236 					l->show();
   237 				else
   238 					l->hide();
   239 				break;
   240 			case Parabel:
   241 				for (int i=0;i<arcsegs;i++)
   242 				{
   243 					cl = scene->addLine(QLineF(i*5,0,i*10,100),pen);
   244 					cl->setZValue(Z_LINK);
   245 					if (visible)
   246 						cl->show();
   247 					else
   248 						cl->hide();
   249 					segment.append(cl);
   250 				}
   251 				pa0.resize (arcsegs+1);
   252 				break;
   253 			case PolyLine:	
   254 				p =scene->addPolygon(QPolygonF(),pen,linkcolor);
   255 				p->setZValue(Z_LINK);
   256 				if (visible)
   257 					p->show();
   258 				else
   259 					p->hide();
   260 				pa0.resize (3);
   261 				break;
   262 			case PolyParabel:	
   263 				p = scene->addPolygon(QPolygonF(),pen,linkcolor);
   264 				p->setZValue(Z_LINK);
   265 				if (visible)
   266 					p->show();
   267 				else
   268 					p->hide();
   269 				pa0.resize (arcsegs*2+2);
   270 				pa1.resize (arcsegs+1);
   271 				pa2.resize (arcsegs+1);
   272 				break;
   273 			default: 
   274 				break;	
   275 		}	
   276 	} 
   277 }
   278 
   279 LinkableMapObj::Style LinkableMapObj::getLinkStyle()
   280 {
   281 	return style;
   282 }
   283 
   284 void LinkableMapObj::setLinkPos(Position lp)
   285 {
   286 	linkpos=lp;
   287 }
   288 
   289 LinkableMapObj::Position LinkableMapObj::getLinkPos()
   290 {
   291 	return linkpos;
   292 }
   293 
   294 void LinkableMapObj::setLinkColor()
   295 {
   296 	// Overloaded in BranchObj and children
   297 	// here only set default color
   298 	VymModel *model=treeItem->getModel();
   299 	if (model)
   300 		setLinkColor (model->getMapDefLinkColor());
   301 }
   302 
   303 void LinkableMapObj::setLinkColor(QColor col)
   304 {
   305 	linkcolor=col;
   306 	pen.setColor(col);
   307     bottomline->setPen( pen );
   308 	switch (style)
   309 	{
   310 		case Line:
   311 			l->setPen( pen);
   312 			break;	
   313 		case Parabel:	
   314 			for (int i=0; i<segment.size(); ++i)
   315 				segment.at(i)->setPen( pen);
   316 			break;
   317 		case PolyLine:
   318 			p->setBrush( QBrush(col));
   319 			p->setPen( pen);
   320 			break;
   321 		case PolyParabel:	
   322 			p->setBrush( QBrush(col));
   323 			p->setPen( pen);
   324 			break;
   325 		default:
   326 			break;
   327 	} 
   328 }
   329 
   330 QColor LinkableMapObj::getLinkColor()
   331 {
   332 	return linkcolor;
   333 }
   334 
   335 void LinkableMapObj::setVisibility (bool v)
   336 {
   337 	MapObj::setVisibility (v);
   338 	updateVisibility();
   339 }
   340 
   341 void LinkableMapObj::setOrientation()
   342 {
   343 	Orientation orientOld=orientation;
   344 
   345 	if (!parObj) 
   346 	{
   347 		orientation=UndefinedOrientation;
   348 		return;
   349 	}
   350 		
   351     // Set orientation, first look for orientation of parent
   352     if (parObj->getOrientation() != UndefinedOrientation ) 
   353 		// use the orientation of the parent:
   354 		orientation=parObj->getOrientation();
   355     else
   356     {
   357 		// calc orientation depending on position rel to parent
   358 		if (absPos.x() < QPointF(parObj->getChildPos() ).x() )
   359 			orientation=LeftOfCenter; 
   360 		else
   361 			orientation=RightOfCenter;
   362     }
   363 	if (orientOld!=orientation) requestReposition();
   364 }
   365 
   366 void LinkableMapObj::updateVisibility()
   367 {
   368 	bool visnow=visible;
   369 
   370 	if (((MapItem*)treeItem)->getHideLinkUnselected()
   371 		&& treeItem->getModel()->getSelectedLMO() !=this)
   372 		visnow=false;
   373 
   374 	if (visnow) 
   375 	{
   376 		if (useBottomline)
   377 			bottomline->show();
   378 		else	
   379 			bottomline->hide();
   380 
   381 		switch (style)
   382 		{
   383 			case Line:
   384 				if (l) l->show();
   385 				break;
   386 			case Parabel:	
   387 				for (int i=0; i<segment.size(); ++i)
   388 					segment.at(i)->show();
   389 				break;	
   390 			case PolyLine:
   391 				if (!p) cout << "LMO::updateVis p==0 (PolyLine)\n"; //FIXME-3
   392 				if (p) p->show();
   393 				break;
   394 			case PolyParabel:	
   395 				if (!p) cout << "LMO::updateVis p==0 (PolyParabel) "<<treeItem->getHeading().toStdString()<<endl; //FIXME-3
   396 				if (p) p->show();
   397 				break;
   398 			default:
   399 				break;
   400 		}
   401 	} else 
   402 	{
   403 		bottomline->hide();
   404 		switch (style)
   405 		{
   406 			case Line:
   407 				if (l) l->hide();
   408 				break;
   409 			case Parabel:	
   410 				for (int i=0; i<segment.size(); ++i)
   411 					segment.at(i)->hide();
   412 				break;	
   413 			case PolyLine:
   414 				if (p) p->hide();
   415 				break;
   416 			case PolyParabel:	
   417 				if (p) p->hide();
   418 				break;
   419 			default:
   420 				break;
   421 		}
   422 	}	
   423 }
   424 
   425 void LinkableMapObj::updateLinkGeometry()
   426 {
   427     // needs:
   428     //	childPos of parent
   429     //	orient   of parent
   430     //	style
   431     // 
   432     // sets:
   433     //	orientation
   434     //	childPos	(by calling setDockPos())
   435     //	parPos		(by calling setDockPos())
   436 	//  bottomlineY
   437     //	drawing of the link itself
   438 
   439 	// updateLinkGeometry is called from move, but called from constructor we don't
   440 	// have parents yet...
   441 
   442 	//cout <<"LMO::updateLinkGeometry: "<<treeItem->getHeadingStd()<<"  "<<parObj<<endl;
   443 	if (!parObj)	
   444 	{
   445 		// If I am a mapcenter, set childPos to middle of MapCenterObj
   446 		childPos.setX( clickBox.topLeft().x() + clickBox.width()/2 );
   447 		childPos.setY( clickBox.topLeft().y() + clickBox.height()/2 );
   448 		parPos=childPos;		
   449 		// Redraw links to children
   450 		for (int i=0; i<treeItem->branchCount(); ++i)
   451 			treeItem->getBranchObjNum(i)->updateLinkGeometry();
   452 		return;	
   453 	}
   454 
   455 	if (style==UndefinedStyle) return;	
   456 
   457 	switch (linkpos)
   458 	{
   459 		case Middle:
   460 			bottomlineY=bbox.top() + bbox.height()/2;	// draw link to middle (of frame)
   461 			break;
   462 		case Bottom:
   463 			//bottomlineY=bbox.bottom()-1;	// draw link to bottom of box
   464 			bottomlineY=bbox.bottom()-botPad;
   465 			break;
   466 	}
   467 	
   468     double p2x,p2y;								// Set P2 Before setting
   469 	if (!link2ParPos)
   470 	{
   471 		p2x=QPointF( parObj->getChildPos() ).x();	// P1, we have to look at
   472 		p2y=QPointF( parObj->getChildPos() ).y();	// orientation
   473 	} else	
   474 	{
   475 		p2x=QPointF( parObj->getParPos() ).x();	
   476 		p2y=QPointF( parObj->getParPos() ).y();
   477 	} 
   478 
   479 	setDockPos(); // Call overloaded method
   480 	setOrientation();
   481 
   482 	double p1x=parPos.x();	// Link is drawn from P1 to P2
   483 	double p1y=parPos.y();
   484 
   485 	double vx=p2x - p1x;	// V=P2-P1
   486 	double vy=p2y - p1y;
   487 
   488 	// Draw the horizontal line below heading (from ChildPos to ParPos)
   489 	bottomline->setLine (QLine (qRound(childPos.x()),
   490 		qRound(childPos.y()),
   491 		qRound(p1x),
   492 		qRound(p1y) ));
   493 
   494 	double a;	// angle
   495 	if (vx > -0.000001 && vx < 0.000001)
   496 		a=M_PI_2;
   497 	else
   498 		a=atan( vy / vx );
   499 	// "turning point" for drawing polygonal links
   500 	QPointF tp (-qRound(sin (a)*thickness_start), qRound(cos (a)*thickness_start));	
   501 	
   502     // Draw the link
   503 	switch (style)
   504 	{
   505 		case Line:
   506 			l->setLine( QLine(qRound (parPos.x()),
   507 				qRound(parPos.y()),
   508 				qRound(p2x),
   509 				qRound(p2y) ));
   510 			break;	
   511 		case Parabel:	
   512 			parabel (pa0, p1x,p1y,p2x,p2y);
   513 			for (int i=0; i<segment.size(); ++i)
   514 				segment.at(i)->setLine(QLineF( pa0.at(i).x(), pa0.at(i).y(),pa0.at(i+1).x(),pa0.at(i+1).y()));
   515 			break;
   516 		case PolyLine:
   517 			pa0.clear();
   518 			pa0<<QPointF (qRound(p2x+tp.x()), qRound(p2y+tp.y()));
   519 			pa0<<QPointF (qRound(p2x-tp.x()), qRound(p2y-tp.y()));
   520 			pa0<<QPointF (qRound (parPos.x()), qRound(parPos.y()) );
   521 			p->setPolygon(QPolygonF (pa0));
   522 			break;
   523 		case PolyParabel:	
   524 			parabel (pa1, p1x,p1y,p2x+tp.x(),p2y+tp.y());
   525 			parabel (pa2, p1x,p1y,p2x-tp.x(),p2y-tp.y());
   526 			pa0.clear();
   527 			for (int i=0;i<=arcsegs;i++)
   528 				pa0 << QPointF (pa1.at(i));
   529 			for (int i=0;i<=arcsegs;i++)
   530 				pa0 << QPointF (pa2.at(arcsegs-i));
   531 			p->setPolygon(QPolygonF (pa0));
   532 			break;
   533 		default:
   534 			break;
   535 	} 
   536 }
   537 	
   538 LinkableMapObj* LinkableMapObj::getParObj()
   539 {
   540     return parObj;
   541 }
   542 
   543 QPointF LinkableMapObj::getChildPos()
   544 {
   545     return childPos;
   546 }
   547 
   548 QPointF LinkableMapObj::getParPos()
   549 {
   550     return parPos;
   551 }
   552 
   553 LinkableMapObj::Orientation LinkableMapObj::getOrientation()
   554 {
   555     return orientation;
   556 }
   557 
   558 QPointF LinkableMapObj::getRandPos()
   559 {
   560 	// Choose a random position with given distance to parent:
   561 	double a=rand()%360 * 2 * M_PI / 360;
   562     return QPointF ( (int)( + 150*cos (a)),
   563                     (int)( + 150*sin (a)));
   564 }
   565 
   566 void LinkableMapObj::reposition()
   567 {
   568 }
   569 
   570 void LinkableMapObj::requestReposition()	//FIXME-3 needed?
   571 {
   572 	if (!repositionRequest)
   573 	{
   574 		// Pass on the request to parental objects, if this hasn't
   575 		// been done yet
   576 		repositionRequest=true;
   577 		if (parObj) parObj->requestReposition();
   578 	}
   579 }
   580 
   581 void LinkableMapObj::forceReposition()
   582 {
   583 	// Sometimes a reposition has to be done immediatly: For example
   584 	// if the note editor flag changes, there is no user event in mapeditor
   585 	// which could collect requests for a reposition.
   586 	// Then we have to call forceReposition()
   587 	// But no rule without exception: While loading a map or undoing it,
   588 	// we want to block expensive repositioning, but just do it once at
   589 	// the end, thus check first:
   590 
   591 	VymModel *model=treeItem->getModel();
   592 	if (model->isRepositionBlocked()) return;	
   593 	
   594 	// Pass on the request to parent objects, if this hasn't been done yet
   595 	if (parObj) 
   596 		parObj->forceReposition(); 
   597 	else 
   598 		reposition(); 
   599 }
   600 
   601 bool LinkableMapObj::repositionRequested()
   602 {
   603 	return repositionRequest;
   604 }
   605 
   606 void LinkableMapObj::parabel (QPolygonF &ya, double p1x, double p1y, double p2x, double p2y)
   607 
   608 {
   609 	double vx=p2x - p1x;	// V=P2-P1
   610 	double vy=p2y - p1y;
   611 
   612 	double dx;				// delta x during calculation of parabel
   613 	
   614 	double pnx;				// next point
   615 	double pny;
   616 	double m;
   617 
   618 	if (vx > -0.0001 && vx < 0.0001)
   619 		m=0;
   620 	else	
   621 		m=(vy / (vx*vx));
   622 	dx=vx/(arcsegs);
   623 	ya.clear();
   624 	ya<<QPointF (p1x,p1y);
   625 	for (int i=1;i<=arcsegs;i++)
   626 	{	
   627 		pnx=p1x+dx;
   628 		pny=m*(pnx-parPos.x())*(pnx-parPos.x())+parPos.y();
   629 		ya<<QPointF (pnx,pny);
   630 		p1x=pnx;
   631 		p1y=pny;
   632 	}	
   633 }
   634