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