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\n";
40 void LinkableMapObj::delLink()
49 while (!segment.isEmpty()) delete segment.takeFirst();
62 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 );
85 bottomline=scene->addLine(QLineF(1,1,1,1),pen);
86 bottomline->setZValue(Z_LINK);
89 topPad=botPad=leftPad=rightPad=0;
91 repositionRequest=false;
98 void LinkableMapObj::copy (LinkableMapObj* other)
101 bboxTotal=other->bboxTotal;
102 setLinkStyle(other->style);
103 setLinkColor (other->linkcolor);
104 relPos=other->relPos;
105 treeItem=other->treeItem;
108 void LinkableMapObj::setParObj(LinkableMapObj* o)
113 void LinkableMapObj::setParObjTmp(LinkableMapObj*,QPointF,int) // FIXME-3 make pure virtual
117 void LinkableMapObj::unsetParObjTmp() // FIXME-3 make pure virtual
121 bool LinkableMapObj::hasParObjTmp()
126 void LinkableMapObj::setUseRelPos (const bool &b)
131 void LinkableMapObj::setRelPos()
135 relPos.setX (absPos.x() - parObj->getChildPos().x() );
136 relPos.setY (absPos.y() - parObj->getChildPos().y() );
137 parObj->calcBBoxSize();
141 void LinkableMapObj::setRelPos(const QPointF &p)
146 parObj->calcBBoxSize();
151 QPointF LinkableMapObj::getRelPos()
153 if (!parObj) return QPointF();
157 qreal LinkableMapObj::getTopPad()
162 qreal LinkableMapObj::getLeftPad()
167 qreal LinkableMapObj::getRightPad()
172 LinkableMapObj::Style LinkableMapObj::getDefLinkStyle (TreeItem *parent)
174 VymModel *model=treeItem->getModel();
177 qWarning ("LMO::getDefLinkStyle model=NULL");
178 //return UndefinedStyle;
180 Style ls=model->getMapLinkStyle();
181 int depth=1+parent->depth();
182 if (depth==0) return UndefinedStyle;
206 return UndefinedStyle;
209 void LinkableMapObj::setLinkStyle(Style newstyle)
217 QGraphicsLineItem *cl;
224 l = scene->addLine(QLineF(1,1,1,1),pen);
225 l->setZValue(Z_LINK);
232 for (int i=0;i<arcsegs;i++)
234 cl = scene->addLine(QLineF(i*5,0,i*10,100),pen);
235 cl->setZValue(Z_LINK);
242 pa0.resize (arcsegs+1);
245 p =scene->addPolygon(QPolygonF(),pen,linkcolor);
246 p->setZValue(Z_LINK);
254 p = scene->addPolygon(QPolygonF(),pen,linkcolor);
255 p->setZValue(Z_LINK);
260 pa0.resize (arcsegs*2+2);
261 pa1.resize (arcsegs+1);
262 pa2.resize (arcsegs+1);
270 LinkableMapObj::Style LinkableMapObj::getLinkStyle()
275 void LinkableMapObj::setLinkPos(Position lp)
280 LinkableMapObj::Position LinkableMapObj::getLinkPos()
285 void LinkableMapObj::setLinkColor()
287 // Overloaded in BranchObj and children
288 // here only set default color
289 VymModel *model=treeItem->getModel();
291 setLinkColor (model->getMapDefLinkColor());
294 void LinkableMapObj::setLinkColor(QColor col)
298 bottomline->setPen( pen );
305 for (int i=0; i<segment.size(); ++i)
306 segment.at(i)->setPen( pen);
309 p->setBrush( QBrush(col));
313 p->setBrush( QBrush(col));
321 QColor LinkableMapObj::getLinkColor()
326 void LinkableMapObj::setVisibility (bool v)
328 MapObj::setVisibility (v);
332 void LinkableMapObj::setOrientation()
334 Orientation orientOld=orientation;
338 orientation=UndefinedOrientation;
342 // Set orientation, first look for orientation of parent
343 if (parObj->getOrientation() != UndefinedOrientation )
344 // use the orientation of the parent:
345 orientation=parObj->getOrientation();
348 // calc orientation depending on position rel to parent
349 if (absPos.x() < QPointF(parObj->getChildPos() ).x() )
350 orientation=LeftOfCenter;
352 orientation=RightOfCenter;
354 if (orientOld!=orientation) requestReposition();
357 void LinkableMapObj::updateVisibility()
361 if (((MapItem*)treeItem)->getHideLinkUnselected()
362 && treeItem->getModel()->getSelectedLMO() !=this)
378 for (int i=0; i<segment.size(); ++i)
379 segment.at(i)->show();
382 if (!p) cout << "LMO::updateVis p==0 (PolyLine)\n"; //FIXME-3
386 if (!p) cout << "LMO::updateVis p==0 (PolyParabel) "<<treeItem->getHeading().toStdString()<<endl; //FIXME-3
401 for (int i=0; i<segment.size(); ++i)
402 segment.at(i)->hide();
416 void LinkableMapObj::updateLinkGeometry()
419 // childPos of parent
425 // childPos (by calling setDockPos())
426 // parPos (by calling setDockPos())
428 // drawing of the link itself
430 // updateLinkGeometry is called from move, but called from constructor we don't
431 // have parents yet...
433 //cout <<"LMO::updateLinkGeometry: "<<treeItem->getHeadingStd()<<" "<<parObj<<endl;
436 // If I am a mapcenter, set childPos to middle of MapCenterObj
437 childPos.setX( clickBox.topLeft().x() + clickBox.width()/2 );
438 childPos.setY( clickBox.topLeft().y() + clickBox.height()/2 );
440 // Redraw links to children
441 for (int i=0; i<treeItem->branchCount(); ++i)
442 treeItem->getBranchObjNum(i)->updateLinkGeometry();
446 if (style==UndefinedStyle) return;
451 bottomlineY=bbox.top() + bbox.height()/2; // draw link to middle (of frame)
454 //bottomlineY=bbox.bottom()-1; // draw link to bottom of box
455 bottomlineY=bbox.bottom()-botPad;
459 double p2x,p2y; // Set P2 Before setting
462 p2x=QPointF( parObj->getChildPos() ).x(); // P1, we have to look at
463 p2y=QPointF( parObj->getChildPos() ).y(); // orientation
466 p2x=QPointF( parObj->getParPos() ).x();
467 p2y=QPointF( parObj->getParPos() ).y();
470 setDockPos(); // Call overloaded method
473 double p1x=parPos.x(); // Link is drawn from P1 to P2
474 double p1y=parPos.y();
476 double vx=p2x - p1x; // V=P2-P1
479 // Draw the horizontal line below heading (from ChildPos to ParPos)
480 bottomline->setLine (QLine (qRound(childPos.x()),
481 qRound(childPos.y()),
486 if (vx > -0.000001 && vx < 0.000001)
490 // "turning point" for drawing polygonal links
491 QPointF tp (-qRound(sin (a)*thickness_start), qRound(cos (a)*thickness_start));
497 l->setLine( QLine(qRound (parPos.x()),
503 parabel (pa0, p1x,p1y,p2x,p2y);
504 for (int i=0; i<segment.size(); ++i)
505 segment.at(i)->setLine(QLineF( pa0.at(i).x(), pa0.at(i).y(),pa0.at(i+1).x(),pa0.at(i+1).y()));
509 pa0<<QPointF (qRound(p2x+tp.x()), qRound(p2y+tp.y()));
510 pa0<<QPointF (qRound(p2x-tp.x()), qRound(p2y-tp.y()));
511 pa0<<QPointF (qRound (parPos.x()), qRound(parPos.y()) );
512 p->setPolygon(QPolygonF (pa0));
515 parabel (pa1, p1x,p1y,p2x+tp.x(),p2y+tp.y());
516 parabel (pa2, p1x,p1y,p2x-tp.x(),p2y-tp.y());
518 for (int i=0;i<=arcsegs;i++)
519 pa0 << QPointF (pa1.at(i));
520 for (int i=0;i<=arcsegs;i++)
521 pa0 << QPointF (pa2.at(arcsegs-i));
522 p->setPolygon(QPolygonF (pa0));
529 LinkableMapObj* LinkableMapObj::getParObj()
534 QPointF LinkableMapObj::getChildPos()
539 QPointF LinkableMapObj::getParPos()
544 LinkableMapObj::Orientation LinkableMapObj::getOrientation()
549 QPointF LinkableMapObj::getRandPos()
551 // Choose a random position with given distance to parent:
552 double a=rand()%360 * 2 * M_PI / 360;
553 return QPointF ( (int)( + 150*cos (a)),
554 (int)( + 150*sin (a)));
557 void LinkableMapObj::reposition()
561 void LinkableMapObj::requestReposition() //FIXME-3 needed?
563 if (!repositionRequest)
565 // Pass on the request to parental objects, if this hasn't
567 repositionRequest=true;
568 if (parObj) parObj->requestReposition();
572 void LinkableMapObj::forceReposition()
574 // Sometimes a reposition has to be done immediatly: For example
575 // if the note editor flag changes, there is no user event in mapeditor
576 // which could collect requests for a reposition.
577 // Then we have to call forceReposition()
578 // But no rule without exception: While loading a map or undoing it,
579 // we want to block expensive repositioning, but just do it once at
580 // the end, thus check first:
582 VymModel *model=treeItem->getModel();
583 if (model->isRepositionBlocked()) return;
585 // Pass on the request to parent objects, if this hasn't been done yet
587 parObj->forceReposition();
592 bool LinkableMapObj::repositionRequested()
594 return repositionRequest;
597 void LinkableMapObj::parabel (QPolygonF &ya, double p1x, double p1y, double p2x, double p2y)
600 double vx=p2x - p1x; // V=P2-P1
603 double dx; // delta x during calculation of parabel
605 double pnx; // next point
609 if (vx > -0.0001 && vx < 0.0001)
615 ya<<QPointF (p1x,p1y);
616 for (int i=1;i<=arcsegs;i++)
619 pny=m*(pnx-parPos.x())*(pnx-parPos.x())+parPos.y();
620 ya<<QPointF (pnx,pny);