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