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