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 ()
67 childPos=QPointF(0,0);
71 orientation=UndefinedOrientation;
78 // TODO instead of linkcolor pen.color() could be used all around
80 pen.setColor (linkcolor);
81 pen.setCapStyle ( Qt::RoundCap );
82 bottomline=scene->addLine(QLineF(1,1,1,1),pen);
83 bottomline->setZValue(Z_LINK);
86 // Prepare showing the selection of a MapObj
89 hideLinkUnselected=false;
91 topPad=botPad=leftPad=rightPad=0;
93 repositionRequest=false;
100 // Crossreference to treemodel
104 void LinkableMapObj::copy (LinkableMapObj* other)
108 bboxTotal=other->bboxTotal;
109 setLinkStyle(other->style);
110 setLinkColor (other->linkcolor);
111 relPos=other->relPos;
112 useOrientation=other->useOrientation;
113 treeItem=other->treeItem;
116 void LinkableMapObj::setTreeItem (TreeItem *ti)
121 TreeItem* LinkableMapObj::getTreeItem () const
126 void LinkableMapObj::setModel (VymModel *vm)
131 VymModel* LinkableMapObj::getModel()
136 void LinkableMapObj::setChildObj(LinkableMapObj* o)
141 void LinkableMapObj::setParObj(LinkableMapObj* o)
144 model=parObj->getModel();
147 void LinkableMapObj::setParObjTmp(LinkableMapObj*,QPointF,int) // FIXME-3 make pure virtual
151 void LinkableMapObj::unsetParObjTmp() // FIXME-3 make pure virtual
155 bool LinkableMapObj::hasParObjTmp()
157 if (parObjTmpBuf) return true;
161 void LinkableMapObj::setUseRelPos (const bool &b)
166 void LinkableMapObj::setRelPos()
170 relPos.setX (absPos.x() - parObj->getChildPos().x() );
171 relPos.setY (absPos.y() - parObj->getChildPos().y() );
172 parObj->calcBBoxSize();
176 void LinkableMapObj::setRelPos(const QPointF &p)
181 parObj->calcBBoxSize();
186 QPointF LinkableMapObj::getRelPos()
188 if (!parObj) return QPointF();
192 qreal LinkableMapObj::getTopPad()
197 qreal LinkableMapObj::getLeftPad()
202 qreal LinkableMapObj::getRightPad()
207 LinkableMapObj::Style LinkableMapObj::getDefLinkStyle ()
211 qWarning ("LMO::getDefLinkStyle model=NULL");
212 //return UndefinedStyle;
214 Style ls=model->getMapLinkStyle();
215 if (depth==0) return UndefinedStyle;
239 return UndefinedStyle;
242 void LinkableMapObj::setLinkStyle(Style newstyle)
244 //if (newstyle=style) return; FIXME-3
249 if (childObj!=NULL && parObj != NULL)
251 QGraphicsLineItem *cl;
258 l = scene->addLine(QLineF(1,1,1,1),pen);
259 l->setZValue(Z_LINK);
266 for (int i=0;i<arcsegs;i++)
268 cl = scene->addLine(QLineF(i*5,0,i*10,100),pen);
269 cl->setZValue(Z_LINK);
276 pa0.resize (arcsegs+1);
279 p =scene->addPolygon(QPolygonF(),pen,linkcolor);
280 p->setZValue(Z_LINK);
288 p = scene->addPolygon(QPolygonF(),pen,linkcolor);
289 p->setZValue(Z_LINK);
294 pa0.resize (arcsegs*2+2);
295 pa1.resize (arcsegs+1);
296 pa2.resize (arcsegs+1);
304 LinkableMapObj::Style LinkableMapObj::getLinkStyle()
309 void LinkableMapObj::setHideLinkUnselected(bool b)
311 hideLinkUnselected=b;
312 setVisibility (visible);
316 bool LinkableMapObj::getHideLinkUnselected()
318 return hideLinkUnselected;
321 void LinkableMapObj::setLinkPos(Position lp)
326 LinkableMapObj::Position LinkableMapObj::getLinkPos()
331 void LinkableMapObj::setLinkColor()
333 // Overloaded in BranchObj and children
334 // here only set default color
336 setLinkColor (model->getMapDefLinkColor());
339 void LinkableMapObj::setLinkColor(QColor col)
343 bottomline->setPen( pen );
350 for (int i=0; i<segment.size(); ++i)
351 segment.at(i)->setPen( pen);
354 p->setBrush( QBrush(col));
358 p->setBrush( QBrush(col));
366 QColor LinkableMapObj::getLinkColor()
371 void LinkableMapObj::setVisibility (bool v)
373 MapObj::setVisibility (v);
376 // We can hide the link, while object is not selected
377 if (hideLinkUnselected && !selected)
389 for (int i=0; i<segment.size(); ++i)
390 segment.at(i)->show();
393 if (!p) cout << "LMO::setVis p==0 (PolyLine)\n"; //FIXME-3
397 if (!p) cout << "LMO::setVis p==0 (PolyParabel) "<<treeItem->getHeading().toStdString()<<endl; //FIXME-3
412 for (int i=0; i<segment.size(); ++i)
413 segment.at(i)->hide();
427 void LinkableMapObj::setOrientation()
429 Orientation orientOld=orientation;
433 orientation=UndefinedOrientation;
437 // Set orientation, first look for orientation of parent
438 if (parObj->getOrientation() != UndefinedOrientation )
439 // use the orientation of the parent:
440 orientation=parObj->getOrientation();
443 // calc orientation depending on position rel to parent
444 if (absPos.x() < QPointF(parObj->getChildPos() ).x() )
445 orientation=LeftOfCenter;
447 orientation=RightOfCenter;
449 if (orientOld!=orientation) requestReposition();
452 void LinkableMapObj::updateLink()
455 // childPos of parent
461 // childPos (by calling setDockPos())
462 // parPos (by calling setDockPos())
464 // drawing of the link itself
466 // updateLink is called from move, but called from constructor we don't
467 // have parents yet...
468 if (style==UndefinedStyle) return;
473 bottomlineY=bbox.top() + bbox.height()/2; // draw link to middle (of frame)
476 bottomlineY=bbox.bottom()-1; // draw link to bottom of box
480 double p2x,p2y; // Set P2 Before setting
483 p2x=QPointF( parObj->getChildPos() ).x(); // P1, we have to look at
484 p2y=QPointF( parObj->getChildPos() ).y(); // orientation
487 p2x=QPointF( parObj->getParPos() ).x();
488 p2y=QPointF( parObj->getParPos() ).y();
491 setDockPos(); // Call overloaded method
494 double p1x=parPos.x(); // Link is drawn from P1 to P2
495 double p1y=parPos.y();
497 double vx=p2x - p1x; // V=P2-P1
500 // Draw the horizontal line below heading (from ChildPos to ParPos)
501 //bottomline->prepareGeometryChange();
502 bottomline->setLine (QLine (qRound(childPos.x()),
503 qRound(childPos.y()),
508 if (vx > -0.000001 && vx < 0.000001)
512 // "turning point" for drawing polygonal links
513 QPointF tp (-qRound(sin (a)*thickness_start), qRound(cos (a)*thickness_start));
519 //l->prepareGeometryChange();
520 l->setLine( QLine(qRound (parPos.x()),
526 parabel (pa0, p1x,p1y,p2x,p2y);
527 for (int i=0; i<segment.size(); ++i)
529 //segment.at(i)->prepareGeometryChange();
530 segment.at(i)->setLine(QLineF( pa0.at(i).x(), pa0.at(i).y(),pa0.at(i+1).x(),pa0.at(i+1).y()));
535 pa0<<QPointF (qRound(p2x+tp.x()), qRound(p2y+tp.y()));
536 pa0<<QPointF (qRound(p2x-tp.x()), qRound(p2y-tp.y()));
537 pa0<<QPointF (qRound (parPos.x()), qRound(parPos.y()) );
538 //p->prepareGeometryChange();
539 p->setPolygon(QPolygonF (pa0));
542 parabel (pa1, p1x,p1y,p2x+tp.x(),p2y+tp.y());
543 parabel (pa2, p1x,p1y,p2x-tp.x(),p2y-tp.y());
545 for (int i=0;i<=arcsegs;i++)
546 pa0 << QPointF (pa1.at(i));
547 for (int i=0;i<=arcsegs;i++)
548 pa0 << QPointF (pa2.at(arcsegs-i));
549 //p->prepareGeometryChange();
550 p->setPolygon(QPolygonF (pa0));
557 LinkableMapObj* LinkableMapObj::getChildObj()
562 LinkableMapObj* LinkableMapObj::getParObj()
567 LinkableMapObj* LinkableMapObj::findObjBySelect (QString s) // FIXME-0 already in VM???
569 LinkableMapObj *lmo=this;
573 while (!s.isEmpty() )
575 part=s.section(",",0,0);
577 num=part.right(part.length() - 3);
581 return false; // in a subtree there is no center
586 lmo=lmo->getTreeItem()->getBranchObjNum (num.toInt());
589 lmo=((BranchObj*)lmo)->getFloatImageNum (num.toUInt());
593 s=s.right(s.length() - part.length() -1 );
600 QPointF LinkableMapObj::getChildPos()
605 QPointF LinkableMapObj::getParPos()
610 void LinkableMapObj::setUseOrientation (const bool &b)
612 if (useOrientation!=b)
619 LinkableMapObj::Orientation LinkableMapObj::getOrientation()
624 int LinkableMapObj::getDepth()
629 QPointF LinkableMapObj::getRandPos()
631 // Choose a random position with given distance to parent:
632 double a=rand()%360 * 2 * M_PI / 360;
633 return QPointF ( (int)( + 150*cos (a)),
634 (int)( + 150*sin (a)));
637 void LinkableMapObj::reposition()
641 void LinkableMapObj::requestReposition()
643 if (!repositionRequest)
645 // Pass on the request to parental objects, if this hasn't
647 repositionRequest=true;
648 if (parObj) parObj->requestReposition();
652 void LinkableMapObj::forceReposition()
654 // Sometimes a reposition has to be done immediatly: For example
655 // if the note editor flag changes, there is no user event in mapeditor
656 // which could collect requests for a reposition.
657 // Then we have to call forceReposition()
658 // But no rule without exception: While loading a map or undoing it,
659 // we want to block expensive repositioning, but just do it once at
660 // the end, thus check first:
662 if (model->isRepositionBlocked()) return;
664 // Pass on the request to parent objects, if this hasn't been done yet
666 parObj->forceReposition();
671 bool LinkableMapObj::repositionRequested()
673 return repositionRequest;
677 void LinkableMapObj::select()
679 // select and unselect are still needed to
680 // handle hiding of links
682 setVisibility (visible);
686 void LinkableMapObj::unselect()
689 // Maybe we have to hide the link:
690 setVisibility (visible);
693 void LinkableMapObj::parabel (QPolygonF &ya, double p1x, double p1y, double p2x, double p2y)
696 double vx=p2x - p1x; // V=P2-P1
699 double dx; // delta x during calculation of parabel
701 double pnx; // next point
705 if (vx > -0.0001 && vx < 0.0001)
711 ya<<QPointF (p1x,p1y);
712 for (int i=1;i<=arcsegs;i++)
715 pny=m*(pnx-parPos.x())*(pnx-parPos.x())+parPos.y();
716 ya<<QPointF (pnx,pny);
722 QString LinkableMapObj::getLinkAttr ()
724 if (hideLinkUnselected)
725 return attribut ("hideLink","true");
727 return attribut ("hideLink","false");