More undo/redo commands. Undo debug output still enabled
3 #include "linkablemapobj.h"
9 #include <Q3PointArray>
12 /////////////////////////////////////////////////////////////////
14 /////////////////////////////////////////////////////////////////
16 LinkableMapObj::LinkableMapObj():MapObj()
18 // cout << "Const LinkableMapObj ()\n";
22 LinkableMapObj::LinkableMapObj(Q3Canvas* c) :MapObj(c)
24 // cout << "Const LinkableMapObj\n";
28 LinkableMapObj::LinkableMapObj (LinkableMapObj* lmo) : MapObj (lmo->canvas)
33 LinkableMapObj::~LinkableMapObj()
41 void LinkableMapObj::delLink()
55 case StylePolyParabel:
64 void LinkableMapObj::init ()
75 orientation=OrientUndef;
80 segment.setAutoDelete (TRUE);
82 Q3PointArray pa(arcsegs*2+2);
84 bottomline=new Q3CanvasLine(canvas);
85 bottomline->setPen( QPen(linkcolor, 1) );
86 bottomline->setZ(Z_LINK);
89 // Prepare showing the selection of a MapObj
90 selbox = new Q3CanvasRectangle (canvas);
91 selbox->setZ(Z_SELBOX);
92 selbox->setBrush( QColor(255,255,0) );
93 selbox->setPen( QPen(QColor(255,255,0) ));
97 hideLinkUnselected=false;
99 topPad=botPad=leftPad=rightPad=0;
102 frame = new FrameObj (canvas);
104 repositionRequest=false;
112 void LinkableMapObj::copy (LinkableMapObj* other)
115 bboxTotal=other->bboxTotal;
116 setLinkStyle(other->style);
117 setLinkColor (other->linkcolor);
118 relPos=other->relPos;
119 useOrientation=other->useOrientation;
123 void LinkableMapObj::setChildObj(LinkableMapObj* o)
128 void LinkableMapObj::setParObj(LinkableMapObj* o)
131 mapEditor=parObj->getMapEditor();
134 void LinkableMapObj::setParObjTmp(LinkableMapObj*,QPoint,int)
138 void LinkableMapObj::unsetParObjTmp()
142 bool LinkableMapObj::hasParObjTmp()
144 if (parObjTmpBuf) return true;
148 void LinkableMapObj::setUseRelPos (const bool &b)
153 void LinkableMapObj::setRelPos()
159 parObj->calcBBoxSize();
160 parObj->requestReposition();
164 void LinkableMapObj::setRelPos(const QPoint &p)
168 { parObj->calcBBoxSize();
169 parObj->requestReposition();
173 QPoint LinkableMapObj::getRelPos()
175 if (!parObj) return QPoint();
176 relPos.setX (absPos.x() - parObj->getChildPos().x() );
177 relPos.setY (absPos.y() - parObj->getChildPos().y() );
181 int LinkableMapObj::getTopPad()
186 int LinkableMapObj::getLeftPad()
191 int LinkableMapObj::getRightPad()
196 LinkStyle LinkableMapObj::getDefLinkStyle ()
198 if (!mapEditor) return StyleUndef;
200 LinkStyle ls=mapEditor->getLinkStyle();
215 case StylePolyParabel:
227 void LinkableMapObj::setLinkStyle(LinkStyle newstyle)
229 //if (newstyle=style) return;
234 if (childObj!=NULL && parObj != NULL)
244 l = new Q3CanvasLine(canvas);
245 l->setPen( QPen(linkcolor, 1) );
253 for (i=0;i<arcsegs;i++)
255 cl = new Q3CanvasLine(canvas);
256 cl->setPen( QPen(linkcolor, 1) );
257 cl->setPoints( 0,0,i*10,100);
265 pa0.resize (arcsegs+1);
268 p = new Q3CanvasPolygon(canvas);
269 p->setBrush( linkcolor );
276 // TODO a bit awkward: draw the lines additionally to polygon, to avoid
277 // missing pixels, when polygon is extremly flat
278 l = new Q3CanvasLine(canvas);
279 l->setPen( QPen(linkcolor, 1) );
286 case StylePolyParabel:
287 p = new Q3CanvasPolygon(canvas);
288 p->setBrush( linkcolor );
294 pa0.resize (arcsegs*2+2);
295 pa1.resize (arcsegs+1);
296 pa2.resize (arcsegs+1);
298 // TODO a bit awkward: draw the lines additionally
299 // to polygon, to avoid missing pixels,
300 // if polygon is extremly flat
301 for (i=0;i<arcsegs;i++)
303 cl = new Q3CanvasLine(canvas);
304 cl->setPen( QPen(linkcolor, 1) );
305 cl->setPoints( 0,0,i*10,100);
320 LinkStyle LinkableMapObj::getLinkStyle()
325 void LinkableMapObj::setHideLinkUnselected(bool b)
327 hideLinkUnselected=b;
328 setVisibility (visible);
332 bool LinkableMapObj::getHideLinkUnselected()
334 return hideLinkUnselected;
337 void LinkableMapObj::setLinkPos(LinkPos lp)
342 LinkPos LinkableMapObj::getLinkPos()
348 void LinkableMapObj::setLinkColor()
350 // Overloaded in BranchObj and childs
351 // here only set default color
353 setLinkColor (mapEditor->getDefLinkColor());
356 void LinkableMapObj::setLinkColor(QColor col)
359 bottomline->setPen( QPen(linkcolor, 1) );
364 l->setPen( QPen(col,1));
367 for (cl=segment.first(); cl; cl=segment.next() )
368 cl->setPen( QPen(col,1));
371 p->setBrush( QBrush(col));
372 l->setPen( QPen(col,1));
374 case StylePolyParabel:
375 p->setBrush( QBrush(col));
376 for (cl=segment.first(); cl; cl=segment.next() )
377 cl->setPen( QPen(col,1));
384 QColor LinkableMapObj::getLinkColor()
389 FrameType LinkableMapObj::getFrameType()
391 return frame->getFrameType();
394 void LinkableMapObj::setFrameType(const FrameType &t)
396 frame->setFrameType(t);
402 void LinkableMapObj::setFrameType(const QString &t)
404 frame->setFrameType(t);
410 void LinkableMapObj::setVisibility (bool v)
413 MapObj::setVisibility (v);
416 // We can hide the link, while object is not selected
417 if (hideLinkUnselected && !selected)
429 for (cl=segment.first(); cl; cl=segment.next() )
436 case StylePolyParabel:
437 for (cl=segment.first(); cl; cl=segment.next() )
453 for (cl=segment.first(); cl; cl=segment.next() )
460 case StylePolyParabel:
461 for (cl=segment.first(); cl; cl=segment.next() )
471 void LinkableMapObj::updateLink()
474 // childPos of parent
480 // childPos (by calling setDockPos())
481 // parPos (by calling setDockPos())
483 // drawing of the link itself
485 // updateLink is called from move, but called from constructor we don't
486 // have parents yet...
487 if (style==StyleUndef) return;
489 if (frame->getFrameType() == NoFrame)
496 bottomlineY=bbox.top()+bbox.height() /2; // draw link to middle (of frame)
499 bottomlineY=bbox.bottom()-1; // draw link to bottom of box
503 double p2x,p2y; // Set P2 Before setting
506 p2x=QPoint( parObj->getChildPos() ).x(); // P1, we have to look at
507 p2y=QPoint( parObj->getChildPos() ).y(); // orientation
510 p2x=QPoint( parObj->getParPos() ).x();
511 p2y=QPoint( parObj->getParPos() ).y();
514 LinkOrient orientOld=orientation;
516 // Set orientation, first look for orientation of parent
517 if (parObj->getOrientation() != OrientUndef )
518 // use the orientation of the parent:
519 orientation=parObj->getOrientation();
522 // calc orientation depending on position rel to mapCenter
523 if (absPos.x() < QPoint(parObj->getChildPos() ).x() )
524 orientation=OrientLeftOfCenter;
526 orientation=OrientRightOfCenter;
529 if ((orientation!=orientOld) && (orientOld!= OrientUndef))
531 // Orientation just changed. Reorient this subbranch, because move is called
532 // before updateLink => Position is still the old one, which could lead to
533 // linking of subranch to itself => segfault
535 // Also possible: called in BranchObj::init(), then orientOld==OrientUndef,
536 // no need to reposition now
542 double p1x=parPos.x(); // Link is drawn from P1 to P2
543 double p1y=parPos.y();
545 double vx=p2x - p1x; // V=P2-P1
548 // Draw the horizontal line below heading (from ChildPos to ParPos)
549 bottomline->setPoints (qRound(childPos.x()),
550 qRound(childPos.y()),
555 if (vx > -0.000001 && vx < 0.000001)
559 // "turning point" for drawing polygonal links
560 QPoint tp (-qRound(sin (a)*thickness_start), qRound(cos (a)*thickness_start));
570 l->setPoints( qRound (parPos.x()),
576 parabel (pa0, p1x,p1y,p2x,p2y);
578 for (cl=segment.first(); cl; cl=segment.next() )
580 cl->setPoints( pa0.point(i).x(), pa0.point(i).y(),pa0.point(i+1).x(),pa0.point(i+1).y());
585 pa0[0]=QPoint (qRound(p2x+tp.x()), qRound(p2y+tp.y()));
586 pa0[1]=QPoint (qRound(p2x-tp.x()), qRound(p2y-tp.y()));
587 pa0[2]=QPoint (qRound (parPos.x()), qRound(parPos.y()) );
589 // here too, draw line to avoid missing pixels
590 l->setPoints( qRound (parPos.x()),
595 case StylePolyParabel:
596 parabel (pa1, p1x,p1y,p2x+tp.x(),p2y+tp.y());
597 parabel (pa2, p1x,p1y,p2x-tp.x(),p2y-tp.y());
598 for (i=0;i<=arcsegs;i++)
600 // Combine the arrays to a single one
602 pa0[i+arcsegs+1]=pa2[arcsegs-i];
606 for (cl=segment.first(); cl; cl=segment.next() )
608 cl->setPoints( pa1.point(i).x(), pa1.point(i).y(),pa1.point(i+1).x(),pa1.point(i+1).y());
617 LinkableMapObj* LinkableMapObj::getChildObj()
622 LinkableMapObj* LinkableMapObj::getParObj()
627 LinkableMapObj* LinkableMapObj::findObjBySelect (QString s)
629 LinkableMapObj *lmo=this;
633 while (!s.isEmpty() )
635 part=s.section(",",0,0);
637 num=part.right(part.length() - 3);
641 return false; // in a subtree there is no center
646 lmo=((BranchObj*)(lmo))->getBranchNum (num.toUInt());
649 lmo=((BranchObj*)(lmo))->getFloatImageNum (num.toUInt());
653 s=s.right(s.length() - part.length() -1 );
660 void LinkableMapObj::setDockPos()
662 cout <<"LMO::setDockPos()\n";
665 QPoint LinkableMapObj::getChildPos()
670 QPoint LinkableMapObj::getParPos()
675 void LinkableMapObj::setUseOrientation (const bool &b)
677 if (useOrientation!=b)
684 LinkOrient LinkableMapObj::getOrientation()
689 int LinkableMapObj::getDepth()
694 void LinkableMapObj::setMapEditor (MapEditor *me)
699 MapEditor* LinkableMapObj::getMapEditor ()
704 QPoint LinkableMapObj::getRandPos()
706 // Choose a random position with given distance to parent:
707 double a=rand()%360 * 2 * M_PI / 360;
708 return QPoint ( (int)( + 150*cos (a)),
709 (int)( + 150*sin (a)));
712 void LinkableMapObj::alignRelativeTo (QPoint ref)
714 // FIXME testing, seems not to be used right now...
715 cout << "LMO::alignRelTo ref="<<ref<<endl;
718 void LinkableMapObj::reposition()
722 // only calculate the sizes once. If the deepest LMO changes its height,
723 // all upper LMOs have to change, too.
724 calcBBoxSizeWithChilds();
726 alignRelativeTo ( QPoint (absPos.x(),
727 absPos.y()-(bboxTotal.height()-bbox.height())/2) );
730 // This is only important for moving branches:
731 // For editing a branch it isn't called...
732 alignRelativeTo ( QPoint (absPos.x(),
733 absPos.y()-(bboxTotal.height()-bbox.height())/2) );
737 void LinkableMapObj::requestReposition()
739 if (!repositionRequest)
741 // Pass on the request to parental objects, if this hasn't
743 repositionRequest=true;
744 if (parObj) parObj->requestReposition();
748 void LinkableMapObj::forceReposition()
750 // Sometimes a reposition has to be done immediatly: For example
751 // if the note editor flag changes, there is no user event in mapeditor
752 // which could collect requests for a reposition.
753 // Then we have to call forceReposition()
754 // But no rule without exception: While loading a map or undoing it,
755 // we want to block expensive repositioning, but just do it once at
756 // the end, thus check first:
758 if (mapEditor->isRepositionBlocked()) return;
760 // Pass on the request to parental objects, if this hasn't been done yet
763 parObj->forceReposition();
768 bool LinkableMapObj::repositionRequested()
770 return repositionRequest;
774 void LinkableMapObj::setSelBox()
776 selbox->setX (clickBox.x() );
777 selbox->setY (clickBox.y() );
778 selbox->setSize (clickBox.width(), clickBox.height() );
781 void LinkableMapObj::select()
787 setVisibility (visible);
791 void LinkableMapObj::unselect()
795 // Maybe we have to hide the link:
796 setVisibility (visible);
799 void LinkableMapObj::parabel (Q3PointArray &ya, double p1x, double p1y, double p2x, double p2y)
802 double vx=p2x - p1x; // V=P2-P1
805 double dx; // delta x during calculation of parabel
807 double pnx; // next point
811 if (vx > -0.0001 && vx < 0.0001)
817 ya.setPoint (0,QPoint (qRound(p1x),qRound(p1y)));
818 for (i=1;i<=arcsegs;i++)
821 pny=m*(pnx-parPos.x())*(pnx-parPos.x())+parPos.y();
822 ya.setPoint (i,QPoint (qRound(pnx),qRound(pny)));
828 QString LinkableMapObj::getLinkAttr ()
830 if (hideLinkUnselected)
831 return attribut ("hideLink","true");
833 return attribut ("hideLink","false");