xlinkobj.cpp
branchqt4-port
changeset 2 608f976aa7bb
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xlinkobj.cpp	Tue Jun 06 14:58:11 2006 +0000
     1.3 @@ -0,0 +1,302 @@
     1.4 +#include "xlinkobj.h"
     1.5 +#include "branchobj.h"
     1.6 +#include "mapeditor.h"
     1.7 +//Added by qt3to4:
     1.8 +#include <Q3PointArray>
     1.9 +
    1.10 +
    1.11 +/////////////////////////////////////////////////////////////////
    1.12 +// XLinkObj
    1.13 +/////////////////////////////////////////////////////////////////
    1.14 +
    1.15 +int XLinkObj::arrowSize=10;						// make instances 
    1.16 +
    1.17 +XLinkObj::XLinkObj ():MapObj() 
    1.18 +{
    1.19 +	//	cout << "Const XLinkObj ()\n";
    1.20 +	init();
    1.21 +}
    1.22 +
    1.23 +XLinkObj::XLinkObj (Q3Canvas* c):MapObj(c)
    1.24 +{
    1.25 +	//	cout << "Const XLinkObj (c)  called from MapCenterObj (c)\n";
    1.26 +	init();
    1.27 +}
    1.28 +
    1.29 +
    1.30 +XLinkObj::~XLinkObj ()
    1.31 +{
    1.32 +	//	cout << "Destr XLinkObj\n";
    1.33 +	if (xLinkState!=undefinedXLink)
    1.34 +		deactivate();
    1.35 +	delete (line);
    1.36 +	delete (poly);
    1.37 +}
    1.38 +
    1.39 +
    1.40 +void XLinkObj::init () 
    1.41 +{
    1.42 +	beginBranch=NULL;
    1.43 +	endBranch=NULL;
    1.44 +	visBranch=NULL;
    1.45 +	xLinkState=undefinedXLink;
    1.46 +
    1.47 +	color=QColor (180,180,180);
    1.48 +	line=new Q3CanvasLine (canvas);
    1.49 +	width=1;
    1.50 +	line->setPen (QPen(color, width));
    1.51 +	line->setZ (Z_XLINK);
    1.52 +
    1.53 +	poly=new Q3CanvasPolygon (canvas);
    1.54 +	poly->setBrush( color );
    1.55 +	poly->setZ (Z_XLINK);
    1.56 +
    1.57 +	setVisibility (false);
    1.58 +}
    1.59 +
    1.60 +void XLinkObj::copy (XLinkObj* other)
    1.61 +{
    1.62 +	// TODO copy not used yet
    1.63 +	MapObj::copy (other);
    1.64 +	setVisibility (other->visible);
    1.65 +	beginBranch=other->beginBranch;
    1.66 +	endBranch=other->endBranch;
    1.67 +	width=other->width;
    1.68 +}
    1.69 +
    1.70 +void XLinkObj::setBegin (BranchObj *bo)
    1.71 +{
    1.72 +	if (bo) 
    1.73 +	{
    1.74 +		xLinkState=initXLink;
    1.75 +		beginBranch=bo;
    1.76 +		beginPos=beginBranch->getChildPos();
    1.77 +	}	
    1.78 +}
    1.79 +
    1.80 +BranchObj* XLinkObj::getBegin ()
    1.81 +{
    1.82 +	return beginBranch;
    1.83 +}
    1.84 +
    1.85 +void XLinkObj::setEnd (BranchObj *bo)
    1.86 +{
    1.87 +	if (bo) 
    1.88 +	{
    1.89 +		xLinkState=initXLink;
    1.90 +		endBranch=bo;
    1.91 +		endPos=endBranch->getChildPos();
    1.92 +	}		
    1.93 +}
    1.94 +
    1.95 +BranchObj* XLinkObj::getEnd()
    1.96 +{
    1.97 +	return endBranch;
    1.98 +}
    1.99 +
   1.100 +void XLinkObj::setWidth (int w)
   1.101 +{
   1.102 +	width=w;
   1.103 +	setColor (color);
   1.104 +}
   1.105 +
   1.106 +int XLinkObj::getWidth()
   1.107 +{
   1.108 +	return width;
   1.109 +}
   1.110 +
   1.111 +void XLinkObj::setColor(QColor c)
   1.112 +{
   1.113 +	color=c;
   1.114 +	line->setPen (QPen(color, width));
   1.115 +	poly->setBrush( color );
   1.116 +}
   1.117 +
   1.118 +QColor XLinkObj::getColor()
   1.119 +{
   1.120 +	return color;
   1.121 +}
   1.122 +
   1.123 +void XLinkObj::setEnd (QPoint p)
   1.124 +{
   1.125 +	endPos=p;
   1.126 +}
   1.127 +
   1.128 +bool XLinkObj::activate ()
   1.129 +{
   1.130 +	if (beginBranch && endBranch)
   1.131 +	{
   1.132 +		if (beginBranch==endBranch) return false;
   1.133 +		xLinkState=activeXLink;
   1.134 +		beginBranch->addXLink (this);
   1.135 +		endBranch->addXLink (this);
   1.136 +		setVisibility ();
   1.137 +		return true;
   1.138 +	} else
   1.139 +		return false;
   1.140 +}
   1.141 +
   1.142 +void XLinkObj::deactivate ()
   1.143 +{
   1.144 +	if (beginBranch)
   1.145 +		beginBranch->removeXLinkRef (this);
   1.146 +	beginBranch=NULL;	
   1.147 +	if (endBranch)
   1.148 +		endBranch->removeXLinkRef (this);
   1.149 +	endBranch=NULL;	
   1.150 +	visBranch=NULL;
   1.151 +	xLinkState=undefinedXLink;
   1.152 +
   1.153 +	line->hide();
   1.154 +}
   1.155 +
   1.156 +bool XLinkObj::isUsed()
   1.157 +{
   1.158 +	if (beginBranch || endBranch || xLinkState!=undefinedXLink)
   1.159 +		return true;
   1.160 +	else
   1.161 +		return false;
   1.162 +}
   1.163 +
   1.164 +void XLinkObj::updateXLink()
   1.165 +{
   1.166 +	QPoint a,b;
   1.167 +	Q3PointArray pa (3);
   1.168 +	if (visBranch)
   1.169 +	{
   1.170 +		// Only one of the linked branches is visible
   1.171 +		a=b=visBranch->getChildPos();
   1.172 +		if (visBranch->getOrientation()==OrientRightOfCenter)
   1.173 +		{
   1.174 +			b.setX (b.x()+25);
   1.175 +			pa.putPoints (0,3,
   1.176 +				b.x(),b.y(),
   1.177 +				b.x()-arrowSize,b.y()-arrowSize,
   1.178 +				b.x()-arrowSize,b.y()+arrowSize
   1.179 +			);
   1.180 +			poly->setPoints (pa);
   1.181 +		} else
   1.182 +		{
   1.183 +			b.setX (b.x()-25);
   1.184 +			pa.putPoints (0,3,
   1.185 +				b.x(),b.y(),
   1.186 +				b.x()+arrowSize,b.y()-arrowSize,
   1.187 +				b.x()+arrowSize,b.y()+arrowSize);
   1.188 +			poly->setPoints (pa);
   1.189 +		}	
   1.190 +	} else
   1.191 +	{
   1.192 +		// Both linked branches are visible
   1.193 +		if (beginBranch)
   1.194 +			// If a link is just drawn in the editor,
   1.195 +			// we have already a beginBranch
   1.196 +			a=beginBranch->getChildPos();
   1.197 +		else
   1.198 +			// This shouldn't be reached normally...
   1.199 +			a=beginPos;
   1.200 +		if (xLinkState==activeXLink && endBranch)
   1.201 +			b=endBranch->getChildPos();
   1.202 +		else
   1.203 +			b=endPos;
   1.204 +	}
   1.205 +
   1.206 +
   1.207 +	if (line->startPoint()==a && line->endPoint()==b && !visBranch)
   1.208 +	{
   1.209 +		// update is called from both branches, so only
   1.210 +		// update if something has changed
   1.211 +		return;
   1.212 +	}	
   1.213 +	else
   1.214 +	{
   1.215 +		beginPos=a;
   1.216 +		endPos=b;
   1.217 +		line->setPen (QPen(color, width));
   1.218 +		line->setPoints (a.x(), a.y(), b.x(), b.y());
   1.219 +	}
   1.220 +}
   1.221 +
   1.222 +BranchObj* XLinkObj::otherBranch(BranchObj* thisBranch)
   1.223 +{
   1.224 +	if (!beginBranch && !endBranch)
   1.225 +		return NULL;
   1.226 +	if (thisBranch==beginBranch)
   1.227 +		return endBranch;
   1.228 +	else	
   1.229 +		return beginBranch;
   1.230 +}
   1.231 +
   1.232 +void XLinkObj::positionBBox()
   1.233 +{
   1.234 +}
   1.235 +
   1.236 +void XLinkObj::calcBBoxSize()
   1.237 +{
   1.238 +}
   1.239 +
   1.240 +void XLinkObj::setVisibility (bool b)
   1.241 +{
   1.242 +	MapObj::setVisibility (b);
   1.243 +	if (b)
   1.244 +	{
   1.245 +		line->show();
   1.246 +		if (visBranch) 
   1.247 +			poly->show();
   1.248 +		else	
   1.249 +			poly->hide();
   1.250 +	}	
   1.251 +	else
   1.252 +	{
   1.253 +		line->hide();
   1.254 +		poly->hide();
   1.255 +	}	
   1.256 +}
   1.257 +
   1.258 +void XLinkObj::setVisibility ()
   1.259 +{
   1.260 +	if (beginBranch && endBranch)
   1.261 +	{
   1.262 +		if(beginBranch->isVisibleObj() && endBranch->isVisibleObj())
   1.263 +		{	// Both ends are visible
   1.264 +			visBranch=NULL;
   1.265 +			setVisibility (true);
   1.266 +		} else
   1.267 +		{
   1.268 +			if(!beginBranch->isVisibleObj() && !endBranch->isVisibleObj())
   1.269 +			{	//None of the ends is visible
   1.270 +				visBranch=NULL;
   1.271 +				setVisibility (false);
   1.272 +			} else
   1.273 +			{	// Just one end is visible, draw a symbol that shows
   1.274 +				// that there is a link to a scrolled branch
   1.275 +				if (beginBranch->isVisibleObj())
   1.276 +					visBranch=beginBranch;
   1.277 +				else
   1.278 +					visBranch=endBranch;
   1.279 +				setVisibility (true);
   1.280 +			}
   1.281 +		}
   1.282 +	}
   1.283 +}
   1.284 +
   1.285 +QString XLinkObj::saveToDir ()
   1.286 +{
   1.287 +	QString s="";
   1.288 +	if (beginBranch && endBranch &&xLinkState==activeXLink)
   1.289 +	{
   1.290 +		if (beginBranch==endBranch && xLinkState)
   1.291 +			s="";
   1.292 +		else
   1.293 +		{
   1.294 +			QString colAttr=attribut ("color",color.name());
   1.295 +			QString widAttr=attribut ("width",QString().setNum(width,10));
   1.296 +			QString begSelAttr=attribut ("beginBranch",beginBranch->getSelectString());
   1.297 +			QString endSelAttr=attribut ("endBranch",  endBranch->getSelectString());
   1.298 +			s=beginElement ("xlink", colAttr +widAttr +begSelAttr +endSelAttr);
   1.299 +
   1.300 +			s+=endElement ("xlink");
   1.301 +		}
   1.302 +	}
   1.303 +	return s;
   1.304 +}
   1.305 +