xlinkobj.cpp
author insilmaril
Thu, 21 Apr 2005 19:14:38 +0000
changeset 97 0b048b6bb6f4
parent 95 f688a9913724
child 102 dba9303a1a5c
permissions -rw-r--r--
xlink color, width and defaults can be saved/ restored now
     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 	width=1;
    42 	color=QColor (180,180,180);
    43 	line=new QCanvasLine (canvas);
    44 	line->setPoints (0,0,200,200);
    45 	line->setPen (QPen(color, 1));
    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 	// FIXME copy not used yet
    58 	cout << "LO::copy called\n";
    59 	MapObj::copy (other);
    60 	setVisibility (other->visible);
    61 	beginBranch=other->beginBranch;
    62 	endBranch=other->endBranch;
    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 void XLinkObj::setEnd (BranchObj *bo)
    76 {
    77 	if (bo) 
    78 	{
    79 		xLinkState=initXLink;
    80 		endBranch=bo;
    81 		endPos=endBranch->getChildPos();
    82 	}		
    83 }
    84 
    85 void XLinkObj::setWidth (int w)
    86 {
    87 	width=w;
    88 	setColor (color);
    89 }
    90 
    91 int XLinkObj::getWidth()
    92 {
    93 	return width;
    94 }
    95 
    96 void XLinkObj::setColor(QColor c)
    97 {
    98 	color=c;
    99 	line->setPen (QPen(color, width));
   100 	poly->setBrush( color );
   101 }
   102 
   103 QColor XLinkObj::getColor()
   104 {
   105 	return color;
   106 }
   107 
   108 void XLinkObj::setEnd (QPoint p)
   109 {
   110 	endPos=p;
   111 }
   112 
   113 bool XLinkObj::activate ()
   114 {
   115 	if (beginBranch && endBranch)
   116 	{
   117 		xLinkState=activeXLink;
   118 		beginBranch->addXLink (this);
   119 		endBranch->addXLink (this);
   120 		setVisibility (true);
   121 		return true;
   122 	} else
   123 		return false;
   124 }
   125 
   126 void XLinkObj::deactivate ()
   127 {
   128 	if (beginBranch)
   129 		beginBranch->removeXLinkRef (this);
   130 	beginBranch=NULL;	
   131 	if (endBranch)
   132 		endBranch->removeXLinkRef (this);
   133 	endBranch=NULL;	
   134 	visBranch=NULL;
   135 	xLinkState=undefinedXLink;
   136 
   137 	line->hide();
   138 }
   139 
   140 bool XLinkObj::isUsed()
   141 {
   142 	if (beginBranch || endBranch || xLinkState!=undefinedXLink)
   143 		return true;
   144 	else
   145 		return false;
   146 }
   147 
   148 void XLinkObj::updateXLink()
   149 {
   150 	QPoint a,b;
   151 	QPointArray pa (3);
   152 	if (visBranch)
   153 	{
   154 		// Only one of the linked branches is visible
   155 		a=b=visBranch->getChildPos();
   156 		if (visBranch->getOrientation()==OrientRightOfCenter)
   157 		{
   158 			b.setX (b.x()+25);
   159 			pa.putPoints (0,3,
   160 				b.x(),b.y(),
   161 				b.x()-arrowSize,b.y()-arrowSize,
   162 				b.x()-arrowSize,b.y()+arrowSize
   163 			);
   164 			poly->setPoints (pa);
   165 		} else
   166 		{
   167 			b.setX (b.x()-25);
   168 			pa.putPoints (0,3,
   169 				b.x(),b.y(),
   170 				b.x()+arrowSize,b.y()-arrowSize,
   171 				b.x()+arrowSize,b.y()+arrowSize);
   172 			poly->setPoints (pa);
   173 		}	
   174 	} else
   175 	{
   176 		// Both linked branches are visible
   177 		if (beginBranch)
   178 			// If a link is just drawn in the editor,
   179 			// we have already a beginBranch
   180 			a=beginBranch->getChildPos();
   181 		else
   182 			// This shouldn't be reached normally...
   183 			a=beginPos;
   184 		if (xLinkState==activeXLink && endBranch)
   185 			b=endBranch->getChildPos();
   186 		else
   187 			b=endPos;
   188 	}
   189 
   190 
   191 	if (line->startPoint()==a && line->endPoint()==b && !visBranch)
   192 	{
   193 		// update is called from both branches, so only
   194 		// update if something has changed
   195 		return;
   196 	}	
   197 	else
   198 	{
   199 		beginPos=a;
   200 		endPos=b;
   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)
   272 	{
   273 		QString colAttr=attribut ("color",color.name());
   274 		QString widAttr=attribut ("width",QString().setNum(width,10));
   275 		QString begSelAttr=attribut ("beginBranch",beginBranch->getSelectString());
   276 		QString endSelAttr=attribut ("endBranch",  endBranch->getSelectString());
   277 		s=beginElement ("xlink", colAttr +widAttr +begSelAttr +endSelAttr);
   278 
   279 		s+=endElement ("xlink");
   280 	}
   281 	return s;
   282 }
   283