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 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)
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
192 (void)dx; (void)dy; (void)outX; (void)outY;
196 QString DCCell::getWorkFilePathForCustomScript() const
198 DCContainer *container = dynamic_cast<DCContainer*>(getContainer());
201 return container->getWorkFilePathForCustomScript(this);
206 void DCCell::setViewPageX(float x)
208 d_vComponent->setPageX(x);
211 void DCCell::setViewPageY(float y)
213 d_vComponent->setPageY(y);
216 void DCCell::setViewSize(float size)
218 if (size > S_MAXSIZE)
220 else if (size < S_MINSIZE)
223 if (d_viewSize != size)
227 d_vComponent->updateShape();
231 void DCCell::setViewHeight(float _height)
233 if (d_viewHeight != _height)
235 d_viewHeight = _height;
237 d_vComponent->updateShape();
241 bool DCCell::renameReceptor(const QString &oldName, const QString &newName)
243 if (newName.length() == 0)
246 TKReceptorMap::iterator newNameIt = mReceptors.find(newName.toStdString());
247 if (newNameIt != mReceptors.end())
250 TKReceptorMap::iterator it = mReceptors.find(oldName.toStdString());
251 if (it != mReceptors.end())
253 TKReceptor *receptor = (*it).second;
254 mReceptors.erase(it);
255 mReceptors.insert( TKReceptorMap::value_type( newName.toStdString(), receptor ) );
261 bool DCCell::removeReceptor(const QString &name)
263 TKReceptorMap::iterator it = mReceptors.find(name.toStdString());
264 if (it != mReceptors.end())
266 TKReceptor *receptor = (*it).second;
267 mReceptors.erase(it);
274 bool DCCell::removeReceptor(DCReceptor *receptor)
276 for ( TKReceptorMap::iterator it = mReceptors.begin(); it != mReceptors.end(); ++it )
278 if (it->second == receptor)
280 mReceptors.erase(it);
288 DCReceptor* DCCell::createReceptor(const QString &receptorName)
290 return dynamic_cast<DCReceptor*>(TKCell::createReceptor(receptorName.toStdString()));
293 bool DCCell::removeAllConnections()
295 TKReceptorMap::iterator it = mReceptors.begin();
296 while(it != mReceptors.end())
298 DCReceptor *receptor = dynamic_cast<DCReceptor*>(it->second);
299 DCAxonTerminal *terminal = receptor->getTarget();
302 DCAxon *receptorTargetCellAxon = dynamic_cast<DCAxon*>(terminal->getOwner());
303 if (receptorTargetCellAxon)
305 receptorTargetCellAxon->removeAxonTerminal(terminal);
308 mReceptors.erase(it++);
312 DCAxon *axon = dynamic_cast<DCAxon*>(mAxon);
314 for (int i = 0; i < axon->getNumberOfTerminals(); i++)
316 DCAxonTerminal *terminal = axon->getTerminalAt(i);
317 DCReceptor *axonTargetReceptor = terminal->getTarget();
318 DCCell *axonTargetCell = axonTargetReceptor->getOwnerCell();
321 axonTargetCell->removeReceptor(axonTargetReceptor);
327 void DCCell::changeName(const QString &newName)
329 mName = newName.toStdString();
330 d_vComponent->updateShape();
333 void DCCell::changePath(const QString &newPath)
335 mLocation = newPath.toStdString();
336 d_vComponent->updateShape();
339 void DCCell::changeType(const QString &type)
342 d_vComponent->updateShape();