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