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