linkablemapobj.cpp
author insilmaril
Thu, 01 Oct 2009 11:28:50 +0000
changeset 798 d251c7b2de54
parent 791 f1006de05c54
child 816 3086ee01554a
permissions -rw-r--r--
Various fixes for relinking and selecting
     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 void LinkableMapObj::setRelPos()
   132 {
   133 	if (parObj)
   134 	{	
   135 		relPos.setX (absPos.x() - parObj->getChildPos().x() );
   136 		relPos.setY (absPos.y() - parObj->getChildPos().y() );
   137 		parObj->calcBBoxSize();
   138 	}	
   139 }
   140 
   141 void LinkableMapObj::setRelPos(const QPointF &p)
   142 {
   143 	relPos=p;
   144 	if (parObj)
   145 	{		
   146 		parObj->calcBBoxSize();
   147 		requestReposition();
   148 	}
   149 }
   150 
   151 QPointF LinkableMapObj::getRelPos()
   152 {
   153 	if (!parObj) return QPointF();
   154 	return relPos;
   155 }
   156 
   157 qreal LinkableMapObj::getTopPad()
   158 {
   159 	return topPad;
   160 }
   161 
   162 qreal LinkableMapObj::getLeftPad()
   163 {
   164 	return leftPad;
   165 }
   166 
   167 qreal LinkableMapObj::getRightPad()
   168 {
   169 	return rightPad;
   170 }
   171 
   172 LinkableMapObj::Style LinkableMapObj::getDefLinkStyle (TreeItem *parent)
   173 {
   174 	VymModel *model=treeItem->getModel();
   175 	if (!model)
   176 	{
   177 		qWarning ("LMO::getDefLinkStyle   model=NULL");
   178 		//return UndefinedStyle;
   179 	}
   180 	Style ls=model->getMapLinkStyle();
   181 	int depth=1+parent->depth();
   182 	if (depth==0) return UndefinedStyle;
   183 	switch (ls)
   184 	{
   185 		case Line: 
   186 			return ls;
   187 			break;
   188 		case Parabel:
   189 			return ls;
   190 			break;
   191 		case PolyLine:	
   192 			if (depth>1)
   193 				return Line;
   194 			else	
   195 				return ls;
   196 			break;
   197 		case PolyParabel:	
   198 			if (depth>1)
   199 				return Parabel;
   200 			else	
   201 				return ls;
   202 			break;
   203 		default: 
   204 			break;	
   205 	}	
   206 	return UndefinedStyle;
   207 }
   208 
   209 void LinkableMapObj::setLinkStyle(Style newstyle)
   210 {
   211 	delLink();
   212 		
   213 	style=newstyle;
   214 
   215     if (parObj != NULL)
   216     {
   217 		QGraphicsLineItem *cl;
   218 		switch (style)
   219 		{
   220 			case UndefinedStyle:
   221 				bottomline->hide();
   222 				break;
   223 			case Line: 
   224 				l = scene->addLine(QLineF(1,1,1,1),pen);
   225 				l->setZValue(Z_LINK);
   226 				if (visible)
   227 					l->show();
   228 				else
   229 					l->hide();
   230 				break;
   231 			case Parabel:
   232 				for (int i=0;i<arcsegs;i++)
   233 				{
   234 					cl = scene->addLine(QLineF(i*5,0,i*10,100),pen);
   235 					cl->setZValue(Z_LINK);
   236 					if (visible)
   237 						cl->show();
   238 					else
   239 						cl->hide();
   240 					segment.append(cl);
   241 				}
   242 				pa0.resize (arcsegs+1);
   243 				break;
   244 			case PolyLine:	
   245 				p =scene->addPolygon(QPolygonF(),pen,linkcolor);
   246 				p->setZValue(Z_LINK);
   247 				if (visible)
   248 					p->show();
   249 				else
   250 					p->hide();
   251 				pa0.resize (3);
   252 				break;
   253 			case PolyParabel:	
   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 (arcsegs*2+2);
   261 				pa1.resize (arcsegs+1);
   262 				pa2.resize (arcsegs+1);
   263 				break;
   264 			default: 
   265 				break;	
   266 		}	
   267 	} 
   268 }
   269 
   270 LinkableMapObj::Style LinkableMapObj::getLinkStyle()
   271 {
   272 	return style;
   273 }
   274 
   275 void LinkableMapObj::setHideLinkUnselected()
   276 {
   277 	setVisibility (visible);
   278 	updateLinkGeometry();
   279 }
   280 
   281 void LinkableMapObj::setLinkPos(Position lp)
   282 {
   283 	linkpos=lp;
   284 }
   285 
   286 LinkableMapObj::Position LinkableMapObj::getLinkPos()
   287 {
   288 	return linkpos;
   289 }
   290 
   291 void LinkableMapObj::setLinkColor()
   292 {
   293 	// Overloaded in BranchObj and children
   294 	// here only set default color
   295 	VymModel *model=treeItem->getModel();
   296 	if (model)
   297 		setLinkColor (model->getMapDefLinkColor());
   298 }
   299 
   300 void LinkableMapObj::setLinkColor(QColor col)
   301 {
   302 	linkcolor=col;
   303 	pen.setColor(col);
   304     bottomline->setPen( pen );
   305 	switch (style)
   306 	{
   307 		case Line:
   308 			l->setPen( pen);
   309 			break;	
   310 		case Parabel:	
   311 			for (int i=0; i<segment.size(); ++i)
   312 				segment.at(i)->setPen( pen);
   313 			break;
   314 		case PolyLine:
   315 			p->setBrush( QBrush(col));
   316 			p->setPen( pen);
   317 			break;
   318 		case PolyParabel:	
   319 			p->setBrush( QBrush(col));
   320 			p->setPen( pen);
   321 			break;
   322 		default:
   323 			break;
   324 	} 
   325 }
   326 
   327 QColor LinkableMapObj::getLinkColor()
   328 {
   329 	return linkcolor;
   330 }
   331 
   332 void LinkableMapObj::setVisibility (bool v)
   333 {
   334 	MapObj::setVisibility (v);
   335 	updateVisibility();
   336 }
   337 
   338 void LinkableMapObj::setOrientation()
   339 {
   340 	Orientation orientOld=orientation;
   341 
   342 	if (!parObj) 
   343 	{
   344 		orientation=UndefinedOrientation;
   345 		return;
   346 	}
   347 		
   348     // Set orientation, first look for orientation of parent
   349     if (parObj->getOrientation() != UndefinedOrientation ) 
   350 		// use the orientation of the parent:
   351 		orientation=parObj->getOrientation();
   352     else
   353     {
   354 		// calc orientation depending on position rel to parent
   355 		if (absPos.x() < QPointF(parObj->getChildPos() ).x() )
   356 			orientation=LeftOfCenter; 
   357 		else
   358 			orientation=RightOfCenter;
   359     }
   360 	if (orientOld!=orientation) requestReposition();
   361 }
   362 
   363 void LinkableMapObj::updateVisibility()
   364 {
   365 	bool visnow=visible;
   366 
   367 	if (((MapItem*)treeItem)->getHideLinkUnselected()
   368 		&& treeItem->getModel()->getSelectedLMO() !=this)
   369 		visnow=false;
   370 
   371 	if (visnow) 
   372 	{
   373 		if (useBottomline)
   374 			bottomline->show();
   375 		else	
   376 			bottomline->hide();
   377 
   378 		switch (style)
   379 		{
   380 			case Line:
   381 				if (l) l->show();
   382 				break;
   383 			case Parabel:	
   384 				for (int i=0; i<segment.size(); ++i)
   385 					segment.at(i)->show();
   386 				break;	
   387 			case PolyLine:
   388 				if (!p) cout << "LMO::updateVis p==0 (PolyLine)\n"; //FIXME-3
   389 				if (p) p->show();
   390 				break;
   391 			case PolyParabel:	
   392 				if (!p) cout << "LMO::updateVis p==0 (PolyParabel) "<<treeItem->getHeading().toStdString()<<endl; //FIXME-3
   393 				if (p) p->show();
   394 				break;
   395 			default:
   396 				break;
   397 		}
   398 	} else 
   399 	{
   400 		bottomline->hide();
   401 		switch (style)
   402 		{
   403 			case Line:
   404 				if (l) l->hide();
   405 				break;
   406 			case Parabel:	
   407 				for (int i=0; i<segment.size(); ++i)
   408 					segment.at(i)->hide();
   409 				break;	
   410 			case PolyLine:
   411 				if (p) p->hide();
   412 				break;
   413 			case PolyParabel:	
   414 				if (p) p->hide();
   415 				break;
   416 			default:
   417 				break;
   418 		}
   419 	}	
   420 }
   421 
   422 void LinkableMapObj::updateLinkGeometry()
   423 {
   424     // needs:
   425     //	childPos of parent
   426     //	orient   of parent
   427     //	style
   428     // 
   429     // sets:
   430     //	orientation
   431     //	childPos	(by calling setDockPos())
   432     //	parPos		(by calling setDockPos())
   433 	//  bottomlineY
   434     //	drawing of the link itself
   435 
   436 	// updateLinkGeometry is called from move, but called from constructor we don't
   437 	// have parents yet...
   438 
   439 //cout <<"LMO::updateLinkGeometry: "<<treeItem->getHeadingStd()<<"  "<<parObj<<endl;
   440 	if (!parObj)	
   441 	{
   442 		// If I am a mapcenter, set childPos to middle of MapCenterObj
   443 		childPos.setX( clickBox.topLeft().x() + clickBox.width()/2 );
   444 		childPos.setY( clickBox.topLeft().y() + clickBox.height()/2 );
   445 		parPos=childPos;		
   446 		// Redraw links to children
   447 		for (int i=0; i<treeItem->branchCount(); ++i)
   448 			treeItem->getBranchObjNum(i)->updateLinkGeometry();
   449 		return;	
   450 	}
   451 
   452 	if (style==UndefinedStyle) return;	
   453 
   454 	switch (linkpos)
   455 	{
   456 		case Middle:
   457 			bottomlineY=bbox.top() + bbox.height()/2;	// draw link to middle (of frame)
   458 			break;
   459 		case Bottom:
   460 			//bottomlineY=bbox.bottom()-1;	// draw link to bottom of box
   461 			bottomlineY=bbox.bottom()-botPad;
   462 			break;
   463 	}
   464 	
   465     double p2x,p2y;								// Set P2 Before setting
   466 	if (!link2ParPos)
   467 	{
   468 		p2x=QPointF( parObj->getChildPos() ).x();	// P1, we have to look at
   469 		p2y=QPointF( parObj->getChildPos() ).y();	// orientation
   470 	} else	
   471 	{
   472 		p2x=QPointF( parObj->getParPos() ).x();	
   473 		p2y=QPointF( parObj->getParPos() ).y();
   474 	} 
   475 
   476 	setDockPos(); // Call overloaded method
   477 	setOrientation();
   478 
   479 	double p1x=parPos.x();	// Link is drawn from P1 to P2
   480 	double p1y=parPos.y();
   481 
   482 	double vx=p2x - p1x;	// V=P2-P1
   483 	double vy=p2y - p1y;
   484 
   485 	// Draw the horizontal line below heading (from ChildPos to ParPos)
   486 	bottomline->setLine (QLine (qRound(childPos.x()),
   487 		qRound(childPos.y()),
   488 		qRound(p1x),
   489 		qRound(p1y) ));
   490 
   491 	double a;	// angle
   492 	if (vx > -0.000001 && vx < 0.000001)
   493 		a=M_PI_2;
   494 	else
   495 		a=atan( vy / vx );
   496 	// "turning point" for drawing polygonal links
   497 	QPointF tp (-qRound(sin (a)*thickness_start), qRound(cos (a)*thickness_start));	
   498 	
   499     // Draw the link
   500 	switch (style)
   501 	{
   502 		case Line:
   503 			l->setLine( QLine(qRound (parPos.x()),
   504 				qRound(parPos.y()),
   505 				qRound(p2x),
   506 				qRound(p2y) ));
   507 			break;	
   508 		case Parabel:	
   509 			parabel (pa0, p1x,p1y,p2x,p2y);
   510 			for (int i=0; i<segment.size(); ++i)
   511 				segment.at(i)->setLine(QLineF( pa0.at(i).x(), pa0.at(i).y(),pa0.at(i+1).x(),pa0.at(i+1).y()));
   512 			break;
   513 		case PolyLine:
   514 			pa0.clear();
   515 			pa0<<QPointF (qRound(p2x+tp.x()), qRound(p2y+tp.y()));
   516 			pa0<<QPointF (qRound(p2x-tp.x()), qRound(p2y-tp.y()));
   517 			pa0<<QPointF (qRound (parPos.x()), qRound(parPos.y()) );
   518 			p->setPolygon(QPolygonF (pa0));
   519 			break;
   520 		case PolyParabel:	
   521 			parabel (pa1, p1x,p1y,p2x+tp.x(),p2y+tp.y());
   522 			parabel (pa2, p1x,p1y,p2x-tp.x(),p2y-tp.y());
   523 			pa0.clear();
   524 			for (int i=0;i<=arcsegs;i++)
   525 				pa0 << QPointF (pa1.at(i));
   526 			for (int i=0;i<=arcsegs;i++)
   527 				pa0 << QPointF (pa2.at(arcsegs-i));
   528 			p->setPolygon(QPolygonF (pa0));
   529 			break;
   530 		default:
   531 			break;
   532 	} 
   533 }
   534 	
   535 LinkableMapObj* LinkableMapObj::getParObj()
   536 {
   537     return parObj;
   538 }
   539 
   540 QPointF LinkableMapObj::getChildPos()
   541 {
   542     return childPos;
   543 }
   544 
   545 QPointF LinkableMapObj::getParPos()
   546 {
   547     return parPos;
   548 }
   549 
   550 LinkableMapObj::Orientation LinkableMapObj::getOrientation()
   551 {
   552     return orientation;
   553 }
   554 
   555 QPointF LinkableMapObj::getRandPos()
   556 {
   557 	// Choose a random position with given distance to parent:
   558 	double a=rand()%360 * 2 * M_PI / 360;
   559     return QPointF ( (int)( + 150*cos (a)),
   560                     (int)( + 150*sin (a)));
   561 }
   562 
   563 void LinkableMapObj::reposition()
   564 {
   565 }
   566 
   567 void LinkableMapObj::requestReposition()	//FIXME-3 needed?
   568 {
   569 	if (!repositionRequest)
   570 	{
   571 		// Pass on the request to parental objects, if this hasn't
   572 		// been done yet
   573 		repositionRequest=true;
   574 		if (parObj) parObj->requestReposition();
   575 	}
   576 }
   577 
   578 void LinkableMapObj::forceReposition()
   579 {
   580 	// Sometimes a reposition has to be done immediatly: For example
   581 	// if the note editor flag changes, there is no user event in mapeditor
   582 	// which could collect requests for a reposition.
   583 	// Then we have to call forceReposition()
   584 	// But no rule without exception: While loading a map or undoing it,
   585 	// we want to block expensive repositioning, but just do it once at
   586 	// the end, thus check first:
   587 
   588 	VymModel *model=treeItem->getModel();
   589 	if (model->isRepositionBlocked()) return;	
   590 	
   591 	// Pass on the request to parent objects, if this hasn't been done yet
   592 	if (parObj) 
   593 		parObj->forceReposition(); 
   594 	else 
   595 		reposition(); 
   596 }
   597 
   598 bool LinkableMapObj::repositionRequested()
   599 {
   600 	return repositionRequest;
   601 }
   602 
   603 void LinkableMapObj::parabel (QPolygonF &ya, double p1x, double p1y, double p2x, double p2y)
   604 
   605 {
   606 	double vx=p2x - p1x;	// V=P2-P1
   607 	double vy=p2y - p1y;
   608 
   609 	double dx;				// delta x during calculation of parabel
   610 	
   611 	double pnx;				// next point
   612 	double pny;
   613 	double m;
   614 
   615 	if (vx > -0.0001 && vx < 0.0001)
   616 		m=0;
   617 	else	
   618 		m=(vy / (vx*vx));
   619 	dx=vx/(arcsegs);
   620 	ya.clear();
   621 	ya<<QPointF (p1x,p1y);
   622 	for (int i=1;i<=arcsegs;i++)
   623 	{	
   624 		pnx=p1x+dx;
   625 		pny=m*(pnx-parPos.x())*(pnx-parPos.x())+parPos.y();
   626 		ya<<QPointF (pnx,pny);
   627 		p1x=pnx;
   628 		p1y=pny;
   629 	}	
   630 }
   631