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()
36 void LinkableMapObj::delLink()
44 while (!segment.isEmpty()) delete segment.takeFirst();
49 case StylePolyParabel:
57 void LinkableMapObj::init ()
65 childPos=QPointF(0,0);
68 orientation=OrientUndef;
75 // TODO instead of linkcolor pen.color() could be used all around
77 pen.setColor (linkcolor);
78 pen.setCapStyle ( Qt::RoundCap );
79 bottomline=scene->addLine(QLineF(1,1,1,1),pen);
80 bottomline->setZValue(Z_LINK);
83 // Prepare showing the selection of a MapObj
84 selbox = scene->addRect(QRectF(0,0,0,0), QPen(QColor(255,255,0) ), QColor(255,255,0));
85 selbox->setZValue(Z_SELBOX);
89 hideLinkUnselected=false;
91 topPad=botPad=leftPad=rightPad=0;
94 frame = new FrameObj (scene);
96 repositionRequest=false;
104 void LinkableMapObj::copy (LinkableMapObj* other)
107 bboxTotal=other->bboxTotal;
108 setLinkStyle(other->style);
109 setLinkColor (other->linkcolor);
110 relPos=other->relPos;
111 useOrientation=other->useOrientation;
115 void LinkableMapObj::setChildObj(LinkableMapObj* o)
120 void LinkableMapObj::setParObj(LinkableMapObj* o)
123 mapEditor=parObj->getMapEditor();
126 void LinkableMapObj::setParObjTmp(LinkableMapObj*,QPointF,int)
130 void LinkableMapObj::unsetParObjTmp()
134 bool LinkableMapObj::hasParObjTmp()
136 if (parObjTmpBuf) return true;
140 void LinkableMapObj::setUseRelPos (const bool &b)
145 void LinkableMapObj::setRelPos()
149 relPos.setX (absPos.x() - parObj->getChildPos().x() );
150 relPos.setY (absPos.y() - parObj->getChildPos().y() );
151 parObj->calcBBoxSize();
154 qWarning ("LMO::setRelPos No parent yet!");
158 void LinkableMapObj::setRelPos(const QPointF &p)
163 parObj->calcBBoxSize();
167 qWarning ("LMO::setRelPos No parent yet!");
171 QPointF LinkableMapObj::getRelPos()
173 if (!parObj) return QPointF();
177 qreal LinkableMapObj::getTopPad()
182 qreal LinkableMapObj::getLeftPad()
187 qreal LinkableMapObj::getRightPad()
192 LinkStyle LinkableMapObj::getDefLinkStyle ()
194 if (mapEditor==0) return StyleUndef;
195 LinkStyle ls=mapEditor->getMapLinkStyle();
210 case StylePolyParabel:
222 void LinkableMapObj::setLinkStyle(LinkStyle newstyle)
224 //if (newstyle=style) return;
229 if (childObj!=NULL && parObj != NULL)
231 QGraphicsLineItem *cl;
238 l = scene->addLine(QLineF(1,1,1,1),pen);
239 l->setZValue(Z_LINK);
246 for (int i=0;i<arcsegs;i++)
248 cl = scene->addLine(QLineF(i*5,0,i*10,100),pen);
249 cl->setZValue(Z_LINK);
256 pa0.resize (arcsegs+1);
259 p =scene->addPolygon(QPolygonF(),pen,linkcolor);
260 p->setZValue(Z_LINK);
267 case StylePolyParabel:
268 p = scene->addPolygon(QPolygonF(),pen,linkcolor);
269 p->setZValue(Z_LINK);
274 pa0.resize (arcsegs*2+2);
275 pa1.resize (arcsegs+1);
276 pa2.resize (arcsegs+1);
284 LinkStyle LinkableMapObj::getLinkStyle()
289 void LinkableMapObj::setHideLinkUnselected(bool b)
291 hideLinkUnselected=b;
292 setVisibility (visible);
296 bool LinkableMapObj::getHideLinkUnselected()
298 return hideLinkUnselected;
301 void LinkableMapObj::setLinkPos(LinkPos lp)
306 LinkPos LinkableMapObj::getLinkPos()
312 void LinkableMapObj::setLinkColor()
314 // Overloaded in BranchObj and childs
315 // here only set default color
317 setLinkColor (mapEditor->getMapDefLinkColor());
320 void LinkableMapObj::setLinkColor(QColor col)
324 bottomline->setPen( pen );
331 for (int i=0; i<segment.size(); ++i)
332 segment.at(i)->setPen( pen);
335 p->setBrush( QBrush(col));
337 case StylePolyParabel:
338 p->setBrush( QBrush(col));
345 QColor LinkableMapObj::getLinkColor()
350 FrameType LinkableMapObj::getFrameType()
352 return frame->getFrameType();
355 void LinkableMapObj::setFrameType(const FrameType &t)
357 frame->setFrameType(t);
363 void LinkableMapObj::setFrameType(const QString &t)
365 frame->setFrameType(t);
371 void LinkableMapObj::setVisibility (bool v)
373 MapObj::setVisibility (v);
376 // We can hide the link, while object is not selected
377 if (hideLinkUnselected && !selected)
389 for (int i=0; i<segment.size(); ++i)
390 segment.at(i)->show();
395 case StylePolyParabel:
410 for (int i=0; i<segment.size(); ++i)
411 segment.at(i)->hide();
416 case StylePolyParabel:
425 void LinkableMapObj::setOrientation()
427 LinkOrient orientOld=orientation;
431 orientation=OrientUndef;
435 // Set orientation, first look for orientation of parent
436 if (parObj->getOrientation() != OrientUndef )
437 // use the orientation of the parent:
438 orientation=parObj->getOrientation();
441 // calc orientation depending on position rel to parent
442 if (absPos.x() < QPointF(parObj->getChildPos() ).x() )
443 orientation=OrientLeftOfCenter;
445 orientation=OrientRightOfCenter;
447 if (orientOld!=orientation) requestReposition();
450 void LinkableMapObj::updateLink()
453 // childPos of parent
459 // childPos (by calling setDockPos())
460 // parPos (by calling setDockPos())
462 // drawing of the link itself
464 // updateLink is called from move, but called from constructor we don't
465 // have parents yet...
466 if (style==StyleUndef) return;
468 if (frame->getFrameType() == NoFrame)
475 bottomlineY=bbox.top()+bbox.height() /2; // draw link to middle (of frame)
478 bottomlineY=bbox.bottom()-1; // draw link to bottom of box
482 double p2x,p2y; // Set P2 Before setting
485 p2x=QPointF( parObj->getChildPos() ).x(); // P1, we have to look at
486 p2y=QPointF( parObj->getChildPos() ).y(); // orientation
489 p2x=QPointF( parObj->getParPos() ).x();
490 p2y=QPointF( parObj->getParPos() ).y();
493 setDockPos(); // Call overloaded method
496 double p1x=parPos.x(); // Link is drawn from P1 to P2
497 double p1y=parPos.y();
499 double vx=p2x - p1x; // V=P2-P1
502 // Draw the horizontal line below heading (from ChildPos to ParPos)
503 //bottomline->prepareGeometryChange();
504 bottomline->setLine (QLine (qRound(childPos.x()),
505 qRound(childPos.y()),
510 if (vx > -0.000001 && vx < 0.000001)
514 // "turning point" for drawing polygonal links
515 QPointF tp (-qRound(sin (a)*thickness_start), qRound(cos (a)*thickness_start));
521 //l->prepareGeometryChange();
522 l->setLine( QLine(qRound (parPos.x()),
528 parabel (pa0, p1x,p1y,p2x,p2y);
529 for (int i=0; i<segment.size(); ++i)
531 //segment.at(i)->prepareGeometryChange();
532 segment.at(i)->setLine(QLineF( pa0.at(i).x(), pa0.at(i).y(),pa0.at(i+1).x(),pa0.at(i+1).y()));
537 pa0<<QPointF (qRound(p2x+tp.x()), qRound(p2y+tp.y()));
538 pa0<<QPointF (qRound(p2x-tp.x()), qRound(p2y-tp.y()));
539 pa0<<QPointF (qRound (parPos.x()), qRound(parPos.y()) );
540 //p->prepareGeometryChange();
541 p->setPolygon(QPolygonF (pa0));
543 case StylePolyParabel:
544 parabel (pa1, p1x,p1y,p2x+tp.x(),p2y+tp.y());
545 parabel (pa2, p1x,p1y,p2x-tp.x(),p2y-tp.y());
547 for (int i=0;i<=arcsegs;i++)
548 pa0 << QPointF (pa1.at(i));
549 for (int i=0;i<=arcsegs;i++)
550 pa0 << QPointF (pa2.at(arcsegs-i));
551 //p->prepareGeometryChange();
552 p->setPolygon(QPolygonF (pa0));
559 LinkableMapObj* LinkableMapObj::getChildObj()
564 LinkableMapObj* LinkableMapObj::getParObj()
569 LinkableMapObj* LinkableMapObj::findObjBySelect (QString s)
571 LinkableMapObj *lmo=this;
575 while (!s.isEmpty() )
577 part=s.section(",",0,0);
579 num=part.right(part.length() - 3);
583 return false; // in a subtree there is no center
588 lmo=((BranchObj*)(lmo))->getBranchNum (num.toInt());
591 lmo=((BranchObj*)(lmo))->getFloatImageNum (num.toUInt());
595 s=s.right(s.length() - part.length() -1 );
602 QPointF LinkableMapObj::getChildPos()
607 QPointF LinkableMapObj::getParPos()
612 void LinkableMapObj::setUseOrientation (const bool &b)
614 if (useOrientation!=b)
621 LinkOrient LinkableMapObj::getOrientation()
626 int LinkableMapObj::getDepth()
631 void LinkableMapObj::setMapEditor (MapEditor *me)
636 MapEditor* LinkableMapObj::getMapEditor ()
641 QPointF LinkableMapObj::getRandPos()
643 // Choose a random position with given distance to parent:
644 double a=rand()%360 * 2 * M_PI / 360;
645 return QPointF ( (int)( + 150*cos (a)),
646 (int)( + 150*sin (a)));
649 void LinkableMapObj::reposition()
653 void LinkableMapObj::requestReposition()
655 if (!repositionRequest)
657 // Pass on the request to parental objects, if this hasn't
659 repositionRequest=true;
660 if (parObj) parObj->requestReposition();
664 void LinkableMapObj::forceReposition()
666 // Sometimes a reposition has to be done immediatly: For example
667 // if the note editor flag changes, there is no user event in mapeditor
668 // which could collect requests for a reposition.
669 // Then we have to call forceReposition()
670 // But no rule without exception: While loading a map or undoing it,
671 // we want to block expensive repositioning, but just do it once at
672 // the end, thus check first:
674 if (mapEditor->isRepositionBlocked()) return;
676 // Pass on the request to parental objects, if this hasn't been done yet
679 parObj->forceReposition();
684 bool LinkableMapObj::repositionRequested()
686 return repositionRequest;
690 void LinkableMapObj::setSelBox()
692 //selbox->prepareGeometryChange();
693 selbox->setRect (clickBox);
696 void LinkableMapObj::select()
701 setVisibility (visible);
705 void LinkableMapObj::unselect()
709 // Maybe we have to hide the link:
710 setVisibility (visible);
713 void LinkableMapObj::parabel (QPolygonF &ya, double p1x, double p1y, double p2x, double p2y)
716 double vx=p2x - p1x; // V=P2-P1
719 double dx; // delta x during calculation of parabel
721 double pnx; // next point
725 if (vx > -0.0001 && vx < 0.0001)
731 ya<<QPointF (qRound(p1x),qRound(p1y));
732 for (int i=1;i<=arcsegs;i++)
735 pny=m*(pnx-parPos.x())*(pnx-parPos.x())+parPos.y();
736 ya<<QPointF (qRound(pnx),qRound(pny));
742 QString LinkableMapObj::getLinkAttr ()
744 if (hideLinkUnselected)
745 return attribut ("hideLink","true");
747 return attribut ("hideLink","false");