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();
538 setDockPos(); // Call overloaded method
541 double p1x=parPos.x(); // Link is drawn from P1 to P2
542 double p1y=parPos.y();
544 double vx=p2x - p1x; // V=P2-P1
547 // Draw the horizontal line below heading (from ChildPos to ParPos)
548 bottomline->setPoints (qRound(childPos.x()),
549 qRound(childPos.y()),
554 if (vx > -0.000001 && vx < 0.000001)
558 // "turning point" for drawing polygonal links
559 QPoint tp (-qRound(sin (a)*thickness_start), qRound(cos (a)*thickness_start));
569 l->setPoints( qRound (parPos.x()),
575 parabel (pa0, p1x,p1y,p2x,p2y);
577 for (cl=segment.first(); cl; cl=segment.next() )
579 cl->setPoints( pa0.point(i).x(), pa0.point(i).y(),pa0.point(i+1).x(),pa0.point(i+1).y());
584 pa0[0]=QPoint (qRound(p2x+tp.x()), qRound(p2y+tp.y()));
585 pa0[1]=QPoint (qRound(p2x-tp.x()), qRound(p2y-tp.y()));
586 pa0[2]=QPoint (qRound (parPos.x()), qRound(parPos.y()) );
588 // here too, draw line to avoid missing pixels
589 l->setPoints( qRound (parPos.x()),
594 case StylePolyParabel:
595 parabel (pa1, p1x,p1y,p2x+tp.x(),p2y+tp.y());
596 parabel (pa2, p1x,p1y,p2x-tp.x(),p2y-tp.y());
597 for (i=0;i<=arcsegs;i++)
599 // Combine the arrays to a single one
601 pa0[i+arcsegs+1]=pa2[arcsegs-i];
605 for (cl=segment.first(); cl; cl=segment.next() )
607 cl->setPoints( pa1.point(i).x(), pa1.point(i).y(),pa1.point(i+1).x(),pa1.point(i+1).y());
616 LinkableMapObj* LinkableMapObj::getChildObj()
621 LinkableMapObj* LinkableMapObj::getParObj()
626 LinkableMapObj* LinkableMapObj::findObjBySelect (QString s)
628 LinkableMapObj *lmo=this;
632 while (!s.isEmpty() )
634 part=s.section(",",0,0);
636 num=part.right(part.length() - 3);
640 return false; // in a subtree there is no center
645 lmo=((BranchObj*)(lmo))->getBranchNum (num.toUInt());
648 lmo=((BranchObj*)(lmo))->getFloatImageNum (num.toUInt());
652 s=s.right(s.length() - part.length() -1 );
659 QPoint LinkableMapObj::getChildPos()
664 QPoint LinkableMapObj::getParPos()
669 void LinkableMapObj::setUseOrientation (const bool &b)
671 if (useOrientation!=b)
678 LinkOrient LinkableMapObj::getOrientation()
683 int LinkableMapObj::getDepth()
688 void LinkableMapObj::setMapEditor (MapEditor *me)
693 MapEditor* LinkableMapObj::getMapEditor ()
698 QPoint LinkableMapObj::getRandPos()
700 // Choose a random position with given distance to parent:
701 double a=rand()%360 * 2 * M_PI / 360;
702 return QPoint ( (int)( + 150*cos (a)),
703 (int)( + 150*sin (a)));
707 void LinkableMapObj::alignRelativeTo (QPoint ref)
709 // Overloaded, only called for BO, FIO, ...
714 void LinkableMapObj::reposition()
716 // FIXME not needed? Is overloaded in BranchObj...
718 cout << "LMO::reposition\n";
721 // only calculate the sizes once. If the deepest LMO changes its height,
722 // all upper LMOs have to change, too.
723 calcBBoxSizeWithChilds();
725 alignRelativeTo ( QPoint (absPos.x(),
726 absPos.y()-(bboxTotal.height()-bbox.height())/2) );
729 // This is only important for moving branches:
730 // For editing a branch it isn't called...
731 cout << " reposition to abs "<<absPos<<endl;
732 alignRelativeTo ( QPoint (absPos.x(),
733 absPos.y()-(bboxTotal.height()-bbox.height())/2) );
738 void LinkableMapObj::requestReposition()
740 if (!repositionRequest)
742 // Pass on the request to parental objects, if this hasn't
744 repositionRequest=true;
745 if (parObj) parObj->requestReposition();
749 void LinkableMapObj::forceReposition()
751 // Sometimes a reposition has to be done immediatly: For example
752 // if the note editor flag changes, there is no user event in mapeditor
753 // which could collect requests for a reposition.
754 // Then we have to call forceReposition()
755 // But no rule without exception: While loading a map or undoing it,
756 // we want to block expensive repositioning, but just do it once at
757 // the end, thus check first:
759 if (mapEditor->isRepositionBlocked()) return;
761 // Pass on the request to parental objects, if this hasn't been done yet
764 parObj->forceReposition();
769 bool LinkableMapObj::repositionRequested()
771 return repositionRequest;
775 void LinkableMapObj::setSelBox()
777 selbox->setX (clickBox.x() );
778 selbox->setY (clickBox.y() );
779 selbox->setSize (clickBox.width(), clickBox.height() );
782 void LinkableMapObj::select()
788 setVisibility (visible);
792 void LinkableMapObj::unselect()
796 // Maybe we have to hide the link:
797 setVisibility (visible);
800 void LinkableMapObj::parabel (Q3PointArray &ya, double p1x, double p1y, double p2x, double p2y)
803 double vx=p2x - p1x; // V=P2-P1
806 double dx; // delta x during calculation of parabel
808 double pnx; // next point
812 if (vx > -0.0001 && vx < 0.0001)
818 ya.setPoint (0,QPoint (qRound(p1x),qRound(p1y)));
819 for (i=1;i<=arcsegs;i++)
822 pny=m*(pnx-parPos.x())*(pnx-parPos.x())+parPos.y();
823 ya.setPoint (i,QPoint (qRound(pnx),qRound(pny)));
829 QString LinkableMapObj::getLinkAttr ()
831 if (hideLinkUnselected)
832 return attribut ("hideLink","true");
834 return attribut ("hideLink","false");