linkablemapobj.cpp
author insilmaril
Fri, 19 Feb 2010 13:47:03 +0000
changeset 823 0bba81dde1bc
parent 816 3086ee01554a
child 835 31841b366d5e
permissions -rw-r--r--
More fixes
     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::setLinkPos(Position lp)
   276 {
   277 	linkpos=lp;
   278 }
   279 
   280 LinkableMapObj::Position LinkableMapObj::getLinkPos()
   281 {
   282 	return linkpos;
   283 }
   284 
   285 void LinkableMapObj::setLinkColor()
   286 {
   287 	// Overloaded in BranchObj and children
   288 	// here only set default color
   289 	VymModel *model=treeItem->getModel();
   290 	if (model)
   291 		setLinkColor (model->getMapDefLinkColor());
   292 }
   293 
   294 void LinkableMapObj::setLinkColor(QColor col)
   295 {
   296 	linkcolor=col;
   297 	pen.setColor(col);
   298     bottomline->setPen( pen );
   299 	switch (style)
   300 	{
   301 		case Line:
   302 			l->setPen( pen);
   303 			break;	
   304 		case Parabel:	
   305 			for (int i=0; i<segment.size(); ++i)
   306 				segment.at(i)->setPen( pen);
   307 			break;
   308 		case PolyLine:
   309 			p->setBrush( QBrush(col));
   310 			p->setPen( pen);
   311 			break;
   312 		case PolyParabel:	
   313 			p->setBrush( QBrush(col));
   314 			p->setPen( pen);
   315 			break;
   316 		default:
   317 			break;
   318 	} 
   319 }
   320 
   321 QColor LinkableMapObj::getLinkColor()
   322 {
   323 	return linkcolor;
   324 }
   325 
   326 void LinkableMapObj::setVisibility (bool v)
   327 {
   328 	MapObj::setVisibility (v);
   329 	updateVisibility();
   330 }
   331 
   332 void LinkableMapObj::setOrientation()
   333 {
   334 	Orientation orientOld=orientation;
   335 
   336 	if (!parObj) 
   337 	{
   338 		orientation=UndefinedOrientation;
   339 		return;
   340 	}
   341 		
   342     // Set orientation, first look for orientation of parent
   343     if (parObj->getOrientation() != UndefinedOrientation ) 
   344 		// use the orientation of the parent:
   345 		orientation=parObj->getOrientation();
   346     else
   347     {
   348 		// calc orientation depending on position rel to parent
   349 		if (absPos.x() < QPointF(parObj->getChildPos() ).x() )
   350 			orientation=LeftOfCenter; 
   351 		else
   352 			orientation=RightOfCenter;
   353     }
   354 	if (orientOld!=orientation) requestReposition();
   355 }
   356 
   357 void LinkableMapObj::updateVisibility()
   358 {
   359 	bool visnow=visible;
   360 
   361 	if (((MapItem*)treeItem)->getHideLinkUnselected()
   362 		&& treeItem->getModel()->getSelectedLMO() !=this)
   363 		visnow=false;
   364 
   365 	if (visnow) 
   366 	{
   367 		if (useBottomline)
   368 			bottomline->show();
   369 		else	
   370 			bottomline->hide();
   371 
   372 		switch (style)
   373 		{
   374 			case Line:
   375 				if (l) l->show();
   376 				break;
   377 			case Parabel:	
   378 				for (int i=0; i<segment.size(); ++i)
   379 					segment.at(i)->show();
   380 				break;	
   381 			case PolyLine:
   382 				if (!p) cout << "LMO::updateVis p==0 (PolyLine)\n"; //FIXME-3
   383 				if (p) p->show();
   384 				break;
   385 			case PolyParabel:	
   386 				if (!p) cout << "LMO::updateVis p==0 (PolyParabel) "<<treeItem->getHeading().toStdString()<<endl; //FIXME-3
   387 				if (p) p->show();
   388 				break;
   389 			default:
   390 				break;
   391 		}
   392 	} else 
   393 	{
   394 		bottomline->hide();
   395 		switch (style)
   396 		{
   397 			case Line:
   398 				if (l) l->hide();
   399 				break;
   400 			case Parabel:	
   401 				for (int i=0; i<segment.size(); ++i)
   402 					segment.at(i)->hide();
   403 				break;	
   404 			case PolyLine:
   405 				if (p) p->hide();
   406 				break;
   407 			case PolyParabel:	
   408 				if (p) p->hide();
   409 				break;
   410 			default:
   411 				break;
   412 		}
   413 	}	
   414 }
   415 
   416 void LinkableMapObj::updateLinkGeometry()
   417 {
   418     // needs:
   419     //	childPos of parent
   420     //	orient   of parent
   421     //	style
   422     // 
   423     // sets:
   424     //	orientation
   425     //	childPos	(by calling setDockPos())
   426     //	parPos		(by calling setDockPos())
   427 	//  bottomlineY
   428     //	drawing of the link itself
   429 
   430 	// updateLinkGeometry is called from move, but called from constructor we don't
   431 	// have parents yet...
   432 
   433 	//cout <<"LMO::updateLinkGeometry: "<<treeItem->getHeadingStd()<<"  "<<parObj<<endl;
   434 	if (!parObj)	
   435 	{
   436 		// If I am a mapcenter, set childPos to middle of MapCenterObj
   437 		childPos.setX( clickBox.topLeft().x() + clickBox.width()/2 );
   438 		childPos.setY( clickBox.topLeft().y() + clickBox.height()/2 );
   439 		parPos=childPos;		
   440 		// Redraw links to children
   441 		for (int i=0; i<treeItem->branchCount(); ++i)
   442 			treeItem->getBranchObjNum(i)->updateLinkGeometry();
   443 		return;	
   444 	}
   445 
   446 	if (style==UndefinedStyle) return;	
   447 
   448 	switch (linkpos)
   449 	{
   450 		case Middle:
   451 			bottomlineY=bbox.top() + bbox.height()/2;	// draw link to middle (of frame)
   452 			break;
   453 		case Bottom:
   454 			//bottomlineY=bbox.bottom()-1;	// draw link to bottom of box
   455 			bottomlineY=bbox.bottom()-botPad;
   456 			break;
   457 	}
   458 	
   459     double p2x,p2y;								// Set P2 Before setting
   460 	if (!link2ParPos)
   461 	{
   462 		p2x=QPointF( parObj->getChildPos() ).x();	// P1, we have to look at
   463 		p2y=QPointF( parObj->getChildPos() ).y();	// orientation
   464 	} else	
   465 	{
   466 		p2x=QPointF( parObj->getParPos() ).x();	
   467 		p2y=QPointF( parObj->getParPos() ).y();
   468 	} 
   469 
   470 	setDockPos(); // Call overloaded method
   471 	setOrientation();
   472 
   473 	double p1x=parPos.x();	// Link is drawn from P1 to P2
   474 	double p1y=parPos.y();
   475 
   476 	double vx=p2x - p1x;	// V=P2-P1
   477 	double vy=p2y - p1y;
   478 
   479 	// Draw the horizontal line below heading (from ChildPos to ParPos)
   480 	bottomline->setLine (QLine (qRound(childPos.x()),
   481 		qRound(childPos.y()),
   482 		qRound(p1x),
   483 		qRound(p1y) ));
   484 
   485 	double a;	// angle
   486 	if (vx > -0.000001 && vx < 0.000001)
   487 		a=M_PI_2;
   488 	else
   489 		a=atan( vy / vx );
   490 	// "turning point" for drawing polygonal links
   491 	QPointF tp (-qRound(sin (a)*thickness_start), qRound(cos (a)*thickness_start));	
   492 	
   493     // Draw the link
   494 	switch (style)
   495 	{
   496 		case Line:
   497 			l->setLine( QLine(qRound (parPos.x()),
   498 				qRound(parPos.y()),
   499 				qRound(p2x),
   500 				qRound(p2y) ));
   501 			break;	
   502 		case Parabel:	
   503 			parabel (pa0, p1x,p1y,p2x,p2y);
   504 			for (int i=0; i<segment.size(); ++i)
   505 				segment.at(i)->setLine(QLineF( pa0.at(i).x(), pa0.at(i).y(),pa0.at(i+1).x(),pa0.at(i+1).y()));
   506 			break;
   507 		case PolyLine:
   508 			pa0.clear();
   509 			pa0<<QPointF (qRound(p2x+tp.x()), qRound(p2y+tp.y()));
   510 			pa0<<QPointF (qRound(p2x-tp.x()), qRound(p2y-tp.y()));
   511 			pa0<<QPointF (qRound (parPos.x()), qRound(parPos.y()) );
   512 			p->setPolygon(QPolygonF (pa0));
   513 			break;
   514 		case PolyParabel:	
   515 			parabel (pa1, p1x,p1y,p2x+tp.x(),p2y+tp.y());
   516 			parabel (pa2, p1x,p1y,p2x-tp.x(),p2y-tp.y());
   517 			pa0.clear();
   518 			for (int i=0;i<=arcsegs;i++)
   519 				pa0 << QPointF (pa1.at(i));
   520 			for (int i=0;i<=arcsegs;i++)
   521 				pa0 << QPointF (pa2.at(arcsegs-i));
   522 			p->setPolygon(QPolygonF (pa0));
   523 			break;
   524 		default:
   525 			break;
   526 	} 
   527 }
   528 	
   529 LinkableMapObj* LinkableMapObj::getParObj()
   530 {
   531     return parObj;
   532 }
   533 
   534 QPointF LinkableMapObj::getChildPos()
   535 {
   536     return childPos;
   537 }
   538 
   539 QPointF LinkableMapObj::getParPos()
   540 {
   541     return parPos;
   542 }
   543 
   544 LinkableMapObj::Orientation LinkableMapObj::getOrientation()
   545 {
   546     return orientation;
   547 }
   548 
   549 QPointF LinkableMapObj::getRandPos()
   550 {
   551 	// Choose a random position with given distance to parent:
   552 	double a=rand()%360 * 2 * M_PI / 360;
   553     return QPointF ( (int)( + 150*cos (a)),
   554                     (int)( + 150*sin (a)));
   555 }
   556 
   557 void LinkableMapObj::reposition()
   558 {
   559 }
   560 
   561 void LinkableMapObj::requestReposition()	//FIXME-3 needed?
   562 {
   563 	if (!repositionRequest)
   564 	{
   565 		// Pass on the request to parental objects, if this hasn't
   566 		// been done yet
   567 		repositionRequest=true;
   568 		if (parObj) parObj->requestReposition();
   569 	}
   570 }
   571 
   572 void LinkableMapObj::forceReposition()
   573 {
   574 	// Sometimes a reposition has to be done immediatly: For example
   575 	// if the note editor flag changes, there is no user event in mapeditor
   576 	// which could collect requests for a reposition.
   577 	// Then we have to call forceReposition()
   578 	// But no rule without exception: While loading a map or undoing it,
   579 	// we want to block expensive repositioning, but just do it once at
   580 	// the end, thus check first:
   581 
   582 	VymModel *model=treeItem->getModel();
   583 	if (model->isRepositionBlocked()) return;	
   584 	
   585 	// Pass on the request to parent objects, if this hasn't been done yet
   586 	if (parObj) 
   587 		parObj->forceReposition(); 
   588 	else 
   589 		reposition(); 
   590 }
   591 
   592 bool LinkableMapObj::repositionRequested()
   593 {
   594 	return repositionRequest;
   595 }
   596 
   597 void LinkableMapObj::parabel (QPolygonF &ya, double p1x, double p1y, double p2x, double p2y)
   598 
   599 {
   600 	double vx=p2x - p1x;	// V=P2-P1
   601 	double vy=p2y - p1y;
   602 
   603 	double dx;				// delta x during calculation of parabel
   604 	
   605 	double pnx;				// next point
   606 	double pny;
   607 	double m;
   608 
   609 	if (vx > -0.0001 && vx < 0.0001)
   610 		m=0;
   611 	else	
   612 		m=(vy / (vx*vx));
   613 	dx=vx/(arcsegs);
   614 	ya.clear();
   615 	ya<<QPointF (p1x,p1y);
   616 	for (int i=1;i<=arcsegs;i++)
   617 	{	
   618 		pnx=p1x+dx;
   619 		pny=m*(pnx-parPos.x())*(pnx-parPos.x())+parPos.y();
   620 		ya<<QPointF (pnx,pny);
   621 		p1x=pnx;
   622 		p1y=pny;
   623 	}	
   624 }
   625