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 "dnqscellcode.h"
37 static QScriptValue scriptPrint(QScriptContext *context, QScriptEngine *engine)
39 if (context->argumentCount() >= 1)
41 QString value = context->argument(0).toString();
42 TKLog::printf(TKLog::NORMAL, "%s", value.toLocal8Bit().data());
44 return engine->nullValue();
47 static QScriptValue scriptFQN(QScriptContext *context, QScriptEngine *engine)
49 if (context->argumentCount() >= 2)
51 const char *location = context->argument(0).toString().toUtf8().constData();
52 const char *name = context->argument(1).toString().toUtf8().constData();
54 std::string fqnString = getFQNString(location, name);
55 return QScriptValue(fqnString.c_str());
59 return engine->nullValue();
64 TKContainer* TKContainer::createContainer()
66 TKContainer *container = new DNQSContainer();
71 DNQSContainer::DNQSContainer()
73 mQSEngine = new QScriptEngine();
74 mQSGlobalObject = mQSEngine->globalObject();
76 mQSGlobalObject.setProperty("print",mQSEngine->newFunction(scriptPrint, 1));
77 mQSGlobalObject.setProperty("FQN",mQSEngine->newFunction(scriptFQN, 2));
79 QScriptValue global = mQSEngine->newObject();
80 mQSGlobalObject.setProperty("global", global,QScriptValue::Undeletable);
81 global.setProperty("container_version", QScriptValue(CONTAINER_VERSION),QScriptValue::ReadOnly|QScriptValue::Undeletable);
83 mQSCellContainer = mQSEngine->newObject();
84 mQSGlobalObject.setProperty("__private_DNCellContainer__",mQSCellContainer);
87 DNQSContainer::~DNQSContainer()
93 void DNQSContainer::setValue(std::string key, float value)
95 mQSGlobalObject.setProperty(QString::fromStdString(key), QScriptValue(value));
98 float DNQSContainer::getValue(std::string key) const
100 QScriptValue v = mQSGlobalObject.property(QString::fromStdString(key));
111 TKCell* DNQSContainer::cellFactory(std::string location, std::string name, std::string type, bool canInterfaceIn, bool canInterfaceOut)
115 if (type == CELLTYPE_JSBASIC || type.length() == 0)
117 cell = new DNQSBasicCell(this, location, name, canInterfaceIn, canInterfaceOut);
120 else if (type == CELLTYPE_IN)
122 cell = new DNQSInputCell(this, location, name);
125 else if (type == CELLTYPE_OUT)
127 cell = new DNQSOutputCell(this, location, name);
130 else if (type == CELLTYPE_BASICSTORAGE)
132 cell = new DNQSBasicStorageCell(this, location, name, canInterfaceIn, canInterfaceOut);
139 TKAxon* DNQSContainer::axonFactory(TKCell *theOwner)
141 return new TKAxon(theOwner);
144 TKReceptor* DNQSContainer::receptorFactory(TKCell *theOwner)
146 return new TKReceptor(theOwner);
149 TKAxonTerminal* DNQSContainer::axonTerminalFactory(TKAxon *theOwner)
151 return new TKAxonTerminal(theOwner);
154 TKCellCode* DNQSContainer::cellCodeFactory(std::string name, std::string cellapi, std::string code)
156 return new DNQSCellCode(name, cellapi, this, code);