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;
109 void LinkableMapObj::copy (LinkableMapObj* other)
112 bboxTotal=other->bboxTotal;
113 setLinkStyle(other->style);
114 setLinkColor (other->linkcolor);
115 relPos=other->relPos;
116 useOrientation=other->useOrientation;
120 void LinkableMapObj::setChildObj(LinkableMapObj* o)
125 void LinkableMapObj::setParObj(LinkableMapObj* o)
128 mapEditor=parObj->getMapEditor();
131 void LinkableMapObj::setParObjTmp(LinkableMapObj*,QPoint,int)
135 void LinkableMapObj::unsetParObjTmp()
139 bool LinkableMapObj::hasParObjTmp()
141 if (parObjTmpBuf) return true;
145 void LinkableMapObj::setUseRelPos (const bool &b)
150 void LinkableMapObj::setRelPos()
154 if (parObj->getOrientation()==OrientLeftOfCenter)
155 relPos.setX ( parObj->x() +parObj->width() - bbox.width() -absPos.x());
157 relPos.setX (absPos.x() - parObj->x() );
160 relPos.setX (absPos.x() - parObj->x() );
161 relPos.setY (absPos.y() - parObj->y() );
164 parObj->calcBBoxSize();
165 parObj->requestReposition();
169 void LinkableMapObj::setRelPos(const QPoint &p)
173 { parObj->calcBBoxSize();
174 parObj->requestReposition();
178 int LinkableMapObj::getTopPad()
183 int LinkableMapObj::getLeftPad()
188 int LinkableMapObj::getRightPad()
193 LinkStyle LinkableMapObj::getDefLinkStyle ()
195 if (!mapEditor) return StyleUndef;
197 LinkStyle ls=mapEditor->getLinkStyle();
212 case StylePolyParabel:
224 void LinkableMapObj::setLinkStyle(LinkStyle newstyle)
226 //if (newstyle=style) return;
231 if (childObj!=NULL && parObj != NULL)
241 l = new QCanvasLine(canvas);
242 l->setPen( QPen(linkcolor, 1) );
250 for (i=0;i<arcsegs;i++)
252 cl = new QCanvasLine(canvas);
253 cl->setPen( QPen(linkcolor, 1) );
254 cl->setPoints( 0,0,i*10,100);
262 pa0.resize (arcsegs+1);
265 p = new QCanvasPolygon(canvas);
266 p->setBrush( linkcolor );
273 // TODO a bit awkward: draw the lines additionally to polygon, to avoid
274 // missing pixels, when polygon is extremly flat
275 l = new QCanvasLine(canvas);
276 l->setPen( QPen(linkcolor, 1) );
283 case StylePolyParabel:
284 p = new QCanvasPolygon(canvas);
285 p->setBrush( linkcolor );
291 pa0.resize (arcsegs*2+2);
292 pa1.resize (arcsegs+1);
293 pa2.resize (arcsegs+1);
295 // TODO a bit awkward: draw the lines additionally
296 // to polygon, to avoid missing pixels,
297 // if polygon is extremly flat
298 for (i=0;i<arcsegs;i++)
300 cl = new QCanvasLine(canvas);
301 cl->setPen( QPen(linkcolor, 1) );
302 cl->setPoints( 0,0,i*10,100);
317 LinkStyle LinkableMapObj::getLinkStyle()
322 void LinkableMapObj::setHideLinkUnselected(bool b)
324 hideLinkUnselected=b;
325 setVisibility (visible);
329 bool LinkableMapObj::getHideLinkUnselected()
331 return hideLinkUnselected;
334 void LinkableMapObj::setLinkPos(LinkPos lp)
339 LinkPos LinkableMapObj::getLinkPos()
345 void LinkableMapObj::setLinkColor()
347 // Overloaded in BranchObj and childs
348 // here only set default color
350 setLinkColor (mapEditor->getDefLinkColor());
353 void LinkableMapObj::setLinkColor(QColor col)
356 bottomline->setPen( QPen(linkcolor, 1) );
361 l->setPen( QPen(col,1));
364 for (cl=segment.first(); cl; cl=segment.next() )
365 cl->setPen( QPen(col,1));
368 p->setBrush( QBrush(col));
369 l->setPen( QPen(col,1));
371 case StylePolyParabel:
372 p->setBrush( QBrush(col));
373 for (cl=segment.first(); cl; cl=segment.next() )
374 cl->setPen( QPen(col,1));
381 QColor LinkableMapObj::getLinkColor()
386 FrameType LinkableMapObj::getFrameType()
388 return frame->getFrameType();
391 void LinkableMapObj::setFrameType(const FrameType &t)
393 frame->setFrameType(t);
399 void LinkableMapObj::setFrameType(const QString &t)
401 frame->setFrameType(t);
407 void LinkableMapObj::setVisibility (bool v)
410 MapObj::setVisibility (v);
412 if (hideLinkUnselected && !selected)
424 for (cl=segment.first(); cl; cl=segment.next() )
431 case StylePolyParabel:
432 for (cl=segment.first(); cl; cl=segment.next() )
448 for (cl=segment.first(); cl; cl=segment.next() )
455 case StylePolyParabel:
456 for (cl=segment.first(); cl; cl=segment.next() )
466 void LinkableMapObj::updateLink()
469 // childPos of parent
478 // drawing of the link itself
481 // updateLink is called from move, but called from constructor we don't
482 // have parents yet...
483 if (style==StyleUndef) return;
485 if (frame->getFrameType() == NoFrame)
492 bottomlineY=clickBox.top()+clickBox.height() /2; // draw link to middle (of frame)
495 bottomlineY=clickBox.bottom()-1; // draw link to bottom of bbox
499 double p2x,p2y; // Set P2 Before setting
502 p2x=QPoint( parObj->getChildPos() ).x(); // P1, we have to look at
503 p2y=QPoint( parObj->getChildPos() ).y(); // orientation
506 p2x=QPoint( parObj->getParPos() ).x();
507 p2y=QPoint( parObj->getParPos() ).y();
510 LinkOrient orientOld=orientation;
512 // Set orientation, first look for orientation of parent
513 if (parObj->getOrientation() != OrientUndef )
514 // use the orientation of the parent:
515 orientation=parObj->getOrientation();
518 // calc orientation depending on position rel to mapCenter
519 if (absPos.x() < QPoint(parObj->getChildPos() ).x() )
520 orientation=OrientLeftOfCenter;
522 orientation=OrientRightOfCenter;
525 if ((orientation!=orientOld) && (orientOld!= OrientUndef))
527 // Orientation just changed. Reorient this subbranch, because move is called
528 // before updateLink => Position is still the old one, which could lead to
529 // linking of subranch to itself => segfault
531 // Also possible: called in BranchObj::init(), then orientOld==OrientUndef,
532 // no need to reposition now
536 if (orientation==OrientLeftOfCenter )
538 childPos=QPoint (absPos.x(),bottomlineY);
539 parPos=QPoint (absPos.x()+ bbox.width(), bottomlineY );
542 childPos=QPoint (absPos.x()+ bbox.width(), bottomlineY );
543 parPos=QPoint (absPos.x(),bottomlineY);
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 QPoint LinkableMapObj::getRelPos()
676 if (!parObj) return QPoint (0,0);
678 absPos.x() - parObj->x(),
679 absPos.y() - parObj->y()
684 void LinkableMapObj::setUseOrientation (const bool &b)
686 if (useOrientation!=b)
693 LinkOrient LinkableMapObj::getOrientation()
698 int LinkableMapObj::getDepth()
703 void LinkableMapObj::setMapEditor (MapEditor *me)
708 MapEditor* LinkableMapObj::getMapEditor ()
713 QPoint LinkableMapObj::getRandPos()
715 // Choose a random position with given distance to parent:
716 double a=rand()%360 * 2 * M_PI / 360;
717 return QPoint ( (int)( + 150*cos (a)),
718 (int)( + 150*sin (a)));
721 void LinkableMapObj::alignRelativeTo (QPoint ref)
723 // FIXME testing, seems not to be used right now...
724 cout << "LMO::alignRelTo ref="<<ref<<endl;
727 void LinkableMapObj::reposition()
731 // only calculate the sizes once. If the deepest LMO changes its height,
732 // all upper LMOs have to change, too.
733 calcBBoxSizeWithChilds();
735 alignRelativeTo ( QPoint (absPos.x(),
736 absPos.y()-(bboxTotal.height()-bbox.height())/2) );
739 // This is only important for moving branches:
740 // For editing a branch it isn't called...
741 alignRelativeTo ( QPoint (absPos.x(),
742 absPos.y()-(bboxTotal.height()-bbox.height())/2) );
746 void LinkableMapObj::requestReposition()
748 if (!repositionRequest)
750 // Pass on the request to parental objects, if this hasn't
752 repositionRequest=true;
753 if (parObj) parObj->requestReposition();
757 void LinkableMapObj::forceReposition()
759 // Sometimes a reposition has to be done immediatly: For example
760 // if the note editor flag changes, there is no user event in mapeditor
761 // which could collect requests for a reposition.
762 // Then we have to call forceReposition()
763 // But no rule without exception: While loading a map or undoing it,
764 // we want to block expensive repositioning, but just do it once at
765 // the end, thus check first:
767 if (mapEditor->isRepositionBlocked()) return;
769 // Pass on the request to parental objects, if this hasn't been done yet
772 parObj->forceReposition();
777 bool LinkableMapObj::repositionRequested()
779 return repositionRequest;
783 void LinkableMapObj::setSelBox()
785 selbox->setX (clickBox.x() );
786 selbox->setY (clickBox.y() );
787 selbox->setSize (clickBox.width(), clickBox.height() );
790 void LinkableMapObj::select()
795 setVisibility (visible);
799 void LinkableMapObj::unselect()
803 setVisibility (visible);
806 void LinkableMapObj::parabel (QPointArray &ya, double p1x, double p1y, double p2x, double p2y)
809 double vx=p2x - p1x; // V=P2-P1
812 double dx; // delta x during calculation of parabel
814 double pnx; // next point
818 if (vx > -0.0001 && vx < 0.0001)
824 ya.setPoint (0,QPoint (qRound(p1x),qRound(p1y)));
825 for (i=1;i<=arcsegs;i++)
828 pny=m*(pnx-parPos.x())*(pnx-parPos.x())+parPos.y();
829 ya.setPoint (i,QPoint (qRound(pnx),qRound(pny)));
835 QString LinkableMapObj::getLinkAttr ()
837 if (hideLinkUnselected)
838 return attribut ("hideLink","true");
840 return attribut ("hideLink","false");