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