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"
23 #include "TKContainer.h"
26 #include "versioninfo.h"
27 #include "dnqsbasiccell.h"
28 #include "dnqsinputcell.h"
29 #include "dnqsoutputcell.h"
30 #include "dnqsbasicstoragecell.h"
32 #include "dnqscellcode.h"
35 const std::string DNQSContainer::CELLTYPE_JSBASIC = "B";
36 const std::string DNQSContainer::CELLTYPE_OUT = "O";
37 const std::string DNQSContainer::CELLTYPE_IN = "I";
38 const std::string DNQSContainer::CELLTYPE_BASICSTORAGE = "BS";
40 static QScriptValue scriptPrint(QScriptContext *context, QScriptEngine *engine)
42 if (context->argumentCount() >= 1)
44 QString value = context->argument(0).toString();
45 TKLog::printf(TKLog::NORMAL, "%s", value.toLocal8Bit().data());
47 return engine->nullValue();
50 static QScriptValue scriptFQN(QScriptContext *context, QScriptEngine *engine)
52 if (context->argumentCount() >= 2)
54 const char *location = context->argument(0).toString().toUtf8().constData();
55 const char *name = context->argument(1).toString().toUtf8().constData();
57 std::string fqnString = getFQNString(location, name);
58 return QScriptValue(fqnString.c_str());
62 return engine->nullValue();
67 TKContainer* TKContainer::createContainer()
69 return 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);
87 mEmptyCellClass = createCellCode("_DNEMPTY",CELLTYPE_JSBASIC,"");
90 DNQSContainer::~DNQSContainer()
95 TKCell* DNQSContainer::createCellWithoutCellCodeClass(std::string theLocation, std::string theName, std::string type, std::string customScript)
98 bool noScript = false;
100 if (type == CELLTYPE_JSBASIC || type.length() == 0)
102 cell = new DNQSBasicCell(this, theLocation, theName, false);
104 else if (type == CELLTYPE_IN)
106 cell = new DNQSInputCell(this, theLocation, theName, true);
109 else if (type == CELLTYPE_OUT)
111 cell = new DNQSOutputCell(this, theLocation, theName, true);
114 else if (type == CELLTYPE_BASICSTORAGE)
116 cell = new DNQSBasicStorageCell(this, theLocation, theName, false);
120 std::string message = std::string("Failed to construct cell '").append(theLocation).append("#").append(theName);
121 message.append("'\nThe cellcode requires type '").append(type).append("' but it's not a supported type.");
122 dnNotifyError("Initialization failed", message);
127 std::string fqnName = getFQNString(theLocation.c_str(), theName.c_str());
128 mCells.insert(TKCellMap::value_type(fqnName, cell));
129 if (cell->isInterface())
131 mInterfaceCells.insert(TKCellMap::value_type(fqnName, cell));
135 cell->setCellCode(mEmptyCellClass,(const void*)customScript.c_str());
139 if (customScript.length()>0)
141 std::string message = std::string("Failed to construct cell '").append(theLocation).append("#").append(theName);
142 message.append("'\nThe cellcode is type '").append(type).append("'. This type doesn't support custom script.");
143 dnNotifyError("Initialization failed",message);
149 std::string fqnName = getFQNString(theLocation.c_str(),theName.c_str());
150 std::string message = "Failed to create a cell. ";
153 message += "Out of memory?";
154 dnNotifyError("Initialization failed", message);
160 TKCell* DNQSContainer::createCellWithCellCodeClass(std::string theLocation, std::string theName, TKCellCode *cellCode, std::string customScript)
164 std::string type = cellCode->getCellAPIName();
166 if (type == CELLTYPE_JSBASIC)
168 cell = new DNQSBasicCell(this, theLocation, theName, false);
170 else if (type == CELLTYPE_BASICSTORAGE)
172 cell = new DNQSBasicStorageCell(this, theLocation, theName, false);
174 else if (type == CELLTYPE_OUT)
176 std::string message = std::string("Failed to construct cell '").append(theLocation).append("#").append(theName);
177 message.append("'\nThe cellcode type '").append(type).append("' doesn't support to have a CellCode class.");
178 dnNotifyError("Initialization failed", message);
180 else if (type == CELLTYPE_IN)
182 std::string message = std::string("Failed to construct cell '").append(theLocation).append("#").append(theName);
183 message.append("'\nThe cellcode requires type '").append(type).append("' doesn't support to have a CellCode class.");
184 dnNotifyError("Initialization failed", message);
188 std::string message = std::string("Failed to construct cell '").append(theLocation).append("#").append(theName);
189 message.append("'\nThe cellcode requires type '").append(type).append("' but it's not a supported type.");
190 dnNotifyError("Initialization failed",message);
196 std::string fqnName = getFQNString(theLocation.c_str(), theName.c_str());
197 mCells.insert(TKCellMap::value_type(fqnName, cell));
198 cell->setCellCode(cellCode,(const void*)customScript.c_str());
200 if (cell->isInterface())
202 mInterfaceCells.insert(TKCellMap::value_type(fqnName, cell));
207 std::string fqnName = getFQNString(theLocation.c_str(),theName.c_str());
208 std::string message = "Failed to create a cell. ";
211 message += "Out of memory?";
212 dnNotifyError("Initialization failed", message);
218 TKCellCode* DNQSContainer::createCellCode(std::string theName, std::string theAPIType, std::string code)
220 DNQSCellCode *cellCode = new DNQSCellCode(theName, theAPIType, this, code);
224 mCellCodes.insert( std::map<std::string, TKCellCode*>::value_type(theName, cellCode));
229 void DNQSContainer::setValue(std::string key, float value)
231 mQSGlobalObject.setProperty(QString::fromStdString(key), QScriptValue(value));
234 float DNQSContainer::getValue(std::string key)
236 QScriptValue v = mQSGlobalObject.property(QString::fromStdString(key));