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()
48 while (!segment.isEmpty()) delete segment.takeFirst();
61 void LinkableMapObj::init ()
66 childPos=QPointF(0,0);
70 orientation=UndefinedOrientation;
77 // TODO instead of linkcolor pen.color() could be used all around
79 pen.setColor (linkcolor);
80 pen.setCapStyle ( Qt::RoundCap );
81 bottomline=scene->addLine(QLineF(1,1,1,1),pen);
82 bottomline->setZValue(Z_LINK);
85 topPad=botPad=leftPad=rightPad=0;
87 repositionRequest=false;
94 void LinkableMapObj::copy (LinkableMapObj* other)
97 bboxTotal=other->bboxTotal;
98 setLinkStyle(other->style);
99 setLinkColor (other->linkcolor);
100 relPos=other->relPos;
101 treeItem=other->treeItem;
104 void LinkableMapObj::setParObj(LinkableMapObj* o)
109 void LinkableMapObj::setParObjTmp(LinkableMapObj*,QPointF,int) // FIXME-3 make pure virtual
113 void LinkableMapObj::unsetParObjTmp() // FIXME-3 make pure virtual
117 bool LinkableMapObj::hasParObjTmp()
119 if (parObjTmpBuf) return true;
123 void LinkableMapObj::setUseRelPos (const bool &b)
128 void LinkableMapObj::setRelPos()
132 relPos.setX (absPos.x() - parObj->getChildPos().x() );
133 relPos.setY (absPos.y() - parObj->getChildPos().y() );
134 parObj->calcBBoxSize();
138 void LinkableMapObj::setRelPos(const QPointF &p)
143 parObj->calcBBoxSize();
148 QPointF LinkableMapObj::getRelPos()
150 if (!parObj) return QPointF();
154 qreal LinkableMapObj::getTopPad()
159 qreal LinkableMapObj::getLeftPad()
164 qreal LinkableMapObj::getRightPad()
169 LinkableMapObj::Style LinkableMapObj::getDefLinkStyle (TreeItem *parent)
171 VymModel *model=treeItem->getModel();
174 qWarning ("LMO::getDefLinkStyle model=NULL");
175 //return UndefinedStyle;
177 Style ls=model->getMapLinkStyle();
178 int depth=1+parent->depth();
179 if (depth==0) return UndefinedStyle;
203 return UndefinedStyle;
206 void LinkableMapObj::setLinkStyle(Style newstyle)
208 //if (newstyle==style) return; FIXME-3
215 QGraphicsLineItem *cl;
222 l = scene->addLine(QLineF(1,1,1,1),pen);
223 l->setZValue(Z_LINK);
230 for (int i=0;i<arcsegs;i++)
232 cl = scene->addLine(QLineF(i*5,0,i*10,100),pen);
233 cl->setZValue(Z_LINK);
240 pa0.resize (arcsegs+1);
243 p =scene->addPolygon(QPolygonF(),pen,linkcolor);
244 p->setZValue(Z_LINK);
252 p = scene->addPolygon(QPolygonF(),pen,linkcolor);
253 p->setZValue(Z_LINK);
258 pa0.resize (arcsegs*2+2);
259 pa1.resize (arcsegs+1);
260 pa2.resize (arcsegs+1);
268 LinkableMapObj::Style LinkableMapObj::getLinkStyle()
273 void LinkableMapObj::setHideLinkUnselected()
275 setVisibility (visible);
276 updateLinkGeometry();
279 void LinkableMapObj::setLinkPos(Position lp)
284 LinkableMapObj::Position LinkableMapObj::getLinkPos()
289 void LinkableMapObj::setLinkColor()
291 // Overloaded in BranchObj and children
292 // here only set default color
293 VymModel *model=treeItem->getModel();
295 setLinkColor (model->getMapDefLinkColor());
298 void LinkableMapObj::setLinkColor(QColor col)
302 bottomline->setPen( pen );
309 for (int i=0; i<segment.size(); ++i)
310 segment.at(i)->setPen( pen);
313 p->setBrush( QBrush(col));
317 p->setBrush( QBrush(col));
325 QColor LinkableMapObj::getLinkColor()
330 void LinkableMapObj::setVisibility (bool v)
332 MapObj::setVisibility (v);
336 void LinkableMapObj::setOrientation()
338 Orientation orientOld=orientation;
342 orientation=UndefinedOrientation;
346 // Set orientation, first look for orientation of parent
347 if (parObj->getOrientation() != UndefinedOrientation )
348 // use the orientation of the parent:
349 orientation=parObj->getOrientation();
352 // calc orientation depending on position rel to parent
353 if (absPos.x() < QPointF(parObj->getChildPos() ).x() )
354 orientation=LeftOfCenter;
356 orientation=RightOfCenter;
358 if (orientOld!=orientation) requestReposition();
361 void LinkableMapObj::updateVisibility()
365 if (((MapItem*)treeItem)->getHideLinkUnselected()
366 && 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->prepareGeometryChange();
481 bottomline->setLine (QLine (qRound(childPos.x()),
482 qRound(childPos.y()),
487 if (vx > -0.000001 && vx < 0.000001)
491 // "turning point" for drawing polygonal links
492 QPointF tp (-qRound(sin (a)*thickness_start), qRound(cos (a)*thickness_start));
498 l->setLine( QLine(qRound (parPos.x()),
504 parabel (pa0, p1x,p1y,p2x,p2y);
505 for (int i=0; i<segment.size(); ++i)
506 segment.at(i)->setLine(QLineF( pa0.at(i).x(), pa0.at(i).y(),pa0.at(i+1).x(),pa0.at(i+1).y()));
510 pa0<<QPointF (qRound(p2x+tp.x()), qRound(p2y+tp.y()));
511 pa0<<QPointF (qRound(p2x-tp.x()), qRound(p2y-tp.y()));
512 pa0<<QPointF (qRound (parPos.x()), qRound(parPos.y()) );
513 p->setPolygon(QPolygonF (pa0));
516 parabel (pa1, p1x,p1y,p2x+tp.x(),p2y+tp.y());
517 parabel (pa2, p1x,p1y,p2x-tp.x(),p2y-tp.y());
519 for (int i=0;i<=arcsegs;i++)
520 pa0 << QPointF (pa1.at(i));
521 for (int i=0;i<=arcsegs;i++)
522 pa0 << QPointF (pa2.at(arcsegs-i));
523 p->setPolygon(QPolygonF (pa0));
530 LinkableMapObj* LinkableMapObj::getParObj()
535 QPointF LinkableMapObj::getChildPos()
540 QPointF LinkableMapObj::getParPos()
545 LinkableMapObj::Orientation LinkableMapObj::getOrientation()
550 QPointF LinkableMapObj::getRandPos()
552 // Choose a random position with given distance to parent:
553 double a=rand()%360 * 2 * M_PI / 360;
554 return QPointF ( (int)( + 150*cos (a)),
555 (int)( + 150*sin (a)));
558 void LinkableMapObj::reposition()
562 void LinkableMapObj::requestReposition() //FIXME-3 needed?
564 if (!repositionRequest)
566 // Pass on the request to parental objects, if this hasn't
568 repositionRequest=true;
569 if (parObj) parObj->requestReposition();
573 void LinkableMapObj::forceReposition()
575 // Sometimes a reposition has to be done immediatly: For example
576 // if the note editor flag changes, there is no user event in mapeditor
577 // which could collect requests for a reposition.
578 // Then we have to call forceReposition()
579 // But no rule without exception: While loading a map or undoing it,
580 // we want to block expensive repositioning, but just do it once at
581 // the end, thus check first:
583 VymModel *model=treeItem->getModel();
584 if (model->isRepositionBlocked()) return;
586 // Pass on the request to parent objects, if this hasn't been done yet
588 parObj->forceReposition();
593 bool LinkableMapObj::repositionRequested()
595 return repositionRequest;
598 void LinkableMapObj::parabel (QPolygonF &ya, double p1x, double p1y, double p2x, double p2y)
601 double vx=p2x - p1x; // V=P2-P1
604 double dx; // delta x during calculation of parabel
606 double pnx; // next point
610 if (vx > -0.0001 && vx < 0.0001)
616 ya<<QPointF (p1x,p1y);
617 for (int i=1;i<=arcsegs;i++)
620 pny=m*(pnx-parPos.x())*(pnx-parPos.x())+parPos.y();
621 ya<<QPointF (pnx,pny);