xlinkobj.cpp
author convert-repo
Fri, 23 Jul 2010 16:43:49 +0000
changeset 849 988f1908a7c4
parent 847 43268373032d
permissions -rw-r--r--
update tags
     1 #include <QDebug>
     2 
     3 #include "xlinkobj.h"
     4 
     5 #include "branchobj.h"
     6 #include "branchitem.h"
     7 
     8 /////////////////////////////////////////////////////////////////
     9 // XLinkObj
    10 /////////////////////////////////////////////////////////////////
    11 
    12 int XLinkObj::arrowSize=10;					// make instances
    13 
    14 XLinkObj::XLinkObj (QGraphicsScene* scene,Link *l):MapObj(scene)
    15 {
    16 	//qDebug()<< "Const XLinkObj (s,Link)";
    17 	link=l;
    18 	init();
    19 }
    20 
    21 
    22 
    23 XLinkObj::~XLinkObj ()
    24 {
    25 	//qDebug() << "Destr XLinkObj";
    26 	delete (line);
    27 	delete (poly);
    28 }
    29 
    30 
    31 void XLinkObj::init () 
    32 {
    33 	visBranch=NULL;
    34 
    35 	pen.setColor ( link->getColor() );
    36 	pen.setWidth ( link->getWidth() );
    37 	pen.setCapStyle (  Qt::RoundCap );
    38 	line=scene->addLine(QLineF(1,1,1,1),pen);
    39 	line->setZValue (Z_LINK);
    40 	poly=scene->addPolygon(QPolygonF(),pen, link->getColor());
    41 	poly->setZValue (Z_LINK);
    42 	setVisibility (true);
    43 }
    44 
    45 void XLinkObj::setEnd (QPointF p)
    46 {
    47 	endPos=p;
    48 }
    49 
    50 
    51 void XLinkObj::updateXLink()
    52 {
    53 	QPointF a,b;
    54 	QPolygonF pa;
    55 	if (visBranch)   
    56 	{
    57 		// Only one of the linked branches is visible
    58 		BranchObj *bo=(BranchObj*)(visBranch->getLMO());
    59 		if (!bo) return;
    60 
    61 		a=b=bo->getChildPos();
    62 		if (bo->getOrientation()==LinkableMapObj::RightOfCenter)
    63 		{
    64 			b.setX (b.x()+25);
    65 			
    66 			pa.clear();
    67 			pa<< QPointF(b.x(),b.y())<<
    68 				QPointF(b.x()-arrowSize,b.y()-arrowSize)<<
    69 				QPointF(b.x()-arrowSize,b.y()+arrowSize);
    70 			poly->setPolygon(pa);
    71 		} else
    72 		{
    73 			b.setX (b.x()-25);
    74 			pa.clear();
    75 			pa<< QPointF(b.x(),b.y())<<
    76 				QPointF(b.x()+arrowSize,b.y()-arrowSize)<<
    77 				QPointF(b.x()+arrowSize,b.y()+arrowSize);
    78 			poly->setPolygon (pa);
    79 		}	
    80 	} else
    81 	{
    82 		// Both linked branches are visible
    83 		BranchItem *bi=link->getBeginBranch();
    84 		if ( bi)
    85 		{
    86 			// If a link is just drawn in the editor,
    87 			// we have already a beginBranch
    88 			BranchObj *bo=(BranchObj*)(bi->getLMO());
    89 			if (bo)	
    90 				a=bo->getChildPos();
    91 			else 
    92 				return;	
    93 		}	
    94 		else
    95 			// This shouldn't be reached normally...
    96 			a=beginPos;
    97 
    98 		bi=link->getEndBranch();
    99 		if (bi)
   100 		{
   101 			BranchObj *bo=(BranchObj*)(bi->getLMO());
   102 			if (bo)	
   103 				b=bo->getChildPos();
   104 			else 
   105 				return;	
   106 		}
   107 		else
   108 			b=endPos;
   109 	}
   110 
   111 	beginPos=a;
   112 	endPos=b;
   113 	pen.setColor ( link->getColor() );
   114 	pen.setWidth ( link->getWidth() );
   115 	poly->setBrush (link->getColor() );
   116 	line->setPen (pen);
   117 	line->setLine(a.x(), a.y(), b.x(), b.y());
   118 }
   119 
   120 void XLinkObj::positionBBox()
   121 {
   122 }
   123 
   124 void XLinkObj::calcBBoxSize()
   125 {
   126 }
   127 
   128 void XLinkObj::setVisibility (bool b)
   129 {
   130 	MapObj::setVisibility (b);
   131 	if (b)
   132 	{
   133 		line->show();
   134 		if (visBranch) 
   135 			poly->show();
   136 		else	
   137 			poly->hide();
   138 	}	
   139 	else
   140 	{
   141 		line->hide();
   142 		poly->hide();
   143 	}	
   144 }
   145 
   146 void XLinkObj::setVisibility ()
   147 {
   148 	BranchItem* beginBI=link->getBeginBranch();
   149 	BranchObj* beginBO=NULL;
   150 	if (beginBI) beginBO=(BranchObj*)(beginBI->getLMO());
   151 
   152 	BranchObj* endBO=NULL;
   153 	BranchItem* endBI=link->getEndBranch();
   154 	if (endBI) endBO=(BranchObj*)(endBI->getLMO());
   155 	if (beginBO && endBO)
   156 	{
   157 		if(beginBO->isVisibleObj() && endBO->isVisibleObj())
   158 		{	// Both ends are visible
   159 			visBranch=NULL;
   160 			setVisibility (true);
   161 		} else
   162 		{
   163 			if(!beginBO->isVisibleObj() && !endBO->isVisibleObj())
   164 			{	//None of the ends is visible
   165 				visBranch=NULL;
   166 				setVisibility (false);
   167 			} else
   168 			{	// Just one end is visible, draw a symbol that shows
   169 				// that there is a link to a scrolled branch
   170 				if (beginBO->isVisibleObj())
   171 					visBranch=beginBI;
   172 				else
   173 					visBranch=endBI;
   174 				setVisibility (true);
   175 			}
   176 		}
   177 	}
   178 }
   179 
   180