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 2/22/2012.
19 #include "dnqscontainer.h"
22 #include "TKCellCode.h"
24 #include "TKReceptor.h"
25 #include "TKAxonTerminal.h"
26 #include "TKContainer.h"
29 #include "versioninfo.h"
30 #include "dnqsbasiccell.h"
31 #include "dnqsinputcell.h"
32 #include "dnqsoutputcell.h"
33 #include "dnqsbasicstoragecell.h"
34 #include "dnplugininputcell.h"
35 #include "dnpluginoutputcell.h"
36 #include "dnqscellcode.h"
39 static QScriptValue scriptPrint(QScriptContext *context, QScriptEngine *engine)
41 if (context->argumentCount() >= 1)
43 QString value = context->argument(0).toString();
44 TKLog::printf(TKLog::NORMAL, "%s", value.toLocal8Bit().data());
46 return engine->nullValue();
49 static QScriptValue scriptFQN(QScriptContext *context, QScriptEngine *engine)
51 if (context->argumentCount() >= 2)
53 const char *location = context->argument(0).toString().toUtf8().constData();
54 const char *name = context->argument(1).toString().toUtf8().constData();
56 std::string fqnString = getFQNString(location, name);
57 return QScriptValue(fqnString.c_str());
61 return engine->nullValue();
66 TKContainer* TKContainer::createContainer()
68 TKContainer *container = new DNQSContainer();
73 DNQSContainer::DNQSContainer()
75 mQSEngine = new QScriptEngine();
76 mQSGlobalObject = mQSEngine->globalObject();
78 mQSGlobalObject.setProperty("print",mQSEngine->newFunction(scriptPrint, 1));
79 mQSGlobalObject.setProperty("FQN",mQSEngine->newFunction(scriptFQN, 2));
81 QScriptValue global = mQSEngine->newObject();
82 mQSGlobalObject.setProperty("global", global,QScriptValue::Undeletable);
83 global.setProperty("container_version", QScriptValue(CONTAINER_VERSION),QScriptValue::ReadOnly|QScriptValue::Undeletable);
85 mQSCellContainer = mQSEngine->newObject();
86 mQSGlobalObject.setProperty("__private_DNCellContainer__",mQSCellContainer);
89 DNQSContainer::~DNQSContainer()
95 void DNQSContainer::setValue(std::string key, float value)
97 mQSGlobalObject.setProperty(QString::fromStdString(key), QScriptValue(value));
100 float DNQSContainer::getValue(std::string key) const
102 QScriptValue v = mQSGlobalObject.property(QString::fromStdString(key));
113 TKCell* DNQSContainer::cellFactory(std::string location, std::string name, std::string type, bool canInterfaceIn, bool canInterfaceOut)
117 if (type == CELLTYPE_JSBASIC || type.length() == 0)
119 cell = new DNQSBasicCell(this, location, name, canInterfaceIn, canInterfaceOut);
121 else if (type == CELLTYPE_IN)
123 cell = new DNQSInputCell(this, location, name);
125 else if (type == CELLTYPE_OUT)
127 cell = new DNQSOutputCell(this, location, name);
129 else if (type == CELLTYPE_BASICSTORAGE)
131 cell = new DNQSBasicStorageCell(this, location, name, canInterfaceIn, canInterfaceOut);
140 TKCell* DNQSContainer::pluginCellFactory(std::string location, std::string fullName, std::string type, std::string pluginName, std::string pluginValue, bool canInterfaceIn, bool canInterfaceOut)
142 Q_ASSERT(canInterfaceIn != canInterfaceOut);
144 (void)canInterfaceOut;
150 cell = new DNPluginInputCell(this, location, fullName, pluginName, pluginValue);
154 cell = new DNPluginOutputCell(this, location, fullName, pluginName, pluginValue);
163 TKAxon* DNQSContainer::axonFactory(TKCell *theOwner)
165 return new TKAxon(theOwner);
168 TKReceptor* DNQSContainer::receptorFactory(TKCell *theOwner)
170 return new TKReceptor(theOwner);
173 TKAxonTerminal* DNQSContainer::axonTerminalFactory(TKAxon *theOwner)
175 return new TKAxonTerminal(theOwner);
178 TKCellCode* DNQSContainer::cellCodeFactory(std::string name, std::string cellapi, std::string code)
180 return new DNQSCellCode(name, cellapi, this, code);