xlinkobj.cpp
author insilmaril
Mon, 23 May 2005 15:28:20 +0000
changeset 103 c810a11d11d9
parent 102 dba9303a1a5c
child 104 270593ab83b2
permissions -rw-r--r--
1.6.6 Exclusive flags added
     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 	// FIXME 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 void XLinkObj::setEnd (BranchObj *bo)
    74 {
    75 	if (bo) 
    76 	{
    77 		xLinkState=initXLink;
    78 		endBranch=bo;
    79 		endPos=endBranch->getChildPos();
    80 	}		
    81 }
    82 
    83 void XLinkObj::setWidth (int w)
    84 {
    85 	width=w;
    86 	setColor (color);
    87 }
    88 
    89 int XLinkObj::getWidth()
    90 {
    91 	cout << "XLO  w="<<width<<endl;
    92 	return width;
    93 }
    94 
    95 void XLinkObj::setColor(QColor c)
    96 {
    97 	color=c;
    98 	line->setPen (QPen(color, width));
    99 	poly->setBrush( color );
   100 }
   101 
   102 QColor XLinkObj::getColor()
   103 {
   104 	return color;
   105 }
   106 
   107 void XLinkObj::setEnd (QPoint p)
   108 {
   109 	endPos=p;
   110 }
   111 
   112 bool XLinkObj::activate ()
   113 {
   114 	if (beginBranch && endBranch)
   115 	{
   116 		if (beginBranch==endBranch)
   117 		{	//FIXME debugging
   118 			cout << "XLO::activate  b=e="<<beginBranch->getHeading()<<endl;
   119 			return false;
   120 		}
   121 		xLinkState=activeXLink;
   122 		beginBranch->addXLink (this);
   123 		endBranch->addXLink (this);
   124 		setVisibility ();
   125 		return true;
   126 	} else
   127 		return false;
   128 }
   129 
   130 void XLinkObj::deactivate ()
   131 {
   132 	if (beginBranch)
   133 		beginBranch->removeXLinkRef (this);
   134 	beginBranch=NULL;	
   135 	if (endBranch)
   136 		endBranch->removeXLinkRef (this);
   137 	endBranch=NULL;	
   138 	visBranch=NULL;
   139 	xLinkState=undefinedXLink;
   140 
   141 	line->hide();
   142 }
   143 
   144 bool XLinkObj::isUsed()
   145 {
   146 	if (beginBranch || endBranch || xLinkState!=undefinedXLink)
   147 		return true;
   148 	else
   149 		return false;
   150 }
   151 
   152 void XLinkObj::updateXLink()
   153 {
   154 	QPoint a,b;
   155 	QPointArray pa (3);
   156 	if (visBranch)
   157 	{
   158 		// Only one of the linked branches is visible
   159 		a=b=visBranch->getChildPos();
   160 		if (visBranch->getOrientation()==OrientRightOfCenter)
   161 		{
   162 			b.setX (b.x()+25);
   163 			pa.putPoints (0,3,
   164 				b.x(),b.y(),
   165 				b.x()-arrowSize,b.y()-arrowSize,
   166 				b.x()-arrowSize,b.y()+arrowSize
   167 			);
   168 			poly->setPoints (pa);
   169 		} else
   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 			poly->setPoints (pa);
   177 		}	
   178 	} else
   179 	{
   180 		// Both linked branches are visible
   181 		if (beginBranch)
   182 			// If a link is just drawn in the editor,
   183 			// we have already a beginBranch
   184 			a=beginBranch->getChildPos();
   185 		else
   186 			// This shouldn't be reached normally...
   187 			a=beginPos;
   188 		if (xLinkState==activeXLink && endBranch)
   189 			b=endBranch->getChildPos();
   190 		else
   191 			b=endPos;
   192 	}
   193 
   194 
   195 	if (line->startPoint()==a && line->endPoint()==b && !visBranch)
   196 	{
   197 		// update is called from both branches, so only
   198 		// update if something has changed
   199 		return;
   200 	}	
   201 	else
   202 	{
   203 		beginPos=a;
   204 		endPos=b;
   205 		line->setPen (QPen(color, width));
   206 		line->setPoints (a.x(), a.y(), b.x(), b.y());
   207 	}
   208 }
   209 
   210 BranchObj* XLinkObj::otherBranch(BranchObj* thisBranch)
   211 {
   212 	if (!beginBranch && !endBranch)
   213 		return NULL;
   214 	if (thisBranch==beginBranch)
   215 		return endBranch;
   216 	else	
   217 		return beginBranch;
   218 }
   219 
   220 void XLinkObj::positionBBox()
   221 {
   222 }
   223 
   224 void XLinkObj::calcBBoxSize()
   225 {
   226 }
   227 
   228 void XLinkObj::setVisibility (bool b)
   229 {
   230 	MapObj::setVisibility (b);
   231 	if (b)
   232 	{
   233 		line->show();
   234 		if (visBranch) 
   235 			poly->show();
   236 		else	
   237 			poly->hide();
   238 	}	
   239 	else
   240 	{
   241 		line->hide();
   242 		poly->hide();
   243 	}	
   244 }
   245 
   246 void XLinkObj::setVisibility ()
   247 {
   248 	if (beginBranch && endBranch)
   249 	{
   250 		if(beginBranch->isVisibleObj() && endBranch->isVisibleObj())
   251 		{	// Both ends are visible
   252 			visBranch=NULL;
   253 			setVisibility (true);
   254 		} else
   255 		{
   256 			if(!beginBranch->isVisibleObj() && !endBranch->isVisibleObj())
   257 			{	//None of the ends is visible
   258 				visBranch=NULL;
   259 				setVisibility (false);
   260 			} else
   261 			{	// Just one end is visible, draw a symbol that shows
   262 				// that there is a link to a scrolled branch
   263 				if (beginBranch->isVisibleObj())
   264 					visBranch=beginBranch;
   265 				else
   266 					visBranch=endBranch;
   267 				setVisibility (true);
   268 			}
   269 		}
   270 	}
   271 }
   272 
   273 QString XLinkObj::saveToDir ()
   274 {
   275 	QString s="";
   276 	if (beginBranch && endBranch &&xLinkState==activeXLink)
   277 	{
   278 		if (beginBranch==endBranch && xLinkState)
   279 		{
   280 			//FIXME testing
   281 			//cout << "   identical ends: "<<beginBranch->getSelectString()<<endl;
   282 			s="";
   283 		} else
   284 		{
   285 			QString colAttr=attribut ("color",color.name());
   286 			QString widAttr=attribut ("width",QString().setNum(width,10));
   287 			QString begSelAttr=attribut ("beginBranch",beginBranch->getSelectString());
   288 			QString endSelAttr=attribut ("endBranch",  endBranch->getSelectString());
   289 			s=beginElement ("xlink", colAttr +widAttr +begSelAttr +endSelAttr);
   290 
   291 			s+=endElement ("xlink");
   292 		}
   293 	}
   294 	//FIXME testing
   295 	//cout << s<<endl;
   296 	return s;
   297 }
   298