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));
325 p->setBrush( QBrush(col));
333 QColor LinkableMapObj::getLinkColor()
338 void LinkableMapObj::setVisibility (bool v)
340 MapObj::setVisibility (v);
343 // We can hide the link, while object is not selected
344 if (hideLinkUnselected && !selected)
356 for (int i=0; i<segment.size(); ++i)
357 segment.at(i)->show();
377 for (int i=0; i<segment.size(); ++i)
378 segment.at(i)->hide();
392 void LinkableMapObj::setOrientation()
394 Orientation orientOld=orientation;
398 orientation=UndefinedOrientation;
402 // Set orientation, first look for orientation of parent
403 if (parObj->getOrientation() != UndefinedOrientation )
404 // use the orientation of the parent:
405 orientation=parObj->getOrientation();
408 // calc orientation depending on position rel to parent
409 if (absPos.x() < QPointF(parObj->getChildPos() ).x() )
410 orientation=LeftOfCenter;
412 orientation=RightOfCenter;
414 if (orientOld!=orientation) requestReposition();
417 void LinkableMapObj::updateLink()
420 // childPos of parent
426 // childPos (by calling setDockPos())
427 // parPos (by calling setDockPos())
429 // drawing of the link itself
431 // updateLink is called from move, but called from constructor we don't
432 // have parents yet...
433 if (style==UndefinedStyle) return;
438 bottomlineY=bbox.top() + bbox.height()/2; // draw link to middle (of frame)
441 bottomlineY=bbox.bottom()-1; // draw link to bottom of box
445 double p2x,p2y; // Set P2 Before setting
448 p2x=QPointF( parObj->getChildPos() ).x(); // P1, we have to look at
449 p2y=QPointF( parObj->getChildPos() ).y(); // orientation
452 p2x=QPointF( parObj->getParPos() ).x();
453 p2y=QPointF( parObj->getParPos() ).y();
456 setDockPos(); // Call overloaded method
459 double p1x=parPos.x(); // Link is drawn from P1 to P2
460 double p1y=parPos.y();
462 double vx=p2x - p1x; // V=P2-P1
465 // Draw the horizontal line below heading (from ChildPos to ParPos)
466 //bottomline->prepareGeometryChange();
467 bottomline->setLine (QLine (qRound(childPos.x()),
468 qRound(childPos.y()),
473 if (vx > -0.000001 && vx < 0.000001)
477 // "turning point" for drawing polygonal links
478 QPointF tp (-qRound(sin (a)*thickness_start), qRound(cos (a)*thickness_start));
484 //l->prepareGeometryChange();
485 l->setLine( QLine(qRound (parPos.x()),
491 parabel (pa0, p1x,p1y,p2x,p2y);
492 for (int i=0; i<segment.size(); ++i)
494 //segment.at(i)->prepareGeometryChange();
495 segment.at(i)->setLine(QLineF( pa0.at(i).x(), pa0.at(i).y(),pa0.at(i+1).x(),pa0.at(i+1).y()));
500 pa0<<QPointF (qRound(p2x+tp.x()), qRound(p2y+tp.y()));
501 pa0<<QPointF (qRound(p2x-tp.x()), qRound(p2y-tp.y()));
502 pa0<<QPointF (qRound (parPos.x()), qRound(parPos.y()) );
503 //p->prepareGeometryChange();
504 p->setPolygon(QPolygonF (pa0));
507 parabel (pa1, p1x,p1y,p2x+tp.x(),p2y+tp.y());
508 parabel (pa2, p1x,p1y,p2x-tp.x(),p2y-tp.y());
510 for (int i=0;i<=arcsegs;i++)
511 pa0 << QPointF (pa1.at(i));
512 for (int i=0;i<=arcsegs;i++)
513 pa0 << QPointF (pa2.at(arcsegs-i));
514 //p->prepareGeometryChange();
515 p->setPolygon(QPolygonF (pa0));
522 LinkableMapObj* LinkableMapObj::getChildObj()
527 LinkableMapObj* LinkableMapObj::getParObj()
532 LinkableMapObj* LinkableMapObj::findObjBySelect (QString s)
534 LinkableMapObj *lmo=this;
538 while (!s.isEmpty() )
540 part=s.section(",",0,0);
542 num=part.right(part.length() - 3);
546 return false; // in a subtree there is no center
551 lmo=((BranchObj*)(lmo))->getBranchNum (num.toInt());
554 lmo=((BranchObj*)(lmo))->getFloatImageNum (num.toUInt());
558 s=s.right(s.length() - part.length() -1 );
565 QPointF LinkableMapObj::getChildPos()
570 QPointF LinkableMapObj::getParPos()
575 void LinkableMapObj::setUseOrientation (const bool &b)
577 if (useOrientation!=b)
584 LinkableMapObj::Orientation LinkableMapObj::getOrientation()
589 int LinkableMapObj::getDepth()
594 void LinkableMapObj::setMapEditor (MapEditor *me)
599 MapEditor* LinkableMapObj::getMapEditor ()
604 QPointF LinkableMapObj::getRandPos()
606 // Choose a random position with given distance to parent:
607 double a=rand()%360 * 2 * M_PI / 360;
608 return QPointF ( (int)( + 150*cos (a)),
609 (int)( + 150*sin (a)));
612 void LinkableMapObj::reposition()
616 void LinkableMapObj::requestReposition()
618 if (!repositionRequest)
620 // Pass on the request to parental objects, if this hasn't
622 repositionRequest=true;
623 if (parObj) parObj->requestReposition();
627 void LinkableMapObj::forceReposition()
629 // Sometimes a reposition has to be done immediatly: For example
630 // if the note editor flag changes, there is no user event in mapeditor
631 // which could collect requests for a reposition.
632 // Then we have to call forceReposition()
633 // But no rule without exception: While loading a map or undoing it,
634 // we want to block expensive repositioning, but just do it once at
635 // the end, thus check first:
637 if (mapEditor->isRepositionBlocked()) return;
639 // Pass on the request to parental objects, if this hasn't been done yet
642 parObj->forceReposition();
647 bool LinkableMapObj::repositionRequested()
649 return repositionRequest;
653 void LinkableMapObj::select()
655 // select and unselect are still needed to
656 // handle hiding of links
658 setVisibility (visible);
662 void LinkableMapObj::unselect()
665 // Maybe we have to hide the link:
666 setVisibility (visible);
669 void LinkableMapObj::parabel (QPolygonF &ya, double p1x, double p1y, double p2x, double p2y)
672 double vx=p2x - p1x; // V=P2-P1
675 double dx; // delta x during calculation of parabel
677 double pnx; // next point
681 if (vx > -0.0001 && vx < 0.0001)
687 ya<<QPointF (qRound(p1x),qRound(p1y));
688 for (int i=1;i<=arcsegs;i++)
691 pny=m*(pnx-parPos.x())*(pnx-parPos.x())+parPos.y();
692 ya<<QPointF (qRound(pnx),qRound(pny));
698 QString LinkableMapObj::getLinkAttr ()
700 if (hideLinkUnselected)
701 return attribut ("hideLink","true");
703 return attribut ("hideLink","false");