1 // Copyright (c) 2012 Dennco Project
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.
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.
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/>.
17 // Created by tkawata on Sep-30, 2012.
21 #include "dcvpagecomponent.h"
22 #include "dccontainer.h"
24 #include "dcreceptor.h"
25 #include "dccellcode.h"
26 #include "DCAxonTerminal.h"
30 const float DCCell::S_MAXSIZE = 5;
31 const float DCCell::S_MINSIZE = 0.5;
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)
37 d_type = QString::fromStdString(type);
42 removeAllConnections();
50 void DCCell::bindComponent(DCVPageComponent *component)
52 d_vComponent = component;
53 d_vComponent->updateShape();
56 std::string DCCell::getReceptorName(DCReceptor *receptor) const
58 for ( TKReceptorMap::const_iterator it = mReceptors.begin(); it != mReceptors.end(); ++it ) {
59 if (it->second == receptor)
67 DCReceptor* DCCell::getReceptor(const QString &receptorName)
69 DCReceptor *receptor = NULL;
70 const TKReceptorMap *receptors = getReceptors();
71 TKReceptorMap::const_iterator it = receptors->begin();
72 while( it != receptors->end())
74 QString aReceptorName = QString::fromStdString((*it).first);
75 if (receptorName == aReceptorName)
77 receptor = dynamic_cast<DCReceptor*>((*it).second);
85 bool DCCell::setCellCode(TKCellCode *code, const void *data)
89 if (d_cellCode != code)
91 d_cellCode = dynamic_cast<DCCellCode*>(code);
92 r = TKCell::setCellCode(code,data);
95 d_vComponent->updateShape();
97 emit cellCodeChanged();
102 DCVCPage* DCCell::getPageBelonging() const
104 return d_vComponent->getPageBelonging();
107 bool DCCell::getIsCellCodeAssgined() const
109 DCContainer *container = dynamic_cast<DCContainer*>(getContainer());
111 return d_cellCode && (container && d_cellCode != container->getEmptyCellCodeClass());
114 DCAxon* DCCell::getAxon() const
116 return dynamic_cast<DCAxon*>(mAxon);
119 QString DCCell::getCustomScript() const
121 return ((DCContainer*)mContainer)->readCustomScriptFromWorkFile(this);
124 bool DCCell::saveCustomScript(const QString &script)
126 return ((DCContainer*)mContainer)->saveCustomScriptToWorkFile(this, script.toStdString());
129 QString DCCell::getType() const
131 DCContainer *container = dynamic_cast<DCContainer*>(getContainer());
133 if (d_cellCode && (container && d_cellCode != container->getEmptyCellCodeClass()))
135 return QString::fromStdString(d_cellCode->getCellAPIName());
143 float DCCell::getViewPageX() const
145 return d_vComponent->getPageX();
148 float DCCell::getViewPageY() const
150 return d_vComponent->getPageY();
153 float DCCell::getViewSize() const
158 float DCCell::getViewHeight() const
163 void DCCell::getViewHCrossPoint(float dx, float dz, float *outX, float *outZ) const
165 float offset = d_viewSize * 0.5;
172 *outZ = dz < 0 ? -offset : offset;
176 float a = fabs(dz / dx);
179 *outX = offset * (dx < 0 ? -1 : 1);
180 *outZ = offset * a * (dz < 0 ? -1 : 1);
184 *outX = offset / a * (dx < 0 ? -1 : 1);
185 *outZ = offset * (dz < 0 ? -1 : 1);
190 void DCCell::getViewVCrossPoint(float dx, float dy, float *outX, float *outY) const
195 void DCCell::setViewPageX(float x)
197 d_vComponent->setPageX(x);
200 void DCCell::setViewPageY(float y)
202 d_vComponent->setPageY(y);
205 void DCCell::setViewSize(float size)
207 if (size > S_MAXSIZE)
209 else if (size < S_MINSIZE)
212 if (d_viewSize != size)
216 d_vComponent->updateShape();
220 void DCCell::setViewHeight(float _height)
222 if (d_viewHeight != _height)
224 d_viewHeight = _height;
226 d_vComponent->updateShape();
230 bool DCCell::renameReceptor(const QString &oldName, const QString &newName)
232 if (newName.length() == 0)
235 TKReceptorMap::iterator newNameIt = mReceptors.find(newName.toStdString());
236 if (newNameIt != mReceptors.end())
239 TKReceptorMap::iterator it = mReceptors.find(oldName.toStdString());
240 if (it != mReceptors.end())
242 TKReceptor *receptor = (*it).second;
243 mReceptors.erase(it);
244 mReceptors.insert( TKReceptorMap::value_type( newName.toStdString(), receptor ) );
250 bool DCCell::removeReceptor(const QString &name)
252 TKReceptorMap::iterator it = mReceptors.find(name.toStdString());
253 if (it != mReceptors.end())
255 TKReceptor *receptor = (*it).second;
256 mReceptors.erase(it);
263 bool DCCell::removeReceptor(DCReceptor *receptor)
265 for ( TKReceptorMap::iterator it = mReceptors.begin(); it != mReceptors.end(); ++it )
267 if (it->second == receptor)
269 mReceptors.erase(it);
277 DCReceptor* DCCell::createReceptor(const QString &receptorName)
279 return dynamic_cast<DCReceptor*>(TKCell::createReceptor(receptorName.toStdString()));
282 bool DCCell::removeAllConnections()
284 TKReceptorMap::iterator it = mReceptors.begin();
285 while(it != mReceptors.end())
287 DCReceptor *receptor = dynamic_cast<DCReceptor*>(it->second);
288 DCAxonTerminal *terminal = receptor->getTarget();
291 DCAxon *receptorTargetCellAxon = dynamic_cast<DCAxon*>(terminal->getOwner());
292 if (receptorTargetCellAxon)
294 receptorTargetCellAxon->removeAxonTerminal(terminal);
297 mReceptors.erase(it++);
301 DCAxon *axon = dynamic_cast<DCAxon*>(mAxon);
303 for (int i = 0; i < axon->getNumberOfTerminals(); i++)
305 DCAxonTerminal *terminal = axon->getTerminalAt(i);
306 DCReceptor *axonTargetReceptor = terminal->getTarget();
307 DCCell *axonTargetCell = axonTargetReceptor->getOwnerCell();
310 axonTargetCell->removeReceptor(axonTargetReceptor);
316 void DCCell::changeName(const QString &newName)
318 mName = newName.toStdString();
319 d_vComponent->updateShape();
322 void DCCell::changePath(const QString &newPath)
324 mLocation = newPath.toStdString();
325 d_vComponent->updateShape();
328 void DCCell::changeType(const QString &type)
331 d_vComponent->updateShape();