4 #include "linkablemapobj.h"
10 /////////////////////////////////////////////////////////////////
12 /////////////////////////////////////////////////////////////////
15 LinkableMapObj::LinkableMapObj():MapObj()
17 // cout << "Const LinkableMapObj ()\n";
22 LinkableMapObj::LinkableMapObj(QGraphicsScene* s, TreeItem *ti) :MapObj(s,ti)
24 // cout << "Const LinkableMapObj s="<<s<<" ti="<<ti<<" treeItem="<<treeItem<<endl;
28 LinkableMapObj::LinkableMapObj (LinkableMapObj* lmo) : MapObj (lmo->scene)
33 LinkableMapObj::~LinkableMapObj()
35 //cout<< "Destructor LMO style="<<style<<" l="<<l<<" p="<<p<<" segment="<<segment.count()<<endl;
44 void LinkableMapObj::delLink()
53 while (!segment.isEmpty()) delete segment.takeFirst();
66 void LinkableMapObj::init ()
72 childPos=QPointF(0,0);
76 orientation=UndefinedOrientation;
83 // TODO instead of linkcolor pen.color() could be used all around
85 pen.setColor (linkcolor);
86 pen.setCapStyle ( Qt::RoundCap );
89 bottomline=scene->addLine(QLineF(1,1,1,1),pen);
90 bottomline->setZValue(Z_LINK);
93 topPad=botPad=leftPad=rightPad=0;
95 repositionRequest=false;
102 void LinkableMapObj::copy (LinkableMapObj* other)
105 bboxTotal=other->bboxTotal;
106 setLinkStyle(other->style);
107 setLinkColor (other->linkcolor);
108 relPos=other->relPos;
109 treeItem=other->treeItem;
112 void LinkableMapObj::setParObj(LinkableMapObj* o)
117 void LinkableMapObj::setParObjTmp(LinkableMapObj*,QPointF,int) // FIXME-3 make pure virtual
121 void LinkableMapObj::unsetParObjTmp() // FIXME-3 make pure virtual
125 bool LinkableMapObj::hasParObjTmp()
130 void LinkableMapObj::setUseRelPos (const bool &b)
135 bool LinkableMapObj::getUseRelPos ()
140 void LinkableMapObj::setRelPos()
144 setRelPos (absPos - parObj->getChildPos() );
146 qWarning ("LMO::setRelPos() parObj==0");
149 void LinkableMapObj::setRelPos(const QPointF &p)
155 parObj->calcBBoxSize();
158 qWarning ("LMO::setRelPos (p) parObj==0");
161 QPointF LinkableMapObj::getRelPos()
165 qWarning("LMO::getRelPos parObj==0");
171 qreal LinkableMapObj::getTopPad()
176 qreal LinkableMapObj::getLeftPad()
181 qreal LinkableMapObj::getRightPad()
186 LinkableMapObj::Style LinkableMapObj::getDefLinkStyle (TreeItem *parent)
188 VymModel *model=treeItem->getModel();
191 qWarning ("LMO::getDefLinkStyle model=NULL");
192 //return UndefinedStyle;
194 Style ls=model->getMapLinkStyle();
195 int depth=1+parent->depth();
196 if (depth==0) return UndefinedStyle;
220 return UndefinedStyle;
223 void LinkableMapObj::setLinkStyle(Style newstyle)
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);
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 LinkableMapObj::Style LinkableMapObj::getLinkStyle()
289 void LinkableMapObj::setLinkPos(Position lp)
294 LinkableMapObj::Position LinkableMapObj::getLinkPos()
299 void LinkableMapObj::setLinkColor()
301 // Overloaded in BranchObj and children
302 // here only set default color
303 VymModel *model=treeItem->getModel();
305 setLinkColor (model->getMapDefLinkColor());
308 void LinkableMapObj::setLinkColor(QColor col)
312 bottomline->setPen( pen );
319 for (int i=0; i<segment.size(); ++i)
320 segment.at(i)->setPen( pen);
323 p->setBrush( QBrush(col));
327 p->setBrush( QBrush(col));
335 QColor LinkableMapObj::getLinkColor()
340 void LinkableMapObj::setVisibility (bool v)
342 MapObj::setVisibility (v);
346 void LinkableMapObj::setOrientation()
348 Orientation orientOld=orientation;
352 orientation=UndefinedOrientation;
356 // Set orientation, first look for orientation of parent
357 if (parObj->getOrientation() != UndefinedOrientation )
358 // use the orientation of the parent:
359 orientation=parObj->getOrientation();
362 // calc orientation depending on position rel to parent
363 if (absPos.x() < QPointF(parObj->getChildPos() ).x() )
364 orientation=LeftOfCenter;
366 orientation=RightOfCenter;
368 if (orientOld!=orientation) requestReposition();
371 void LinkableMapObj::updateVisibility()
375 if (((MapItem*)treeItem)->getHideLinkUnselected()
376 && treeItem->getModel()->getSelectedLMO() !=this)
392 for (int i=0; i<segment.size(); ++i)
393 segment.at(i)->show();
396 if (!p) cout << "LMO::updateVis p==0 (PolyLine)\n"; //FIXME-3
400 if (!p) cout << "LMO::updateVis p==0 (PolyParabel) "<<treeItem->getHeading().toStdString()<<endl; //FIXME-3
415 for (int i=0; i<segment.size(); ++i)
416 segment.at(i)->hide();
430 void LinkableMapObj::updateLinkGeometry()
433 // childPos of parent
439 // childPos (by calling setDockPos())
440 // parPos (by calling setDockPos())
442 // drawing of the link itself
444 // updateLinkGeometry is called from move, but called from constructor we don't
445 // have parents yet...
447 //cout <<"LMO::updateLinkGeometry: "<<treeItem->getHeadingStd()<<" "<<parObj<<endl;
450 // If I am a mapcenter, set childPos to middle of MapCenterObj
451 childPos.setX( clickBox.topLeft().x() + clickBox.width()/2 );
452 childPos.setY( clickBox.topLeft().y() + clickBox.height()/2 );
454 // Redraw links to children
455 for (int i=0; i<treeItem->branchCount(); ++i)
456 treeItem->getBranchObjNum(i)->updateLinkGeometry();
460 if (style==UndefinedStyle) return;
465 bottomlineY=bbox.top() + bbox.height()/2; // draw link to middle (of frame)
468 //bottomlineY=bbox.bottom()-1; // draw link to bottom of box
469 bottomlineY=bbox.bottom()-botPad;
473 double p2x,p2y; // Set P2 Before setting
476 p2x=QPointF( parObj->getChildPos() ).x(); // P1, we have to look at
477 p2y=QPointF( parObj->getChildPos() ).y(); // orientation
480 p2x=QPointF( parObj->getParPos() ).x();
481 p2y=QPointF( parObj->getParPos() ).y();
484 setDockPos(); // Call overloaded method
487 double p1x=parPos.x(); // Link is drawn from P1 to P2
488 double p1y=parPos.y();
490 double vx=p2x - p1x; // V=P2-P1
493 // Draw the horizontal line below heading (from ChildPos to ParPos)
494 bottomline->setLine (QLine (qRound(childPos.x()),
495 qRound(childPos.y()),
500 if (vx > -0.000001 && vx < 0.000001)
504 // "turning point" for drawing polygonal links
505 QPointF tp (-qRound(sin (a)*thickness_start), qRound(cos (a)*thickness_start));
511 l->setLine( QLine(qRound (parPos.x()),
517 parabel (pa0, p1x,p1y,p2x,p2y);
518 for (int i=0; i<segment.size(); ++i)
519 segment.at(i)->setLine(QLineF( pa0.at(i).x(), pa0.at(i).y(),pa0.at(i+1).x(),pa0.at(i+1).y()));
523 pa0<<QPointF (qRound(p2x+tp.x()), qRound(p2y+tp.y()));
524 pa0<<QPointF (qRound(p2x-tp.x()), qRound(p2y-tp.y()));
525 pa0<<QPointF (qRound (parPos.x()), qRound(parPos.y()) );
526 p->setPolygon(QPolygonF (pa0));
529 parabel (pa1, p1x,p1y,p2x+tp.x(),p2y+tp.y());
530 parabel (pa2, p1x,p1y,p2x-tp.x(),p2y-tp.y());
532 for (int i=0;i<=arcsegs;i++)
533 pa0 << QPointF (pa1.at(i));
534 for (int i=0;i<=arcsegs;i++)
535 pa0 << QPointF (pa2.at(arcsegs-i));
536 p->setPolygon(QPolygonF (pa0));
543 LinkableMapObj* LinkableMapObj::getParObj()
548 QPointF LinkableMapObj::getChildPos()
553 QPointF LinkableMapObj::getParPos()
558 LinkableMapObj::Orientation LinkableMapObj::getOrientation()
563 QPointF LinkableMapObj::getRandPos()
565 // Choose a random position with given distance to parent:
566 double a=rand()%360 * 2 * M_PI / 360;
567 return QPointF ( (int)( + 150*cos (a)),
568 (int)( + 150*sin (a)));
571 void LinkableMapObj::reposition()
575 void LinkableMapObj::requestReposition() //FIXME-3 needed?
577 if (!repositionRequest)
579 // Pass on the request to parental objects, if this hasn't
581 repositionRequest=true;
582 if (parObj) parObj->requestReposition();
586 void LinkableMapObj::forceReposition()
588 // Sometimes a reposition has to be done immediatly: For example
589 // if the note editor flag changes, there is no user event in mapeditor
590 // which could collect requests for a reposition.
591 // Then we have to call forceReposition()
592 // But no rule without exception: While loading a map or undoing it,
593 // we want to block expensive repositioning, but just do it once at
594 // the end, thus check first:
596 VymModel *model=treeItem->getModel();
597 if (model->isRepositionBlocked()) return;
599 // Pass on the request to parent objects, if this hasn't been done yet
601 parObj->forceReposition();
606 bool LinkableMapObj::repositionRequested()
608 return repositionRequest;
611 void LinkableMapObj::parabel (QPolygonF &ya, double p1x, double p1y, double p2x, double p2y)
614 double vx=p2x - p1x; // V=P2-P1
617 double dx; // delta x during calculation of parabel
619 double pnx; // next point
623 if (vx > -0.0001 && vx < 0.0001)
629 ya<<QPointF (p1x,p1y);
630 for (int i=1;i<=arcsegs;i++)
633 pny=m*(pnx-parPos.x())*(pnx-parPos.x())+parPos.y();
634 ya<<QPointF (pnx,pny);