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 relPos.setX (absPos.x() - parObj->getChildPos().x() );
141 relPos.setY (absPos.y() - parObj->getChildPos().y() );
142 parObj->calcBBoxSize();
146 void LinkableMapObj::setRelPos(const QPointF &p)
151 parObj->calcBBoxSize();
156 QPointF LinkableMapObj::getRelPos()
160 qWarning("LMO::getRelPos parObj==0");
166 qreal LinkableMapObj::getTopPad()
171 qreal LinkableMapObj::getLeftPad()
176 qreal LinkableMapObj::getRightPad()
181 LinkableMapObj::Style LinkableMapObj::getDefLinkStyle (TreeItem *parent)
183 VymModel *model=treeItem->getModel();
186 qWarning ("LMO::getDefLinkStyle model=NULL");
187 //return UndefinedStyle;
189 Style ls=model->getMapLinkStyle();
190 int depth=1+parent->depth();
191 if (depth==0) return UndefinedStyle;
215 return UndefinedStyle;
218 void LinkableMapObj::setLinkStyle(Style newstyle)
226 QGraphicsLineItem *cl;
233 l = scene->addLine(QLineF(1,1,1,1),pen);
234 l->setZValue(Z_LINK);
241 for (int i=0;i<arcsegs;i++)
243 cl = scene->addLine(QLineF(i*5,0,i*10,100),pen);
244 cl->setZValue(Z_LINK);
251 pa0.resize (arcsegs+1);
254 p =scene->addPolygon(QPolygonF(),pen,linkcolor);
255 p->setZValue(Z_LINK);
263 p = scene->addPolygon(QPolygonF(),pen,linkcolor);
264 p->setZValue(Z_LINK);
269 pa0.resize (arcsegs*2+2);
270 pa1.resize (arcsegs+1);
271 pa2.resize (arcsegs+1);
279 LinkableMapObj::Style LinkableMapObj::getLinkStyle()
284 void LinkableMapObj::setLinkPos(Position lp)
289 LinkableMapObj::Position LinkableMapObj::getLinkPos()
294 void LinkableMapObj::setLinkColor()
296 // Overloaded in BranchObj and children
297 // here only set default color
298 VymModel *model=treeItem->getModel();
300 setLinkColor (model->getMapDefLinkColor());
303 void LinkableMapObj::setLinkColor(QColor col)
307 bottomline->setPen( pen );
314 for (int i=0; i<segment.size(); ++i)
315 segment.at(i)->setPen( pen);
318 p->setBrush( QBrush(col));
322 p->setBrush( QBrush(col));
330 QColor LinkableMapObj::getLinkColor()
335 void LinkableMapObj::setVisibility (bool v)
337 MapObj::setVisibility (v);
341 void LinkableMapObj::setOrientation()
343 Orientation orientOld=orientation;
347 orientation=UndefinedOrientation;
351 // Set orientation, first look for orientation of parent
352 if (parObj->getOrientation() != UndefinedOrientation )
353 // use the orientation of the parent:
354 orientation=parObj->getOrientation();
357 // calc orientation depending on position rel to parent
358 if (absPos.x() < QPointF(parObj->getChildPos() ).x() )
359 orientation=LeftOfCenter;
361 orientation=RightOfCenter;
363 if (orientOld!=orientation) requestReposition();
366 void LinkableMapObj::updateVisibility()
370 if (((MapItem*)treeItem)->getHideLinkUnselected()
371 && treeItem->getModel()->getSelectedLMO() !=this)
387 for (int i=0; i<segment.size(); ++i)
388 segment.at(i)->show();
391 if (!p) cout << "LMO::updateVis p==0 (PolyLine)\n"; //FIXME-3
395 if (!p) cout << "LMO::updateVis p==0 (PolyParabel) "<<treeItem->getHeading().toStdString()<<endl; //FIXME-3
410 for (int i=0; i<segment.size(); ++i)
411 segment.at(i)->hide();
425 void LinkableMapObj::updateLinkGeometry()
428 // childPos of parent
434 // childPos (by calling setDockPos())
435 // parPos (by calling setDockPos())
437 // drawing of the link itself
439 // updateLinkGeometry is called from move, but called from constructor we don't
440 // have parents yet...
442 //cout <<"LMO::updateLinkGeometry: "<<treeItem->getHeadingStd()<<" "<<parObj<<endl;
445 // If I am a mapcenter, set childPos to middle of MapCenterObj
446 childPos.setX( clickBox.topLeft().x() + clickBox.width()/2 );
447 childPos.setY( clickBox.topLeft().y() + clickBox.height()/2 );
449 // Redraw links to children
450 for (int i=0; i<treeItem->branchCount(); ++i)
451 treeItem->getBranchObjNum(i)->updateLinkGeometry();
455 if (style==UndefinedStyle) return;
460 bottomlineY=bbox.top() + bbox.height()/2; // draw link to middle (of frame)
463 //bottomlineY=bbox.bottom()-1; // draw link to bottom of box
464 bottomlineY=bbox.bottom()-botPad;
468 double p2x,p2y; // Set P2 Before setting
471 p2x=QPointF( parObj->getChildPos() ).x(); // P1, we have to look at
472 p2y=QPointF( parObj->getChildPos() ).y(); // orientation
475 p2x=QPointF( parObj->getParPos() ).x();
476 p2y=QPointF( parObj->getParPos() ).y();
479 setDockPos(); // Call overloaded method
482 double p1x=parPos.x(); // Link is drawn from P1 to P2
483 double p1y=parPos.y();
485 double vx=p2x - p1x; // V=P2-P1
488 // Draw the horizontal line below heading (from ChildPos to ParPos)
489 bottomline->setLine (QLine (qRound(childPos.x()),
490 qRound(childPos.y()),
495 if (vx > -0.000001 && vx < 0.000001)
499 // "turning point" for drawing polygonal links
500 QPointF tp (-qRound(sin (a)*thickness_start), qRound(cos (a)*thickness_start));
506 l->setLine( QLine(qRound (parPos.x()),
512 parabel (pa0, p1x,p1y,p2x,p2y);
513 for (int i=0; i<segment.size(); ++i)
514 segment.at(i)->setLine(QLineF( pa0.at(i).x(), pa0.at(i).y(),pa0.at(i+1).x(),pa0.at(i+1).y()));
518 pa0<<QPointF (qRound(p2x+tp.x()), qRound(p2y+tp.y()));
519 pa0<<QPointF (qRound(p2x-tp.x()), qRound(p2y-tp.y()));
520 pa0<<QPointF (qRound (parPos.x()), qRound(parPos.y()) );
521 p->setPolygon(QPolygonF (pa0));
524 parabel (pa1, p1x,p1y,p2x+tp.x(),p2y+tp.y());
525 parabel (pa2, p1x,p1y,p2x-tp.x(),p2y-tp.y());
527 for (int i=0;i<=arcsegs;i++)
528 pa0 << QPointF (pa1.at(i));
529 for (int i=0;i<=arcsegs;i++)
530 pa0 << QPointF (pa2.at(arcsegs-i));
531 p->setPolygon(QPolygonF (pa0));
538 LinkableMapObj* LinkableMapObj::getParObj()
543 QPointF LinkableMapObj::getChildPos()
548 QPointF LinkableMapObj::getParPos()
553 LinkableMapObj::Orientation LinkableMapObj::getOrientation()
558 QPointF LinkableMapObj::getRandPos()
560 // Choose a random position with given distance to parent:
561 double a=rand()%360 * 2 * M_PI / 360;
562 return QPointF ( (int)( + 150*cos (a)),
563 (int)( + 150*sin (a)));
566 void LinkableMapObj::reposition()
570 void LinkableMapObj::requestReposition() //FIXME-3 needed?
572 if (!repositionRequest)
574 // Pass on the request to parental objects, if this hasn't
576 repositionRequest=true;
577 if (parObj) parObj->requestReposition();
581 void LinkableMapObj::forceReposition()
583 // Sometimes a reposition has to be done immediatly: For example
584 // if the note editor flag changes, there is no user event in mapeditor
585 // which could collect requests for a reposition.
586 // Then we have to call forceReposition()
587 // But no rule without exception: While loading a map or undoing it,
588 // we want to block expensive repositioning, but just do it once at
589 // the end, thus check first:
591 VymModel *model=treeItem->getModel();
592 if (model->isRepositionBlocked()) return;
594 // Pass on the request to parent objects, if this hasn't been done yet
596 parObj->forceReposition();
601 bool LinkableMapObj::repositionRequested()
603 return repositionRequest;
606 void LinkableMapObj::parabel (QPolygonF &ya, double p1x, double p1y, double p2x, double p2y)
609 double vx=p2x - p1x; // V=P2-P1
612 double dx; // delta x during calculation of parabel
614 double pnx; // next point
618 if (vx > -0.0001 && vx < 0.0001)
624 ya<<QPointF (p1x,p1y);
625 for (int i=1;i<=arcsegs;i++)
628 pny=m*(pnx-parPos.x())*(pnx-parPos.x())+parPos.y();
629 ya<<QPointF (pnx,pny);