added linkobj.*
authorinsilmaril
Sun, 30 Jan 2005 23:28:08 +0000
changeset 860ed77eb5d1cd
parent 85 7e69734a51c4
child 87 5cf1836a440a
added linkobj.*
linkobj.cpp
linkobj.h
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/linkobj.cpp	Sun Jan 30 23:28:08 2005 +0000
     1.3 @@ -0,0 +1,159 @@
     1.4 +#include "linkobj.h"
     1.5 +#include "branchobj.h"
     1.6 +
     1.7 +
     1.8 +/////////////////////////////////////////////////////////////////
     1.9 +// LinkObj
    1.10 +/////////////////////////////////////////////////////////////////
    1.11 +
    1.12 +LinkObj::LinkObj ():MapObj() 
    1.13 +{
    1.14 +//	cout << "Const LinkObj ()\n";
    1.15 +    init();
    1.16 +}
    1.17 +
    1.18 +LinkObj::LinkObj (QCanvas* c):MapObj(c)
    1.19 +{
    1.20 +//	cout << "Const LinkObj (c)  called from MapCenterObj (c)\n";
    1.21 +    init();
    1.22 +}
    1.23 +
    1.24 +
    1.25 +LinkObj::~LinkObj ()
    1.26 +{
    1.27 +//	cout << "Destr LinkObj\n";
    1.28 +	if (linkState!=undefinedLink)
    1.29 +		deactivate();
    1.30 +	delete (line);
    1.31 +}
    1.32 +
    1.33 +void LinkObj::init () 
    1.34 +{
    1.35 +	beginBranch=NULL;
    1.36 +	endBranch=NULL;
    1.37 +	linkState=undefinedLink;
    1.38 +
    1.39 +	line=new QCanvasLine (canvas);
    1.40 +	line->setPoints (0,0,200,200);
    1.41 +	line->setPen (QPen(QColor(200,200,200), 1));
    1.42 +
    1.43 +	setVisibility (false);
    1.44 +}
    1.45 +
    1.46 +void LinkObj::copy (LinkObj* other)
    1.47 +{
    1.48 +	// FIXME copy not used yet
    1.49 +	cout << "LO::copy called\n";
    1.50 +	MapObj::copy (other);
    1.51 +	setVisibility (other->visible);
    1.52 +	beginBranch=other->beginBranch;
    1.53 +	endBranch=other->endBranch;
    1.54 +}
    1.55 +
    1.56 +void LinkObj::setBegin (BranchObj *bo)
    1.57 +{
    1.58 +	if (bo) 
    1.59 +	{
    1.60 +		linkState=initLink;
    1.61 +		beginBranch=bo;
    1.62 +		beginPos=beginBranch->getChildPos();
    1.63 +	}	
    1.64 +}
    1.65 +
    1.66 +void LinkObj::setEnd (BranchObj *bo)
    1.67 +{
    1.68 +	if (bo) 
    1.69 +	{
    1.70 +		linkState=initLink;
    1.71 +		endBranch=bo;
    1.72 +		endPos=endBranch->getChildPos();
    1.73 +	}		
    1.74 +}
    1.75 +
    1.76 +void LinkObj::setEnd (QPoint p)
    1.77 +{
    1.78 +	endPos=p;
    1.79 +}
    1.80 +
    1.81 +bool LinkObj::activate ()
    1.82 +{
    1.83 +	if (beginBranch && endBranch)
    1.84 +	{
    1.85 +		linkState=activeLink;
    1.86 +		beginBranch->addLink (this);
    1.87 +		endBranch->addLink (this);
    1.88 +		setVisibility (true);
    1.89 +		return true;
    1.90 +	} else
    1.91 +		return false;
    1.92 +}
    1.93 +
    1.94 +void LinkObj::deactivate ()
    1.95 +{
    1.96 +	if (beginBranch)
    1.97 +		beginBranch->removeLink (this);
    1.98 +	beginBranch=NULL;	
    1.99 +	if (endBranch)
   1.100 +		endBranch->removeLink (this);
   1.101 +	endBranch=NULL;	
   1.102 +	linkState=undefinedLink;
   1.103 +
   1.104 +	line->hide();
   1.105 +}
   1.106 +
   1.107 +bool LinkObj::isUsed()
   1.108 +{
   1.109 +	if (beginBranch || endBranch || linkState!=undefinedLink)
   1.110 +		return true;
   1.111 +	else
   1.112 +		return false;
   1.113 +}
   1.114 +
   1.115 +void LinkObj::updateLink()
   1.116 +{
   1.117 +	QPoint a,b;
   1.118 +	if (beginBranch)
   1.119 +		// If a link is just drawed in the editor,
   1.120 +		// we have already a beginBranch
   1.121 +		a=beginBranch->getChildPos();
   1.122 +	else
   1.123 +		// This shouldn't be reached normally...
   1.124 +		a=beginPos;
   1.125 +	if (linkState==activeLink && endBranch)
   1.126 +		b=endBranch->getChildPos();
   1.127 +	else
   1.128 +		b=endPos;
   1.129 +	
   1.130 +	if (line->startPoint()==a && line->endPoint()==b)
   1.131 +		// update is called from both branches, so only
   1.132 +		// update if needed
   1.133 +		return;
   1.134 +	else
   1.135 +	{
   1.136 +		beginPos=a;
   1.137 +		endPos=b;
   1.138 +		line->setPoints (a.x(), a.y(), b.x(), b.y());
   1.139 +	}
   1.140 +}
   1.141 +
   1.142 +void LinkObj::positionBBox()
   1.143 +{
   1.144 +}
   1.145 +
   1.146 +void LinkObj::calcBBoxSize()
   1.147 +{
   1.148 +}
   1.149 +
   1.150 +void LinkObj::setVisibility (bool b)
   1.151 +{
   1.152 +	MapObj::setVisibility (b);
   1.153 +	if (b)
   1.154 +	{
   1.155 +		line->show();
   1.156 +	}	
   1.157 +	else
   1.158 +	{
   1.159 +		line->hide();
   1.160 +	}	
   1.161 +}
   1.162 +
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/linkobj.h	Sun Jan 30 23:28:08 2005 +0000
     2.3 @@ -0,0 +1,36 @@
     2.4 +#ifndef LINKOBJ_H
     2.5 +#define LINKOBJ_H
     2.6 +
     2.7 +#include "linkablemapobj.h"
     2.8 +
     2.9 +enum LinkState {undefinedLink,initLink,activeLink,deleteLink};
    2.10 +
    2.11 +/////////////////////////////////////////////////////////////////////////////
    2.12 +class LinkObj:public MapObj {
    2.13 +public:
    2.14 +    LinkObj ();
    2.15 +    LinkObj (QCanvas*);
    2.16 +    ~LinkObj ();
    2.17 +    virtual void init ();
    2.18 +    virtual void copy (LinkObj*);
    2.19 +	void setBegin (BranchObj*);
    2.20 +	void setEnd   (BranchObj*);
    2.21 +	void setEnd   (QPoint);
    2.22 +	bool activate ();			// Sets pointers in branchObjects
    2.23 +	void deactivate();			// removes those pointers
    2.24 +	bool isUsed();				// true, if at least on branch uses it
    2.25 +	void updateLink();
    2.26 +	void positionBBox();
    2.27 +	void calcBBoxSize();
    2.28 +	void setVisibility (bool);
    2.29 +
    2.30 +private:
    2.31 +	QCanvasLine *line;
    2.32 +	BranchObj *beginBranch;
    2.33 +	BranchObj *endBranch;
    2.34 +	LinkState linkState;	// init during drawing or active
    2.35 +	QPoint beginPos;
    2.36 +	QPoint   endPos;
    2.37 +};
    2.38 +
    2.39 +#endif