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"
35 #include "dnqscellcode.h"
38 static QScriptValue scriptPrint(QScriptContext *context, QScriptEngine *engine)
40 if (context->argumentCount() >= 1)
42 QString value = context->argument(0).toString();
43 TKLog::printf(TKLog::NORMAL, "%s", value.toLocal8Bit().data());
45 return engine->nullValue();
48 static QScriptValue scriptFQN(QScriptContext *context, QScriptEngine *engine)
50 if (context->argumentCount() >= 2)
52 const char *location = context->argument(0).toString().toUtf8().constData();
53 const char *name = context->argument(1).toString().toUtf8().constData();
55 std::string fqnString = getFQNString(location, name);
56 return QScriptValue(fqnString.c_str());
60 return engine->nullValue();
65 TKContainer* TKContainer::createContainer()
67 TKContainer *container = new DNQSContainer();
72 DNQSContainer::DNQSContainer()
74 mQSEngine = new QScriptEngine();
75 mQSGlobalObject = mQSEngine->globalObject();
77 mQSGlobalObject.setProperty("print",mQSEngine->newFunction(scriptPrint, 1));
78 mQSGlobalObject.setProperty("FQN",mQSEngine->newFunction(scriptFQN, 2));
80 QScriptValue global = mQSEngine->newObject();
81 mQSGlobalObject.setProperty("global", global,QScriptValue::Undeletable);
82 global.setProperty("container_version", QScriptValue(CONTAINER_VERSION),QScriptValue::ReadOnly|QScriptValue::Undeletable);
84 mQSCellContainer = mQSEngine->newObject();
85 mQSGlobalObject.setProperty("__private_DNCellContainer__",mQSCellContainer);
88 DNQSContainer::~DNQSContainer()
94 void DNQSContainer::setValue(std::string key, float value)
96 mQSGlobalObject.setProperty(QString::fromStdString(key), QScriptValue(value));
99 float DNQSContainer::getValue(std::string key) const
101 QScriptValue v = mQSGlobalObject.property(QString::fromStdString(key));
112 TKCell* DNQSContainer::cellFactory(std::string location, std::string name, std::string type, bool canInterface)
116 if (type == CELLTYPE_JSBASIC || type.length() == 0)
118 cell = new DNQSBasicCell(this, location, name, canInterface);
121 else if (type == CELLTYPE_IN)
123 cell = new DNQSInputCell(this, location, name, canInterface);
126 else if (type == CELLTYPE_OUT)
128 cell = new DNQSOutputCell(this, location, name, canInterface);
131 else if (type == CELLTYPE_BASICSTORAGE)
133 cell = new DNQSBasicStorageCell(this, location, name, canInterface);
140 TKAxon* DNQSContainer::axonFactory(TKCell *theOwner)
142 return new TKAxon(theOwner);
145 TKReceptor* DNQSContainer::receptorFactory(TKCell *theOwner)
147 return new TKReceptor(theOwner);
150 TKAxonTerminal* DNQSContainer::axonTerminalFactory(TKAxon *theOwner)
152 return new TKAxonTerminal(theOwner);
155 TKCellCode* DNQSContainer::cellCodeFactory(std::string name, std::string cellapi, std::string code)
157 return new DNQSCellCode(name, cellapi, this, code);