linkobj.cpp
author insilmaril
Mon, 18 Apr 2005 06:17:00 +0000
changeset 95 f688a9913724
parent 94 6783e13bb05d
permissions -rw-r--r--
added basic xLink functions
insilmaril@86
     1
#include "linkobj.h"
insilmaril@86
     2
#include "branchobj.h"
insilmaril@86
     3
insilmaril@86
     4
insilmaril@86
     5
/////////////////////////////////////////////////////////////////
insilmaril@86
     6
// LinkObj
insilmaril@86
     7
/////////////////////////////////////////////////////////////////
insilmaril@86
     8
insilmaril@86
     9
LinkObj::LinkObj ():MapObj() 
insilmaril@86
    10
{
insilmaril@94
    11
	//	cout << "Const LinkObj ()\n";
insilmaril@94
    12
	init();
insilmaril@86
    13
}
insilmaril@86
    14
insilmaril@86
    15
LinkObj::LinkObj (QCanvas* c):MapObj(c)
insilmaril@86
    16
{
insilmaril@94
    17
	//	cout << "Const LinkObj (c)  called from MapCenterObj (c)\n";
insilmaril@94
    18
	init();
insilmaril@86
    19
}
insilmaril@86
    20
insilmaril@86
    21
insilmaril@86
    22
LinkObj::~LinkObj ()
insilmaril@86
    23
{
insilmaril@94
    24
	//	cout << "Destr LinkObj\n";
insilmaril@86
    25
	if (linkState!=undefinedLink)
insilmaril@86
    26
		deactivate();
insilmaril@86
    27
	delete (line);
insilmaril@86
    28
}
insilmaril@86
    29
insilmaril@86
    30
void LinkObj::init () 
insilmaril@86
    31
{
insilmaril@86
    32
	beginBranch=NULL;
insilmaril@86
    33
	endBranch=NULL;
insilmaril@94
    34
	visBranch=NULL;
insilmaril@86
    35
	linkState=undefinedLink;
insilmaril@86
    36
insilmaril@86
    37
	line=new QCanvasLine (canvas);
insilmaril@86
    38
	line->setPoints (0,0,200,200);
insilmaril@86
    39
	line->setPen (QPen(QColor(200,200,200), 1));
insilmaril@86
    40
insilmaril@86
    41
	setVisibility (false);
insilmaril@86
    42
}
insilmaril@86
    43
insilmaril@86
    44
void LinkObj::copy (LinkObj* other)
insilmaril@86
    45
{
insilmaril@86
    46
	// FIXME copy not used yet
insilmaril@86
    47
	cout << "LO::copy called\n";
insilmaril@86
    48
	MapObj::copy (other);
insilmaril@86
    49
	setVisibility (other->visible);
insilmaril@86
    50
	beginBranch=other->beginBranch;
insilmaril@86
    51
	endBranch=other->endBranch;
insilmaril@86
    52
}
insilmaril@86
    53
insilmaril@86
    54
void LinkObj::setBegin (BranchObj *bo)
insilmaril@86
    55
{
insilmaril@86
    56
	if (bo) 
insilmaril@86
    57
	{
insilmaril@86
    58
		linkState=initLink;
insilmaril@86
    59
		beginBranch=bo;
insilmaril@86
    60
		beginPos=beginBranch->getChildPos();
insilmaril@86
    61
	}	
insilmaril@86
    62
}
insilmaril@86
    63
insilmaril@86
    64
void LinkObj::setEnd (BranchObj *bo)
insilmaril@86
    65
{
insilmaril@86
    66
	if (bo) 
insilmaril@86
    67
	{
insilmaril@86
    68
		linkState=initLink;
insilmaril@86
    69
		endBranch=bo;
insilmaril@86
    70
		endPos=endBranch->getChildPos();
insilmaril@86
    71
	}		
insilmaril@86
    72
}
insilmaril@86
    73
insilmaril@86
    74
void LinkObj::setEnd (QPoint p)
insilmaril@86
    75
{
insilmaril@86
    76
	endPos=p;
insilmaril@86
    77
}
insilmaril@86
    78
insilmaril@86
    79
bool LinkObj::activate ()
insilmaril@86
    80
{
insilmaril@86
    81
	if (beginBranch && endBranch)
insilmaril@86
    82
	{
insilmaril@86
    83
		linkState=activeLink;
insilmaril@86
    84
		beginBranch->addLink (this);
insilmaril@86
    85
		endBranch->addLink (this);
insilmaril@86
    86
		setVisibility (true);
insilmaril@86
    87
		return true;
insilmaril@86
    88
	} else
insilmaril@86
    89
		return false;
insilmaril@86
    90
}
insilmaril@86
    91
insilmaril@86
    92
void LinkObj::deactivate ()
insilmaril@86
    93
{
insilmaril@86
    94
	if (beginBranch)
insilmaril@94
    95
		beginBranch->removeLinkRef (this);
insilmaril@86
    96
	beginBranch=NULL;	
insilmaril@86
    97
	if (endBranch)
insilmaril@94
    98
		endBranch->removeLinkRef (this);
insilmaril@86
    99
	endBranch=NULL;	
insilmaril@94
   100
	visBranch=NULL;
insilmaril@86
   101
	linkState=undefinedLink;
insilmaril@86
   102
insilmaril@86
   103
	line->hide();
insilmaril@86
   104
}
insilmaril@86
   105
insilmaril@86
   106
