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