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);
72 orientation=UndefinedOrientation;
79 // TODO instead of linkcolor pen.color() could be used all around
81 pen.setColor (linkcolor);
82 pen.setCapStyle ( Qt::RoundCap );
83 bottomline=scene->addLine(QLineF(1,1,1,1),pen);
84 bottomline->setZValue(Z_LINK);
87 // Prepare showing the selection of a MapObj
90 hideLinkUnselected=false;
92 topPad=botPad=leftPad=rightPad=0;
94 repositionRequest=false;
104 // Crossreference to treemodel
108 void LinkableMapObj::copy (LinkableMapObj* other)
112 bboxTotal=other->bboxTotal;
113 setLinkStyle(other->style);
114 setLinkColor (other->linkcolor);
115 relPos=other->relPos;
116 useOrientation=other->useOrientation;
118 treeItem=other->treeItem;
121 void LinkableMapObj::setTreeItem (TreeItem *ti)
126 TreeItem* LinkableMapObj::getTreeItem ()
131 void LinkableMapObj::setModel (VymModel *vm)
136 VymModel* LinkableMapObj::getModel()
141 void LinkableMapObj::setChildObj(LinkableMapObj* o)
146 void LinkableMapObj::setParObj(LinkableMapObj* o)
149 mapEditor=parObj->getMapEditor();
150 model=parObj->getModel();
153 void LinkableMapObj::setParObjTmp(LinkableMapObj*,QPointF,int) // FIXME make pure virtual
157 void LinkableMapObj::unsetParObjTmp() // FIXME make pure virtual
161 bool LinkableMapObj::hasParObjTmp()
163 if (parObjTmpBuf) return true;
167 void LinkableMapObj::setUseRelPos (const bool &b)
172 void LinkableMapObj::setRelPos()
176 relPos.setX (absPos.x() - parObj->getChildPos().x() );
177 relPos.setY (absPos.y() - parObj->getChildPos().y() );
178 parObj->calcBBoxSize();
182 void LinkableMapObj::setRelPos(const QPointF &p)
187 parObj->calcBBoxSize();
192 QPointF LinkableMapObj::getRelPos()
194 if (!parObj) return QPointF();
198 qreal LinkableMapObj::getTopPad()
203 qreal LinkableMapObj::getLeftPad()
208 qreal LinkableMapObj::getRightPad()
213 LinkableMapObj::Style LinkableMapObj::getDefLinkStyle ()
217 qWarning ("LMO::getDefLinkStyle model=NULL");
218 //return UndefinedStyle;
220 Style ls=model->getMapLinkStyle();
221 if (depth==0) return UndefinedStyle;
245 return UndefinedStyle;
248 void LinkableMapObj::setLinkStyle(Style newstyle)
250 //if (newstyle=style) return; FIXME
255 if (childObj!=NULL && parObj != NULL)
257 QGraphicsLineItem *cl;
264 l = scene->addLine(QLineF(1,1,1,1),pen);
265 l->setZValue(Z_LINK);
272 for (int i=0;i<arcsegs;i++)
274 cl = scene->addLine(QLineF(i*5,0,i*10,100),pen);
275 cl->setZValue(Z_LINK);
282 pa0.resize (arcsegs+1);
285 p =scene->addPolygon(QPolygonF(),pen,linkcolor);
286 p->setZValue(Z_LINK);
294 p = scene->addPolygon(QPolygonF(),pen,linkcolor);
295 p->setZValue(Z_LINK);
300 pa0.resize (arcsegs*2+2);
301 pa1.resize (arcsegs+1);
302 pa2.resize (arcsegs+1);
310 LinkableMapObj::Style LinkableMapObj::getLinkStyle()
315 void LinkableMapObj::setHideLinkUnselected(bool b)
317 hideLinkUnselected=b;
318 setVisibility (visible);
322 bool LinkableMapObj::getHideLinkUnselected()
324 return hideLinkUnselected;
327 void LinkableMapObj::setLinkPos(Position lp)
332 LinkableMapObj::Position LinkableMapObj::getLinkPos()
337 void LinkableMapObj::setID (const QString &s)
342 QString LinkableMapObj::getID()
347 void LinkableMapObj::setLinkColor()
349 // Overloaded in BranchObj and children
350 // here only set default color
352 setLinkColor (model->getMapDefLinkColor());
355 void LinkableMapObj::setLinkColor(QColor col)
359 bottomline->setPen( pen );
366 for (int i=0; i<segment.size(); ++i)
367 segment.at(i)->setPen( pen);
370 p->setBrush( QBrush(col));
374 p->setBrush( QBrush(col));
382 QColor LinkableMapObj::getLinkColor()
387 void LinkableMapObj::setVisibility (bool v)
389 MapObj::setVisibility (v);
392 // We can hide the link, while object is not selected
393 if (hideLinkUnselected && !selected)
405 for (int i=0; i<segment.size(); ++i)
406 segment.at(i)->show();
409 if (!p) cout << "LMO::setVis p==0 (PolyLine)\n"; //FIXME
413 if (!p) cout << "LMO::setVis p==0 (PolyParabel) "<<((BranchObj*)this)->getHeading().toStdString()<<endl; //FIXME
428 for (int i=0; i<segment.size(); ++i)
429 segment.at(i)->hide();
443 void LinkableMapObj::setOrientation()
445 Orientation orientOld=orientation;
449 orientation=UndefinedOrientation;
453 // Set orientation, first look for orientation of parent
454 if (parObj->getOrientation() != UndefinedOrientation )
455 // use the orientation of the parent:
456 orientation=parObj->getOrientation();
459 // calc orientation depending on position rel to parent
460 if (absPos.x() < QPointF(parObj->getChildPos() ).x() )
461 orientation=LeftOfCenter;
463 orientation=RightOfCenter;
465 if (orientOld!=orientation) requestReposition();
468 void LinkableMapObj::updateLink()
471 // childPos of parent
477 // childPos (by calling setDockPos())
478 // parPos (by calling setDockPos())
480 // drawing of the link itself
482 // updateLink is called from move, but called from constructor we don't
483 // have parents yet...
484 if (style==UndefinedStyle) return;
489 bottomlineY=bbox.top() + bbox.height()/2; // draw link to middle (of frame)
492 bottomlineY=bbox.bottom()-1; // draw link to bottom of box
496 double p2x,p2y; // Set P2 Before setting
499 p2x=QPointF( parObj->getChildPos() ).x(); // P1, we have to look at
500 p2y=QPointF( parObj->getChildPos() ).y(); // orientation
503 p2x=QPointF( parObj->getParPos() ).x();
504 p2y=QPointF( parObj->getParPos() ).y();
507 setDockPos(); // Call overloaded method
510 double p1x=parPos.x(); // Link is drawn from P1 to P2
511 double p1y=parPos.y();
513 double vx=p2x - p1x; // V=P2-P1
516 // Draw the horizontal line below heading (from ChildPos to ParPos)
517 //bottomline->prepareGeometryChange();
518 bottomline->setLine (QLine (qRound(childPos.x()),
519 qRound(childPos.y()),
524 if (vx > -0.000001 && vx < 0.000001)
528 // "turning point" for drawing polygonal links
529 QPointF tp (-qRound(sin (a)*thickness_start), qRound(cos (a)*thickness_start));
535 //l->prepareGeometryChange();
536 l->setLine( QLine(qRound (parPos.x()),
542 parabel (pa0, p1x,p1y,p2x,p2y);
543 for (int i=0; i<segment.size(); ++i)
545 //segment.at(i)->prepareGeometryChange();
546 segment.at(i)->setLine(QLineF( pa0.at(i).x(), pa0.at(i).y(),pa0.at(i+1).x(),pa0.at(i+1).y()));
551 pa0<<QPointF (qRound(p2x+tp.x()), qRound(p2y+tp.y()));
552 pa0<<QPointF (qRound(p2x-tp.x()), qRound(p2y-tp.y()));
553 pa0<<QPointF (qRound (parPos.x()), qRound(parPos.y()) );
554 //p->prepareGeometryChange();
555 p->setPolygon(QPolygonF (pa0));
558 parabel (pa1, p1x,p1y,p2x+tp.x(),p2y+tp.y());
559 parabel (pa2, p1x,p1y,p2x-tp.x(),p2y-tp.y());
561 for (int i=0;i<=arcsegs;i++)
562 pa0 << QPointF (pa1.at(i));
563 for (int i=0;i<=arcsegs;i++)
564 pa0 << QPointF (pa2.at(arcsegs-i));
565 //p->prepareGeometryChange();
566 p->setPolygon(QPolygonF (pa0));
573 LinkableMapObj* LinkableMapObj::getChildObj()
578 LinkableMapObj* LinkableMapObj::getParObj()
583 LinkableMapObj* LinkableMapObj::findObjBySelect (QString s)
585 LinkableMapObj *lmo=this;
589 while (!s.isEmpty() )
591 part=s.section(",",0,0);
593 num=part.right(part.length() - 3);
597 return false; // in a subtree there is no center
602 lmo=((BranchObj*)lmo)->getBranchNum (num.toInt());
605 lmo=((BranchObj*)lmo)->getFloatImageNum (num.toUInt());
609 s=s.right(s.length() - part.length() -1 );
616 QPointF LinkableMapObj::getChildPos()
621 QPointF LinkableMapObj::getParPos()
626 void LinkableMapObj::setUseOrientation (const bool &b)
628 if (useOrientation!=b)
635 LinkableMapObj::Orientation LinkableMapObj::getOrientation()
640 int LinkableMapObj::getDepth()
645 void LinkableMapObj::setMapEditor (MapEditor *me)
648 model=mapEditor->getModel();
651 MapEditor* LinkableMapObj::getMapEditor ()
656 QPointF LinkableMapObj::getRandPos()
658 // Choose a random position with given distance to parent:
659 double a=rand()%360 * 2 * M_PI / 360;
660 return QPointF ( (int)( + 150*cos (a)),
661 (int)( + 150*sin (a)));
664 void LinkableMapObj::reposition()
668 void LinkableMapObj::requestReposition()
670 if (!repositionRequest)
672 // Pass on the request to parental objects, if this hasn't
674 repositionRequest=true;
675 if (parObj) parObj->requestReposition();
679 void LinkableMapObj::forceReposition()
681 // Sometimes a reposition has to be done immediatly: For example
682 // if the note editor flag changes, there is no user event in mapeditor
683 // which could collect requests for a reposition.
684 // Then we have to call forceReposition()
685 // But no rule without exception: While loading a map or undoing it,
686 // we want to block expensive repositioning, but just do it once at
687 // the end, thus check first:
689 if (model->isRepositionBlocked()) return;
691 // Pass on the request to parent objects, if this hasn't been done yet
693 parObj->forceReposition();
698 bool LinkableMapObj::repositionRequested()
700 return repositionRequest;
704 void LinkableMapObj::select()
706 // select and unselect are still needed to
707 // handle hiding of links
709 setVisibility (visible);
713 void LinkableMapObj::unselect()
716 // Maybe we have to hide the link:
717 setVisibility (visible);
720 void LinkableMapObj::parabel (QPolygonF &ya, double p1x, double p1y, double p2x, double p2y)
723 double vx=p2x - p1x; // V=P2-P1
726 double dx; // delta x during calculation of parabel
728 double pnx; // next point
732 if (vx > -0.0001 && vx < 0.0001)
738 ya<<QPointF (p1x,p1y);
739 for (int i=1;i<=arcsegs;i++)
742 pny=m*(pnx-parPos.x())*(pnx-parPos.x())+parPos.y();
743 ya<<QPointF (pnx,pny);
749 QString LinkableMapObj::getLinkAttr ()
751 if (hideLinkUnselected)
752 return attribut ("hideLink","true");
754 return attribut ("hideLink","false");