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