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;
90 // Crossreference to treemodel
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::setTreeItem (TreeItem *ti)
109 TreeItem* LinkableMapObj::getTreeItem () const
114 void LinkableMapObj::setParObj(LinkableMapObj* o)
119 void LinkableMapObj::setParObjTmp(LinkableMapObj*,QPointF,int) // FIXME-3 make pure virtual
123 void LinkableMapObj::unsetParObjTmp() // FIXME-3 make pure virtual
127 bool LinkableMapObj::hasParObjTmp()
129 if (parObjTmpBuf) return true;
133 void LinkableMapObj::setUseRelPos (const bool &b)
138 void LinkableMapObj::setRelPos()
142 relPos.setX (absPos.x() - parObj->getChildPos().x() );
143 relPos.setY (absPos.y() - parObj->getChildPos().y() );
144 parObj->calcBBoxSize();
148 void LinkableMapObj::setRelPos(const QPointF &p)
153 parObj->calcBBoxSize();
158 QPointF LinkableMapObj::getRelPos()
160 if (!parObj) return QPointF();
164 qreal LinkableMapObj::getTopPad()
169 qreal LinkableMapObj::getLeftPad()
174 qreal LinkableMapObj::getRightPad()
179 LinkableMapObj::Style LinkableMapObj::getDefLinkStyle ()
181 VymModel *model=treeItem->getModel();
184 qWarning ("LMO::getDefLinkStyle model=NULL");
185 //return UndefinedStyle;
187 Style ls=model->getMapLinkStyle();
188 int depth=treeItem->depth();
189 if (depth==0) return UndefinedStyle;
213 return UndefinedStyle;
216 void LinkableMapObj::setLinkStyle(Style newstyle)
218 //if (newstyle=style) return; FIXME-3
225 QGraphicsLineItem *cl;
232 l = scene->addLine(QLineF(1,1,1,1),pen);
233 l->setZValue(Z_LINK);
240 for (int i=0;i<arcsegs;i++)
242 cl = scene->addLine(QLineF(i*5,0,i*10,100),pen);
243 cl->setZValue(Z_LINK);
250 pa0.resize (arcsegs+1);
253 p =scene->addPolygon(QPolygonF(),pen,linkcolor);
254 p->setZValue(Z_LINK);
262 p = scene->addPolygon(QPolygonF(),pen,linkcolor);
263 p->setZValue(Z_LINK);
268 pa0.resize (arcsegs*2+2);
269 pa1.resize (arcsegs+1);
270 pa2.resize (arcsegs+1);
278 LinkableMapObj::Style LinkableMapObj::getLinkStyle()
283 void LinkableMapObj::setHideLinkUnselected()
285 setVisibility (visible);
286 updateLinkGeometry();
289 void LinkableMapObj::setLinkPos(Position lp)
294 LinkableMapObj::Position LinkableMapObj::getLinkPos()
299 void LinkableMapObj::setLinkColor()
301 // Overloaded in BranchObj and children
302 // here only set default color
303 VymModel *model=treeItem->getModel();
305 setLinkColor (model->getMapDefLinkColor());
308 void LinkableMapObj::setLinkColor(QColor col)
312 bottomline->setPen( pen );
319 for (int i=0; i<segment.size(); ++i)
320 segment.at(i)->setPen( pen);
323 p->setBrush( QBrush(col));
327 p->setBrush( QBrush(col));
335 QColor LinkableMapObj::getLinkColor()
340 void LinkableMapObj::setVisibility (bool v)
342 MapObj::setVisibility (v);
346 void LinkableMapObj::setOrientation()
348 Orientation orientOld=orientation;
352 orientation=UndefinedOrientation;
356 // Set orientation, first look for orientation of parent
357 if (parObj->getOrientation() != UndefinedOrientation )
358 // use the orientation of the parent:
359 orientation=parObj->getOrientation();
362 // calc orientation depending on position rel to parent
363 if (absPos.x() < QPointF(parObj->getChildPos() ).x() )
364 orientation=LeftOfCenter;
366 orientation=RightOfCenter;
368 if (orientOld!=orientation) requestReposition();
371 void LinkableMapObj::updateVisibility()
375 if (((MapItem*)treeItem)->getHideLinkUnselected()
376 && 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...
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
467 double p2x,p2y; // Set P2 Before setting
470 p2x=QPointF( parObj->getChildPos() ).x(); // P1, we have to look at
471 p2y=QPointF( parObj->getChildPos() ).y(); // orientation
474 p2x=QPointF( parObj->getParPos() ).x();
475 p2y=QPointF( parObj->getParPos() ).y();
478 setDockPos(); // Call overloaded method
481 double p1x=parPos.x(); // Link is drawn from P1 to P2
482 double p1y=parPos.y();
484 double vx=p2x - p1x; // V=P2-P1
487 // Draw the horizontal line below heading (from ChildPos to ParPos)
488 //bottomline->prepareGeometryChange();
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()
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);