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 // Prepare showing the selection of a MapObj
85 hideLinkUnselected=false;
87 topPad=botPad=leftPad=rightPad=0;
89 repositionRequest=false;
96 // Crossreference to treemodel
100 void LinkableMapObj::copy (LinkableMapObj* other)
103 bboxTotal=other->bboxTotal;
104 setLinkStyle(other->style);
105 setLinkColor (other->linkcolor);
106 relPos=other->relPos;
107 useOrientation=other->useOrientation;
108 treeItem=other->treeItem;
111 void LinkableMapObj::setTreeItem (TreeItem *ti)
116 TreeItem* LinkableMapObj::getTreeItem () const
121 void LinkableMapObj::setParObj(LinkableMapObj* o)
126 void LinkableMapObj::setParObjTmp(LinkableMapObj*,QPointF,int) // FIXME-3 make pure virtual
130 void LinkableMapObj::unsetParObjTmp() // FIXME-3 make pure virtual
134 bool LinkableMapObj::hasParObjTmp()
136 if (parObjTmpBuf) return true;
140 void LinkableMapObj::setUseRelPos (const bool &b)
145 void LinkableMapObj::setRelPos()
149 relPos.setX (absPos.x() - parObj->getChildPos().x() );
150 relPos.setY (absPos.y() - parObj->getChildPos().y() );
151 parObj->calcBBoxSize();
155 void LinkableMapObj::setRelPos(const QPointF &p)
160 parObj->calcBBoxSize();
165 QPointF LinkableMapObj::getRelPos()
167 if (!parObj) return QPointF();
171 qreal LinkableMapObj::getTopPad()
176 qreal LinkableMapObj::getLeftPad()
181 qreal LinkableMapObj::getRightPad()
186 LinkableMapObj::Style LinkableMapObj::getDefLinkStyle ()
188 VymModel *model=treeItem->getModel();
191 qWarning ("LMO::getDefLinkStyle model=NULL");
192 //return UndefinedStyle;
194 Style ls=model->getMapLinkStyle();
195 int depth=treeItem->depth();
196 if (depth==0) return UndefinedStyle;
220 return UndefinedStyle;
223 void LinkableMapObj::setLinkStyle(Style newstyle)
225 //if (newstyle=style) return; FIXME-3
232 QGraphicsLineItem *cl;
239 l = scene->addLine(QLineF(1,1,1,1),pen);
240 l->setZValue(Z_LINK);
247 for (int i=0;i<arcsegs;i++)
249 cl = scene->addLine(QLineF(i*5,0,i*10,100),pen);
250 cl->setZValue(Z_LINK);
257 pa0.resize (arcsegs+1);
260 p =scene->addPolygon(QPolygonF(),pen,linkcolor);
261 p->setZValue(Z_LINK);
269 p = scene->addPolygon(QPolygonF(),pen,linkcolor);
270 p->setZValue(Z_LINK);
275 pa0.resize (arcsegs*2+2);
276 pa1.resize (arcsegs+1);
277 pa2.resize (arcsegs+1);
285 LinkableMapObj::Style LinkableMapObj::getLinkStyle()
290 void LinkableMapObj::setHideLinkUnselected(bool b)
292 hideLinkUnselected=b;
293 setVisibility (visible);
297 bool LinkableMapObj::getHideLinkUnselected()
299 return hideLinkUnselected;
302 void LinkableMapObj::setLinkPos(Position lp)
307 LinkableMapObj::Position LinkableMapObj::getLinkPos()
312 void LinkableMapObj::setLinkColor()
314 // Overloaded in BranchObj and children
315 // here only set default color
316 VymModel *model=treeItem->getModel();
318 setLinkColor (model->getMapDefLinkColor());
321 void LinkableMapObj::setLinkColor(QColor col)
325 bottomline->setPen( pen );
332 for (int i=0; i<segment.size(); ++i)
333 segment.at(i)->setPen( pen);
336 p->setBrush( QBrush(col));
340 p->setBrush( QBrush(col));
348 QColor LinkableMapObj::getLinkColor()
353 void LinkableMapObj::setVisibility (bool v)
355 MapObj::setVisibility (v);
358 // We can hide the link, while object is not selected
359 if (hideLinkUnselected && !selected)
371 for (int i=0; i<segment.size(); ++i)
372 segment.at(i)->show();
375 if (!p) cout << "LMO::setVis p==0 (PolyLine)\n"; //FIXME-3
379 if (!p) cout << "LMO::setVis p==0 (PolyParabel) "<<treeItem->getHeading().toStdString()<<endl; //FIXME-3
394 for (int i=0; i<segment.size(); ++i)
395 segment.at(i)->hide();
409 void LinkableMapObj::setOrientation()
411 Orientation orientOld=orientation;
415 orientation=UndefinedOrientation;
419 // Set orientation, first look for orientation of parent
420 if (parObj->getOrientation() != UndefinedOrientation )
421 // use the orientation of the parent:
422 orientation=parObj->getOrientation();
425 // calc orientation depending on position rel to parent
426 if (absPos.x() < QPointF(parObj->getChildPos() ).x() )
427 orientation=LeftOfCenter;
429 orientation=RightOfCenter;
431 if (orientOld!=orientation) requestReposition();
434 void LinkableMapObj::updateLink()
437 // childPos of parent
443 // childPos (by calling setDockPos())
444 // parPos (by calling setDockPos())
446 // drawing of the link itself
448 // updateLink is called from move, but called from constructor we don't
449 // have parents yet...
453 // If I am a mapcenter, set childPos to middle of MapCenterObj
454 childPos.setX( clickBox.topLeft().x() + clickBox.width()/2 );
455 childPos.setY( clickBox.topLeft().y() + clickBox.height()/2 );
457 // Redraw links to children
458 for (int i=0; i<treeItem->branchCount(); ++i)
459 treeItem->getBranchObjNum(i)->updateLink();
463 if (style==UndefinedStyle) return;
468 bottomlineY=bbox.top() + bbox.height()/2; // draw link to middle (of frame)
471 bottomlineY=bbox.bottom()-1; // draw link to bottom of box
475 double p2x,p2y; // Set P2 Before setting
478 p2x=QPointF( parObj->getChildPos() ).x(); // P1, we have to look at
479 p2y=QPointF( parObj->getChildPos() ).y(); // orientation
482 p2x=QPointF( parObj->getParPos() ).x();
483 p2y=QPointF( parObj->getParPos() ).y();
486 setDockPos(); // Call overloaded method
489 double p1x=parPos.x(); // Link is drawn from P1 to P2
490 double p1y=parPos.y();
492 double vx=p2x - p1x; // V=P2-P1
495 // Draw the horizontal line below heading (from ChildPos to ParPos)
496 //bottomline->prepareGeometryChange();
497 bottomline->setLine (QLine (qRound(childPos.x()),
498 qRound(childPos.y()),
503 if (vx > -0.000001 && vx < 0.000001)
507 // "turning point" for drawing polygonal links
508 QPointF tp (-qRound(sin (a)*thickness_start), qRound(cos (a)*thickness_start));
514 //l->prepareGeometryChange();
515 l->setLine( QLine(qRound (parPos.x()),
521 parabel (pa0, p1x,p1y,p2x,p2y);
522 for (int i=0; i<segment.size(); ++i)
524 //segment.at(i)->prepareGeometryChange();
525 segment.at(i)->setLine(QLineF( pa0.at(i).x(), pa0.at(i).y(),pa0.at(i+1).x(),pa0.at(i+1).y()));
530 pa0<<QPointF (qRound(p2x+tp.x()), qRound(p2y+tp.y()));
531 pa0<<QPointF (qRound(p2x-tp.x()), qRound(p2y-tp.y()));
532 pa0<<QPointF (qRound (parPos.x()), qRound(parPos.y()) );
533 //p->prepareGeometryChange();
534 p->setPolygon(QPolygonF (pa0));
537 parabel (pa1, p1x,p1y,p2x+tp.x(),p2y+tp.y());
538 parabel (pa2, p1x,p1y,p2x-tp.x(),p2y-tp.y());
540 for (int i=0;i<=arcsegs;i++)
541 pa0 << QPointF (pa1.at(i));
542 for (int i=0;i<=arcsegs;i++)
543 pa0 << QPointF (pa2.at(arcsegs-i));
544 //p->prepareGeometryChange();
545 p->setPolygon(QPolygonF (pa0));
552 LinkableMapObj* LinkableMapObj::getParObj()
557 QPointF LinkableMapObj::getChildPos()
562 QPointF LinkableMapObj::getParPos()
567 void LinkableMapObj::setUseOrientation (const bool &b)
569 if (useOrientation!=b)
576 LinkableMapObj::Orientation LinkableMapObj::getOrientation()
581 QPointF LinkableMapObj::getRandPos()
583 // Choose a random position with given distance to parent:
584 double a=rand()%360 * 2 * M_PI / 360;
585 return QPointF ( (int)( + 150*cos (a)),
586 (int)( + 150*sin (a)));
589 void LinkableMapObj::reposition()
593 void LinkableMapObj::requestReposition()
595 if (!repositionRequest)
597 // Pass on the request to parental objects, if this hasn't
599 repositionRequest=true;
600 if (parObj) parObj->requestReposition();
604 void LinkableMapObj::forceReposition()
606 // Sometimes a reposition has to be done immediatly: For example
607 // if the note editor flag changes, there is no user event in mapeditor
608 // which could collect requests for a reposition.
609 // Then we have to call forceReposition()
610 // But no rule without exception: While loading a map or undoing it,
611 // we want to block expensive repositioning, but just do it once at
612 // the end, thus check first:
614 VymModel *model=treeItem->getModel();
615 if (model->isRepositionBlocked()) return;
617 // Pass on the request to parent objects, if this hasn't been done yet
619 parObj->forceReposition();
624 bool LinkableMapObj::repositionRequested()
626 return repositionRequest;
630 void LinkableMapObj::select()
632 // select and unselect are still needed to
633 // handle hiding of links
635 setVisibility (visible);
639 void LinkableMapObj::unselect()
642 // Maybe we have to hide the link:
643 setVisibility (visible);
646 void LinkableMapObj::parabel (QPolygonF &ya, double p1x, double p1y, double p2x, double p2y)
649 double vx=p2x - p1x; // V=P2-P1
652 double dx; // delta x during calculation of parabel
654 double pnx; // next point
658 if (vx > -0.0001 && vx < 0.0001)
664 ya<<QPointF (p1x,p1y);
665 for (int i=1;i<=arcsegs;i++)
668 pny=m*(pnx-parPos.x())*(pnx-parPos.x())+parPos.y();
669 ya<<QPointF (pnx,pny);
675 QString LinkableMapObj::getLinkAttr ()
677 if (hideLinkUnselected)
678 return attribut ("hideLink","true");
680 return attribut ("hideLink","false");