]> git.sven.stormbind.net Git - sven/vym.git/blob - xlink.cpp
4c286dc90c28241cad6d0947358bd7d4d12bcc19
[sven/vym.git] / xlink.cpp
1 #include <QDebug>
2
3 #include "xlink.h"
4
5 #include "branchitem.h"
6 #include "misc.h"
7 #include "vymmodel.h"
8 #include "xlinkitem.h"
9 #include "xlinkobj.h"
10
11 class VymModel;
12
13 /////////////////////////////////////////////////////////////////
14 // Link
15 /////////////////////////////////////////////////////////////////
16
17 Link::Link (VymModel *m)
18 {
19     //qDebug() << "Const Link () this="<<this;
20     model=m;
21     init();
22 }
23
24 Link::~Link ()
25 {
26 //    qDebug()<<"* Destr Link begin this="<<this<<"  bLI="<<beginLinkItem<<"  eLI="<<endLinkItem;
27     deactivate();
28 //    qDebug()<<"* Destr Link end   this="<<this;
29 }
30
31 void Link::init () 
32 {
33     xlo=NULL;
34     beginBranch=NULL;
35     endBranch=NULL;
36     beginLinkItem=NULL;
37     endLinkItem=NULL;
38     xLinkState=Link::undefinedXLink;
39
40     type=Bezier;
41     pen=model->getMapDefXLinkPen();
42 }
43
44 VymModel* Link::getModel()
45 {
46     return model;
47 }
48
49 void Link::setBeginBranch (BranchItem *bi)
50 {
51     if (bi) 
52     {
53         xLinkState=initXLink;
54         beginBranch=bi;
55     }   
56 }
57
58 BranchItem* Link::getBeginBranch ()
59 {
60     return beginBranch;
61 }
62
63 void Link::setEndBranch (BranchItem *bi)
64 {
65     if (bi) 
66     {
67         endBranch=bi;
68         if (xlo) xlo->initC1();
69     }       
70 }
71
72 BranchItem* Link::getEndBranch()
73 {
74     return endBranch;
75 }
76
77 void Link::setEndPoint (QPointF p)
78 {
79     // Used only while creating the link, without endBranch
80     if (xlo) xlo->setEnd (p);
81 }
82
83 void Link::setBeginLinkItem (XLinkItem *li)
84 {
85     if (li) 
86     {
87         xLinkState=initXLink;
88         beginLinkItem=li;
89     }   
90 }
91
92 XLinkItem* Link::getBeginLinkItem ()
93 {
94     return beginLinkItem;
95 }
96
97 void Link::setEndLinkItem (XLinkItem *li)
98 {
99     if (li) 
100     {
101         xLinkState=initXLink;
102         endLinkItem=li;
103     }       
104 }
105
106 XLinkItem* Link::getEndLinkItem()
107 {
108     return endLinkItem;
109 }
110
111 XLinkItem* Link::getOtherEnd (XLinkItem *xli)
112 {
113     if (xli==beginLinkItem) return endLinkItem;
114     if (xli==endLinkItem) return beginLinkItem;
115     return NULL;
116 }
117
118 void Link::setPen (const QPen &p)
119 {
120     pen = p;
121     if (xlo) xlo->updateXLink();
122 }
123
124 QPen Link::getPen ()
125 {
126     return pen;
127 }
128
129 void Link::setLinkType (const QString &s)
130 {
131     if (s=="Linear")
132         type=Linear;
133     else if (s=="Bezier")       
134         type=Bezier;
135     else
136         qWarning()<<"Link::setLinkType  Unknown type: "<<s;
137 }
138
139 void Link::setStyleBegin (const QString &s)
140 {
141     if (xlo) 
142     {
143         xlo->setStyleBegin( s );
144         xlo->updateXLink();
145     }
146 }
147
148 QString Link::getStyleBeginString()
149 {
150     if (xlo)
151         return ArrowObj::styleToString( xlo->getStyleBegin() );
152     else 
153         return QString();
154 }
155
156 void Link::setStyleEnd (const QString &s)
157 {
158     if (xlo) 
159     {
160         xlo->setStyleEnd( s );
161         xlo->updateXLink();
162     }
163 }
164
165 QString Link::getStyleEndString()
166 {
167     if (xlo)
168         return ArrowObj::styleToString( xlo->getStyleEnd() );
169     else 
170         return QString();
171 }
172
173 bool Link::activate ()  
174 {
175     if (beginBranch && endBranch)
176     {
177         if ( beginBranch == endBranch) return false;
178         xLinkState = activeXLink;
179         model->updateActions();
180         return true;
181     } else
182         return false;
183 }
184
185 void Link::deactivate ()    
186 {
187     // Remove pointers from XLinkItem to Link and
188     // delete XLinkObj
189
190 //    qDebug()<<"Link::deactivate ******************************";
191     xLinkState=deleteXLink;
192     if (beginLinkItem) beginLinkItem->setLink (NULL);
193     if (endLinkItem) endLinkItem->setLink (NULL);
194     if (xlo)
195     {
196         delete (xlo);  
197         xlo=NULL;
198     }
199 }
200
201 Link::XLinkState Link::getState()
202 {
203     return xLinkState;
204 }
205
206 void Link::removeXLinkItem (XLinkItem *xli)
207 {
208     // Only mark _one_ end for removal here!
209     if (xli==beginLinkItem) beginLinkItem=NULL;
210     if (xli==endLinkItem) endLinkItem=NULL;
211     xLinkState=deleteXLink;
212 }
213
214 void Link::updateLink()
215 {
216     if(xlo ) xlo->updateXLink();
217 }
218
219 QString Link::saveToDir ()
220 {
221 //    qDebug()<<"Link::saveToDir  this="<<this<<" beginBranch="<<beginBranch<<"  endBranch="<<endBranch<<"  state="<<xLinkState;
222     QString s="";
223     if (beginBranch && endBranch && xLinkState==activeXLink)
224     {
225         if (beginBranch==endBranch )
226             qWarning ("Link::saveToDir  ignored, because beginBranch==endBranch, ");
227         else
228         {
229             QString colAttr=attribut ("color",pen.color().name());
230             QString widAttr=attribut ("width",QString().setNum(pen.width(),10));
231             QString styAttr=attribut ("penstyle",penStyleToString (pen.style()));
232             QString ctrlAttr;
233             QString typeAttr;
234             switch (type)
235             {
236                 case Linear: 
237                     typeAttr=attribut("type","Linear"); 
238                     break;
239                 case Bezier: 
240                     typeAttr=attribut("type","Bezier"); 
241                     if (xlo)
242                     {
243                         ctrlAttr +=attribut ("c0",pointToString (xlo->getC0() ) );
244                         ctrlAttr +=attribut ("c1",pointToString (xlo->getC1() ) );
245                     }
246                     break;
247             }
248             QString begSelAttr=attribut ("beginID",model->getSelectString(beginBranch));
249             QString endSelAttr=attribut ("endID",  model->getSelectString(endBranch));
250             QString styleAttr;
251             if (xlo)
252             {
253                 styleAttr = QString(" styleBegin=\"%1\"").arg( ArrowObj::styleToString( xlo->getStyleBegin() ));
254                 styleAttr+= QString(" styleEnd=\"%1\""  ).arg( ArrowObj::styleToString( xlo->getStyleEnd() ));
255             }
256             s=singleElement ("xlink", 
257                 colAttr 
258                 +widAttr 
259                 +styAttr 
260                 +typeAttr 
261                 +ctrlAttr
262                 +begSelAttr 
263                 +endSelAttr
264                 +styleAttr);
265         }
266     }
267     return s;
268 }
269
270 XLinkObj* Link::getXLinkObj()
271 {
272     return xlo;
273 }
274
275 XLinkObj* Link::createMapObj()  
276 {
277     if (!xlo) xlo=new XLinkObj (beginBranch->getLMO(),this);  
278     xlo->setVisibility();
279     return xlo;
280 }
281
282 MapObj* Link::getMO()
283 {
284     return xlo;
285 }
286