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()
158 parObj->calcBBoxSize();
162 void LinkableMapObj::setRelPos(const QPoint &p)
167 parObj->calcBBoxSize();
172 QPoint LinkableMapObj::getRelPos()
174 if (!parObj) return QPoint();
175 relPos.setX (absPos.x() - parObj->getChildPos().x() );
176 relPos.setY (absPos.y() - parObj->getChildPos().y() );
180 int LinkableMapObj::getTopPad()
185 int LinkableMapObj::getLeftPad()
190 int LinkableMapObj::getRightPad()
195 LinkStyle LinkableMapObj::getDefLinkStyle ()
197 if (!mapEditor) return StyleUndef;
199 LinkStyle ls=mapEditor->getLinkStyle();
214 case StylePolyParabel:
226 void LinkableMapObj::setLinkStyle(LinkStyle newstyle)
228 //if (newstyle=style) return;
233 if (childObj!=NULL && parObj != NULL)
243 l = new Q3CanvasLine(canvas);
244 l->setPen( QPen(linkcolor, 1) );
252 for (i=0;i<arcsegs;i++)
254 cl = new Q3CanvasLine(canvas);
255 cl->setPen( QPen(linkcolor, 1) );
256 cl->setPoints( 0,0,i*10,100);
264 pa0.resize (arcsegs+1);
267 p = new Q3CanvasPolygon(canvas);
268 p->setBrush( linkcolor );
275 // TODO a bit awkward: draw the lines additionally to polygon, to avoid
276 // missing pixels, when polygon is extremly flat
277 l = new Q3CanvasLine(canvas);
278 l->setPen( QPen(linkcolor, 1) );
285 case StylePolyParabel:
286 p = new Q3CanvasPolygon(canvas);
287 p->setBrush( linkcolor );
293 pa0.resize (arcsegs*2+2);
294 pa1.resize (arcsegs+1);
295 pa2.resize (arcsegs+1);
297 // TODO a bit awkward: draw the lines additionally
298 // to polygon, to avoid missing pixels,
299 // if polygon is extremly flat
300 for (i=0;i<arcsegs;i++)
302 cl = new Q3CanvasLine(canvas);
303 cl->setPen( QPen(linkcolor, 1) );
304 cl->setPoints( 0,0,i*10,100);
319 LinkStyle LinkableMapObj::getLinkStyle()
324 void LinkableMapObj::setHideLinkUnselected(bool b)
326 hideLinkUnselected=b;
327 setVisibility (visible);
331 bool LinkableMapObj::getHideLinkUnselected()
333 return hideLinkUnselected;
336 void LinkableMapObj::setLinkPos(LinkPos lp)
341 LinkPos LinkableMapObj::getLinkPos()
347 void LinkableMapObj::setLinkColor()
349 // Overloaded in BranchObj and childs
350 // here only set default color
352 setLinkColor (mapEditor->getDefLinkColor());
355 void LinkableMapObj::setLinkColor(QColor col)
358 bottomline->setPen( QPen(linkcolor, 1) );
363 l->setPen( QPen(col,1));
366 for (cl=segment.first(); cl; cl=segment.next() )
367 cl->setPen( QPen(col,1));
370 p->setBrush( QBrush(col));
371 l->setPen( QPen(col,1));
373 case StylePolyParabel:
374 p->setBrush( QBrush(col));
375 for (cl=segment.first(); cl; cl=segment.next() )
376 cl->setPen( QPen(col,1));
383 QColor LinkableMapObj::getLinkColor()
388 FrameType LinkableMapObj::getFrameType()
390 return frame->getFrameType();
393 void LinkableMapObj::setFrameType(const FrameType &t)
395 frame->setFrameType(t);
401 void LinkableMapObj::setFrameType(const QString &t)
403 frame->setFrameType(t);
409 void LinkableMapObj::setVisibility (bool v)
412 MapObj::setVisibility (v);
415 // We can hide the link, while object is not selected
416 if (hideLinkUnselected && !selected)
428 for (cl=segment.first(); cl; cl=segment.next() )
435 case StylePolyParabel:
436 for (cl=segment.first(); cl; cl=segment.next() )
452 for (cl=segment.first(); cl; cl=segment.next() )
459 case StylePolyParabel:
460 for (cl=segment.first(); cl; cl=segment.next() )
470 void LinkableMapObj::setOrientation()
472 LinkOrient orientOld=orientation;
476 orientation=OrientUndef;
480 // Set orientation, first look for orientation of parent
481 if (parObj->getOrientation() != OrientUndef )
482 // use the orientation of the parent:
483 orientation=parObj->getOrientation();
486 // calc orientation depending on position rel to parent
487 if (absPos.x() < QPoint(parObj->getChildPos() ).x() )
488 orientation=OrientLeftOfCenter;
490 orientation=OrientRightOfCenter;
492 if (orientOld!=orientation) requestReposition();
495 void LinkableMapObj::updateLink()
498 // childPos of parent
504 // childPos (by calling setDockPos())
505 // parPos (by calling setDockPos())
507 // drawing of the link itself
509 // updateLink is called from move, but called from constructor we don't
510 // have parents yet...
511 if (style==StyleUndef) return;
513 if (frame->getFrameType() == NoFrame)
520 bottomlineY=bbox.top()+bbox.height() /2; // draw link to middle (of frame)
523 bottomlineY=bbox.bottom()-1; // draw link to bottom of box
527 double p2x,p2y; // Set P2 Before setting
530 p2x=QPoint( parObj->getChildPos() ).x(); // P1, we have to look at
531 p2y=QPoint( parObj->getChildPos() ).y(); // orientation
534 p2x=QPoint( parObj->getParPos() ).x();
535 p2y=QPoint( parObj->getParPos() ).y();
540 setDockPos(); // Call overloaded method
542 LinkOrient orientOld=orientation;
543 if ((orientation!=orientOld) && (orientOld!= OrientUndef))
545 // Orientation just changed. Reorient this subbranch, because move is called
546 // before updateLink => Position is still the old one, which could lead to
547 // linking of subranch to itself => segfault
549 // Also possible: called in BranchObj::init(), then orientOld==OrientUndef,
550 // no need to reposition now
551 cout <<"Trying to reorientate... orient="<<orientation<<endl;
557 double p1x=parPos.x(); // Link is drawn from P1 to P2
558 double p1y=parPos.y();
560 double vx=p2x - p1x; // V=P2-P1
563 // Draw the horizontal line below heading (from ChildPos to ParPos)
564 bottomline->setPoints (qRound(childPos.x()),
565 qRound(childPos.y()),
570 if (vx > -0.000001 && vx < 0.000001)
574 // "turning point" for drawing polygonal links
575 QPoint tp (-qRound(sin (a)*thickness_start), qRound(cos (a)*thickness_start));
585 l->setPoints( qRound (parPos.x()),
591 parabel (pa0, p1x,p1y,p2x,p2y);
593 for (cl=segment.first(); cl; cl=segment.next() )
595 cl->setPoints( pa0.point(i).x(), pa0.point(i).y(),pa0.point(i+1).x(),pa0.point(i+1).y());
600 pa0[0]=QPoint (qRound(p2x+tp.x()), qRound(p2y+tp.y()));
601 pa0[1]=QPoint (qRound(p2x-tp.x()), qRound(p2y-tp.y()));
602 pa0[2]=QPoint (qRound (parPos.x()), qRound(parPos.y()) );
604 // here too, draw line to avoid missing pixels
605 l->setPoints( qRound (parPos.x()),
610 case StylePolyParabel:
611 parabel (pa1, p1x,p1y,p2x+tp.x(),p2y+tp.y());
612 parabel (pa2, p1x,p1y,p2x-tp.x(),p2y-tp.y());
613 for (i=0;i<=arcsegs;i++)
615 // Combine the arrays to a single one
617 pa0[i+arcsegs+1]=pa2[arcsegs-i];
621 for (cl=segment.first(); cl; cl=segment.next() )
623 cl->setPoints( pa1.point(i).x(), pa1.point(i).y(),pa1.point(i+1).x(),pa1.point(i+1).y());
632 LinkableMapObj* LinkableMapObj::getChildObj()
637 LinkableMapObj* LinkableMapObj::getParObj()
642 LinkableMapObj* LinkableMapObj::findObjBySelect (QString s)
644 LinkableMapObj *lmo=this;
648 while (!s.isEmpty() )
650 part=s.section(",",0,0);
652 num=part.right(part.length() - 3);
656 return false; // in a subtree there is no center
661 lmo=((BranchObj*)(lmo))->getBranchNum (num.toUInt());
664 lmo=((BranchObj*)(lmo))->getFloatImageNum (num.toUInt());
668 s=s.right(s.length() - part.length() -1 );
675 QPoint LinkableMapObj::getChildPos()
680 QPoint LinkableMapObj::getParPos()
685 void LinkableMapObj::setUseOrientation (const bool &b)
687 if (useOrientation!=b)
694 LinkOrient LinkableMapObj::getOrientation()
699 int LinkableMapObj::getDepth()
704 void LinkableMapObj::setMapEditor (MapEditor *me)
709 MapEditor* LinkableMapObj::getMapEditor ()
714 QPoint LinkableMapObj::getRandPos()
716 // Choose a random position with given distance to parent:
717 double a=rand()%360 * 2 * M_PI / 360;
718 return QPoint ( (int)( + 150*cos (a)),
719 (int)( + 150*sin (a)));
723 void LinkableMapObj::alignRelativeTo (QPoint ref)
725 // Overloaded, only called for BO, FIO, ...
730 void LinkableMapObj::reposition()
732 // FIXME not needed? Is overloaded in BranchObj...
734 cout << "LMO::reposition\n";
737 // only calculate the sizes once. If the deepest LMO changes its height,
738 // all upper LMOs have to change, too.
739 calcBBoxSizeWithChilds();
741 alignRelativeTo ( QPoint (absPos.x(),
742 absPos.y()-(bboxTotal.height()-bbox.height())/2) );
745 // This is only important for moving branches:
746 // For editing a branch it isn't called...
747 cout << " reposition to abs "<<absPos<<endl;
748 alignRelativeTo ( QPoint (absPos.x(),
749 absPos.y()-(bboxTotal.height()-bbox.height())/2) );
754 void LinkableMapObj::requestReposition()
756 if (!repositionRequest)
758 // Pass on the request to parental objects, if this hasn't
760 repositionRequest=true;
761 if (parObj) parObj->requestReposition();
765 void LinkableMapObj::forceReposition()
767 // Sometimes a reposition has to be done immediatly: For example
768 // if the note editor flag changes, there is no user event in mapeditor
769 // which could collect requests for a reposition.
770 // Then we have to call forceReposition()
771 // But no rule without exception: While loading a map or undoing it,
772 // we want to block expensive repositioning, but just do it once at
773 // the end, thus check first:
775 if (mapEditor->isRepositionBlocked()) return;
777 // Pass on the request to parental objects, if this hasn't been done yet
780 parObj->forceReposition();
785 bool LinkableMapObj::repositionRequested()
787 return repositionRequest;
791 void LinkableMapObj::setSelBox()
793 selbox->setX (clickBox.x() );
794 selbox->setY (clickBox.y() );
795 selbox->setSize (clickBox.width(), clickBox.height() );
798 void LinkableMapObj::select()
804 setVisibility (visible);
808 void LinkableMapObj::unselect()
812 // Maybe we have to hide the link:
813 setVisibility (visible);
816 void LinkableMapObj::parabel (Q3PointArray &ya, double p1x, double p1y, double p2x, double p2y)
819 double vx=p2x - p1x; // V=P2-P1
822 double dx; // delta x during calculation of parabel
824 double pnx; // next point
828 if (vx > -0.0001 && vx < 0.0001)
834 ya.setPoint (0,QPoint (qRound(p1x),qRound(p1y)));
835 for (i=1;i<=arcsegs;i++)
838 pny=m*(pnx-parPos.x())*(pnx-parPos.x())+parPos.y();
839 ya.setPoint (i,QPoint (qRound(pnx),qRound(pny)));
845 QString LinkableMapObj::getLinkAttr ()
847 if (hideLinkUnselected)
848 return attribut ("hideLink","true");
850 return attribut ("hideLink","false");