3 #include "linkablemapobj.h"
7 /////////////////////////////////////////////////////////////////
9 /////////////////////////////////////////////////////////////////
11 LinkableMapObj::LinkableMapObj():MapObj()
13 // cout << "Const LinkableMapObj ()\n";
17 LinkableMapObj::LinkableMapObj(QGraphicsScene* s) :MapObj(s)
19 // cout << "Const LinkableMapObj (s)\n";
23 LinkableMapObj::LinkableMapObj (LinkableMapObj* lmo) : MapObj (lmo->scene)
28 LinkableMapObj::~LinkableMapObj()
34 void LinkableMapObj::delLink()
42 while (!segment.isEmpty()) delete segment.takeFirst();
55 void LinkableMapObj::init ()
63 childPos=QPointF(0,0);
66 orientation=UndefinedOrientation;
73 // TODO instead of linkcolor pen.color() could be used all around
75 pen.setColor (linkcolor);
76 pen.setCapStyle ( Qt::RoundCap );
77 bottomline=scene->addLine(QLineF(1,1,1,1),pen);
78 bottomline->setZValue(Z_LINK);
81 // Prepare showing the selection of a MapObj
84 hideLinkUnselected=false;
86 topPad=botPad=leftPad=rightPad=0;
88 repositionRequest=false;
96 void LinkableMapObj::copy (LinkableMapObj* other)
99 bboxTotal=other->bboxTotal;
100 setLinkStyle(other->style);
101 setLinkColor (other->linkcolor);
102 relPos=other->relPos;
103 useOrientation=other->useOrientation;
107 void LinkableMapObj::setChildObj(LinkableMapObj* o)
112 void LinkableMapObj::setParObj(LinkableMapObj* o)
115 mapEditor=parObj->getMapEditor();
118 void LinkableMapObj::setParObjTmp(LinkableMapObj*,QPointF,int)
122 void LinkableMapObj::unsetParObjTmp()
126 bool LinkableMapObj::hasParObjTmp()
128 if (parObjTmpBuf) return true;
132 void LinkableMapObj::setUseRelPos (const bool &b)
137 void LinkableMapObj::setRelPos()
141 relPos.setX (absPos.x() - parObj->getChildPos().x() );
142 relPos.setY (absPos.y() - parObj->getChildPos().y() );
143 parObj->calcBBoxSize();
147 void LinkableMapObj::setRelPos(const QPointF &p)
152 parObj->calcBBoxSize();
157 QPointF LinkableMapObj::getRelPos()
159 if (!parObj) return QPointF();
163 qreal LinkableMapObj::getTopPad()
168 qreal LinkableMapObj::getLeftPad()
173 qreal LinkableMapObj::getRightPad()
178 LinkableMapObj::Style LinkableMapObj::getDefLinkStyle ()
180 if (mapEditor==0) return UndefinedStyle;
181 Style ls=mapEditor->getMapLinkStyle();
205 return UndefinedStyle;
208 void LinkableMapObj::setLinkStyle(Style newstyle)
210 //if (newstyle=style) return;
215 if (childObj!=NULL && parObj != NULL)
217 QGraphicsLineItem *cl;
224 l = scene->addLine(QLineF(1,1,1,1),pen);
225 l->setZValue(Z_LINK);
232 for (int i=0;i<arcsegs;i++)
234 cl = scene->addLine(QLineF(i*5,0,i*10,100),pen);
235 cl->setZValue(Z_LINK);
242 pa0.resize (arcsegs+1);
245 p =scene->addPolygon(QPolygonF(),pen,linkcolor);
246 p->setZValue(Z_LINK);
254 p = scene->addPolygon(QPolygonF(),pen,linkcolor);
255 p->setZValue(Z_LINK);
260 pa0.resize (arcsegs*2+2);
261 pa1.resize (arcsegs+1);
262 pa2.resize (arcsegs+1);
270 LinkableMapObj::Style LinkableMapObj::getLinkStyle()
275 void LinkableMapObj::setHideLinkUnselected(bool b)
277 hideLinkUnselected=b;
278 setVisibility (visible);
282 bool LinkableMapObj::getHideLinkUnselected()
284 return hideLinkUnselected;
287 void LinkableMapObj::setLinkPos(Position lp)
292 LinkableMapObj::Position LinkableMapObj::getLinkPos()
298 void LinkableMapObj::setLinkColor()
300 // Overloaded in BranchObj and childs
301 // here only set default color
303 setLinkColor (mapEditor->getMapDefLinkColor());
306 void LinkableMapObj::setLinkColor(QColor col)
310 bottomline->setPen( pen );
317 for (int i=0; i<segment.size(); ++i)
318 segment.at(i)->setPen( pen);
321 p->setBrush( QBrush(col));
324 p->setBrush( QBrush(col));
331 QColor LinkableMapObj::getLinkColor()
336 void LinkableMapObj::setVisibility (bool v)
338 MapObj::setVisibility (v);
341 // We can hide the link, while object is not selected
342 if (hideLinkUnselected && !selected)
354 for (int i=0; i<segment.size(); ++i)
355 segment.at(i)->show();
375 for (int i=0; i<segment.size(); ++i)
376 segment.at(i)->hide();
390 void LinkableMapObj::setOrientation()
392 Orientation orientOld=orientation;
396 orientation=UndefinedOrientation;
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();
406 // calc orientation depending on position rel to parent
407 if (absPos.x() < QPointF(parObj->getChildPos() ).x() )
408 orientation=LeftOfCenter;
410 orientation=RightOfCenter;
412 if (orientOld!=orientation) requestReposition();
415 void LinkableMapObj::updateLink()
418 // childPos of parent
424 // childPos (by calling setDockPos())
425 // parPos (by calling setDockPos())
427 // drawing of the link itself
429 // updateLink is called from move, but called from constructor we don't
430 // have parents yet...
431 if (style==UndefinedStyle) return;
436 bottomlineY=bbox.top() + bbox.height()/2; // draw link to middle (of frame)
439 bottomlineY=bbox.bottom()-1; // draw link to bottom of box
443 double p2x,p2y; // Set P2 Before setting
446 p2x=QPointF( parObj->getChildPos() ).x(); // P1, we have to look at
447 p2y=QPointF( parObj->getChildPos() ).y(); // orientation
450 p2x=QPointF( parObj->getParPos() ).x();
451 p2y=QPointF( parObj->getParPos() ).y();
454 setDockPos(); // Call overloaded method
457 double p1x=parPos.x(); // Link is drawn from P1 to P2
458 double p1y=parPos.y();
460 double vx=p2x - p1x; // V=P2-P1
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()),
471 if (vx > -0.000001 && vx < 0.000001)
475 // "turning point" for drawing polygonal links
476 QPointF tp (-qRound(sin (a)*thickness_start), qRound(cos (a)*thickness_start));
482 //l->prepareGeometryChange();
483 l->setLine( QLine(qRound (parPos.x()),
489 parabel (pa0, p1x,p1y,p2x,p2y);
490 for (int i=0; i<segment.size(); ++i)
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()));
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));
505 parabel (pa1, p1x,p1y,p2x+tp.x(),p2y+tp.y());
506 parabel (pa2, p1x,p1y,p2x-tp.x(),p2y-tp.y());
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));
520 LinkableMapObj* LinkableMapObj::getChildObj()
525 LinkableMapObj* LinkableMapObj::getParObj()
530 LinkableMapObj* LinkableMapObj::findObjBySelect (QString s)
532 LinkableMapObj *lmo=this;
536 while (!s.isEmpty() )
538 part=s.section(",",0,0);
540 num=part.right(part.length() - 3);
544 return false; // in a subtree there is no center
549 lmo=((BranchObj*)(lmo))->getBranchNum (num.toInt());
552 lmo=((BranchObj*)(lmo))->getFloatImageNum (num.toUInt());
556 s=s.right(s.length() - part.length() -1 );
563 QPointF LinkableMapObj::getChildPos()
568 QPointF LinkableMapObj::getParPos()
573 void LinkableMapObj::setUseOrientation (const bool &b)
575 if (useOrientation!=b)
582 LinkableMapObj::Orientation LinkableMapObj::getOrientation()
587 int LinkableMapObj::getDepth()
592 void LinkableMapObj::setMapEditor (MapEditor *me)
597 MapEditor* LinkableMapObj::getMapEditor ()
602 QPointF LinkableMapObj::getRandPos()
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)));
610 void LinkableMapObj::reposition()
614 void LinkableMapObj::requestReposition()
616 if (!repositionRequest)
618 // Pass on the request to parental objects, if this hasn't
620 repositionRequest=true;
621 if (parObj) parObj->requestReposition();
625 void LinkableMapObj::forceReposition()
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:
635 if (mapEditor->isRepositionBlocked()) return;
637 // Pass on the request to parental objects, if this hasn't been done yet
640 parObj->forceReposition();
645 bool LinkableMapObj::repositionRequested()
647 return repositionRequest;
651 void LinkableMapObj::select()
653 // select and unselect are still needed to
654 // handle hiding of links
656 setVisibility (visible);
660 void LinkableMapObj::unselect()
663 // Maybe we have to hide the link:
664 setVisibility (visible);
667 void LinkableMapObj::parabel (QPolygonF &ya, double p1x, double p1y, double p2x, double p2y)
670 double vx=p2x - p1x; // V=P2-P1
673 double dx; // delta x during calculation of parabel
675 double pnx; // next point
679 if (vx > -0.0001 && vx < 0.0001)
685 ya<<QPointF (qRound(p1x),qRound(p1y));
686 for (int i=1;i<=arcsegs;i++)
689 pny=m*(pnx-parPos.x())*(pnx-parPos.x())+parPos.y();
690 ya<<QPointF (qRound(pnx),qRound(pny));
696 QString LinkableMapObj::getLinkAttr ()
698 if (hideLinkUnselected)
699 return attribut ("hideLink","true");
701 return attribut ("hideLink","false");