OSDN Git Service

[denncoCreator] refactoring
[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 canInterface) :
34     TKCell(container, location, name, canInterface), 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::getIsCellCodeClassAssgined() 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     //TODO
193 }
194
195 void DCCell::setViewPageX(float x)
196 {
197     d_vComponent->setPageX(x);
198 }
199
200 void DCCell::setViewPageY(float y)
201 {
202     d_vComponent->setPageY(y);
203 }
204
205 void DCCell::setViewSize(float size)
206 {
207     if (size > S_MAXSIZE)
208         size = S_MAXSIZE;
209     else if (size < S_MINSIZE)
210         size = S_MINSIZE;
211
212     if (d_viewSize != size)
213     {
214         d_viewSize = size;
215         if (d_vComponent)
216             d_vComponent->updateShape();
217     }
218 }
219
220 void DCCell::setViewHeight(float _height)
221 {
222     if (d_viewHeight != _height)
223     {
224         d_viewHeight = _height;
225         if (d_vComponent)
226             d_vComponent->updateShape();
227     }
228 }
229
230 bool DCCell::renameReceptor(const QString &oldName, const QString &newName)
231 {
232     if (newName.length() == 0)
233         return false;
234
235     TKReceptorMap::iterator newNameIt = mReceptors.find(newName.toStdString());
236     if (newNameIt != mReceptors.end())
237         return false;
238
239     TKReceptorMap::iterator it = mReceptors.find(oldName.toStdString());
240     if (it != mReceptors.end())
241     {
242         TKReceptor *receptor = (*it).second;
243         mReceptors.erase(it);
244         mReceptors.insert( TKReceptorMap::value_type( newName.toStdString(), receptor ) );
245         return true;
246     }
247     return false;
248 }
249
250 bool DCCell::removeReceptor(const QString &name)
251 {
252     TKReceptorMap::iterator it = mReceptors.find(name.toStdString());
253     if (it != mReceptors.end())
254     {
255         TKReceptor *receptor = (*it).second;
256         mReceptors.erase(it);
257         delete receptor;
258         return true;
259     }
260     return false;
261 }
262
263 bool DCCell::removeReceptor(DCReceptor *receptor)
264 {
265     for ( TKReceptorMap::iterator it = mReceptors.begin(); it != mReceptors.end(); ++it )
266     {
267         if (it->second == receptor)
268         {
269             mReceptors.erase(it);
270             delete receptor;
271             return true;
272         }
273     }
274     return false;
275 }
276
277 DCReceptor* DCCell::createReceptor(const QString &receptorName)
278 {
279     return dynamic_cast<DCReceptor*>(TKCell::createReceptor(receptorName.toStdString()));
280 }
281
282 bool DCCell::removeAllConnections()
283 {
284     TKReceptorMap::iterator it = mReceptors.begin();
285     while(it != mReceptors.end())
286     {
287         DCReceptor *receptor = dynamic_cast<DCReceptor*>(it->second);
288         DCAxonTerminal *terminal = receptor->getTarget();
289         if (terminal)
290         {
291             DCAxon *receptorTargetCellAxon = dynamic_cast<DCAxon*>(terminal->getOwner());
292             if (receptorTargetCellAxon)
293             {
294                 receptorTargetCellAxon->removeAxonTerminal(terminal);
295             }
296         }
297         mReceptors.erase(it++);
298         delete receptor;
299     }
300
301     DCAxon *axon = dynamic_cast<DCAxon*>(mAxon);
302
303     for (int i = 0; i < axon->getNumberOfTerminals(); i++)
304     {
305         DCAxonTerminal *terminal = axon->getTerminalAt(i);
306         DCReceptor *axonTargetReceptor = terminal->getTarget();
307         DCCell *axonTargetCell = axonTargetReceptor->getOwnerCell();
308         if (axonTargetCell)
309         {
310             axonTargetCell->removeReceptor(axonTargetReceptor);
311         }
312     }
313     return true;
314 }
315
316 void DCCell::changeName(const QString &newName)
317 {
318     mName = newName.toStdString();
319     d_vComponent->updateShape();
320 }
321
322 void DCCell::changePath(const QString &newPath)
323 {
324     mLocation = newPath.toStdString();
325     d_vComponent->updateShape();
326 }
327
328 void DCCell::changeType(const QString &type)
329 {
330     d_type = type;
331     d_vComponent->updateShape();
332 }