xlinkobj.cpp
author insilmaril
Wed, 31 May 2006 12:27:38 +0000
changeset 333 94cd6a451593
parent 298 d1d636eec6b1
child 347 4a3c06183639
permissions -rw-r--r--
1.7.18
     1 #include "xlinkobj.h"
     2 #include "branchobj.h"
     3 #include "mapeditor.h"
     4 
     5 
     6 /////////////////////////////////////////////////////////////////
     7 // XLinkObj
     8 /////////////////////////////////////////////////////////////////
     9 
    10 int XLinkObj::arrowSize=10;						// make instances 
    11 
    12 XLinkObj::XLinkObj ():MapObj() 
    13 {
    14 	//	cout << "Const XLinkObj ()\n";
    15 	init();
    16 }
    17 
    18 XLinkObj::XLinkObj (QCanvas* c):MapObj(c)
    19 {
    20 	//	cout << "Const XLinkObj (c)  called from MapCenterObj (c)\n";
    21 	init();
    22 }
    23 
    24 
    25 XLinkObj::~XLinkObj ()
    26 {
    27 	//	cout << "Destr XLinkObj\n";
    28 	if (xLinkState!=undefinedXLink)
    29 		deactivate();
    30 	delete (line);
    31 	delete (poly);
    32 }
    33 
    34 void XLinkObj::init () 
    35 {
    36 	beginBranch=NULL;
    37 	endBranch=NULL;
    38 	visBranch=NULL;
    39 	xLinkState=undefinedXLink;
    40 
    41 	color=QColor (180,180,180);
    42 	line=new QCanvasLine (canvas);
    43 	width=1;
    44 	line->setPen (QPen(color, width));
    45 	line->setZ (Z_XLINK);
    46 
    47 	poly=new QCanvasPolygon (canvas);
    48 	poly->setBrush( color );
    49 	poly->setZ (Z_XLINK);
    50 
    51 	setVisibility (false);
    52 }
    53 
    54 void XLinkObj::copy (XLinkObj* other)
    55 {
    56 	// TODO copy not used yet
    57 	MapObj::copy (other);
    58 	setVisibility (other->visible);
    59 	beginBranch=other->beginBranch;
    60 	endBranch=other->endBranch;
    61 }
    62 
    63 void XLinkObj::setBegin (BranchObj *bo)
    64 {
    65 	if (bo) 
    66 	{
    67 		xLinkState=initXLink;
    68 		beginBranch=bo;
    69 		beginPos=beginBranch->getChildPos();
    70 	}	
    71 }
    72 
    73 BranchObj* XLinkObj::getBegin ()
    74 {
    75 	return beginBranch;
    76 }
    77 
    78 void XLinkObj::setEnd (BranchObj *bo)
    79 {
    80 	if (bo) 
    81 	{
    82 		xLinkState=initXLink;
    83 		endBranch=bo;
    84 		endPos=endBranch->getChildPos();
    85 	}		
    86 }
    87 
    88 BranchObj* XLinkObj::getEnd()
    89 {
    90 	return endBranch;
    91 }
    92 
    93 void XLinkObj::setWidth (int w)
    94 {
    95 	width=w;
    96 	setColor (color);
    97 }
    98 
    99 int XLinkObj::getWidth()
   100 {
   101 	return width;
   102 }
   103 
   104 void XLinkObj::setColor(QColor c)
   105 {
   106 	color=c;
   107 	line->setPen (QPen(color, width));
   108 	poly->setBrush( color );
   109 }
   110 
   111 QColor XLinkObj::getColor()
   112 {
   113 	return color;
   114 }
   115 
   116 void XLinkObj::setEnd (QPoint p)
   117 {
   118 	endPos=p;
   119 }
   120 
   121 bool XLinkObj::activate ()
   122 {
   123 	if (beginBranch && endBranch)
   124 	{
   125 		if (beginBranch==endBranch) return false;
   126 		xLinkState=activeXLink;
   127 		beginBranch->addXLink (this);
   128 		endBranch->addXLink (this);
   129 		setVisibility ();
   130 		return true;
   131 	} else
   132 		return false;
   133 }
   134 
   135 void XLinkObj::deactivate ()
   136 {
   137 	if (beginBranch)
   138 		beginBranch->removeXLinkRef (this);
   139 	beginBranch=NULL;	
   140 	if (endBranch)
   141 		endBranch->removeXLinkRef (this);
   142 	endBranch=NULL;	
   143 	visBranch=NULL;
   144 	xLinkState=undefinedXLink;
   145 
   146 	line->hide();
   147 }
   148 
   149 bool XLinkObj::isUsed()
   150 {
   151 	if (beginBranch || endBranch || xLinkState!=undefinedXLink)
   152 		return true;
   153 	else
   154 		return false;
   155 }
   156 
   157 void XLinkObj::updateXLink()
   158 {
   159 	QPoint a,b;
   160 	QPointArray pa (3);
   161 	if (visBranch)
   162 	{
   163 		// Only one of the linked branches is visible
   164 		a=b=visBranch->getChildPos();
   165 		if (visBranch->getOrientation()==OrientRightOfCenter)
   166 		{
   167 			b.setX (b.x()+25);
   168 			pa.putPoints (0,3,
   169 				b.x(),b.y(),
   170 				b.x()-arrowSize,b.y()-arrowSize,
   171 				b.x()-arrowSize,b.y()+arrowSize
   172 			);
   173 			poly->setPoints (pa);
   174 		} else
   175 		{
   176 			b.setX (b.x()-25);
   177 			pa.putPoints (0,3,
   178 				b.x(),b.y(),
   179 				b.x()+arrowSize,b.y()-arrowSize,
   180 				b.x()+arrowSize,b.y()+arrowSize);
   181 			poly->setPoints (pa);
   182 		}	
   183 	} else
   184 	{
   185 		// Both linked branches are visible
   186 		if (beginBranch)
   187 			// If a link is just drawn in the editor,
   188 			// we have already a beginBranch
   189 			a=beginBranch->getChildPos();
   190 		else
   191 			// This shouldn't be reached normally...
   192 			a=beginPos;
   193 		if (xLinkState==activeXLink && endBranch)
   194 			b=endBranch->getChildPos();
   195 		else
   196 			b=endPos;
   197 	}
   198 
   199 
   200 	if (line->startPoint()==a && line->endPoint()==b && !visBranch)
   201 	{
   202 		// update is called from both branches, so only
   203 		// update if something has changed
   204 		return;
   205 	}	
   206 	else
   207 	{
   208 		beginPos=a;
   209 		endPos=b;
   210 		line->setPen (QPen(color, width));
   211 		line->setPoints (a.x(), a.y(), b.x(), b.y());
   212 	}
   213 }
   214 
   215 BranchObj* XLinkObj::otherBranch(BranchObj* thisBranch)
   216 {
   217 	if (!beginBranch && !endBranch)
   218 		return NULL;
   219 	if (thisBranch==beginBranch)
   220 		return endBranch;
   221 	else	
   222 		return beginBranch;
   223 }
   224 
   225 void XLinkObj::positionBBox()
   226 {
   227 }
   228 
   229 void XLinkObj::calcBBoxSize()
   230 {
   231 }
   232 
   233 void XLinkObj::setVisibility (bool b)
   234 {
   235 	MapObj::setVisibility (b);
   236 	if (b)
   237 	{
   238 		line->show();
   239 		if (visBranch) 
   240 			poly->show();
   241 		else	
   242 			poly->hide();
   243 	}	
   244 	else
   245 	{
   246 		line->hide();
   247 		poly->hide();
   248 	}	
   249 }
   250 
   251 void XLinkObj::setVisibility ()
   252 {
   253 	if (beginBranch && endBranch)
   254 	{
   255 		if(beginBranch->isVisibleObj() && endBranch->isVisibleObj())
   256 		{	// Both ends are visible
   257 			visBranch=NULL;
   258 			setVisibility (true);
   259 		} else
   260 		{
   261 			if(!beginBranch->isVisibleObj() && !endBranch->isVisibleObj())
   262 			{	//None of the ends is visible
   263 				visBranch=NULL;
   264 				setVisibility (false);
   265 			} else
   266 			{	// Just one end is visible, draw a symbol that shows
   267 				// that there is a link to a scrolled branch
   268 				if (beginBranch->isVisibleObj())
   269 					visBranch=beginBranch;
   270 				else
   271 					visBranch=endBranch;
   272 				setVisibility (true);
   273 			}
   274 		}
   275 	}
   276 }
   277 
   278 QString XLinkObj::saveToDir ()
   279 {
   280 	QString s="";
   281 	if (beginBranch && endBranch &&xLinkState==activeXLink)
   282 	{
   283 		if (beginBranch==endBranch && xLinkState)
   284 			s="";
   285 		else
   286 		{
   287 			QString colAttr=attribut ("color",color.name());
   288 			QString widAttr=attribut ("width",QString().setNum(width,10));
   289 			QString begSelAttr=attribut ("beginBranch",beginBranch->getSelectString());
   290 			QString endSelAttr=attribut ("endBranch",  endBranch->getSelectString());
   291 			s=beginElement ("xlink", colAttr +widAttr +begSelAttr +endSelAttr);
   292 
   293 			s+=endElement ("xlink");
   294 		}
   295 	}
   296 	return s;
   297 }
   298