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