3 #include "linkablemapobj.h"
10 /////////////////////////////////////////////////////////////////
12 /////////////////////////////////////////////////////////////////
14 LinkableMapObj::LinkableMapObj():MapObj()
16 // cout << "Const LinkableMapObj ()\n";
20 LinkableMapObj::LinkableMapObj(QCanvas* c) :MapObj(c)
22 // cout << "Const LinkableMapObj\n";
26 LinkableMapObj::LinkableMapObj (LinkableMapObj* lmo) : MapObj (lmo->canvas)
31 LinkableMapObj::~LinkableMapObj()
39 void LinkableMapObj::delLink()
53 case StylePolyParabel:
62 void LinkableMapObj::init ()
73 orientation=OrientUndef;
78 segment.setAutoDelete (TRUE);
80 QPointArray pa(arcsegs*2+2);
82 bottomline=new QCanvasLine(canvas);
83 bottomline->setPen( QPen(linkcolor, 1) );
84 bottomline->setZ(Z_LINK);
87 // Prepare showing the selection of a MapObj
88 selbox = new QCanvasRectangle (canvas);
89 selbox->setZ(Z_SELBOX);
90 selbox->setBrush( QColor(255,255,0) );
91 selbox->setPen( QPen(QColor(255,255,0) ));
95 hideLinkUnselected=false;
97 topPad=botPad=leftPad=rightPad=0;
100 frame = new FrameObj (canvas);
102 repositionRequest=false;
110 void LinkableMapObj::copy (LinkableMapObj* other)
113 bboxTotal=other->bboxTotal;
114 setLinkStyle(other->style);
115 setLinkColor (other->linkcolor);
116 relPos=other->relPos;
117 useOrientation=other->useOrientation;
121 void LinkableMapObj::setChildObj(LinkableMapObj* o)
126 void LinkableMapObj::setParObj(LinkableMapObj* o)
129 mapEditor=parObj->getMapEditor();
132 void LinkableMapObj::setParObjTmp(LinkableMapObj*,QPoint,int)
136 void LinkableMapObj::unsetParObjTmp()
140 bool LinkableMapObj::hasParObjTmp()
142 if (parObjTmpBuf) return true;
146 void LinkableMapObj::setUseRelPos (const bool &b)
151 void LinkableMapObj::setRelPos()
155 relPos.setX (absPos.x() - parObj->getChildPos().x() );
156 relPos.setY (absPos.y() - parObj->getChildPos().y() );
158 parObj->calcBBoxSize();
159 parObj->requestReposition();
163 void LinkableMapObj::setRelPos(const QPoint &p)
167 { parObj->calcBBoxSize();
168 parObj->requestReposition();
172 int LinkableMapObj::getTopPad()
177 int LinkableMapObj::getLeftPad()
182 int LinkableMapObj::getRightPad()
187 LinkStyle LinkableMapObj::getDefLinkStyle ()
189 if (!mapEditor) return StyleUndef;
191 LinkStyle ls=mapEditor->getLinkStyle();
206 case StylePolyParabel:
218 void LinkableMapObj::setLinkStyle(LinkStyle newstyle)
220 //if (newstyle=style) return;
225 if (childObj!=NULL && parObj != NULL)
235 l = new QCanvasLine(canvas);
236 l->setPen( QPen(linkcolor, 1) );
244 for (i=0;i<arcsegs;i++)
246 cl = new QCanvasLine(canvas);
247 cl->setPen( QPen(linkcolor, 1) );
248 cl->setPoints( 0,0,i*10,100);
256 pa0.resize (arcsegs+1);
259 p = new QCanvasPolygon(canvas);
260 p->setBrush( linkcolor );
267 // TODO a bit awkward: draw the lines additionally to polygon, to avoid
268 // missing pixels, when polygon is extremly flat
269 l = new QCanvasLine(canvas);
270 l->setPen( QPen(linkcolor, 1) );
277 case StylePolyParabel:
278 p = new QCanvasPolygon(canvas);
279 p->setBrush( linkcolor );
285 pa0.resize (arcsegs*2+2);
286 pa1.resize (arcsegs+1);
287 pa2.resize (arcsegs+1);
289 // TODO a bit awkward: draw the lines additionally
290 // to polygon, to avoid missing pixels,
291 // if polygon is extremly flat
292 for (i=0;i<arcsegs;i++)
294 cl = new QCanvasLine(canvas);
295 cl->setPen( QPen(linkcolor, 1) );
296 cl->setPoints( 0,0,i*10,100);
311 LinkStyle LinkableMapObj::getLinkStyle()
316 void LinkableMapObj::setHideLinkUnselected(bool b)
318 hideLinkUnselected=b;
319 setVisibility (visible);
323 bool LinkableMapObj::getHideLinkUnselected()
325 return hideLinkUnselected;
328 void LinkableMapObj::setLinkPos(LinkPos lp)
333 LinkPos LinkableMapObj::getLinkPos()
339 void LinkableMapObj::setLinkColor()
341 // Overloaded in BranchObj and childs
342 // here only set default color
344 setLinkColor (mapEditor->getDefLinkColor());
347 void LinkableMapObj::setLinkColor(QColor col)
350 bottomline->setPen( QPen(linkcolor, 1) );
355 l->setPen( QPen(col,1));
358 for (cl=segment.first(); cl; cl=segment.next() )
359 cl->setPen( QPen(col,1));
362 p->setBrush( QBrush(col));
363 l->setPen( QPen(col,1));
365 case StylePolyParabel:
366 p->setBrush( QBrush(col));
367 for (cl=segment.first(); cl; cl=segment.next() )
368 cl->setPen( QPen(col,1));
375 QColor LinkableMapObj::getLinkColor()
380 FrameType LinkableMapObj::getFrameType()
382 return frame->getFrameType();
385 void LinkableMapObj::setFrameType(const FrameType &t)
387 frame->setFrameType(t);
393 void LinkableMapObj::setFrameType(const QString &t)
395 frame->setFrameType(t);
401 void LinkableMapObj::setVisibility (bool v)
404 MapObj::setVisibility (v);
406 if (hideLinkUnselected && !selected)
418 for (cl=segment.first(); cl; cl=segment.next() )
425 case StylePolyParabel:
426 for (cl=segment.first(); cl; cl=segment.next() )
442 for (cl=segment.first(); cl; cl=segment.next() )
449 case StylePolyParabel:
450 for (cl=segment.first(); cl; cl=segment.next() )
460 void LinkableMapObj::updateLink()
463 // childPos of parent
469 // childPos (by calling setDockPos())
470 // parPos (by calling setDockPos())
472 // drawing of the link itself
474 // updateLink is called from move, but called from constructor we don't
475 // have parents yet...
476 if (style==StyleUndef) return;
478 if (frame->getFrameType() == NoFrame)
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=QPoint( parObj->getChildPos() ).x(); // P1, we have to look at
496 p2y=QPoint( parObj->getChildPos() ).y(); // orientation
499 p2x=QPoint( parObj->getParPos() ).x();
500 p2y=QPoint( parObj->getParPos() ).y();
503 LinkOrient orientOld=orientation;
505 // Set orientation, first look for orientation of parent
506 if (parObj->getOrientation() != OrientUndef )
507 // use the orientation of the parent:
508 orientation=parObj->getOrientation();
511 // calc orientation depending on position rel to mapCenter
512 if (absPos.x() < QPoint(parObj->getChildPos() ).x() )
513 orientation=OrientLeftOfCenter;
515 orientation=OrientRightOfCenter;
518 if ((orientation!=orientOld) && (orientOld!= OrientUndef))
520 // Orientation just changed. Reorient this subbranch, because move is called
521 // before updateLink => Position is still the old one, which could lead to
522 // linking of subranch to itself => segfault
524 // Also possible: called in BranchObj::init(), then orientOld==OrientUndef,
525 // no need to reposition now
531 double p1x=parPos.x(); // Link is drawn from P1 to P2
532 double p1y=parPos.y();
534 double vx=p2x - p1x; // V=P2-P1
537 // Draw the horizontal line below heading (from ChildPos to ParPos)
538 bottomline->setPoints (qRound(childPos.x()),
539 qRound(childPos.y()),
544 if (vx > -0.000001 && vx < 0.000001)
548 // "turning point" for drawing polygonal links
549 QPoint tp (-qRound(sin (a)*thickness_start), qRound(cos (a)*thickness_start));
559 l->setPoints( qRound (parPos.x()),
565 parabel (pa0, p1x,p1y,p2x,p2y);
567 for (cl=segment.first(); cl; cl=segment.next() )
569 cl->setPoints( pa0.point(i).x(), pa0.point(i).y(),pa0.point(i+1).x(),pa0.point(i+1).y());
574 pa0[0]=QPoint (qRound(p2x+tp.x()), qRound(p2y+tp.y()));
575 pa0[1]=QPoint (qRound(p2x-tp.x()), qRound(p2y-tp.y()));
576 pa0[2]=QPoint (qRound (parPos.x()), qRound(parPos.y()) );
578 // here too, draw line to avoid missing pixels
579 l->setPoints( qRound (parPos.x()),
584 case StylePolyParabel:
585 parabel (pa1, p1x,p1y,p2x+tp.x(),p2y+tp.y());
586 parabel (pa2, p1x,p1y,p2x-tp.x(),p2y-tp.y());
587 for (i=0;i<=arcsegs;i++)
589 // Combine the arrays to a single one
591 pa0[i+arcsegs+1]=pa2[arcsegs-i];
595 for (cl=segment.first(); cl; cl=segment.next() )
597 cl->setPoints( pa1.point(i).x(), pa1.point(i).y(),pa1.point(i+1).x(),pa1.point(i+1).y());
606 LinkableMapObj* LinkableMapObj::getChildObj()
611 LinkableMapObj* LinkableMapObj::getParObj()
616 LinkableMapObj* LinkableMapObj::findObjBySelect (QString s)
618 LinkableMapObj *lmo=this;
622 while (!s.isEmpty() )
624 part=s.section(",",0,0);
626 num=part.right(part.length() - 3);
630 return false; // in a subtree there is no center
635 lmo=((BranchObj*)(lmo))->getBranchNum (num.toUInt());
638 lmo=((BranchObj*)(lmo))->getFloatImageNum (num.toUInt());
642 s=s.right(s.length() - part.length() -1 );
649 void LinkableMapObj::setDockPos()
651 cout <<"LMO::setDockPos()\n";
654 QPoint LinkableMapObj::getChildPos()
659 QPoint LinkableMapObj::getParPos()
664 QPoint LinkableMapObj::getRelPos()
667 /* FIXME not needed? relPos was moved in 1.7.10 from
668 floatobj to linkablemapobj. Before we had:
670 if (!parObj) return QPoint (0,0);
672 absPos.x() - parObj->x(),
673 absPos.y() - parObj->y()
679 void LinkableMapObj::setUseOrientation (const bool &b)
681 if (useOrientation!=b)
688 LinkOrient LinkableMapObj::getOrientation()
693 int LinkableMapObj::getDepth()
698 void LinkableMapObj::setMapEditor (MapEditor *me)
703 MapEditor* LinkableMapObj::getMapEditor ()
708 QPoint LinkableMapObj::getRandPos()
710 // Choose a random position with given distance to parent:
711 double a=rand()%360 * 2 * M_PI / 360;
712 return QPoint ( (int)( + 150*cos (a)),
713 (int)( + 150*sin (a)));
716 void LinkableMapObj::alignRelativeTo (QPoint ref)
718 // FIXME testing, seems not to be used right now...
719 cout << "LMO::alignRelTo ref="<<ref<<endl;
722 void LinkableMapObj::reposition()
726 // only calculate the sizes once. If the deepest LMO changes its height,
727 // all upper LMOs have to change, too.
728 calcBBoxSizeWithChilds();
730 alignRelativeTo ( QPoint (absPos.x(),
731 absPos.y()-(bboxTotal.height()-bbox.height())/2) );
734 // This is only important for moving branches:
735 // For editing a branch it isn't called...
736 alignRelativeTo ( QPoint (absPos.x(),
737 absPos.y()-(bboxTotal.height()-bbox.height())/2) );
741 void LinkableMapObj::requestReposition()
743 if (!repositionRequest)
745 // Pass on the request to parental objects, if this hasn't
747 repositionRequest=true;
748 if (parObj) parObj->requestReposition();
752 void LinkableMapObj::forceReposition()
754 // Sometimes a reposition has to be done immediatly: For example
755 // if the note editor flag changes, there is no user event in mapeditor
756 // which could collect requests for a reposition.
757 // Then we have to call forceReposition()
758 // But no rule without exception: While loading a map or undoing it,
759 // we want to block expensive repositioning, but just do it once at
760 // the end, thus check first:
762 if (mapEditor->isRepositionBlocked()) return;
764 // Pass on the request to parental objects, if this hasn't been done yet
767 parObj->forceReposition();
772 bool LinkableMapObj::repositionRequested()
774 return repositionRequest;
778 void LinkableMapObj::setSelBox()
780 selbox->setX (clickBox.x() );
781 selbox->setY (clickBox.y() );
782 selbox->setSize (clickBox.width(), clickBox.height() );
785 void LinkableMapObj::select()
790 setVisibility (visible);
794 void LinkableMapObj::unselect()
798 setVisibility (visible);
801 void LinkableMapObj::parabel (QPointArray &ya, double p1x, double p1y, double p2x, double p2y)
804 double vx=p2x - p1x; // V=P2-P1
807 double dx; // delta x during calculation of parabel
809 double pnx; // next point
813 if (vx > -0.0001 && vx < 0.0001)
819 ya.setPoint (0,QPoint (qRound(p1x),qRound(p1y)));
820 for (i=1;i<=arcsegs;i++)
823 pny=m*(pnx-parPos.x())*(pnx-parPos.x())+parPos.y();
824 ya.setPoint (i,QPoint (qRound(pnx),qRound(pny)));
830 QString LinkableMapObj::getLinkAttr ()
832 if (hideLinkUnselected)
833 return attribut ("hideLink","true");
835 return attribut ("hideLink","false");