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