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;
99 void LinkableMapObj::copy (LinkableMapObj* other)
102 bboxTotal=other->bboxTotal;
103 setLinkStyle(other->style);
104 setLinkColor (other->linkcolor);
105 relPos=other->relPos;
106 useOrientation=other->useOrientation;
110 void LinkableMapObj::setChildObj(LinkableMapObj* o)
115 void LinkableMapObj::setParObj(LinkableMapObj* o)
118 mapEditor=parObj->getMapEditor();
121 void LinkableMapObj::setParObjTmp(LinkableMapObj*,QPointF,int)
125 void LinkableMapObj::unsetParObjTmp()
129 bool LinkableMapObj::hasParObjTmp()
131 if (parObjTmpBuf) return true;
135 void LinkableMapObj::setUseRelPos (const bool &b)
140 void LinkableMapObj::setRelPos()
144 relPos.setX (absPos.x() - parObj->getChildPos().x() );
145 relPos.setY (absPos.y() - parObj->getChildPos().y() );
146 parObj->calcBBoxSize();
150 void LinkableMapObj::setRelPos(const QPointF &p)
155 parObj->calcBBoxSize();
160 QPointF LinkableMapObj::getRelPos()
162 if (!parObj) return QPointF();
166 qreal LinkableMapObj::getTopPad()
171 qreal LinkableMapObj::getLeftPad()
176 qreal LinkableMapObj::getRightPad()
181 LinkableMapObj::Style LinkableMapObj::getDefLinkStyle ()
183 if (!mapEditor) return UndefinedStyle;
184 Style ls=mapEditor->getMapLinkStyle();
208 return UndefinedStyle;
211 void LinkableMapObj::setLinkStyle(Style newstyle)
213 //if (newstyle=style) return;
218 if (childObj!=NULL && parObj != NULL)
220 QGraphicsLineItem *cl;
227 l = scene->addLine(QLineF(1,1,1,1),pen);
228 l->setZValue(Z_LINK);
235 for (int i=0;i<arcsegs;i++)
237 cl = scene->addLine(QLineF(i*5,0,i*10,100),pen);
238 cl->setZValue(Z_LINK);
245 pa0.resize (arcsegs+1);
248 p =scene->addPolygon(QPolygonF(),pen,linkcolor);
249 p->setZValue(Z_LINK);
257 p = scene->addPolygon(QPolygonF(),pen,linkcolor);
258 p->setZValue(Z_LINK);
263 pa0.resize (arcsegs*2+2);
264 pa1.resize (arcsegs+1);
265 pa2.resize (arcsegs+1);
273 LinkableMapObj::Style LinkableMapObj::getLinkStyle()
278 void LinkableMapObj::setHideLinkUnselected(bool b)
280 hideLinkUnselected=b;
281 setVisibility (visible);
285 bool LinkableMapObj::getHideLinkUnselected()
287 return hideLinkUnselected;
290 void LinkableMapObj::setLinkPos(Position lp)
295 LinkableMapObj::Position LinkableMapObj::getLinkPos()
300 void LinkableMapObj::setID (const QString &s)
305 QString LinkableMapObj::getID()
310 void LinkableMapObj::setLinkColor()
312 // Overloaded in BranchObj and childs
313 // here only set default color
315 setLinkColor (mapEditor->getMapDefLinkColor());
318 void LinkableMapObj::setLinkColor(QColor col)
322 bottomline->setPen( pen );
329 for (int i=0; i<segment.size(); ++i)
330 segment.at(i)->setPen( pen);
333 p->setBrush( QBrush(col));
337 p->setBrush( QBrush(col));
345 QColor LinkableMapObj::getLinkColor()
350 void LinkableMapObj::setVisibility (bool v)
352 MapObj::setVisibility (v);
355 // We can hide the link, while object is not selected
356 if (hideLinkUnselected && !selected)
368 for (int i=0; i<segment.size(); ++i)
369 segment.at(i)->show();
389 for (int i=0; i<segment.size(); ++i)
390 segment.at(i)->hide();
404 void LinkableMapObj::setOrientation()
406 Orientation orientOld=orientation;
410 orientation=UndefinedOrientation;
414 // Set orientation, first look for orientation of parent
415 if (parObj->getOrientation() != UndefinedOrientation )
416 // use the orientation of the parent:
417 orientation=parObj->getOrientation();
420 // calc orientation depending on position rel to parent
421 if (absPos.x() < QPointF(parObj->getChildPos() ).x() )
422 orientation=LeftOfCenter;
424 orientation=RightOfCenter;
426 if (orientOld!=orientation) requestReposition();
429 void LinkableMapObj::updateLink()
432 // childPos of parent
438 // childPos (by calling setDockPos())
439 // parPos (by calling setDockPos())
441 // drawing of the link itself
443 // updateLink is called from move, but called from constructor we don't
444 // have parents yet...
445 if (style==UndefinedStyle) return;
450 bottomlineY=bbox.top() + bbox.height()/2; // draw link to middle (of frame)
453 bottomlineY=bbox.bottom()-1; // draw link to bottom of box
457 double p2x,p2y; // Set P2 Before setting
460 p2x=QPointF( parObj->getChildPos() ).x(); // P1, we have to look at
461 p2y=QPointF( parObj->getChildPos() ).y(); // orientation
464 p2x=QPointF( parObj->getParPos() ).x();
465 p2y=QPointF( parObj->getParPos() ).y();
468 setDockPos(); // Call overloaded method
471 double p1x=parPos.x(); // Link is drawn from P1 to P2
472 double p1y=parPos.y();
474 double vx=p2x - p1x; // V=P2-P1
477 // Draw the horizontal line below heading (from ChildPos to ParPos)
478 //bottomline->prepareGeometryChange();
479 bottomline->setLine (QLine (qRound(childPos.x()),
480 qRound(childPos.y()),
485 if (vx > -0.000001 && vx < 0.000001)
489 // "turning point" for drawing polygonal links
490 QPointF tp (-qRound(sin (a)*thickness_start), qRound(cos (a)*thickness_start));
496 //l->prepareGeometryChange();
497 l->setLine( QLine(qRound (parPos.x()),
503 parabel (pa0, p1x,p1y,p2x,p2y);
504 for (int i=0; i<segment.size(); ++i)
506 //segment.at(i)->prepareGeometryChange();
507 segment.at(i)->setLine(QLineF( pa0.at(i).x(), pa0.at(i).y(),pa0.at(i+1).x(),pa0.at(i+1).y()));
512 pa0<<QPointF (qRound(p2x+tp.x()), qRound(p2y+tp.y()));
513 pa0<<QPointF (qRound(p2x-tp.x()), qRound(p2y-tp.y()));
514 pa0<<QPointF (qRound (parPos.x()), qRound(parPos.y()) );
515 //p->prepareGeometryChange();
516 p->setPolygon(QPolygonF (pa0));
519 parabel (pa1, p1x,p1y,p2x+tp.x(),p2y+tp.y());
520 parabel (pa2, p1x,p1y,p2x-tp.x(),p2y-tp.y());
522 for (int i=0;i<=arcsegs;i++)
523 pa0 << QPointF (pa1.at(i));
524 for (int i=0;i<=arcsegs;i++)
525 pa0 << QPointF (pa2.at(arcsegs-i));
526 //p->prepareGeometryChange();
527 p->setPolygon(QPolygonF (pa0));
534 LinkableMapObj* LinkableMapObj::getChildObj()
539 LinkableMapObj* LinkableMapObj::getParObj()
544 LinkableMapObj* LinkableMapObj::findObjBySelect (QString s)
546 LinkableMapObj *lmo=this;
550 while (!s.isEmpty() )
552 part=s.section(",",0,0);
554 num=part.right(part.length() - 3);
558 return false; // in a subtree there is no center
563 lmo=((BranchObj*)lmo)->getBranchNum (num.toInt());
566 lmo=((BranchObj*)lmo)->getFloatImageNum (num.toUInt());
570 s=s.right(s.length() - part.length() -1 );
577 QPointF LinkableMapObj::getChildPos()
582 QPointF LinkableMapObj::getParPos()
587 void LinkableMapObj::setUseOrientation (const bool &b)
589 if (useOrientation!=b)
596 LinkableMapObj::Orientation LinkableMapObj::getOrientation()
601 int LinkableMapObj::getDepth()
606 void LinkableMapObj::setMapEditor (MapEditor *me)
611 MapEditor* LinkableMapObj::getMapEditor ()
616 QPointF LinkableMapObj::getRandPos()
618 // Choose a random position with given distance to parent:
619 double a=rand()%360 * 2 * M_PI / 360;
620 return QPointF ( (int)( + 150*cos (a)),
621 (int)( + 150*sin (a)));
624 void LinkableMapObj::reposition()
628 void LinkableMapObj::requestReposition()
630 if (!repositionRequest)
632 // Pass on the request to parental objects, if this hasn't
634 repositionRequest=true;
635 if (parObj) parObj->requestReposition();
639 void LinkableMapObj::forceReposition()
641 // Sometimes a reposition has to be done immediatly: For example
642 // if the note editor flag changes, there is no user event in mapeditor
643 // which could collect requests for a reposition.
644 // Then we have to call forceReposition()
645 // But no rule without exception: While loading a map or undoing it,
646 // we want to block expensive repositioning, but just do it once at
647 // the end, thus check first:
649 if (mapEditor->isRepositionBlocked()) return;
651 // Pass on the request to parental objects, if this hasn't been done yet
654 parObj->forceReposition();
659 bool LinkableMapObj::repositionRequested()
661 return repositionRequest;
665 void LinkableMapObj::select()
667 // select and unselect are still needed to
668 // handle hiding of links
670 setVisibility (visible);
674 void LinkableMapObj::unselect()
677 // Maybe we have to hide the link:
678 setVisibility (visible);
681 void LinkableMapObj::parabel (QPolygonF &ya, double p1x, double p1y, double p2x, double p2y)
684 double vx=p2x - p1x; // V=P2-P1
687 double dx; // delta x during calculation of parabel
689 double pnx; // next point
693 if (vx > -0.0001 && vx < 0.0001)
699 ya<<QPointF (p1x,p1y);
700 for (int i=1;i<=arcsegs;i++)
703 pny=m*(pnx-parPos.x())*(pnx-parPos.x())+parPos.y();
704 ya<<QPointF (pnx,pny);
710 QString LinkableMapObj::getLinkAttr ()
712 if (hideLinkUnselected)
713 return attribut ("hideLink","true");
715 return attribut ("hideLink","false");