xlinkitem.cpp
author insilmaril
Thu, 26 Nov 2009 14:55:23 +0000
changeset 810 a9295db4dcbf
parent 795 6b0a5f4923d3
child 814 31de6677aa96
permissions -rw-r--r--
More fixes to new findwidget
     1 #include <QGraphicsScene>
     2 #include "xlinkitem.h"
     3 
     4 #include "branchitem.h"
     5 #include "linkablemapobj.h"
     6 #include "vymmodel.h"
     7 #include "xlinkobj.h"
     8 
     9 #include <iostream>
    10 using namespace std;
    11 
    12 /////////////////////////////////////////////////////////////////
    13 // XLinkItem
    14 /////////////////////////////////////////////////////////////////
    15 
    16 XLinkItem::XLinkItem (const QList<QVariant> &data, TreeItem *parent):MapItem (data,parent)
    17 
    18 {
    19 	//cout << "Const XLinkItem () "<<this<<endl;
    20 	init();
    21 }
    22 
    23 XLinkItem::~XLinkItem ()
    24 {
    25 	cout << "Destr XLinkItem "<<this<<"  lmo="<<lmo<<endl;
    26 	if (lmo){cout <<" calling delete\n"; delete (lmo);}
    27 	if (partnerXLink)
    28 	{
    29 		// Also delete partner 
    30 		cout << "  deleting partner="<<partnerXLink<<endl;
    31 		partnerXLink->partnerXLink=NULL;	// avoid endless recusion
    32 		model->deleteItem (partnerXLink);
    33 	}
    34 }
    35 
    36 
    37 void XLinkItem::init () 
    38 {
    39 	setType (XLink);
    40 	beginBranch=NULL;
    41 	endBranch=NULL;
    42 	partnerXLink=NULL;
    43 	isBeginXLink=true;
    44 	xLinkState=XLinkItem::undefinedXLink;
    45 
    46 	color=QColor (180,180,180);
    47 	width=1;
    48 }
    49 
    50 void XLinkItem::setBegin (BranchItem *bi)
    51 {
    52 	if (bi) 
    53 	{
    54 		xLinkState=initXLink;
    55 		beginBranch=bi;
    56 	}	
    57 }
    58 
    59 BranchItem* XLinkItem::getBegin ()
    60 {
    61 	return beginBranch;
    62 }
    63 
    64 void XLinkItem::setEnd (BranchItem *bi)
    65 {
    66 	if (bi) 
    67 	{
    68 		xLinkState=initXLink;
    69 		endBranch=bi;
    70 	}		
    71 }
    72 
    73 BranchItem* XLinkItem::getEnd()
    74 {
    75 	return endBranch;
    76 }
    77 
    78 void XLinkItem::setWidth (int w)
    79 {
    80 	if (isBeginXLink)
    81 	{
    82 		width=w;
    83 		if (lmo) ((XLinkObj*)lmo)->updateXLink();
    84 		return;
    85 	}
    86 	if (partnerXLink)
    87 		partnerXLink->setWidth (w);
    88 }
    89 
    90 int XLinkItem::getWidth()
    91 {
    92 	if (isBeginXLink) return width;
    93 	if (partnerXLink)
    94 		return partnerXLink->getWidth();
    95 	else
    96 		return -1;
    97 }
    98 
    99 void XLinkItem::setColor(QColor c)
   100 {
   101 	if (isBeginXLink)
   102 	{
   103 		color=c;
   104 		if (lmo) ((XLinkObj*)lmo)->updateXLink();
   105 		return;
   106 	}	
   107 	if (partnerXLink)
   108 		partnerXLink->setColor (c);
   109 }
   110 
   111 QColor XLinkItem::getColor()
   112 {
   113 	if (isBeginXLink) return color;
   114 	if (partnerXLink)
   115 		return partnerXLink->getColor();
   116 	else
   117 		return QColor();
   118 }
   119 
   120 void XLinkItem::setEnd (QPointF p)
   121 {
   122 	if (lmo) ((XLinkObj*)lmo)->setEnd (p);
   123 }
   124 
   125 bool XLinkItem::activate ()	
   126 {
   127 	if (beginBranch && endBranch)
   128 	{
   129 		if (beginBranch==endBranch) return false;
   130 
   131 		partnerXLink=model->createXLink (endBranch);
   132 		partnerXLink->setBegin (beginBranch);
   133 		partnerXLink->setEnd (endBranch);
   134 		partnerXLink->partnerXLink=this;
   135 		partnerXLink->isBeginXLink=false;
   136 
   137 		xLinkState=activeXLink;
   138 		partnerXLink->xLinkState=activeXLink;
   139 		partnerXLink->setHeading ("xLink to: "+beginBranch->getHeading());
   140 		setHeading ("xLink to: "+endBranch->getHeading());
   141 
   142 		model->updateActions();
   143 		return true;
   144 	} else
   145 		return false;
   146 }
   147 
   148 bool XLinkItem::isBegin()
   149 {
   150 	return isBeginXLink;
   151 }
   152 
   153 void XLinkItem::updateXLink()
   154 {
   155 	if(lmo && isBeginXLink) 
   156 		((XLinkObj*)lmo)->updateXLink();
   157 	else 
   158 		if (partnerXLink) partnerXLink->updateXLink();
   159 }
   160 
   161 void XLinkItem::updateVisibility()
   162 {
   163 	if (lmo) lmo->updateVisibility();
   164 }
   165 
   166 BranchItem* XLinkItem::getPartnerBranch()
   167 {
   168 	if (!beginBranch && !endBranch)
   169 		return NULL;
   170 	if (isBeginXLink)
   171 		return endBranch;
   172 	else	
   173 		return beginBranch;
   174 }
   175 
   176 
   177 XLinkItem* XLinkItem::getPartnerXLink()
   178 {
   179 	return partnerXLink;
   180 }
   181 
   182 
   183 QString XLinkItem::saveToDir ()
   184 {
   185 	QString s="";
   186 	if (beginBranch && endBranch && xLinkState==activeXLink)
   187 	{
   188 		if (beginBranch==endBranch )
   189 			qWarning ("XLI::saveToDir  beginBranch==endBranch"); //FIXME-3	s=""
   190 		else
   191 		{
   192 			QString colAttr=attribut ("color",color.name());
   193 			QString widAttr=attribut ("width",QString().setNum(width,10));
   194 			QString begSelAttr=attribut ("beginID",model->getSelectString(beginBranch));
   195 			QString endSelAttr=attribut ("endID",  model->getSelectString(endBranch));
   196 			s=beginElement ("xlink", colAttr +widAttr +begSelAttr +endSelAttr);
   197 
   198 			s+=endElement ("xlink");
   199 		}
   200 	}
   201 	return s;
   202 }
   203 
   204 XLinkObj* XLinkItem::createMapObj(QGraphicsScene *scene)  
   205 {
   206 	XLinkObj* xlo=new XLinkObj (scene,this);
   207 	lmo=(LinkableMapObj*)xlo;
   208 	return xlo;
   209 }
   210