1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/xlinkitem.cpp Thu Sep 17 09:41:09 2009 +0000
1.3 @@ -0,0 +1,210 @@
1.4 +#include <QGraphicsScene>
1.5 +#include "xlinkitem.h"
1.6 +
1.7 +#include "branchitem.h"
1.8 +#include "linkablemapobj.h"
1.9 +#include "vymmodel.h"
1.10 +#include "xlinkobj.h"
1.11 +
1.12 +#include <iostream>
1.13 +using namespace std;
1.14 +
1.15 +/////////////////////////////////////////////////////////////////
1.16 +// XLinkItem
1.17 +/////////////////////////////////////////////////////////////////
1.18 +
1.19 +XLinkItem::XLinkItem (const QList<QVariant> &data, TreeItem *parent):MapItem (data,parent)
1.20 +
1.21 +{
1.22 + //cout << "Const XLinkItem () "<<this<<endl;
1.23 + init();
1.24 +}
1.25 +
1.26 +XLinkItem::~XLinkItem ()
1.27 +{
1.28 + cout << "Destr XLinkItem "<<this<<" lmo="<<lmo<<endl;
1.29 + if (lmo){cout <<" calling delete\n"; delete (lmo);}
1.30 + if (partnerXLink)
1.31 + {
1.32 + // Also delete partner
1.33 + cout << " deleting partner="<<partnerXLink<<endl;
1.34 + partnerXLink->partnerXLink=NULL; // avoid endless recusion
1.35 + model->deleteItem (partnerXLink);
1.36 + }
1.37 +}
1.38 +
1.39 +
1.40 +void XLinkItem::init ()
1.41 +{
1.42 + setType (XLink);
1.43 + beginBranch=NULL;
1.44 + endBranch=NULL;
1.45 + partnerXLink=NULL;
1.46 + isBeginXLink=true;
1.47 + xLinkState=XLinkItem::undefinedXLink;
1.48 +
1.49 + color=QColor (180,180,180);
1.50 + width=1;
1.51 +}
1.52 +
1.53 +void XLinkItem::setBegin (BranchItem *bi)
1.54 +{
1.55 + if (bi)
1.56 + {
1.57 + xLinkState=initXLink;
1.58 + beginBranch=bi;
1.59 + }
1.60 +}
1.61 +
1.62 +BranchItem* XLinkItem::getBegin ()
1.63 +{
1.64 + return beginBranch;
1.65 +}
1.66 +
1.67 +void XLinkItem::setEnd (BranchItem *bi)
1.68 +{
1.69 + if (bi)
1.70 + {
1.71 + xLinkState=initXLink;
1.72 + endBranch=bi;
1.73 + }
1.74 +}
1.75 +
1.76 +BranchItem* XLinkItem::getEnd()
1.77 +{
1.78 + return endBranch;
1.79 +}
1.80 +
1.81 +void XLinkItem::setWidth (int w)
1.82 +{
1.83 + if (isBeginXLink)
1.84 + {
1.85 + width=w;
1.86 + if (lmo) ((XLinkObj*)lmo)->updateXLink();
1.87 + return;
1.88 + }
1.89 + if (partnerXLink)
1.90 + partnerXLink->setWidth (w);
1.91 +}
1.92 +
1.93 +int XLinkItem::getWidth()
1.94 +{
1.95 + if (isBeginXLink) return width;
1.96 + if (partnerXLink)
1.97 + return partnerXLink->getWidth();
1.98 + else
1.99 + return -1;
1.100 +}
1.101 +
1.102 +void XLinkItem::setColor(QColor c)
1.103 +{
1.104 + if (isBeginXLink)
1.105 + {
1.106 + color=c;
1.107 + if (lmo) ((XLinkObj*)lmo)->updateXLink();
1.108 + return;
1.109 + }
1.110 + if (partnerXLink)
1.111 + partnerXLink->setColor (c);
1.112 +}
1.113 +
1.114 +QColor XLinkItem::getColor()
1.115 +{
1.116 + if (isBeginXLink) return color;
1.117 + if (partnerXLink)
1.118 + return partnerXLink->getColor();
1.119 + else
1.120 + return QColor();
1.121 +}
1.122 +
1.123 +void XLinkItem::setEnd (QPointF p)
1.124 +{
1.125 + if (lmo) ((XLinkObj*)lmo)->setEnd (p);
1.126 +}
1.127 +
1.128 +bool XLinkItem::activate ()
1.129 +{
1.130 + if (beginBranch && endBranch)
1.131 + {
1.132 + if (beginBranch==endBranch) return false;
1.133 +
1.134 + partnerXLink=model->createXLink (endBranch);
1.135 + partnerXLink->setBegin (beginBranch);
1.136 + partnerXLink->setEnd (endBranch);
1.137 + partnerXLink->partnerXLink=this;
1.138 + partnerXLink->isBeginXLink=false;
1.139 +
1.140 + xLinkState=activeXLink;
1.141 + partnerXLink->xLinkState=activeXLink;
1.142 + partnerXLink->setHeading ("xLink to: "+beginBranch->getHeading());
1.143 + setHeading ("xLink to: "+endBranch->getHeading());
1.144 +
1.145 + model->updateActions();
1.146 + return true;
1.147 + } else
1.148 + return false;
1.149 +}
1.150 +
1.151 +bool XLinkItem::isBegin()
1.152 +{
1.153 + return isBeginXLink;
1.154 +}
1.155 +
1.156 +void XLinkItem::updateXLink()
1.157 +{
1.158 + if(lmo && isBeginXLink)
1.159 + ((XLinkObj*)lmo)->updateXLink();
1.160 + else
1.161 + if (partnerXLink) partnerXLink->updateXLink();
1.162 +}
1.163 +
1.164 +void XLinkItem::updateVisibility()
1.165 +{
1.166 + if (lmo) lmo->updateVisibility();
1.167 +}
1.168 +
1.169 +BranchItem* XLinkItem::getPartnerBranch()
1.170 +{
1.171 + if (!beginBranch && !endBranch)
1.172 + return NULL;
1.173 + if (isBeginXLink)
1.174 + return endBranch;
1.175 + else
1.176 + return beginBranch;
1.177 +}
1.178 +
1.179 +
1.180 +XLinkItem* XLinkItem::getPartnerXLink()
1.181 +{
1.182 + return partnerXLink;
1.183 +}
1.184 +
1.185 +
1.186 +QString XLinkItem::saveToDir ()
1.187 +{
1.188 + QString s="";
1.189 + if (beginBranch && endBranch && xLinkState==activeXLink)
1.190 + {
1.191 + if (beginBranch==endBranch )
1.192 + qWarning ("XLI::saveToDir beginBranch==endBranch"); //FIXME-3 s=""
1.193 + else
1.194 + {
1.195 + QString colAttr=attribut ("color",color.name());
1.196 + QString widAttr=attribut ("width",QString().setNum(width,10));
1.197 + QString begSelAttr=attribut ("beginID",model->getSelectString(beginBranch));
1.198 + QString endSelAttr=attribut ("endID", model->getSelectString(endBranch));
1.199 + s=beginElement ("xlink", colAttr +widAttr +begSelAttr +endSelAttr);
1.200 +
1.201 + s+=endElement ("xlink");
1.202 + }
1.203 + }
1.204 + return s;
1.205 +}
1.206 +
1.207 +XLinkObj* XLinkItem::createMapObj(QGraphicsScene *scene)
1.208 +{
1.209 + XLinkObj* xlo=new XLinkObj (scene,this);
1.210 + lmo=(LinkableMapObj*)xlo;
1.211 + return xlo;
1.212 +}
1.213 +