bool LinkObj::isUsed()
insilmaril@86
   107
{
insilmaril@86
   108
	if (beginBranch || endBranch || linkState!=undefinedLink)
insilmaril@86
   109
		return true;
insilmaril@86
   110
	else
insilmaril@86
   111
		return false;
insilmaril@86
   112
}
insilmaril@86
   113
insilmaril@86
   114
void LinkObj::updateLink()
insilmaril@86
   115
{
insilmaril@86
   116
	QPoint a,b;
insilmaril@94
   117
	if (visBranch)
insilmaril@94
   118
	{
insilmaril@94
   119
		// Only one of the linked branches is visible
insilmaril@94
   120
		a=b=visBranch->getChildPos();
insilmaril@94
   121
		if (visBranch->getOrientation()==OrientRightOfCenter)
insilmaril@94
   122
			b.setX (b.x()+25);
insilmaril@94
   123
		else
insilmaril@94
   124
			b.setX (b.x()-25);
insilmaril@94
   125
	} else
insilmaril@94
   126
	{
insilmaril@94
   127
		// Both linked branches are visible
insilmaril@94
   128
		if (beginBranch)
insilmaril@94
   129
			// If a link is just drawn in the editor,
insilmaril@94
   130
			// we have already a beginBranch
insilmaril@94
   131
			a=beginBranch->getChildPos();
insilmaril@94
   132
		else
insilmaril@94
   133
			// This shouldn't be reached normally...
insilmaril@94
   134
			a=beginPos;
insilmaril@94
   135
		if (linkState==activeLink && endBranch)
insilmaril@94
   136
			b=endBranch->getChildPos();
insilmaril@94
   137
		else
insilmaril@94
   138
			b=endPos;
insilmaril@94
   139
	}
insilmaril@94
   140
insilmaril@94
   141
insilmaril@94
   142
	if (line->startPoint()==a && line->endPoint()==b && !visBranch)
insilmaril@94
   143
	{
insilmaril@86
   144
		// update is called from both branches, so only
insilmaril@86
   145
		// update if needed
insilmaril@94
   146
		cout <<"LO__updateL  returnung...\n";
insilmaril@86
   147
		return;
insilmaril@94
   148
	}	
insilmaril@86
   149
	else
insilmaril@86
   150
	{
insilmaril@86
   151
		beginPos=a;
insilmaril@86
   152
		endPos=b;
insilmaril@86
   153
		line->setPoints (a.x(), a.y(), b.x(), b.y());
insilmaril@86
   154
	}
insilmaril@86
   155
}
insilmaril@86
   156
insilmaril@89
   157
BranchObj* LinkObj::otherBranch(BranchObj* thisBranch)
insilmaril@89
   158
{
insilmaril@89
   159
	if (!beginBranch && !endBranch)
insilmaril@89
   160
		return NULL;
insilmaril@89
   161
	if (thisBranch==beginBranch)
insilmaril@89
   162
		return endBranch;
insilmaril@89
   163
	else	
insilmaril@89
   164
		return beginBranch;
insilmaril@89
   165
}
insilmaril@89
   166
insilmaril@86
   167
void LinkObj::positionBBox()
insilmaril@86
   168
{
insilmaril@86
   169
}
insilmaril@86
   170
insilmaril@86
   171
void LinkObj::calcBBoxSize()
insilmaril@86
   172
{
insilmaril@86
   173
}
insilmaril@86
   174
insilmaril@86
   175
void LinkObj::setVisibility (bool b)
insilmaril@86
   176
{
insilmaril@86
   177
	MapObj::setVisibility (b);
insilmaril@86
   178
	if (b)
insilmaril@86
   179
	{
insilmaril@86
   180
		line->show();
insilmaril@86
   181
	}	
insilmaril@86
   182
	else
insilmaril@86
   183
	{
insilmaril@86
   184
		line->hide();
insilmaril@86
   185
	}	
insilmaril@86
   186
}
insilmaril@86
   187
insilmaril@94
   188
void LinkObj::setVisibility ()
insilmaril@94
   189
{
insilmaril@94
   190
	if (beginBranch && endBranch)
insilmaril@94
   191
	{
insilmaril@94
   192
		if(beginBranch->isVisibleObj() && endBranch->isVisibleObj())
insilmaril@94
   193
		{	// Both ends are visible
insilmaril@94
   194
			setVisibility (true);
insilmaril@94
   195
			visBranch=NULL;
insilmaril@94
   196
		} else
insilmaril@94
   197
		{
insilmaril@94
   198
			if(!beginBranch->isVisibleObj() && !endBranch->isVisibleObj())
insilmaril@94
   199
			{	//None of the ends is visible
insilmaril@94
   200
				setVisibility (false);
insilmaril@94
   201
				visBranch=NULL;
insilmaril@94
   202
			} else
insilmaril@94
   203
			{	// Just one end is visible, draw a symbol that shows
insilmaril@94
   204
				// that there is a link to a scrolled branch
insilmaril@94
   205
				setVisibility (true);
insilmaril@94
   206
				if (beginBranch->isVisibleObj())
insilmaril@94
   207
					visBranch=beginBranch;
insilmaril@94
   208
				else
insilmaril@94
   209
					visBranch=endBranch;
insilmaril@94
   210
					
insilmaril@94
   211
			}
insilmaril@94
   212
		}
insilmaril@94
   213
	}
insilmaril@94
   214
}
insilmaril@94
   215