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