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