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()
157 relPos.setX (absPos.x() - parObj->getChildPos().x() );
158 relPos.setY (absPos.y() - parObj->getChildPos().y() );
159 parObj->calcBBoxSize();
162 qWarning ("LMO::setRelPos No parent yet!");
166 void LinkableMapObj::setRelPos(const QPoint &p)
171 parObj->calcBBoxSize();
175 qWarning ("LMO::setRelPos No parent yet!");
179 QPoint LinkableMapObj::getRelPos()
181 if (!parObj) return QPoint();
185 int LinkableMapObj::getTopPad()
190 int LinkableMapObj::getLeftPad()
195 int LinkableMapObj::getRightPad()
200 LinkStyle LinkableMapObj::getDefLinkStyle ()
202 if (!mapEditor) return StyleUndef;
204 LinkStyle ls=mapEditor->getMapLinkStyle();
219 case StylePolyParabel:
231 void LinkableMapObj::setLinkStyle(LinkStyle newstyle)
233 //if (newstyle=style) return;
238 if (childObj!=NULL && parObj != NULL)
248 l = new Q3CanvasLine(canvas);
249 l->setPen( QPen(linkcolor, 1) );
257 for (i=0;i<arcsegs;i++)
259 cl = new Q3CanvasLine(canvas);
260 cl->setPen( QPen(linkcolor, 1) );
261 cl->setPoints( 0,0,i*10,100);
269 pa0.resize (arcsegs+1);
272 p = new Q3CanvasPolygon(canvas);
273 p->setBrush( linkcolor );
280 // TODO a bit awkward: draw the lines additionally to polygon, to avoid
281 // missing pixels, when polygon is extremly flat
282 l = new Q3CanvasLine(canvas);
283 l->setPen( QPen(linkcolor, 1) );
290 case StylePolyParabel:
291 p = new Q3CanvasPolygon(canvas);
292 p->setBrush( linkcolor );
298 pa0.resize (arcsegs*2+2);
299 pa1.resize (arcsegs+1);
300 pa2.resize (arcsegs+1);
302 // TODO a bit awkward: draw the lines additionally
303 // to polygon, to avoid missing pixels,
304 // if polygon is extremly flat
305 for (i=0;i<arcsegs;i++)
307 cl = new Q3CanvasLine(canvas);
308 cl->setPen( QPen(linkcolor, 1) );
309 cl->setPoints( 0,0,i*10,100);
324 LinkStyle LinkableMapObj::getLinkStyle()
329 void LinkableMapObj::setHideLinkUnselected(bool b)
331 hideLinkUnselected=b;
332 setVisibility (visible);
336 bool LinkableMapObj::getHideLinkUnselected()
338 return hideLinkUnselected;
341 void LinkableMapObj::setLinkPos(LinkPos lp)
346 LinkPos LinkableMapObj::getLinkPos()
352 void LinkableMapObj::setLinkColor()
354 // Overloaded in BranchObj and childs
355 // here only set default color
357 setLinkColor (mapEditor->getMapDefLinkColor());
360 void LinkableMapObj::setLinkColor(QColor col)
363 bottomline->setPen( QPen(linkcolor, 1) );
368 l->setPen( QPen(col,1));
371 for (cl=segment.first(); cl; cl=segment.next() )
372 cl->setPen( QPen(col,1));
375 p->setBrush( QBrush(col));
376 l->setPen( QPen(col,1));
378 case StylePolyParabel:
379 p->setBrush( QBrush(col));
380 for (cl=segment.first(); cl; cl=segment.next() )
381 cl->setPen( QPen(col,1));
388 QColor LinkableMapObj::getLinkColor()
393 FrameType LinkableMapObj::getFrameType()
395 return frame->getFrameType();
398 void LinkableMapObj::setFrameType(const FrameType &t)
400 frame->setFrameType(t);
406 void LinkableMapObj::setFrameType(const QString &t)
408 frame->setFrameType(t);
414 void LinkableMapObj::setVisibility (bool v)
417 MapObj::setVisibility (v);
420 // We can hide the link, while object is not selected
421 if (hideLinkUnselected && !selected)
433 for (cl=segment.first(); cl; cl=segment.next() )
440 case StylePolyParabel:
441 for (cl=segment.first(); cl; cl=segment.next() )
457 for (cl=segment.first(); cl; cl=segment.next() )
464 case StylePolyParabel:
465 for (cl=segment.first(); cl; cl=segment.next() )
475 void LinkableMapObj::setOrientation()
477 LinkOrient orientOld=orientation;
481 orientation=OrientUndef;
485 // Set orientation, first look for orientation of parent
486 if (parObj->getOrientation() != OrientUndef )
487 // use the orientation of the parent:
488 orientation=parObj->getOrientation();
491 // calc orientation depending on position rel to parent
492 if (absPos.x() < QPoint(parObj->getChildPos() ).x() )
493 orientation=OrientLeftOfCenter;
495 orientation=OrientRightOfCenter;
497 if (orientOld!=orientation) requestReposition();
500 void LinkableMapObj::updateLink()
503 // childPos of parent
509 // childPos (by calling setDockPos())
510 // parPos (by calling setDockPos())
512 // drawing of the link itself
514 // updateLink is called from move, but called from constructor we don't
515 // have parents yet...
516 if (style==StyleUndef) return;
518 if (frame->getFrameType() == NoFrame)
525 bottomlineY=bbox.top()+bbox.height() /2; // draw link to middle (of frame)
528 bottomlineY=bbox.bottom()-1; // draw link to bottom of box
532 double p2x,p2y; // Set P2 Before setting
535 p2x=QPoint( parObj->getChildPos() ).x(); // P1, we have to look at
536 p2y=QPoint( parObj->getChildPos() ).y(); // orientation
539 p2x=QPoint( parObj->getParPos() ).x();
540 p2y=QPoint( parObj->getParPos() ).y();
543 setDockPos(); // Call overloaded method
546 double p1x=parPos.x(); // Link is drawn from P1 to P2
547 double p1y=parPos.y();
549 double vx=p2x - p1x; // V=P2-P1
552 // Draw the horizontal line below heading (from ChildPos to ParPos)
553 bottomline->setPoints (qRound(childPos.x()),
554 qRound(childPos.y()),
559 if (vx > -0.000001 && vx < 0.000001)
563 // "turning point" for drawing polygonal links
564 QPoint tp (-qRound(sin (a)*thickness_start), qRound(cos (a)*thickness_start));
574 l->setPoints( qRound (parPos.x()),
580 parabel (pa0, p1x,p1y,p2x,p2y);
582 for (cl=segment.first(); cl; cl=segment.next() )
584 cl->setPoints( pa0.point(i).x(), pa0.point(i).y(),pa0.point(i+1).x(),pa0.point(i+1).y());
589 pa0[0]=QPoint (qRound(p2x+tp.x()), qRound(p2y+tp.y()));
590 pa0[1]=QPoint (qRound(p2x-tp.x()), qRound(p2y-tp.y()));
591 pa0[2]=QPoint (qRound (parPos.x()), qRound(parPos.y()) );
593 // here too, draw line to avoid missing pixels
594 l->setPoints( qRound (parPos.x()),
599 case StylePolyParabel:
600 parabel (pa1, p1x,p1y,p2x+tp.x(),p2y+tp.y());
601 parabel (pa2, p1x,p1y,p2x-tp.x(),p2y-tp.y());
602 for (i=0;i<=arcsegs;i++)
604 // Combine the arrays to a single one
606 pa0[i+arcsegs+1]=pa2[arcsegs-i];
610 for (cl=segment.first(); cl; cl=segment.next() )
612 cl->setPoints( pa1.point(i).x(), pa1.point(i).y(),pa1.point(i+1).x(),pa1.point(i+1).y());
621 LinkableMapObj* LinkableMapObj::getChildObj()
626 LinkableMapObj* LinkableMapObj::getParObj()
631 LinkableMapObj* LinkableMapObj::findObjBySelect (QString s)
633 LinkableMapObj *lmo=this;
637 while (!s.isEmpty() )
639 part=s.section(",",0,0);
641 num=part.right(part.length() - 3);
645 return false; // in a subtree there is no center
650 lmo=((BranchObj*)(lmo))->getBranchNum (num.toUInt());
653 lmo=((BranchObj*)(lmo))->getFloatImageNum (num.toUInt());
657 s=s.right(s.length() - part.length() -1 );
664 QPoint LinkableMapObj::getChildPos()
669 QPoint LinkableMapObj::getParPos()
674 void LinkableMapObj::setUseOrientation (const bool &b)
676 if (useOrientation!=b)
683 LinkOrient LinkableMapObj::getOrientation()
688 int LinkableMapObj::getDepth()
693 void LinkableMapObj::setMapEditor (MapEditor *me)
698 MapEditor* LinkableMapObj::getMapEditor ()
703 QPoint LinkableMapObj::getRandPos()
705 // Choose a random position with given distance to parent:
706 double a=rand()%360 * 2 * M_PI / 360;
707 return QPoint ( (int)( + 150*cos (a)),
708 (int)( + 150*sin (a)));
712 void LinkableMapObj::alignRelativeTo (QPoint ref)
714 // Overloaded, only called for BO, FIO, ...
719 void LinkableMapObj::reposition()
721 cout << "LMO::reposition\n";
722 // FIXME not needed? Is overloaded in BranchObj...
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 cout << " reposition to abs "<<absPos<<endl;
737 alignRelativeTo ( QPoint (absPos.x(),
738 absPos.y()-(bboxTotal.height()-bbox.height())/2) );
743 void LinkableMapObj::requestReposition()
745 if (!repositionRequest)
747 // Pass on the request to parental objects, if this hasn't
749 repositionRequest=true;
750 if (parObj) parObj->requestReposition();
754 void LinkableMapObj::forceReposition()
756 // Sometimes a reposition has to be done immediatly: For example
757 // if the note editor flag changes, there is no user event in mapeditor
758 // which could collect requests for a reposition.
759 // Then we have to call forceReposition()
760 // But no rule without exception: While loading a map or undoing it,
761 // we want to block expensive repositioning, but just do it once at
762 // the end, thus check first:
764 if (mapEditor->isRepositionBlocked()) return;
766 // Pass on the request to parental objects, if this hasn't been done yet
769 parObj->forceReposition();
774 bool LinkableMapObj::repositionRequested()
776 return repositionRequest;
780 void LinkableMapObj::setSelBox()
782 selbox->setX (clickBox.x() );
783 selbox->setY (clickBox.y() );
784 selbox->setSize (clickBox.width(), clickBox.height() );
787 void LinkableMapObj::select()
793 setVisibility (visible);
797 void LinkableMapObj::unselect()
801 // Maybe we have to hide the link:
802 setVisibility (visible);
805 void LinkableMapObj::parabel (Q3PointArray &ya, double p1x, double p1y, double p2x, double p2y)
808 double vx=p2x - p1x; // V=P2-P1
811 double dx; // delta x during calculation of parabel
813 double pnx; // next point
817 if (vx > -0.0001 && vx < 0.0001)
823 ya.setPoint (0,QPoint (qRound(p1x),qRound(p1y)));
824 for (i=1;i<=arcsegs;i++)
827 pny=m*(pnx-parPos.x())*(pnx-parPos.x())+parPos.y();
828 ya.setPoint (i,QPoint (qRound(pnx),qRound(pny)));
834 QString LinkableMapObj::getLinkAttr ()
836 if (hideLinkUnselected)
837 return attribut ("hideLink","true");
839 return attribut ("hideLink","false");