4 #include "linkablemapobj.h"
10 /////////////////////////////////////////////////////////////////
12 /////////////////////////////////////////////////////////////////
14 LinkableMapObj::LinkableMapObj():MapObj()
16 // cout << "Const LinkableMapObj ()\n";
20 LinkableMapObj::LinkableMapObj(QGraphicsScene* s) :MapObj(s)
22 // cout << "Const LinkableMapObj (s)\n";
26 LinkableMapObj::LinkableMapObj (LinkableMapObj* lmo) : MapObj (lmo->scene)
31 LinkableMapObj::~LinkableMapObj()
37 void LinkableMapObj::delLink()
45 while (!segment.isEmpty()) delete segment.takeFirst();
58 void LinkableMapObj::init ()
68 childPos=QPointF(0,0);
71 orientation=UndefinedOrientation;
78 // TODO instead of linkcolor pen.color() could be used all around
80 pen.setColor (linkcolor);
81 pen.setCapStyle ( Qt::RoundCap );
82 bottomline=scene->addLine(QLineF(1,1,1,1),pen);
83 bottomline->setZValue(Z_LINK);
86 // Prepare showing the selection of a MapObj
89 hideLinkUnselected=false;
91 topPad=botPad=leftPad=rightPad=0;
93 repositionRequest=false;
104 void LinkableMapObj::copy (LinkableMapObj* other)
108 bboxTotal=other->bboxTotal;
109 setLinkStyle(other->style);
110 setLinkColor (other->linkcolor);
111 relPos=other->relPos;
112 useOrientation=other->useOrientation;
116 void LinkableMapObj::setModel (VymModel *vm)
121 VymModel* LinkableMapObj::getModel()
126 void LinkableMapObj::setChildObj(LinkableMapObj* o)
131 void LinkableMapObj::setParObj(LinkableMapObj* o)
134 mapEditor=parObj->getMapEditor();
135 model=parObj->getModel();
138 void LinkableMapObj::setParObjTmp(LinkableMapObj*,QPointF,int) // FIXME make pure virtual
142 void LinkableMapObj::unsetParObjTmp() // FIXME make pure virtual
146 bool LinkableMapObj::hasParObjTmp()
148 if (parObjTmpBuf) return true;
152 void LinkableMapObj::setUseRelPos (const bool &b)
157 void LinkableMapObj::setRelPos()
161 relPos.setX (absPos.x() - parObj->getChildPos().x() );
162 relPos.setY (absPos.y() - parObj->getChildPos().y() );
163 parObj->calcBBoxSize();
167 void LinkableMapObj::setRelPos(const QPointF &p)
172 parObj->calcBBoxSize();
177 QPointF LinkableMapObj::getRelPos()
179 if (!parObj) return QPointF();
183 qreal LinkableMapObj::getTopPad()
188 qreal LinkableMapObj::getLeftPad()
193 qreal LinkableMapObj::getRightPad()
198 LinkableMapObj::Style LinkableMapObj::getDefLinkStyle ()
202 qWarning ("LMO::getDefLinkStyle model=NULL");
203 return UndefinedStyle;
205 Style ls=model->getMapLinkStyle();
229 return UndefinedStyle;
232 void LinkableMapObj::setLinkStyle(Style newstyle)
234 //if (newstyle=style) return;
239 if (childObj!=NULL && parObj != NULL)
241 QGraphicsLineItem *cl;
248 l = scene->addLine(QLineF(1,1,1,1),pen);
249 l->setZValue(Z_LINK);
256 for (int i=0;i<arcsegs;i++)
258 cl = scene->addLine(QLineF(i*5,0,i*10,100),pen);
259 cl->setZValue(Z_LINK);
266 pa0.resize (arcsegs+1);
269 p =scene->addPolygon(QPolygonF(),pen,linkcolor);
270 p->setZValue(Z_LINK);
278 p = scene->addPolygon(QPolygonF(),pen,linkcolor);
279 p->setZValue(Z_LINK);
284 pa0.resize (arcsegs*2+2);
285 pa1.resize (arcsegs+1);
286 pa2.resize (arcsegs+1);
294 LinkableMapObj::Style LinkableMapObj::getLinkStyle()
299 void LinkableMapObj::setHideLinkUnselected(bool b)
301 hideLinkUnselected=b;
302 setVisibility (visible);
306 bool LinkableMapObj::getHideLinkUnselected()
308 return hideLinkUnselected;
311 void LinkableMapObj::setLinkPos(Position lp)
316 LinkableMapObj::Position LinkableMapObj::getLinkPos()
321 void LinkableMapObj::setID (const QString &s)
326 QString LinkableMapObj::getID()
331 void LinkableMapObj::setLinkColor()
333 // Overloaded in BranchObj and children
334 // here only set default color
336 setLinkColor (model->getMapDefLinkColor());
339 void LinkableMapObj::setLinkColor(QColor col)
343 bottomline->setPen( pen );
350 for (int i=0; i<segment.size(); ++i)
351 segment.at(i)->setPen( pen);
354 p->setBrush( QBrush(col));
358 p->setBrush( QBrush(col));
366 QColor LinkableMapObj::getLinkColor()
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();
410 for (int i=0; i<segment.size(); ++i)
411 segment.at(i)->hide();
425 void LinkableMapObj::setOrientation()
427 Orientation orientOld=orientation;
431 orientation=UndefinedOrientation;
435 // Set orientation, first look for orientation of parent
436 if (parObj->getOrientation() != UndefinedOrientation )
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=LeftOfCenter;
445 orientation=RightOfCenter;
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==UndefinedStyle) return;
471 bottomlineY=bbox.top() + bbox.height()/2; // draw link to middle (of frame)
474 bottomlineY=bbox.bottom()-1; // draw link to bottom of box
478 double p2x,p2y; // Set P2 Before setting
481 p2x=QPointF( parObj->getChildPos() ).x(); // P1, we have to look at
482 p2y=QPointF( parObj->getChildPos() ).y(); // orientation
485 p2x=QPointF( parObj->getParPos() ).x();
486 p2y=QPointF( parObj->getParPos() ).y();
489 setDockPos(); // Call overloaded method
492 double p1x=parPos.x(); // Link is drawn from P1 to P2
493 double p1y=parPos.y();
495 double vx=p2x - p1x; // V=P2-P1
498 // Draw the horizontal line below heading (from ChildPos to ParPos)
499 //bottomline->prepareGeometryChange();
500 bottomline->setLine (QLine (qRound(childPos.x()),
501 qRound(childPos.y()),
506 if (vx > -0.000001 && vx < 0.000001)
510 // "turning point" for drawing polygonal links
511 QPointF tp (-qRound(sin (a)*thickness_start), qRound(cos (a)*thickness_start));
517 //l->prepareGeometryChange();
518 l->setLine( QLine(qRound (parPos.x()),
524 parabel (pa0, p1x,p1y,p2x,p2y);
525 for (int i=0; i<segment.size(); ++i)
527 //segment.at(i)->prepareGeometryChange();
528 segment.at(i)->setLine(QLineF( pa0.at(i).x(), pa0.at(i).y(),pa0.at(i+1).x(),pa0.at(i+1).y()));
533 pa0<<QPointF (qRound(p2x+tp.x()), qRound(p2y+tp.y()));
534 pa0<<QPointF (qRound(p2x-tp.x()), qRound(p2y-tp.y()));
535 pa0<<QPointF (qRound (parPos.x()), qRound(parPos.y()) );
536 //p->prepareGeometryChange();
537 p->setPolygon(QPolygonF (pa0));
540 parabel (pa1, p1x,p1y,p2x+tp.x(),p2y+tp.y());
541 parabel (pa2, p1x,p1y,p2x-tp.x(),p2y-tp.y());
543 for (int i=0;i<=arcsegs;i++)
544 pa0 << QPointF (pa1.at(i));
545 for (int i=0;i<=arcsegs;i++)
546 pa0 << QPointF (pa2.at(arcsegs-i));
547 //p->prepareGeometryChange();
548 p->setPolygon(QPolygonF (pa0));
555 LinkableMapObj* LinkableMapObj::getChildObj()
560 LinkableMapObj* LinkableMapObj::getParObj()
565 LinkableMapObj* LinkableMapObj::findObjBySelect (QString s)
567 LinkableMapObj *lmo=this;
571 while (!s.isEmpty() )
573 part=s.section(",",0,0);
575 num=part.right(part.length() - 3);
579 return false; // in a subtree there is no center
584 lmo=((BranchObj*)lmo)->getBranchNum (num.toInt());
587 lmo=((BranchObj*)lmo)->getFloatImageNum (num.toUInt());
591 s=s.right(s.length() - part.length() -1 );
598 QPointF LinkableMapObj::getChildPos()
603 QPointF LinkableMapObj::getParPos()
608 void LinkableMapObj::setUseOrientation (const bool &b)
610 if (useOrientation!=b)
617 LinkableMapObj::Orientation LinkableMapObj::getOrientation()
622 int LinkableMapObj::getDepth()
627 void LinkableMapObj::setMapEditor (MapEditor *me)
630 model=mapEditor->getModel();
633 MapEditor* LinkableMapObj::getMapEditor ()
638 QPointF LinkableMapObj::getRandPos()
640 // Choose a random position with given distance to parent:
641 double a=rand()%360 * 2 * M_PI / 360;
642 return QPointF ( (int)( + 150*cos (a)),
643 (int)( + 150*sin (a)));
646 void LinkableMapObj::reposition()
650 void LinkableMapObj::requestReposition()
652 if (!repositionRequest)
654 // Pass on the request to parental objects, if this hasn't
656 repositionRequest=true;
657 if (parObj) parObj->requestReposition();
661 void LinkableMapObj::forceReposition()
663 // Sometimes a reposition has to be done immediatly: For example
664 // if the note editor flag changes, there is no user event in mapeditor
665 // which could collect requests for a reposition.
666 // Then we have to call forceReposition()
667 // But no rule without exception: While loading a map or undoing it,
668 // we want to block expensive repositioning, but just do it once at
669 // the end, thus check first:
671 if (model->isRepositionBlocked()) return;
673 // Pass on the request to parent objects, if this hasn't been done yet
675 parObj->forceReposition();
680 bool LinkableMapObj::repositionRequested()
682 return repositionRequest;
686 void LinkableMapObj::select()
688 // select and unselect are still needed to
689 // handle hiding of links
691 setVisibility (visible);
695 void LinkableMapObj::unselect()
698 // Maybe we have to hide the link:
699 setVisibility (visible);
702 void LinkableMapObj::parabel (QPolygonF &ya, double p1x, double p1y, double p2x, double p2y)
705 double vx=p2x - p1x; // V=P2-P1
708 double dx; // delta x during calculation of parabel
710 double pnx; // next point
714 if (vx > -0.0001 && vx < 0.0001)
720 ya<<QPointF (p1x,p1y);
721 for (int i=1;i<=arcsegs;i++)
724 pny=m*(pnx-parPos.x())*(pnx-parPos.x())+parPos.y();
725 ya<<QPointF (pnx,pny);
731 QString LinkableMapObj::getLinkAttr ()
733 if (hideLinkUnselected)
734 return attribut ("hideLink","true");
736 return attribut ("hideLink","false");