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