linkobj.cpp
author insilmaril
Mon, 31 Jan 2005 09:47:43 +0000
changeset 88 9771028de303
parent 86 0ed77eb5d1cd
child 89 9db3eaa21237
permissions -rw-r--r--
fixed segfault when loading .vyp
     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 void LinkObj::positionBBox()
   140 {
   141 }
   142 
   143 void LinkObj::calcBBoxSize()
   144 {
   145 }
   146 
   147 void LinkObj::setVisibility (bool b)
   148 {
   149 	MapObj::setVisibility (b);
   150 	if (b)
   151 	{
   152 		line->show();
   153 	}	
   154 	else
   155 	{
   156 		line->hide();
   157 	}	
   158 }
   159