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