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