xlinkobj.cpp
author insilmaril
Mon, 18 Apr 2005 06:37:48 +0000
changeset 96 598768200cfa
parent 95 f688a9913724
child 97 0b048b6bb6f4
permissions -rw-r--r--
Removed linkobj.*
     1 #include "xlinkobj.h"
     2 #include "branchobj.h"
     3 
     4 
     5 /////////////////////////////////////////////////////////////////
     6 // XLinkObj
     7 /////////////////////////////////////////////////////////////////
     8 
     9 int XLinkObj::arrowSize=10;						// make instances 
    10 QColor XLinkObj::defXLinkColor=QColor(180,180,180); 
    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 	xLinkColor=defXLinkColor;
    42 	line=new QCanvasLine (canvas);
    43 	line->setPoints (0,0,200,200);
    44 	line->setPen (QPen(xLinkColor, 1));
    45 
    46 	poly=new QCanvasPolygon (canvas);
    47 	poly->setBrush( xLinkColor );
    48 
    49 	setVisibility (false);
    50 }
    51 
    52 void XLinkObj::copy (XLinkObj* other)
    53 {
    54 	// FIXME copy not used yet
    55 	cout << "LO::copy called\n";
    56 	MapObj::copy (other);
    57 	setVisibility (other->visible);
    58 	beginBranch=other->beginBranch;
    59 	endBranch=other->endBranch;
    60 }
    61 
    62 void XLinkObj::setBegin (BranchObj *bo)
    63 {
    64 	if (bo) 
    65 	{
    66 		xLinkState=initXLink;
    67 		beginBranch=bo;
    68 		beginPos=beginBranch->getChildPos();
    69 	}	
    70 }
    71 
    72 void XLinkObj::setEnd (BranchObj *bo)
    73 {
    74 	if (bo) 
    75 	{
    76 		xLinkState=initXLink;
    77 		endBranch=bo;
    78 		endPos=endBranch->getChildPos();
    79 	}		
    80 }
    81 
    82 void XLinkObj::setColor(QColor c)
    83 {
    84 	xLinkColor=c;
    85 }
    86 
    87 void XLinkObj::setEnd (QPoint p)
    88 {
    89 	endPos=p;
    90 }
    91 
    92 bool XLinkObj::activate ()
    93 {
    94 	if (beginBranch && endBranch)
    95 	{
    96 		xLinkState=activeXLink;
    97 		beginBranch->addXLink (this);
    98 		endBranch->addXLink (this);
    99 		setVisibility (true);
   100 		return true;
   101 	} else
   102 		return false;
   103 }
   104 
   105 void XLinkObj::deactivate ()
   106 {
   107 	if (beginBranch)
   108 		beginBranch->removeXLinkRef (this);
   109 	beginBranch=NULL;	
   110 	if (endBranch)
   111 		endBranch->removeXLinkRef (this);
   112 	endBranch=NULL;	
   113 	visBranch=NULL;
   114 	xLinkState=undefinedXLink;
   115 
   116 	line->hide();
   117 }
   118 
   119 bool XLinkObj::isUsed()
   120 {
   121 	if (beginBranch || endBranch || xLinkState!=undefinedXLink)
   122 		return true;
   123 	else
   124 		return false;
   125 }
   126 
   127 void XLinkObj::updateXLink()
   128 {
   129 	QPoint a,b;
   130 	QPointArray pa (3);
   131 	if (visBranch)
   132 	{
   133 		// Only one of the linked branches is visible
   134 		a=b=visBranch->getChildPos();
   135 		if (visBranch->getOrientation()==OrientRightOfCenter)
   136 		{
   137 			b.setX (b.x()+25);
   138 			pa.putPoints (0,3,
   139 				b.x(),b.y(),
   140 				b.x()-arrowSize,b.y()-arrowSize,
   141 				b.x()-arrowSize,b.y()+arrowSize
   142 			);
   143 			poly->setPoints (pa);
   144 		} else
   145 		{
   146 			b.setX (b.x()-25);
   147 			pa.putPoints (0,3,
   148 				b.x(),b.y(),
   149 				b.x()+arrowSize,b.y()-arrowSize,
   150 				b.x()+arrowSize,b.y()+arrowSize);
   151 			poly->setPoints (pa);
   152 		}	
   153 	} else
   154 	{
   155 		// Both linked branches are visible
   156 		if (beginBranch)
   157 			// If a link is just drawn in the editor,
   158 			// we have already a beginBranch
   159 			a=beginBranch->getChildPos();
   160 		else
   161 			// This shouldn't be reached normally...
   162 			a=beginPos;
   163 		if (xLinkState==activeXLink && endBranch)
   164 			b=endBranch->getChildPos();
   165 		else
   166 			b=endPos;
   167 	}
   168 
   169 
   170 	if (line->startPoint()==a && line->endPoint()==b && !visBranch)
   171 	{
   172 		// update is called from both branches, so only
   173 		// update if something has changed
   174 		return;
   175 	}	
   176 	else
   177 	{
   178 		beginPos=a;
   179 		endPos=b;
   180 		line->setPoints (a.x(), a.y(), b.x(), b.y());
   181 	}
   182 }
   183 
   184 BranchObj* XLinkObj::otherBranch(BranchObj* thisBranch)
   185 {
   186 	if (!beginBranch && !endBranch)
   187 		return NULL;
   188 	if (thisBranch==beginBranch)
   189 		return endBranch;
   190 	else	
   191 		return beginBranch;
   192 }
   193 
   194 void XLinkObj::positionBBox()
   195 {
   196 }
   197 
   198 void XLinkObj::calcBBoxSize()
   199 {
   200 }
   201 
   202 void XLinkObj::setVisibility (bool b)
   203 {
   204 	MapObj::setVisibility (b);
   205 	if (b)
   206 	{
   207 		line->show();
   208 		if (visBranch) 
   209 			poly->show();
   210 		else	
   211 			poly->hide();
   212 	}	
   213 	else
   214 	{
   215 		line->hide();
   216 		poly->hide();
   217 	}	
   218 }
   219 
   220 void XLinkObj::setVisibility ()
   221 {
   222 	if (beginBranch && endBranch)
   223 	{
   224 		if(beginBranch->isVisibleObj() && endBranch->isVisibleObj())
   225 		{	// Both ends are visible
   226 			visBranch=NULL;
   227 			setVisibility (true);
   228 		} else
   229 		{
   230 			if(!beginBranch->isVisibleObj() && !endBranch->isVisibleObj())
   231 			{	//None of the ends is visible
   232 				visBranch=NULL;
   233 				setVisibility (false);
   234 			} else
   235 			{	// Just one end is visible, draw a symbol that shows
   236 				// that there is a link to a scrolled branch
   237 				if (beginBranch->isVisibleObj())
   238 					visBranch=beginBranch;
   239 				else
   240 					visBranch=endBranch;
   241 				setVisibility (true);
   242 			}
   243 		}
   244 	}
   245 }
   246 
   247 QString XLinkObj::saveToDir ()
   248 {
   249 	QString s;
   250 	if (beginBranch && endBranch)
   251 	{
   252 		QString colAttr=attribut ("color",xLinkColor.name());
   253 		QString begSelAttr=attribut ("beginBranch",beginBranch->getSelectString());
   254 		QString endSelAttr=attribut ("endBranch",  endBranch->getSelectString());
   255 		s=beginElement ("xlink", colAttr +begSelAttr +endSelAttr);
   256 
   257 		s+=endElement ("xlink");
   258 	}
   259 	return s;
   260 }
   261