linkablemapobj.cpp
author insilmaril
Wed, 18 May 2005 07:39:51 +0000
changeset 100 f79a53ee8cf6
parent 97 0b048b6bb6f4
child 104 270593ab83b2
permissions -rw-r--r--
fixed some bugs in xlinks, more remove/insert functions
     1 #include <cmath>
     2 
     3 #include "linkablemapobj.h"
     4 #include "branchobj.h"
     5 #include "mapeditor.h"
     6 
     7 #include "version.h"
     8 
     9 
    10 /////////////////////////////////////////////////////////////////
    11 // LinkableMapObj
    12 /////////////////////////////////////////////////////////////////
    13 
    14 LinkableMapObj::LinkableMapObj():MapObj()
    15 {
    16   //  cout << "Const LinkableMapObj ()\n";
    17     init ();
    18 }
    19 
    20 LinkableMapObj::LinkableMapObj(QCanvas* c) :MapObj(c)
    21 {
    22 //    cout << "Const LinkableMapObj\n";
    23     init ();
    24 }
    25 
    26 LinkableMapObj::LinkableMapObj (LinkableMapObj* lmo) : MapObj (lmo->canvas)
    27 {
    28     copy (lmo);
    29 }
    30 
    31 LinkableMapObj::~LinkableMapObj()
    32 {
    33     delete (bottomline);
    34     delete (selbox);
    35 	delete (frame);
    36 	delLink();
    37 }
    38 
    39 void LinkableMapObj::delLink()
    40 {
    41 	switch (style)
    42 	{
    43 		case StyleLine:
    44 			delete (l);
    45 			break;
    46 		case StyleParabel:
    47 			segment.clear();
    48 			break;
    49 		case StylePolyLine:
    50 			delete (p);
    51 			delete (l);
    52 			break;
    53 		case StylePolyParabel:
    54 			delete (p);
    55 			segment.clear();
    56 			break;
    57 		default:
    58 			break;
    59 	}		
    60 }
    61 
    62 void LinkableMapObj::init ()
    63 {
    64     depth=-1;	
    65     childObj=NULL;
    66     parObj=NULL;
    67     parObjTmpBuf=NULL;
    68     parPos=QPoint(0,0);
    69     childPos=QPoint(0,0);
    70 	link2ParPos=false;
    71     l=NULL;
    72     orientation=OrientUndef;
    73     linkwidth=20;		
    74 	thickness_start=8;
    75     style=StyleUndef;
    76 	linkpos=LinkBottom;
    77     segment.setAutoDelete (TRUE);
    78     arcsegs=13;
    79 	QPointArray pa(arcsegs*2+2);
    80     
    81     bottomline=new QCanvasLine(canvas);
    82     bottomline->setPen( QPen(linkcolor, 1) );
    83     bottomline->setZ(Z_LINK);
    84     bottomline->show();
    85 
    86     // Prepare showing the selection of a MapObj
    87     selbox = new QCanvasRectangle (canvas);
    88     selbox->setZ(Z_SELBOX);
    89     selbox->setBrush( QColor(255,255,0) );
    90     selbox->setPen( QPen(QColor(255,255,0) ));
    91     selbox->hide();
    92     selected=false;
    93 
    94 	// initialize frame
    95 	frame = new FrameObj (canvas);
    96 	
    97 	repositionRequest=false;
    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 }
   107 
   108 void LinkableMapObj::setChildObj(LinkableMapObj* o)
   109 {
   110     childObj=o;
   111 }
   112 
   113 void LinkableMapObj::setParObj(LinkableMapObj* o)
   114 {
   115     parObj=o;
   116 	mapEditor=parObj->getMapEditor();
   117 }
   118 
   119 void LinkableMapObj::setParObjTmp(LinkableMapObj*,QPoint,int)
   120 {
   121 }
   122 
   123 void LinkableMapObj::unsetParObjTmp()
   124 {
   125 }
   126 
   127 LinkStyle LinkableMapObj::getDefLinkStyle ()
   128 {
   129 	LinkStyle ls=mapEditor->getLinkStyle();
   130 	switch (ls)
   131 	{
   132 		case StyleLine: 
   133 			return ls;
   134 			break;
   135 		case StyleParabel:
   136 			return ls;
   137 			break;
   138 		case StylePolyLine:	
   139 			if (depth>1)
   140 				return StyleLine;
   141 			else	
   142 				return ls;
   143 			break;
   144 		case StylePolyParabel:	
   145 			if (depth>1)
   146 				return StyleParabel;
   147 			else	
   148 				return ls;
   149 			break;
   150 		default: 
   151 			break;	
   152 	}	
   153 	return StyleUndef;
   154 }
   155 
   156 void LinkableMapObj::setLinkStyle(LinkStyle newstyle)
   157 {
   158 	//if (newstyle=style) return;
   159 	delLink();
   160 		
   161 	style=newstyle;
   162 
   163     if (childObj!=NULL && parObj != NULL)
   164     {
   165 		int i;
   166 		QCanvasLine* cl;
   167 		switch (style)
   168 		{
   169 			case StyleUndef:
   170 				bottomline->hide();
   171 				break;
   172 			case StyleLine: 
   173 				l = new QCanvasLine(canvas);
   174 				l->setPen( QPen(linkcolor, 1) );
   175 				l->setZ(Z_LINK);
   176 				if (visible)
   177 					l->show();
   178 				else
   179 					l->hide();
   180 				break;
   181 			case StyleParabel:
   182 				for (i=0;i<arcsegs;i++)
   183 				{
   184 					cl = new QCanvasLine(canvas);
   185 					cl->setPen( QPen(linkcolor, 1) );
   186 					cl->setPoints( 0,0,i*10,100);
   187 					cl->setZ(Z_LINK);
   188 					if (visible)
   189 						cl->show();
   190 					else
   191 						cl->hide();
   192 					segment.append(cl);
   193 				}
   194 				pa0.resize (arcsegs+1);
   195 				break;
   196 			case StylePolyLine:	
   197 				p = new QCanvasPolygon(canvas);
   198 				p->setBrush( linkcolor );
   199 				p->setZ(Z_LINK);
   200 				if (visible)
   201 					p->show();
   202 				else
   203 					p->hide();
   204 				pa0.resize (3);
   205 				// TODO
   206 				// a bit awkward: draw the lines additionally to polygon, to avoid
   207 				// missing pixels, when polygon is extremly flat
   208 				l = new QCanvasLine(canvas);
   209 				l->setPen( QPen(linkcolor, 1) );
   210 				l->setZ(Z_LINK);
   211 				if (visible)
   212 					l->show();
   213 				else
   214 					l->hide();
   215 				break;
   216 			case StylePolyParabel:	
   217 				p = new QCanvasPolygon(canvas);
   218 				p->setBrush( linkcolor );
   219 				p->setZ(Z_LINK);
   220 				if (visible)
   221 					p->show();
   222 				else
   223 					p->hide();
   224 				pa0.resize (arcsegs*2+2);
   225 				pa1.resize (arcsegs+1);
   226 				pa2.resize (arcsegs+1);
   227 
   228 				// TODO
   229 				// a bit awkward: draw the lines additionally 
   230 				// to polygon, to avoid missing pixels, 
   231 				// if polygon is extremly flat
   232 				for (i=0;i<arcsegs;i++)
   233 				{
   234 					cl = new QCanvasLine(canvas);
   235 					cl->setPen( QPen(linkcolor, 1) );
   236 					cl->setPoints( 0,0,i*10,100);
   237 					cl->setZ(Z_LINK);
   238 					if (visible)
   239 						cl->show();
   240 					else
   241 						cl->hide();
   242 					segment.append(cl);
   243 				}
   244 				break;
   245 			default: 
   246 				break;	
   247 		}	
   248 		// FIXME updateLink is usually called (multiple times) later:
   249 		//updateLink();
   250 	} else
   251 	{
   252 		qWarning ("Error: ChildObj or parObj == NULL in LinkableMapObj::setLinkStyle\n");
   253 	}
   254 }
   255 
   256 LinkStyle LinkableMapObj::getLinkStyle()
   257 {
   258 	return style;
   259 }
   260 
   261 void LinkableMapObj::setLinkPos(LinkPos lp)
   262 {
   263 	linkpos=lp;
   264 }
   265 
   266 LinkPos LinkableMapObj::getLinkPos()
   267 {
   268 	return linkpos;
   269 }
   270 
   271 
   272 void LinkableMapObj::setLinkColor()
   273 {
   274 	// Overloaded in BranchObj and childs
   275 	// here only set default color
   276 	setLinkColor (mapEditor->getDefLinkColor());
   277 }
   278 
   279 void LinkableMapObj::setLinkColor(QColor col)
   280 {
   281 	linkcolor=col;
   282     bottomline->setPen( QPen(linkcolor, 1) );
   283 	QCanvasLine *cl;
   284 	switch (style)
   285 	{
   286 		case StyleLine:
   287 			l->setPen( QPen(col,1));
   288 			break;	
   289 		case StyleParabel:	
   290 			for (cl=segment.first(); cl; cl=segment.next() )
   291 				cl->setPen( QPen(col,1));
   292 			break;
   293 		case StylePolyLine:
   294 			p->setBrush( QBrush(col));
   295 			l->setPen( QPen(col,1));
   296 			break;
   297 		case StylePolyParabel:	
   298 			p->setBrush( QBrush(col));
   299 			for (cl=segment.first(); cl; cl=segment.next() )
   300 				cl->setPen( QPen(col,1));
   301 			break;
   302 		default:
   303 			break;
   304 	} // switch (style)	
   305 	//FIXME updateLink();
   306 }
   307 
   308 QColor LinkableMapObj::getLinkColor()
   309 {
   310 	return linkcolor;
   311 }
   312 
   313 FrameType LinkableMapObj::getFrameType()
   314 {
   315 	return frame->getFrameType();
   316 }
   317 
   318 void LinkableMapObj::setFrameType(const FrameType &t)
   319 {
   320 	frame->setFrameType(t);
   321 	calcBBoxSize();
   322 	positionBBox();
   323 	requestReposition();
   324 }
   325 
   326 void LinkableMapObj::setFrameType(const QString &t)
   327 {
   328 	frame->setFrameType(t);
   329 	calcBBoxSize();
   330 	positionBBox();
   331 	requestReposition();
   332 }
   333 
   334 void LinkableMapObj::setVisibility (bool v)
   335 {
   336 	MapObj::setVisibility (v);
   337 	if (visible) 
   338 	{
   339 		bottomline->show();
   340 		// FIXME lines and segments should be done in LMO?
   341 		if (style==StyleLine && l) 
   342 		{
   343 			l->show();
   344 		} else
   345 		{
   346 			QCanvasLine* cl;
   347 			for (cl=segment.first(); cl; cl=segment.next() )
   348 				cl->show();
   349 		} 
   350 	} else 
   351 	{
   352 		bottomline->hide();
   353 		if (style==StyleLine && l) 
   354 		{
   355 			l->hide();
   356 		} else
   357 		{
   358 			QCanvasLine* cl;
   359 			for (cl=segment.first(); cl; cl=segment.next() )
   360 				cl->hide();
   361 		} 
   362 	}	
   363 }
   364 
   365 void LinkableMapObj::updateLink()
   366 {
   367     // needs:
   368     //	childPos of parent
   369     //	orient   of parent
   370     //	style
   371     // 
   372     // sets:
   373     //	orientation
   374     //	childPos
   375     //	parPos
   376 	//  offset
   377     //	drawing of the link itself
   378 
   379 
   380 	// updateLink is called from move, but called from constructor we don't
   381 	// have parents yet...
   382 	if (style==StyleUndef) return;	
   383 
   384 	if (frame->getFrameType() == NoFrame)
   385 		linkpos=LinkBottom;
   386 	else	
   387 		linkpos=LinkMiddle;
   388 	switch (linkpos)
   389 	{
   390 		case LinkMiddle:
   391 			offset=bbox.height() /2;
   392 			break;
   393 		default :
   394 			offset=bbox.height()-1;			// draw link to bottom of bbox
   395 			break;
   396 	}
   397 	
   398     double p2x,p2y;								// Set P2 Before setting
   399 	if (!link2ParPos)
   400 	{
   401 		p2x=QPoint( parObj->getChildPos() ).x();	// P1, we have to look at
   402 		p2y=QPoint( parObj->getChildPos() ).y();	// orientation
   403 	} else	
   404 	{
   405 		p2x=QPoint( parObj->getParPos() ).x();	
   406 		p2y=QPoint( parObj->getParPos() ).y();
   407 	} 
   408 
   409 	LinkOrient orientOld=orientation;
   410 
   411     // Set orientation, first look for orientation of parent
   412     if (parObj->getOrientation() != OrientUndef ) 
   413 		// use the orientation of the parent:
   414 		orientation=parObj->getOrientation();
   415     else
   416     {
   417 		// calc orientation depending on position rel to mapCenter
   418 		if (absPos.x() < QPoint(parObj->getChildPos() ).x() )
   419 			orientation=OrientLeftOfCenter; 
   420 		else
   421 			orientation=OrientRightOfCenter;
   422     }
   423 
   424 	if ((orientation!=orientOld) && (orientOld!= OrientUndef))
   425 	{
   426 		// Orientation just changed. Reorient this subbranch, because move is called
   427 		// before updateLink => Position is still the old one, which could lead to 
   428 		// linking of subranch to itself => segfault
   429 		//
   430 		// Also possible: called in BranchObj::init(), then orientOld==OrientUndef,
   431 		// no need to reposition now
   432 		reposition();
   433 	}
   434 	
   435     if (orientation==OrientLeftOfCenter )
   436     {
   437 		childPos=QPoint (absPos.x(),absPos.y()+offset);
   438 		parPos=QPoint (absPos.x()+ bbox.width(), absPos.y() + offset );
   439     } else
   440     {
   441 		childPos=QPoint (absPos.x()+ bbox.width(), absPos.y() + offset ); 
   442 		parPos=QPoint (absPos.x(),absPos.y()+offset);
   443     }
   444 
   445 	double p1x=parPos.x();	// Link is drawn from P1 to P2
   446 	double p1y=parPos.y();
   447 
   448 	double vx=p2x - p1x;	// V=P2-P1
   449 	double vy=p2y - p1y;
   450 
   451 	// Draw the horizontal line below heading (from ChildPos to ParPos)
   452 	bottomline->setPoints (lrint(childPos.x()),
   453 		lrint(childPos.y()),
   454 		lrint(p1x),
   455 		lrint(p1y) );
   456 
   457 	double a;	// angle
   458 	if (vx > -0.000001 && vx < 0.000001)
   459 		a=M_PI_2;
   460 	else
   461 		a=atan( vy / vx );
   462 	// "turning point" for drawing polygonal links
   463 	QPoint tp (-lrint(sin (a)*thickness_start), lrint(cos (a)*thickness_start));	
   464 	
   465 	QCanvasLine *cl;
   466 
   467 	int i;
   468 
   469     // Draw the link
   470 	switch (style)
   471 	{
   472 		case StyleLine:
   473 			l->setPoints( lrint (parPos.x()),
   474 				lrint(parPos.y()),
   475 				lrint(p2x),
   476 				lrint(p2y) );
   477 			break;	
   478 		case StyleParabel:	
   479 			parabel (pa0, p1x,p1y,p2x,p2y);
   480 			i=0;
   481 			for (cl=segment.first(); cl; cl=segment.next() )
   482 			{	
   483 				cl->setPoints( pa0.point(i).x(), pa0.point(i).y(),pa0.point(i+1).x(),pa0.point(i+1).y());
   484 				i++;
   485 			}
   486 			break;
   487 		case StylePolyLine:
   488 			pa0[0]=QPoint (lrint(p2x+tp.x()), lrint(p2y+tp.y()));
   489 			pa0[1]=QPoint (lrint(p2x-tp.x()), lrint(p2y-tp.y()));
   490 			pa0[2]=QPoint (lrint (parPos.x()), lrint(parPos.y()) );
   491 			p->setPoints (pa0);
   492 			// here too, draw line to avoid missing pixels
   493 			l->setPoints( lrint (parPos.x()),
   494 				lrint(parPos.y()),
   495 				lrint(p2x),
   496 				lrint(p2y) );
   497 			break;
   498 		case StylePolyParabel:	
   499 			parabel (pa1, p1x,p1y,p2x+tp.x(),p2y+tp.y());
   500 			parabel (pa2, p1x,p1y,p2x-tp.x(),p2y-tp.y());
   501 			for (i=0;i<=arcsegs;i++)
   502 			{
   503 				// Combine the arrays to a single one
   504 				pa0[i]=pa1[i];
   505 				pa0[i+arcsegs+1]=pa2[arcsegs-i];
   506 			}	
   507 			p->setPoints (pa0);
   508 			i=0;
   509 			for (cl=segment.first(); cl; cl=segment.next() )
   510 			{	
   511 				cl->setPoints( pa1.point(i).x(), pa1.point(i).y(),pa1.point(i+1).x(),pa1.point(i+1).y());
   512 				i++;
   513 			}
   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::findObj (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 		
   542 		if (typ=="mc:")
   543 		{
   544 			if (depth>0)
   545 				return false;	// in a subtree there is no center
   546 			else
   547 				break;
   548 		} else
   549 			if (typ=="bo:")
   550 				lmo=((BranchObj*)(lmo))->getBranchNum (num.toUInt());
   551 			else
   552 				if (typ=="fi:")
   553 					lmo=((BranchObj*)(lmo))->getFloatImageNum (num.toUInt());
   554 		if (!lmo) break;
   555 		
   556 		if (s.contains(","))
   557 			s=s.right(s.length() - part.length() -1 );
   558 		else	
   559 			break;
   560 	}
   561 	return lmo;
   562 }
   563 
   564 QPoint LinkableMapObj::getChildPos()
   565 {
   566     return childPos;
   567 }
   568 
   569 QPoint LinkableMapObj::getParPos()
   570 {
   571     return parPos;
   572 }
   573 
   574 QPoint LinkableMapObj::getRelPos()
   575 {
   576 	if (!parObj) return QPoint (0,0);
   577     return QPoint(
   578 		absPos.x() - parObj->x(),
   579 		absPos.y() - parObj->y()
   580 	);
   581 }
   582 
   583 LinkOrient LinkableMapObj::getOrientation()
   584 {
   585     return orientation;
   586 }
   587 
   588 int LinkableMapObj::getDepth()
   589 {
   590     return depth;
   591 }
   592 
   593 void LinkableMapObj::setMapEditor (MapEditor *me)
   594 {
   595 	mapEditor=me;
   596 }
   597 
   598 MapEditor* LinkableMapObj::getMapEditor ()
   599 {
   600 	return mapEditor;
   601 }
   602 
   603 QPoint LinkableMapObj::getRandPos()
   604 {
   605 	// Choose a random position with given distance to parent:
   606 	double a=rand()%360 * 2 * M_PI / 360;
   607     return QPoint ( (int)( + 150*cos (a)),
   608                     (int)( + 150*sin (a)));
   609 }
   610 
   611 void LinkableMapObj::alignRelativeTo (QPoint ref)
   612 {
   613 	cout << "LMO::alignRelTo   ref="<<ref<<endl;
   614 	//FIXME 
   615 }
   616 
   617 void LinkableMapObj::reposition()
   618 {
   619 	if (depth==0)
   620 	{
   621 		// only calculate the sizes once. If the deepest LMO changes its height,
   622 		// all upper LMOs have to change, too.
   623 		calcBBoxSizeWithChilds();
   624 
   625 	    alignRelativeTo ( QPoint (absPos.x(),
   626 							absPos.y()-(bboxTotal.height()-bbox.height())/2) );
   627 	} else
   628 	{
   629 		// This is only important for moving branches:
   630 		// For editing a branch it isn't called...
   631 	    alignRelativeTo ( QPoint (absPos.x(),
   632 							absPos.y()-(bboxTotal.height()-bbox.height())/2) );
   633 	}
   634 }
   635 
   636 void LinkableMapObj::requestReposition()
   637 {
   638 	if (!repositionRequest)
   639 	{
   640 		// Pass on the request to parental objects, if this hasn't
   641 		// been done yet
   642 		repositionRequest=true;
   643 		if (parObj) parObj->requestReposition();
   644 	}
   645 }
   646 
   647 void LinkableMapObj::forceReposition()
   648 {
   649 	// Sometimes a reposition has to be done immediatly: For example
   650 	// if the note editor flag changes, there is no user event in mapeditor
   651 	// which could collect requests for a reposition.
   652 	// Then we have to call forceReposition()
   653 	// But no rule without exception: While loading a map or undoing it,
   654 	// we want to block expensive repositioning, but just do it once at
   655 	// the end, thus check first:
   656 
   657 	if (mapEditor->blockReposition()) return;
   658 	
   659 	// Pass on the request to parental objects, if this hasn't been done yet
   660 	
   661 	if (parObj) 
   662 		parObj->forceReposition(); 
   663 	else 
   664 		reposition(); 
   665 }
   666 
   667 bool LinkableMapObj::repositionRequested()
   668 {
   669 	return repositionRequest;
   670 }
   671 
   672 
   673 void LinkableMapObj::setSelBox()
   674 {
   675     selbox->setX (bbox.x() );
   676     selbox->setY (bbox.y() );
   677     selbox->setSize (bbox.width(), bbox.height() );
   678 }
   679 
   680 void LinkableMapObj::select()
   681 {
   682 	setSelBox();
   683     selected=true;
   684     selbox->show();
   685 }
   686 
   687 
   688 void LinkableMapObj::unselect()
   689 {
   690     selected=false;
   691     selbox->hide();
   692 }
   693 
   694 void LinkableMapObj::parabel (QPointArray &ya, double p1x, double p1y, double p2x, double p2y)
   695 
   696 {
   697 	double vx=p2x - p1x;	// V=P2-P1
   698 	double vy=p2y - p1y;
   699 
   700 	double dx;				// delta x during calculation of parabel
   701 	
   702 	double pnx;				// next point
   703 	double pny;
   704 	double m;
   705 
   706 	if (vx > -0.0001 && vx < 0.0001)
   707 		m=0;
   708 	else	
   709 		m=(vy / (vx*vx));
   710 	dx=vx/(arcsegs);
   711 	int i;
   712 	ya.setPoint (0,QPoint (lrint(p1x),lrint(p1y)));
   713 	for (i=1;i<=arcsegs;i++)
   714 	{	
   715 		pnx=p1x+dx;
   716 		pny=m*(pnx-parPos.x())*(pnx-parPos.x())+parPos.y();
   717 		ya.setPoint (i,QPoint (lrint(pnx),lrint(pny)));
   718 		p1x=pnx;
   719 		p1y=pny;
   720 	}	
   721 }
   722