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