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 ()
63 childPos=QPointF(0,0);
67 orientation=UndefinedOrientation;
74 // TODO instead of linkcolor pen.color() could be used all around
76 pen.setColor (linkcolor);
77 pen.setCapStyle ( Qt::RoundCap );
78 bottomline=scene->addLine(QLineF(1,1,1,1),pen);
79 bottomline->setZValue(Z_LINK);
82 topPad=botPad=leftPad=rightPad=0;
84 repositionRequest=false;
91 // Crossreference to treemodel
95 void LinkableMapObj::copy (LinkableMapObj* other)
98 bboxTotal=other->bboxTotal;
99 setLinkStyle(other->style);
100 setLinkColor (other->linkcolor);
101 relPos=other->relPos;
102 useOrientation=other->useOrientation;
103 treeItem=other->treeItem;
106 void LinkableMapObj::setTreeItem (TreeItem *ti)
111 TreeItem* LinkableMapObj::getTreeItem () const
116 void LinkableMapObj::setParObj(LinkableMapObj* o)
121 void LinkableMapObj::setParObjTmp(LinkableMapObj*,QPointF,int) // FIXME-3 make pure virtual
125 void LinkableMapObj::unsetParObjTmp() // FIXME-3 make pure virtual
129 bool LinkableMapObj::hasParObjTmp()
131 if (parObjTmpBuf) return true;
135 void LinkableMapObj::setUseRelPos (const bool &b)
140 void LinkableMapObj::setRelPos()
144 relPos.setX (absPos.x() - parObj->getChildPos().x() );
145 relPos.setY (absPos.y() - parObj->getChildPos().y() );
146 parObj->calcBBoxSize();
150 void LinkableMapObj::setRelPos(const QPointF &p)
155 parObj->calcBBoxSize();
160 QPointF LinkableMapObj::getRelPos()
162 if (!parObj) return QPointF();
166 qreal LinkableMapObj::getTopPad()
171 qreal LinkableMapObj::getLeftPad()
176 qreal LinkableMapObj::getRightPad()
181 LinkableMapObj::Style LinkableMapObj::getDefLinkStyle ()
183 VymModel *model=treeItem->getModel();
186 qWarning ("LMO::getDefLinkStyle model=NULL");
187 //return UndefinedStyle;
189 Style ls=model->getMapLinkStyle();
190 int depth=treeItem->depth();
191 if (depth==0) return UndefinedStyle;
215 return UndefinedStyle;
218 void LinkableMapObj::setLinkStyle(Style newstyle)
220 //if (newstyle=style) return; FIXME-3
227 QGraphicsLineItem *cl;
234 l = scene->addLine(QLineF(1,1,1,1),pen);
235 l->setZValue(Z_LINK);
242 for (int i=0;i<arcsegs;i++)
244 cl = scene->addLine(QLineF(i*5,0,i*10,100),pen);
245 cl->setZValue(Z_LINK);
252 pa0.resize (arcsegs+1);
255 p =scene->addPolygon(QPolygonF(),pen,linkcolor);
256 p->setZValue(Z_LINK);
264 p = scene->addPolygon(QPolygonF(),pen,linkcolor);
265 p->setZValue(Z_LINK);
270 pa0.resize (arcsegs*2+2);
271 pa1.resize (arcsegs+1);
272 pa2.resize (arcsegs+1);
280 LinkableMapObj::Style LinkableMapObj::getLinkStyle()
285 void LinkableMapObj::setHideLinkUnselected()
287 setVisibility (visible);
288 updateLinkGeometry();
291 void LinkableMapObj::setLinkPos(Position lp)
296 LinkableMapObj::Position LinkableMapObj::getLinkPos()
301 void LinkableMapObj::setLinkColor()
303 // Overloaded in BranchObj and children
304 // here only set default color
305 VymModel *model=treeItem->getModel();
307 setLinkColor (model->getMapDefLinkColor());
310 void LinkableMapObj::setLinkColor(QColor col)
314 bottomline->setPen( pen );
321 for (int i=0; i<segment.size(); ++i)
322 segment.at(i)->setPen( pen);
325 p->setBrush( QBrush(col));
329 p->setBrush( QBrush(col));
337 QColor LinkableMapObj::getLinkColor()
342 void LinkableMapObj::setVisibility (bool v)
344 MapObj::setVisibility (v);
348 void LinkableMapObj::setOrientation()
350 Orientation orientOld=orientation;
354 orientation=UndefinedOrientation;
358 // Set orientation, first look for orientation of parent
359 if (parObj->getOrientation() != UndefinedOrientation )
360 // use the orientation of the parent:
361 orientation=parObj->getOrientation();
364 // calc orientation depending on position rel to parent
365 if (absPos.x() < QPointF(parObj->getChildPos() ).x() )
366 orientation=LeftOfCenter;
368 orientation=RightOfCenter;
370 if (orientOld!=orientation) requestReposition();
373 void LinkableMapObj::updateVisibility()
377 if (((MapItem*)treeItem)->getHideLinkUnselected()
378 && treeItem->getModel()->getSelectedLMO() !=this)
390 for (int i=0; i<segment.size(); ++i)
391 segment.at(i)->show();
394 if (!p) cout << "LMO::updateVis p==0 (PolyLine)\n"; //FIXME-3
398 if (!p) cout << "LMO::updateVis p==0 (PolyParabel) "<<treeItem->getHeading().toStdString()<<endl; //FIXME-3
413 for (int i=0; i<segment.size(); ++i)
414 segment.at(i)->hide();
428 void LinkableMapObj::updateLinkGeometry()
431 // childPos of parent
437 // childPos (by calling setDockPos())
438 // parPos (by calling setDockPos())
440 // drawing of the link itself
442 // updateLinkGeometry is called from move, but called from constructor we don't
443 // have parents yet...
447 // If I am a mapcenter, set childPos to middle of MapCenterObj
448 childPos.setX( clickBox.topLeft().x() + clickBox.width()/2 );
449 childPos.setY( clickBox.topLeft().y() + clickBox.height()/2 );
451 // Redraw links to children
452 for (int i=0; i<treeItem->branchCount(); ++i)
453 treeItem->getBranchObjNum(i)->updateLinkGeometry();
457 if (style==UndefinedStyle) return;
462 bottomlineY=bbox.top() + bbox.height()/2; // draw link to middle (of frame)
465 bottomlineY=bbox.bottom()-1; // draw link to bottom of box
469 double p2x,p2y; // Set P2 Before setting
472 p2x=QPointF( parObj->getChildPos() ).x(); // P1, we have to look at
473 p2y=QPointF( parObj->getChildPos() ).y(); // orientation
476 p2x=QPointF( parObj->getParPos() ).x();
477 p2y=QPointF( parObj->getParPos() ).y();
480 setDockPos(); // Call overloaded method
483 double p1x=parPos.x(); // Link is drawn from P1 to P2
484 double p1y=parPos.y();
486 double vx=p2x - p1x; // V=P2-P1
489 // Draw the horizontal line below heading (from ChildPos to ParPos)
490 //bottomline->prepareGeometryChange();
491 bottomline->setLine (QLine (qRound(childPos.x()),
492 qRound(childPos.y()),
497 if (vx > -0.000001 && vx < 0.000001)
501 // "turning point" for drawing polygonal links
502 QPointF tp (-qRound(sin (a)*thickness_start), qRound(cos (a)*thickness_start));
508 l->setLine( QLine(qRound (parPos.x()),
514 parabel (pa0, p1x,p1y,p2x,p2y);
515 for (int i=0; i<segment.size(); ++i)
516 segment.at(i)->setLine(QLineF( pa0.at(i).x(), pa0.at(i).y(),pa0.at(i+1).x(),pa0.at(i+1).y()));
520 pa0<<QPointF (qRound(p2x+tp.x()), qRound(p2y+tp.y()));
521 pa0<<QPointF (qRound(p2x-tp.x()), qRound(p2y-tp.y()));
522 pa0<<QPointF (qRound (parPos.x()), qRound(parPos.y()) );
523 p->setPolygon(QPolygonF (pa0));
526 parabel (pa1, p1x,p1y,p2x+tp.x(),p2y+tp.y());
527 parabel (pa2, p1x,p1y,p2x-tp.x(),p2y-tp.y());
529 for (int i=0;i<=arcsegs;i++)
530 pa0 << QPointF (pa1.at(i));
531 for (int i=0;i<=arcsegs;i++)
532 pa0 << QPointF (pa2.at(arcsegs-i));
533 p->setPolygon(QPolygonF (pa0));
540 LinkableMapObj* LinkableMapObj::getParObj()
545 QPointF LinkableMapObj::getChildPos()
550 QPointF LinkableMapObj::getParPos()
555 void LinkableMapObj::setUseOrientation (const bool &b)
557 if (useOrientation!=b)
564 LinkableMapObj::Orientation LinkableMapObj::getOrientation()
569 QPointF LinkableMapObj::getRandPos()
571 // Choose a random position with given distance to parent:
572 double a=rand()%360 * 2 * M_PI / 360;
573 return QPointF ( (int)( + 150*cos (a)),
574 (int)( + 150*sin (a)));
577 void LinkableMapObj::reposition()
581 void LinkableMapObj::requestReposition()
583 if (!repositionRequest)
585 // Pass on the request to parental objects, if this hasn't
587 repositionRequest=true;
588 if (parObj) parObj->requestReposition();
592 void LinkableMapObj::forceReposition()
594 // Sometimes a reposition has to be done immediatly: For example
595 // if the note editor flag changes, there is no user event in mapeditor
596 // which could collect requests for a reposition.
597 // Then we have to call forceReposition()
598 // But no rule without exception: While loading a map or undoing it,
599 // we want to block expensive repositioning, but just do it once at
600 // the end, thus check first:
602 VymModel *model=treeItem->getModel();
603 if (model->isRepositionBlocked()) return;
605 // Pass on the request to parent objects, if this hasn't been done yet
607 parObj->forceReposition();
612 bool LinkableMapObj::repositionRequested()
614 return repositionRequest;
617 void LinkableMapObj::parabel (QPolygonF &ya, double p1x, double p1y, double p2x, double p2y)
620 double vx=p2x - p1x; // V=P2-P1
623 double dx; // delta x during calculation of parabel
625 double pnx; // next point
629 if (vx > -0.0001 && vx < 0.0001)
635 ya<<QPointF (p1x,p1y);
636 for (int i=1;i<=arcsegs;i++)
639 pny=m*(pnx-parPos.x())*(pnx-parPos.x())+parPos.y();
640 ya<<QPointF (pnx,pny);