OSDN Git Service

[denncoCreator] fixed a crash bug.
[dennco/denncoCreator.git] / Source / dccell.cpp
1 //  Copyright (c) 2012 Dennco Project
2 //
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 //
17 //  Created by tkawata on Sep-30, 2012.
18 //
19 #include "dccell.h"
20
21 #include "dcvpagecomponent.h"
22 #include "dccontainer.h"
23 #include "dcaxon.h"
24 #include "dcreceptor.h"
25 #include "dccellcode.h"
26 #include "dcaxonterminal.h"
27
28 #include "DNUtils.h"
29
30 const float DCCell::S_MAXSIZE = 5;
31 const float DCCell::S_MINSIZE = 0.5;
32
33 DCCell::DCCell(DCContainer *container, std::string location, std::string name, std::string type, bool canInputInterface, bool canOutputInterface) :
34     TKCell(container, location, name, canInputInterface, canOutputInterface), d_vComponent(NULL), d_cellCode(NULL),
35     d_viewSize(0.5), d_viewHeight(0.5)
36 {
37     d_type = QString::fromStdString(type);
38 }
39
40 DCCell::~DCCell()
41 {
42     removeAllConnections();
43     if (d_vComponent)
44     {
45         delete d_vComponent;
46         d_vComponent = NULL;
47     }
48 }
49
50 void DCCell::bindComponent(DCVPageComponent *component)
51 {
52     d_vComponent = component;
53     d_vComponent->updateShape();
54 }
55
56 std::string DCCell::getReceptorName(DCReceptor *receptor) const
57 {
58     for ( TKReceptorMap::const_iterator it = mReceptors.begin(); it != mReceptors.end(); ++it ) {
59         if (it->second == receptor)
60         {
61             return it->first;
62         }
63     }
64     return "";
65 }
66
67 DCReceptor* DCCell::getReceptor(const QString &receptorName)
68 {
69     DCReceptor *receptor = NULL;
70     const TKReceptorMap *receptors = getReceptors();
71     TKReceptorMap::const_iterator it = receptors->begin();
72     while( it != receptors->end())
73     {
74         QString aReceptorName = QString::fromStdString((*it).first);
75         if (receptorName == aReceptorName)
76         {
77             receptor = dynamic_cast<DCReceptor*>((*it).second);
78             break;
79         }
80         ++it;
81     }
82     return receptor;
83 }
84
85 bool DCCell::setCellCode(TKCellCode *code, const void *data)
86 {
87     bool r = false;
88
89     if (d_cellCode != code)
90     {
91         d_cellCode = dynamic_cast<DCCellCode*>(code);
92         r = TKCell::setCellCode(code,data);
93
94         if (d_vComponent)
95             d_vComponent->updateShape();
96
97         emit cellCodeChanged();
98     }
99     return r;
100 }
101
102 DCVCPage* DCCell::getPageBelonging() const
103 {
104     return d_vComponent->getPageBelonging();
105 }
106
107 bool DCCell::getIsCellCodeAssgined() const
108 {
109     DCContainer *container = dynamic_cast<DCContainer*>(getContainer());
110
111     return d_cellCode && (container && d_cellCode != container->getEmptyCellCodeClass());
112 }
113
114 DCAxon* DCCell::getAxon() const
115 {
116     return dynamic_cast<DCAxon*>(mAxon);
117 }
118
119 QString DCCell::getCustomScript() const
120 {
121     return ((DCContainer*)mContainer)->readCustomScriptFromWorkFile(this);
122 }
123
124 bool DCCell::saveCustomScript(const QString &script)
125 {
126     return ((DCContainer*)mContainer)->saveCustomScriptToWorkFile(this, script.toStdString());
127 }
128
129 QString DCCell::getType() const
130 {
131     DCContainer *container = dynamic_cast<DCContainer*>(getContainer());
132
133     if (d_cellCode && (container && d_cellCode != container->getEmptyCellCodeClass()))
134     {
135         return QString::fromStdString(d_cellCode->getCellAPIName());
136     }
137     else
138     {
139         return d_type;
140     }
141 }
142
143 float DCCell::getViewPageX() const
144 {
145     return d_vComponent->getPageX();
146 }
147
148 float DCCell::getViewPageY() const
149 {
150     return d_vComponent->getPageY();
151 }
152
153 float DCCell::getViewSize() const
154 {
155     return d_viewSize;
156 }
157
158 float DCCell::getViewHeight() const
159 {
160     return d_viewHeight;
161 }
162
163 void DCCell::getViewHCrossPoint(float dx, float dz, float *outX, float *outZ) const
164 {
165     float offset = d_viewSize * 0.5;
166
167     *outX = 0;
168     *outZ = 0;
169
170     if (dx == 0)
171     {
172         *outZ = dz < 0 ? -offset : offset;
173     }
174     else
175     {
176         float a = fabs(dz / dx);
177         if (a < 1)
178         {
179             *outX = offset * (dx < 0 ? -1 : 1);
180             *outZ = offset * a * (dz < 0 ? -1 : 1);
181         }
182         else
183         {
184             *outX = offset / a * (dx < 0 ? -1 : 1);
185             *outZ = offset * (dz < 0 ? -1 : 1);
186         }
187     }
188 }
189
190 void DCCell::getViewVCrossPoint(float dx, float dy, float *outX, float *outY) const
191 {
192     (void)dx; (void)dy; (void)outX; (void)outY;
193     //TODO
194 }
195
196 QString DCCell::getWorkFilePathForCustomScript() const
197 {
198     DCContainer *container = dynamic_cast<DCContainer*>(getContainer());
199     if (container)
200     {
201         return container->getWorkFilePathForCustomScript(this);
202     }
203     return "";
204 }
205
206 void DCCell::setViewPageX(float x)
207 {
208     d_vComponent->setPageX(x);
209 }
210
211 void DCCell::setViewPageY(float y)
212 {
213     d_vComponent->setPageY(y);
214 }
215
216 void DCCell::setViewSize(float size)
217 {
218     if (size > S_MAXSIZE)
219         size = S_MAXSIZE;
220     else if (size < S_MINSIZE)
221         size = S_MINSIZE;
222
223     if (d_viewSize != size)
224     {
225         d_viewSize = size;
226         if (d_vComponent)
227             d_vComponent->updateShape();
228     }
229 }
230
231 void DCCell::setViewHeight(float _height)
232 {
233     if (d_viewHeight != _height)
234     {
235         d_viewHeight = _height;
236         if (d_vComponent)
237             d_vComponent->updateShape();
238     }
239 }
240
241 bool DCCell::renameReceptor(const QString &oldName, const QString &newName)
242 {
243     if (newName.length() == 0)
244         return false;
245
246     TKReceptorMap::iterator newNameIt = mReceptors.find(newName.toStdString());
247     if (newNameIt != mReceptors.end())
248         return false;
249
250     TKReceptorMap::iterator it = mReceptors.find(oldName.toStdString());
251     if (it != mReceptors.end())
252     {
253         TKReceptor *receptor = (*it).second;
254         mReceptors.erase(it);
255         mReceptors.insert( TKReceptorMap::value_type( newName.toStdString(), receptor ) );
256         return true;
257     }
258     return false;
259 }
260
261 bool DCCell::removeReceptor(const QString &name)
262 {
263     TKReceptorMap::iterator it = mReceptors.find(name.toStdString());
264     if (it != mReceptors.end())
265     {
266         TKReceptor *receptor = (*it).second;
267         mReceptors.erase(it);
268         delete receptor;
269         return true;
270     }
271     return false;
272 }
273
274 bool DCCell::removeReceptor(DCReceptor *receptor)
275 {
276     for ( TKReceptorMap::iterator it = mReceptors.begin(); it != mReceptors.end(); ++it )
277     {
278         if (it->second == receptor)
279         {
280             mReceptors.erase(it);
281             delete receptor;
282             return true;
283         }
284     }
285     return false;
286 }
287
288 DCReceptor* DCCell::createReceptor(const QString &receptorName)
289 {
290     return dynamic_cast<DCReceptor*>(TKCell::createReceptor(receptorName.toStdString()));
291 }
292
293 bool DCCell::removeAllConnections()
294 {
295     TKReceptorMap::iterator it = mReceptors.begin();
296     while(it != mReceptors.end())
297     {
298         DCReceptor *receptor = dynamic_cast<DCReceptor*>(it->second);
299         DCAxonTerminal *terminal = receptor->getTarget();
300         if (terminal)
301         {
302             DCAxon *receptorTargetCellAxon = dynamic_cast<DCAxon*>(terminal->getOwner());
303             if (receptorTargetCellAxon)
304             {
305                 receptorTargetCellAxon->removeAxonTerminal(terminal);
306             }
307         }
308         mReceptors.erase(it++);
309         delete receptor;
310     }
311
312     DCAxon *axon = dynamic_cast<DCAxon*>(mAxon);
313
314     for (int i = 0; i < axon->getNumberOfTerminals(); i++)
315     {
316         DCAxonTerminal *terminal = axon->getTerminalAt(i);
317         DCReceptor *axonTargetReceptor = terminal->getTarget();
318         DCCell *axonTargetCell = axonTargetReceptor->getOwnerCell();
319         if (axonTargetCell)
320         {
321             axonTargetCell->removeReceptor(axonTargetReceptor);
322         }
323     }
324     return true;
325 }
326
327 void DCCell::changeName(const QString &newName)
328 {
329     mName = newName.toStdString();
330     d_vComponent->updateShape();
331 }
332
333 void DCCell::changePath(const QString &newPath)
334 {
335     mLocation = newPath.toStdString();
336     d_vComponent->updateShape();
337 }
338
339 void DCCell::changeType(const QString &type)
340 {
341     d_type = type;
342     d_vComponent->updateShape();
343 }