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