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