insilmaril@2: #include "xlinkobj.h" insilmaril@2: #include "branchobj.h" insilmaril@2: #include "mapeditor.h" insilmaril@2: //Added by qt3to4: insilmaril@2: #include insilmaril@2: insilmaril@2: insilmaril@2: ///////////////////////////////////////////////////////////////// insilmaril@2: // XLinkObj insilmaril@2: ///////////////////////////////////////////////////////////////// insilmaril@2: insilmaril@2: int XLinkObj::arrowSize=10; // make instances insilmaril@2: insilmaril@2: XLinkObj::XLinkObj ():MapObj() insilmaril@2: { insilmaril@2: // cout << "Const XLinkObj ()\n"; insilmaril@2: init(); insilmaril@2: } insilmaril@2: insilmaril@2: XLinkObj::XLinkObj (Q3Canvas* c):MapObj(c) insilmaril@2: { insilmaril@2: // cout << "Const XLinkObj (c) called from MapCenterObj (c)\n"; insilmaril@2: init(); insilmaril@2: } insilmaril@2: insilmaril@2: insilmaril@2: XLinkObj::~XLinkObj () insilmaril@2: { insilmaril@2: // cout << "Destr XLinkObj\n"; insilmaril@2: if (xLinkState!=undefinedXLink) insilmaril@2: deactivate(); insilmaril@2: delete (line); insilmaril@2: delete (poly); insilmaril@2: } insilmaril@2: insilmaril@2: insilmaril@2: void XLinkObj::init () insilmaril@2: { insilmaril@2: beginBranch=NULL; insilmaril@2: endBranch=NULL; insilmaril@2: visBranch=NULL; insilmaril@2: xLinkState=undefinedXLink; insilmaril@2: insilmaril@2: color=QColor (180,180,180); insilmaril@2: line=new Q3CanvasLine (canvas); insilmaril@2: width=1; insilmaril@2: line->setPen (QPen(color, width)); insilmaril@2: line->setZ (Z_XLINK); insilmaril@2: insilmaril@2: poly=new Q3CanvasPolygon (canvas); insilmaril@2: poly->setBrush( color ); insilmaril@2: poly->setZ (Z_XLINK); insilmaril@2: insilmaril@2: setVisibility (false); insilmaril@2: } insilmaril@2: insilmaril@2: void XLinkObj::copy (XLinkObj* other) insilmaril@2: { insilmaril@2: // TODO copy not used yet insilmaril@2: MapObj::copy (other); insilmaril@2: setVisibility (other->visible); insilmaril@2: beginBranch=other->beginBranch; insilmaril@2: endBranch=other->endBranch; insilmaril@2: width=other->width; insilmaril@2: } insilmaril@2: insilmaril@2: void XLinkObj::setBegin (BranchObj *bo) insilmaril@2: { insilmaril@2: if (bo) insilmaril@2: { insilmaril@2: xLinkState=initXLink; insilmaril@2: beginBranch=bo; insilmaril@2: beginPos=beginBranch->getChildPos(); insilmaril@2: } insilmaril@2: } insilmaril@2: insilmaril@2: BranchObj* XLinkObj::getBegin () insilmaril@2: { insilmaril@2: return beginBranch; insilmaril@2: } insilmaril@2: insilmaril@2: void XLinkObj::setEnd (BranchObj *bo) insilmaril@2: { insilmaril@2: if (bo) insilmaril@2: { insilmaril@2: xLinkState=initXLink; insilmaril@2: endBranch=bo; insilmaril@2: endPos=endBranch->getChildPos(); insilmaril@2: } insilmaril@2: } insilmaril@2: insilmaril@2: BranchObj* XLinkObj::getEnd() insilmaril@2: { insilmaril@2: return endBranch; insilmaril@2: } insilmaril@2: insilmaril@2: void XLinkObj::setWidth (int w) insilmaril@2: { insilmaril@2: width=w; insilmaril@2: setColor (color); insilmaril@2: } insilmaril@2: insilmaril@2: int XLinkObj::getWidth() insilmaril@2: { insilmaril@2: return width; insilmaril@2: } insilmaril@2: insilmaril@2: void XLinkObj::setColor(QColor c) insilmaril@2: { insilmaril@2: color=c; insilmaril@2: line->setPen (QPen(color, width)); insilmaril@2: poly->setBrush( color ); insilmaril@2: } insilmaril@2: insilmaril@2: QColor XLinkObj::getColor() insilmaril@2: { insilmaril@2: return color; insilmaril@2: } insilmaril@2: insilmaril@2: void XLinkObj::setEnd (QPoint p) insilmaril@2: { insilmaril@2: endPos=p; insilmaril@2: } insilmaril@2: insilmaril@2: bool XLinkObj::activate () insilmaril@2: { insilmaril@2: if (beginBranch && endBranch) insilmaril@2: { insilmaril@2: if (beginBranch==endBranch) return false; insilmaril@2: xLinkState=activeXLink; insilmaril@2: beginBranch->addXLink (this); insilmaril@2: endBranch->addXLink (this); insilmaril@2: setVisibility (); insilmaril@2: return true; insilmaril@2: } else insilmaril@2: return false; insilmaril@2: } insilmaril@2: insilmaril@2: void XLinkObj::deactivate () insilmaril@2: { insilmaril@2: if (beginBranch) insilmaril@2: beginBranch->removeXLinkRef (this); insilmaril@2: beginBranch=NULL; insilmaril@2: if (endBranch) insilmaril@2: endBranch->removeXLinkRef (this); insilmaril@2: endBranch=NULL; insilmaril@2: visBranch=NULL; insilmaril@2: xLinkState=undefinedXLink; insilmaril@2: insilmaril@2: line->hide(); insilmaril@2: } insilmaril@2: insilmaril@2: bool XLinkObj::isUsed() insilmaril@2: { insilmaril@2: if (beginBranch || endBranch || xLinkState!=undefinedXLink) insilmaril@2: return true; insilmaril@2: else insilmaril@2: return false; insilmaril@2: } insilmaril@2: insilmaril@2: void XLinkObj::updateXLink() insilmaril@2: { insilmaril@2: QPoint a,b; insilmaril@2: Q3PointArray pa (3); insilmaril@2: if (visBranch) insilmaril@2: { insilmaril@2: // Only one of the linked branches is visible insilmaril@2: a=b=visBranch->getChildPos(); insilmaril@2: if (visBranch->getOrientation()==OrientRightOfCenter) insilmaril@2: { insilmaril@2: b.setX (b.x()+25); insilmaril@2: pa.putPoints (0,3, insilmaril@2: b.x(),b.y(), insilmaril@2: b.x()-arrowSize,b.y()-arrowSize, insilmaril@2: b.x()-arrowSize,b.y()+arrowSize insilmaril@2: ); insilmaril@2: poly->setPoints (pa); insilmaril@2: } else insilmaril@2: { insilmaril@2: b.setX (b.x()-25); insilmaril@2: pa.putPoints (0,3, insilmaril@2: b.x(),b.y(), insilmaril@2: b.x()+arrowSize,b.y()-arrowSize, insilmaril@2: b.x()+arrowSize,b.y()+arrowSize); insilmaril@2: poly->setPoints (pa); insilmaril@2: } insilmaril@2: } else insilmaril@2: { insilmaril@2: // Both linked branches are visible insilmaril@2: if (beginBranch) insilmaril@2: // If a link is just drawn in the editor, insilmaril@2: // we have already a beginBranch insilmaril@2: a=beginBranch->getChildPos(); insilmaril@2: else insilmaril@2: // This shouldn't be reached normally... insilmaril@2: a=beginPos; insilmaril@2: if (xLinkState==activeXLink && endBranch) insilmaril@2: b=endBranch->getChildPos(); insilmaril@2: else insilmaril@2: b=endPos; insilmaril@2: } insilmaril@2: insilmaril@2: insilmaril@2: if (line->startPoint()==a && line->endPoint()==b && !visBranch) insilmaril@2: { insilmaril@2: // update is called from both branches, so only insilmaril@2: // update if something has changed insilmaril@2: return; insilmaril@2: } insilmaril@2: else insilmaril@2: { insilmaril@2: beginPos=a; insilmaril@2: endPos=b; insilmaril@2: line->setPen (QPen(color, width)); insilmaril@2: line->setPoints (a.x(), a.y(), b.x(), b.y()); insilmaril@2: } insilmaril@2: } insilmaril@2: insilmaril@2: BranchObj* XLinkObj::otherBranch(BranchObj* thisBranch) insilmaril@2: { insilmaril@2: if (!beginBranch && !endBranch) insilmaril@2: return NULL; insilmaril@2: if (thisBranch==beginBranch) insilmaril@2: return endBranch; insilmaril@2: else insilmaril@2: return beginBranch; insilmaril@2: } insilmaril@2: insilmaril@2: void XLinkObj::positionBBox() insilmaril@2: { insilmaril@2: } insilmaril@2: insilmaril@2: void XLinkObj::calcBBoxSize() insilmaril@2: { insilmaril@2: } insilmaril@2: insilmaril@2: void XLinkObj::setVisibility (bool b) insilmaril@2: { insilmaril@2: MapObj::setVisibility (b); insilmaril@2: if (b) insilmaril@2: { insilmaril@2: line->show(); insilmaril@2: if (visBranch) insilmaril@2: poly->show(); insilmaril@2: else insilmaril@2: poly->hide(); insilmaril@2: } insilmaril@2: else insilmaril@2: { insilmaril@2: line->hide(); insilmaril@2: poly->hide(); insilmaril@2: } insilmaril@2: } insilmaril@2: insilmaril@2: void XLinkObj::setVisibility () insilmaril@2: { insilmaril@2: if (beginBranch && endBranch) insilmaril@2: { insilmaril@2: if(beginBranch->isVisibleObj() && endBranch->isVisibleObj()) insilmaril@2: { // Both ends are visible insilmaril@2: visBranch=NULL; insilmaril@2: setVisibility (true); insilmaril@2: } else insilmaril@2: { insilmaril@2: if(!beginBranch->isVisibleObj() && !endBranch->isVisibleObj()) insilmaril@2: { //None of the ends is visible insilmaril@2: visBranch=NULL; insilmaril@2: setVisibility (false); insilmaril@2: } else insilmaril@2: { // Just one end is visible, draw a symbol that shows insilmaril@2: // that there is a link to a scrolled branch insilmaril@2: if (beginBranch->isVisibleObj()) insilmaril@2: visBranch=beginBranch; insilmaril@2: else insilmaril@2: visBranch=endBranch; insilmaril@2: setVisibility (true); insilmaril@2: } insilmaril@2: } insilmaril@2: } insilmaril@2: } insilmaril@2: insilmaril@2: QString XLinkObj::saveToDir () insilmaril@2: { insilmaril@2: QString s=""; insilmaril@2: if (beginBranch && endBranch &&xLinkState==activeXLink) insilmaril@2: { insilmaril@2: if (beginBranch==endBranch && xLinkState) insilmaril@2: s=""; insilmaril@2: else insilmaril@2: { insilmaril@2: QString colAttr=attribut ("color",color.name()); insilmaril@2: QString widAttr=attribut ("width",QString().setNum(width,10)); insilmaril@2: QString begSelAttr=attribut ("beginBranch",beginBranch->getSelectString()); insilmaril@2: QString endSelAttr=attribut ("endBranch", endBranch->getSelectString()); insilmaril@2: s=beginElement ("xlink", colAttr +widAttr +begSelAttr +endSelAttr); insilmaril@2: insilmaril@2: s+=endElement ("xlink"); insilmaril@2: } insilmaril@2: } insilmaril@2: return s; insilmaril@2: } insilmaril@2: