linkobj.cpp
author insilmaril
Thu, 24 Mar 2005 20:48:58 +0000
changeset 92 ec8c73975c1a
parent 89 9db3eaa21237
child 94 6783e13bb05d
permissions -rw-r--r--
added modifier modes (color,link,copy). Not fully functional yet
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@86
    11
//	cout << "Const LinkObj ()\n";
insilmaril@86
    12
    init();
insilmaril@86
    13
}
insilmaril@86
    14
insilmaril@86
    15
LinkObj::LinkObj (QCanvas* c):MapObj(c)
insilmaril@86
    16
{
insilmaril@86
    17
//	cout << "Const LinkObj (c)  called from MapCenterObj (c)\n";
insilmaril@86
    18
    init();
insilmaril@86
    19
}
insilmaril@86
    20
insilmaril@86
    21
insilmaril@86
    22
LinkObj::~LinkObj ()
insilmaril@86
    23
{
insilmaril@86
    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@86
    34
	linkState=undefinedLink;
insilmaril@86
    35
insilmaril@86
    36
	line=new QCanvasLine (canvas);
insilmaril@86
    37
	line->setPoints (0,0,200,200);
insilmaril@86
    38
	line->setPen (QPen(QColor(200,200,200), 1));
insilmaril@86
    39
insilmaril@86
    40
	setVisibility (false);
insilmaril@86
    41
}
insilmaril@86
    42
insilmaril@86
    43
void LinkObj::copy (LinkObj* other)
insilmaril@86
    44
{
insilmaril@86
    45
	// FIXME copy not used yet
insilmaril@86
    46
	cout << "LO::copy called\n";
insilmaril@86
    47
	MapObj::copy (other);
insilmaril@86
    48
	setVisibility (other->visible);
insilmaril@86
    49
	beginBranch=other->beginBranch;
insilmaril@86
    50
	endBranch=other->endBranch;
insilmaril@86
    51
}
insilmaril@86
    52
insilmaril@86
    53
void LinkObj::setBegin (BranchObj *bo)
insilmaril@86
    54
{
insilmaril@86
    55
	if (bo) 
insilmaril@86
    56
	{
insilmaril@86
    57
		linkState=initLink;
insilmaril@86
    58
		beginBranch=bo;
insilmaril@86
    59
		beginPos=beginBranch->getChildPos();
insilmaril@86
    60
	}	
insilmaril@86
    61
}
insilmaril@86
    62
insilmaril@86
    63
void LinkObj::setEnd (BranchObj *bo)
insilmaril@86
    64
{
insilmaril@86
    65
	if (bo) 
insilmaril@86
    66
	{
insilmaril@86
    67
		linkState=initLink;
insilmaril@86
    68
		endBranch=bo;
insilmaril@86
    69
		endPos=endBranch->getChildPos();
insilmaril@86
    70
	}		
insilmaril@86
    71
}
insilmaril@86
    72
insilmaril@86
    73
void LinkObj::setEnd (QPoint p)
insilmaril@86
    74
{
insilmaril@86
    75
	endPos=p;
insilmaril@86
    76
}
insilmaril@86
    77
insilmaril@86
    78
bool LinkObj::activate ()
insilmaril@86
    79
{
insilmaril@86
    80
	if (beginBranch && endBranch)
insilmaril@86
    81
	{
insilmaril@86
    82
		linkState=activeLink;
insilmaril@86
    83
		beginBranch->addLink (this);
insilmaril@86
    84
		endBranch->addLink (this);
insilmaril@86
    85
		setVisibility (true);
insilmaril@86
    86
		return true;
insilmaril@86
    87
	} else
insilmaril@86
    88
		return false;
insilmaril@86
    89
}
insilmaril@86
    90
insilmaril@86
    91
void LinkObj::deactivate ()
insilmaril@86
    92
{
insilmaril@86
    93
	if (beginBranch)
insilmaril@86
    94
		beginBranch->removeLink (this);
insilmaril@86
    95
	beginBranch=NULL;	
insilmaril@86
    96
	if (endBranch)
insilmaril@86
    97
		endBranch->removeLink (this);
insilmaril@86
    98
	endBranch=NULL;	
insilmaril@86
    99
	linkState=undefinedLink;
insilmaril@86
   100
insilmaril@86
   101
	line->hide();
insilmaril@86
   102
}
insilmaril@86
   103
insilmaril@86
   104
bool LinkObj::isUsed()
insilmaril@86
   105
{
insilmaril@86
   106
	if (beginBranch || endBranch || linkState!=undefinedLink)
insilmaril@86
   107
		return true;
insilmaril@86
   108
	else
insilmaril@86
   109
		return false;
insilmaril@86
   110
}
insilmaril@86
   111
insilmaril@86
   112
void LinkObj::updateLink()
insilmaril@86
   113
{
insilmaril@86
   114
	QPoint a,b;
insilmaril@86
   115
	if (beginBranch)
insilmaril@86
   116
		// If a link is just drawed in the editor,
insilmaril@86
   117
		// we have already a beginBranch
insilmaril@86
   118
		a=beginBranch->getChildPos();
insilmaril@86
   119
	else
insilmaril@86
   120
		// This shouldn't be reached normally...
insilmaril@86
   121
		a=beginPos;
insilmaril@86
   122
	if (linkState==activeLink && endBranch)
insilmaril@86
   123
		b=endBranch->getChildPos();
insilmaril@86
   124
	else
insilmaril@86
   125
		b=endPos;
insilmaril@86
   126
	
insilmaril@86
   127
	if (line->startPoint()==a && line->endPoint()==b)
insilmaril@86
   128
		// update is called from both branches, so only
insilmaril@86
   129
		// update if needed
insilmaril@86
   130
		return;
insilmaril@86
   131
	else
insilmaril@86
   132
	{
insilmaril@86
   133
		beginPos=a;
insilmaril@86
   134
		endPos=b;
insilmaril@86
   135
		line->setPoints (a.x(), a.y(), b.x(), b.y());
insilmaril@86
   136
	}
insilmaril@86
   137
}
insilmaril@86
   138
insilmaril@89
   139
BranchObj* LinkObj::otherBranch(BranchObj* thisBranch)
insilmaril@89
   140
{
insilmaril@89
   141
	if (!beginBranch && !endBranch)
insilmaril@89
   142
		return NULL;
insilmaril@89
   143
	if (thisBranch==beginBranch)
insilmaril@89
   144
		return endBranch;
insilmaril@89
   145
	else	
insilmaril@89
   146
		return beginBranch;
insilmaril@89
   147
}
insilmaril@89
   148
insilmaril@86
   149
void LinkObj::positionBBox()
insilmaril@86
   150
{
insilmaril@86
   151
}
insilmaril@86
   152
insilmaril@86
   153
void LinkObj::calcBBoxSize()
insilmaril@86
   154
{
insilmaril@86
   155
}
insilmaril@86
   156
insilmaril@86
   157
void LinkObj::setVisibility (bool b)
insilmaril@86
   158
{
insilmaril@86
   159
	MapObj::setVisibility (b);
insilmaril@86
   160
	if (b)
insilmaril@86
   161
	{
insilmaril@86
   162
		line->show();
insilmaril@86
   163
	}	
insilmaril@86
   164
	else
insilmaril@86
   165
	{
insilmaril@86
   166
		line->hide();
insilmaril@86
   167
	}	
insilmaril@86
   168
}
insilmaril@86
   169