Various fixes, also from 1.12. branch
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 ()
68 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;
103 // Crossreference to treemodel
107 void LinkableMapObj::copy (LinkableMapObj* other)
111 bboxTotal=other->bboxTotal;
112 setLinkStyle(other->style);
113 setLinkColor (other->linkcolor);
114 relPos=other->relPos;
115 useOrientation=other->useOrientation;
117 treeItem=other->treeItem;
120 void LinkableMapObj::setTreeItem (TreeItem *ti)
125 TreeItem* LinkableMapObj::getTreeItem ()
130 void LinkableMapObj::setModel (VymModel *vm)
135 VymModel* LinkableMapObj::getModel()
140 void LinkableMapObj::setChildObj(LinkableMapObj* o)
145 void LinkableMapObj::setParObj(LinkableMapObj* o)
148 mapEditor=parObj->getMapEditor();
149 model=parObj->getModel();
152 void LinkableMapObj::setParObjTmp(LinkableMapObj*,QPointF,int) // FIXME make pure virtual
156 void LinkableMapObj::unsetParObjTmp() // FIXME make pure virtual
160 bool LinkableMapObj::hasParObjTmp()
162 if (parObjTmpBuf) return true;
166 void LinkableMapObj::setUseRelPos (const bool &b)
171 void LinkableMapObj::setRelPos()
175 relPos.setX (absPos.x() - parObj->getChildPos().x() );
176 relPos.setY (absPos.y() - parObj->getChildPos().y() );
177 parObj->calcBBoxSize();
181 void LinkableMapObj::setRelPos(const QPointF &p)
186 parObj->calcBBoxSize();
191 QPointF LinkableMapObj::getRelPos()
193 if (!parObj) return QPointF();
197 qreal LinkableMapObj::getTopPad()
202 qreal LinkableMapObj::getLeftPad()
207 qreal LinkableMapObj::getRightPad()
212 LinkableMapObj::Style LinkableMapObj::getDefLinkStyle ()
216 qWarning ("LMO::getDefLinkStyle model=NULL");
217 return UndefinedStyle;
219 Style ls=model->getMapLinkStyle();
243 return UndefinedStyle;
246 void LinkableMapObj::setLinkStyle(Style newstyle)
248 //if (newstyle=style) return;
253 if (childObj!=NULL && parObj != NULL)
255 QGraphicsLineItem *cl;
262 l = scene->addLine(QLineF(1,1,1,1),pen);
263 l->setZValue(Z_LINK);
270 for (int i=0;i<arcsegs;i++)
272 cl = scene->addLine(QLineF(i*5,0,i*10,100),pen);
273 cl->setZValue(Z_LINK);
280 pa0.resize (arcsegs+1);
283 p =scene->addPolygon(QPolygonF(),pen,linkcolor);
284 p->setZValue(Z_LINK);
292 p = scene->addPolygon(QPolygonF(),pen,linkcolor);
293 p->setZValue(Z_LINK);
298 pa0.resize (arcsegs*2+2);
299 pa1.resize (arcsegs+1);
300 pa2.resize (arcsegs+1);
308 LinkableMapObj::Style LinkableMapObj::getLinkStyle()
313 void LinkableMapObj::setHideLinkUnselected(bool b)
315 hideLinkUnselected=b;
316 setVisibility (visible);
320 bool LinkableMapObj::getHideLinkUnselected()
322 return hideLinkUnselected;
325 void LinkableMapObj::setLinkPos(Position lp)
330 LinkableMapObj::Position LinkableMapObj::getLinkPos()
335 void LinkableMapObj::setID (const QString &s)
340 QString LinkableMapObj::getID()
345 void LinkableMapObj::setLinkColor()
347 // Overloaded in BranchObj and children
348 // here only set default color
350 setLinkColor (model->getMapDefLinkColor());
353 void LinkableMapObj::setLinkColor(QColor col)
357 bottomline->setPen( pen );
364 for (int i=0; i<segment.size(); ++i)
365 segment.at(i)->setPen( pen);
368 p->setBrush( QBrush(col));
372 p->setBrush( QBrush(col));
380 QColor LinkableMapObj::getLinkColor()
385 void LinkableMapObj::setVisibility (bool v)
387 MapObj::setVisibility (v);
390 // We can hide the link, while object is not selected
391 if (hideLinkUnselected && !selected)
403 for (int i=0; i<segment.size(); ++i)
404 segment.at(i)->show();
424 for (int i=0; i<segment.size(); ++i)
425 segment.at(i)->hide();
439 void LinkableMapObj::setOrientation()
441 Orientation orientOld=orientation;
445 orientation=UndefinedOrientation;
449 // Set orientation, first look for orientation of parent
450 if (parObj->getOrientation() != UndefinedOrientation )
451 // use the orientation of the parent:
452 orientation=parObj->getOrientation();
455 // calc orientation depending on position rel to parent
456 if (absPos.x() < QPointF(parObj->getChildPos() ).x() )
457 orientation=LeftOfCenter;
459 orientation=RightOfCenter;
461 if (orientOld!=orientation) requestReposition();
464 void LinkableMapObj::updateLink()
467 // childPos of parent
473 // childPos (by calling setDockPos())
474 // parPos (by calling setDockPos())
476 // drawing of the link itself
478 // updateLink is called from move, but called from constructor we don't
479 // have parents yet...
480 if (style==UndefinedStyle) return;
485 bottomlineY=bbox.top() + bbox.height()/2; // draw link to middle (of frame)
488 bottomlineY=bbox.bottom()-1; // draw link to bottom of box
492 double p2x,p2y; // Set P2 Before setting
495 p2x=QPointF( parObj->getChildPos() ).x(); // P1, we have to look at
496 p2y=QPointF( parObj->getChildPos() ).y(); // orientation
499 p2x=QPointF( parObj->getParPos() ).x();
500 p2y=QPointF( parObj->getParPos() ).y();
503 setDockPos(); // Call overloaded method
506 double p1x=parPos.x(); // Link is drawn from P1 to P2
507 double p1y=parPos.y();
509 double vx=p2x - p1x; // V=P2-P1
512 // Draw the horizontal line below heading (from ChildPos to ParPos)
513 //bottomline->prepareGeometryChange();
514 bottomline->setLine (QLine (qRound(childPos.x()),
515 qRound(childPos.y()),
520 if (vx > -0.000001 && vx < 0.000001)
524 // "turning point" for drawing polygonal links
525 QPointF tp (-qRound(sin (a)*thickness_start), qRound(cos (a)*thickness_start));
531 //l->prepareGeometryChange();
532 l->setLine( QLine(qRound (parPos.x()),
538 parabel (pa0, p1x,p1y,p2x,p2y);
539 for (int i=0; i<segment.size(); ++i)
541 //segment.at(i)->prepareGeometryChange();
542 segment.at(i)->setLine(QLineF( pa0.at(i).x(), pa0.at(i).y(),pa0.at(i+1).x(),pa0.at(i+1).y()));
547 pa0<<QPointF (qRound(p2x+tp.x()), qRound(p2y+tp.y()));
548 pa0<<QPointF (qRound(p2x-tp.x()), qRound(p2y-tp.y()));
549 pa0<<QPointF (qRound (parPos.x()), qRound(parPos.y()) );
550 //p->prepareGeometryChange();
551 p->setPolygon(QPolygonF (pa0));
554 parabel (pa1, p1x,p1y,p2x+tp.x(),p2y+tp.y());
555 parabel (pa2, p1x,p1y,p2x-tp.x(),p2y-tp.y());
557 for (int i=0;i<=arcsegs;i++)
558 pa0 << QPointF (pa1.at(i));
559 for (int i=0;i<=arcsegs;i++)
560 pa0 << QPointF (pa2.at(arcsegs-i));
561 //p->prepareGeometryChange();
562 p->setPolygon(QPolygonF (pa0));
569 LinkableMapObj* LinkableMapObj::getChildObj()
574 LinkableMapObj* LinkableMapObj::getParObj()
579 LinkableMapObj* LinkableMapObj::findObjBySelect (QString s)
581 LinkableMapObj *lmo=this;
585 while (!s.isEmpty() )
587 part=s.section(",",0,0);
589 num=part.right(part.length() - 3);
593 return false; // in a subtree there is no center
598 lmo=((BranchObj*)lmo)->getBranchNum (num.toInt());
601 lmo=((BranchObj*)lmo)->getFloatImageNum (num.toUInt());
605 s=s.right(s.length() - part.length() -1 );
612 QPointF LinkableMapObj::getChildPos()
617 QPointF LinkableMapObj::getParPos()
622 void LinkableMapObj::setUseOrientation (const bool &b)
624 if (useOrientation!=b)
631 LinkableMapObj::Orientation LinkableMapObj::getOrientation()
636 int LinkableMapObj::getDepth()
641 void LinkableMapObj::setMapEditor (MapEditor *me)
644 model=mapEditor->getModel();
647 MapEditor* LinkableMapObj::getMapEditor ()
652 QPointF LinkableMapObj::getRandPos()
654 // Choose a random position with given distance to parent:
655 double a=rand()%360 * 2 * M_PI / 360;
656 return QPointF ( (int)( + 150*cos (a)),
657 (int)( + 150*sin (a)));
660 void LinkableMapObj::reposition()
664 void LinkableMapObj::requestReposition()
666 if (!repositionRequest)
668 // Pass on the request to parental objects, if this hasn't
670 repositionRequest=true;
671 if (parObj) parObj->requestReposition();
675 void LinkableMapObj::forceReposition()
677 // Sometimes a reposition has to be done immediatly: For example
678 // if the note editor flag changes, there is no user event in mapeditor
679 // which could collect requests for a reposition.
680 // Then we have to call forceReposition()
681 // But no rule without exception: While loading a map or undoing it,
682 // we want to block expensive repositioning, but just do it once at
683 // the end, thus check first:
685 if (model->isRepositionBlocked()) return;
687 // Pass on the request to parent objects, if this hasn't been done yet
689 parObj->forceReposition();
694 bool LinkableMapObj::repositionRequested()
696 return repositionRequest;
700 void LinkableMapObj::select()
702 // select and unselect are still needed to
703 // handle hiding of links
705 setVisibility (visible);
709 void LinkableMapObj::unselect()
712 // Maybe we have to hide the link:
713 setVisibility (visible);
716 void LinkableMapObj::parabel (QPolygonF &ya, double p1x, double p1y, double p2x, double p2y)
719 double vx=p2x - p1x; // V=P2-P1
722 double dx; // delta x during calculation of parabel
724 double pnx; // next point
728 if (vx > -0.0001 && vx < 0.0001)
734 ya<<QPointF (p1x,p1y);
735 for (int i=1;i<=arcsegs;i++)
738 pny=m*(pnx-parPos.x())*(pnx-parPos.x())+parPos.y();
739 ya<<QPointF (pnx,pny);
745 QString LinkableMapObj::getLinkAttr ()
747 if (hideLinkUnselected)
748 return attribut ("hideLink","true");
750 return attribut ("hideLink","false");