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 bool LinkableMapObj::getUseRelPos ()
136 void LinkableMapObj::setRelPos()
140 setRelPos (absPos - parObj->getChildPos() );
142 qWarning ("LMO::setRelPos() parObj==0");
145 void LinkableMapObj::setRelPos(const QPointF &p)
151 parObj->calcBBoxSize();
154 qWarning ("LMO::setRelPos (p) parObj==0");
157 QPointF LinkableMapObj::getRelPos()
161 qWarning("LMO::getRelPos parObj==0");
167 qreal LinkableMapObj::getTopPad()
172 qreal LinkableMapObj::getLeftPad()
177 qreal LinkableMapObj::getRightPad()
182 LinkableMapObj::Style LinkableMapObj::getDefLinkStyle (TreeItem *parent)
184 VymModel *model=treeItem->getModel();
187 qWarning ("LMO::getDefLinkStyle model=NULL");
188 //return UndefinedStyle;
190 Style ls=model->getMapLinkStyle();
191 int depth=1+parent->depth();
192 if (depth==0) return UndefinedStyle;
216 return UndefinedStyle;
219 void LinkableMapObj::setLinkStyle(Style newstyle)
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::setLinkPos(Position lp)
290 LinkableMapObj::Position LinkableMapObj::getLinkPos()
295 void LinkableMapObj::setLinkColor()
297 // Overloaded in BranchObj and children
298 // here only set default color
299 VymModel *model=treeItem->getModel();
301 setLinkColor (model->getMapDefLinkColor());
304 void LinkableMapObj::setLinkColor(QColor col)
308 bottomline->setPen( pen );
315 for (int i=0; i<segment.size(); ++i)
316 segment.at(i)->setPen( pen);
319 p->setBrush( QBrush(col));
323 p->setBrush( QBrush(col));
331 QColor LinkableMapObj::getLinkColor()
336 void LinkableMapObj::setVisibility (bool v)
338 MapObj::setVisibility (v);
342 void LinkableMapObj::setOrientation()
344 Orientation orientOld=orientation;
348 orientation=UndefinedOrientation;
352 // Set orientation, first look for orientation of parent
353 if (parObj->getOrientation() != UndefinedOrientation )
354 // use the orientation of the parent:
355 orientation=parObj->getOrientation();
358 // calc orientation depending on position rel to parent
359 if (absPos.x() < QPointF(parObj->getChildPos() ).x() )
360 orientation=LeftOfCenter;
362 orientation=RightOfCenter;
364 if (orientOld!=orientation) requestReposition();
367 void LinkableMapObj::updateVisibility()
371 if (((MapItem*)treeItem)->getHideLinkUnselected()
372 && treeItem->getModel()->getSelectedLMO() !=this)
388 for (int i=0; i<segment.size(); ++i)
389 segment.at(i)->show();
392 if (!p) cout << "LMO::updateVis p==0 (PolyLine)\n"; //FIXME-3
396 if (!p) cout << "LMO::updateVis p==0 (PolyParabel) "<<treeItem->getHeading().toStdString()<<endl; //FIXME-3
411 for (int i=0; i<segment.size(); ++i)
412 segment.at(i)->hide();
426 void LinkableMapObj::updateLinkGeometry()
429 // childPos of parent
435 // childPos (by calling setDockPos())
436 // parPos (by calling setDockPos())
438 // drawing of the link itself
440 // updateLinkGeometry is called from move, but called from constructor we don't
441 // have parents yet...
443 //cout <<"LMO::updateLinkGeometry: "<<treeItem->getHeadingStd()<<" "<<parObj<<endl;
446 // If I am a mapcenter, set childPos to middle of MapCenterObj
447 childPos.setX( clickBox.topLeft().x() + clickBox.width()/2 );
448 childPos.setY( clickBox.topLeft().y() + clickBox.height()/2 );
450 // Redraw links to children
451 for (int i=0; i<treeItem->branchCount(); ++i)
452 treeItem->getBranchObjNum(i)->updateLinkGeometry();
456 if (style==UndefinedStyle) return;
461 bottomlineY=bbox.top() + bbox.height()/2; // draw link to middle (of frame)
464 //bottomlineY=bbox.bottom()-1; // draw link to bottom of box
465 bottomlineY=bbox.bottom()-botPad;
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->setLine (QLine (qRound(childPos.x()),
491 qRound(childPos.y()),
496 if (vx > -0.000001 && vx < 0.000001)
500 // "turning point" for drawing polygonal links
501 QPointF tp (-qRound(sin (a)*thickness_start), qRound(cos (a)*thickness_start));
507 l->setLine( QLine(qRound (parPos.x()),
513 parabel (pa0, p1x,p1y,p2x,p2y);
514 for (int i=0; i<segment.size(); ++i)
515 segment.at(i)->setLine(QLineF( pa0.at(i).x(), pa0.at(i).y(),pa0.at(i+1).x(),pa0.at(i+1).y()));
519 pa0<<QPointF (qRound(p2x+tp.x()), qRound(p2y+tp.y()));
520 pa0<<QPointF (qRound(p2x-tp.x()), qRound(p2y-tp.y()));
521 pa0<<QPointF (qRound (parPos.x()), qRound(parPos.y()) );
522 p->setPolygon(QPolygonF (pa0));
525 parabel (pa1, p1x,p1y,p2x+tp.x(),p2y+tp.y());
526 parabel (pa2, p1x,p1y,p2x-tp.x(),p2y-tp.y());
528 for (int i=0;i<=arcsegs;i++)
529 pa0 << QPointF (pa1.at(i));
530 for (int i=0;i<=arcsegs;i++)
531 pa0 << QPointF (pa2.at(arcsegs-i));
532 p->setPolygon(QPolygonF (pa0));
539 LinkableMapObj* LinkableMapObj::getParObj()
544 QPointF LinkableMapObj::getChildPos()
549 QPointF LinkableMapObj::getParPos()
554 LinkableMapObj::Orientation LinkableMapObj::getOrientation()
559 QPointF LinkableMapObj::getRandPos()
561 // Choose a random position with given distance to parent:
562 double a=rand()%360 * 2 * M_PI / 360;
563 return QPointF ( (int)( + 150*cos (a)),
564 (int)( + 150*sin (a)));
567 void LinkableMapObj::reposition()
571 void LinkableMapObj::requestReposition() //FIXME-3 needed?
573 if (!repositionRequest)
575 // Pass on the request to parental objects, if this hasn't
577 repositionRequest=true;
578 if (parObj) parObj->requestReposition();
582 void LinkableMapObj::forceReposition()
584 // Sometimes a reposition has to be done immediatly: For example
585 // if the note editor flag changes, there is no user event in mapeditor
586 // which could collect requests for a reposition.
587 // Then we have to call forceReposition()
588 // But no rule without exception: While loading a map or undoing it,
589 // we want to block expensive repositioning, but just do it once at
590 // the end, thus check first:
592 VymModel *model=treeItem->getModel();
593 if (model->isRepositionBlocked()) return;
595 // Pass on the request to parent objects, if this hasn't been done yet
597 parObj->forceReposition();
602 bool LinkableMapObj::repositionRequested()
604 return repositionRequest;
607 void LinkableMapObj::parabel (QPolygonF &ya, double p1x, double p1y, double p2x, double p2y)
610 double vx=p2x - p1x; // V=P2-P1
613 double dx; // delta x during calculation of parabel
615 double pnx; // next point
619 if (vx > -0.0001 && vx < 0.0001)
625 ya<<QPointF (p1x,p1y);
626 for (int i=1;i<=arcsegs;i++)
629 pny=m*(pnx-parPos.x())*(pnx-parPos.x())+parPos.y();
630 ya<<QPointF (pnx,pny);