OSDN Git Service

5cc1d16a5a0b62281fab23b54db7d100aa0b88c5
[dennco/dennco.git] / Source / QtScript / dnqscontainer.cpp
1 //  Copyright (c) 2012 Dennco Project
2 //
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.
7 //
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.
12 //
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/>.
15
16 //
17 //  Created by tkawata on 2/22/2012.
18 //
19 #include "dnqscontainer.h"
20
21 #include "TKCell.h"
22 #include "TKCellCode.h"
23 #include "TKContainer.h"
24 #include "TKLog.h"
25 #include "DNUtils.h"
26 #include "versioninfo.h"
27 #include "dnqsbasiccell.h"
28 #include "dnqsinputcell.h"
29 #include "dnqsoutputcell.h"
30 #include "dnqsbasicstoragecell.h"
31 #include "TKUICell.h"
32 #include "dnqscellcode.h"
33 #include "DNGlobal.h"
34
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";
39
40 static QScriptValue scriptPrint(QScriptContext *context, QScriptEngine *engine)
41 {
42     if (context->argumentCount() >= 1)
43     {
44         QString value = context->argument(0).toString();
45         TKLog::printf(TKLog::NORMAL, "%s", value.toLocal8Bit().data());
46     }
47     return engine->nullValue();
48 }
49
50 static QScriptValue scriptFQN(QScriptContext *context, QScriptEngine *engine)
51 {
52     if (context->argumentCount() >= 2)
53     {
54         const char *location = context->argument(0).toString().toUtf8().constData();
55         const char *name = context->argument(1).toString().toUtf8().constData();
56
57         std::string fqnString = getFQNString(location, name);
58         return QScriptValue(fqnString.c_str());
59     }
60     else
61     {
62         return engine->nullValue();
63     }
64 }
65
66 //static
67 TKContainer* TKContainer::createContainer()
68 {
69     return new DNQSContainer;
70 }
71
72 DNQSContainer::DNQSContainer()
73 {
74     mQSEngine = new QScriptEngine();
75     mQSGlobalObject = mQSEngine->globalObject();
76
77     mQSGlobalObject.setProperty("print",mQSEngine->newFunction(scriptPrint, 1));
78     mQSGlobalObject.setProperty("FQN",mQSEngine->newFunction(scriptFQN, 2));
79
80     QScriptValue global = mQSEngine->newObject();
81     mQSGlobalObject.setProperty("global", global,QScriptValue::Undeletable);
82     global.setProperty("container_version", QScriptValue(CONTAINER_VERSION),QScriptValue::ReadOnly|QScriptValue::Undeletable);
83
84     mQSCellContainer = mQSEngine->newObject();
85     mQSGlobalObject.setProperty("__private_DNCellContainer__",mQSCellContainer);
86
87     mEmptyCellClass = createCellCode("_DNEMPTY",CELLTYPE_JSBASIC,"");
88 }
89
90 DNQSContainer::~DNQSContainer()
91 {
92     if (mQSEngine)
93         delete mQSEngine;
94 }
95 TKCell* DNQSContainer::createCellWithoutCellCodeClass(std::string theLocation, std::string theName, std::string type, std::string customScript)
96 {
97     TKCell *cell = NULL;
98     bool    noScript = false;
99
100     if (type == CELLTYPE_JSBASIC || type.length() == 0)
101     {
102         cell = new DNQSBasicCell(this, theLocation, theName, false);
103     }
104     else if (type == CELLTYPE_IN)
105     {
106         cell = new DNQSInputCell(this, theLocation, theName, true);
107         noScript = true;
108     }
109     else if (type == CELLTYPE_OUT)
110     {
111         cell = new DNQSOutputCell(this, theLocation, theName, true);
112         noScript = true;
113     }
114     else if (type == CELLTYPE_BASICSTORAGE)
115     {
116         cell = new DNQSBasicStorageCell(this, theLocation, theName, false);
117     }
118     else
119     {
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);
123     }
124
125     if (cell)
126     {
127         std::string fqnName = getFQNString(theLocation.c_str(), theName.c_str());
128         mCells.insert(TKCellMap::value_type(fqnName, cell));
129         if (cell->isInterface())
130         {
131             mInterfaceCells.insert(TKCellMap::value_type(fqnName, cell));
132         }
133         if (!noScript)
134         {
135             cell->setCellCode(mEmptyCellClass,(const void*)customScript.c_str());
136         }
137         else
138         {
139             if (customScript.length()>0)
140             {
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);
144             }
145         }
146     }
147     else
148     {
149         std::string fqnName = getFQNString(theLocation.c_str(),theName.c_str());
150         std::string message = "Failed to create a cell. ";
151         message += fqnName;
152         message += "\n";
153         message += "Out of memory?";
154         dnNotifyError("Initialization failed", message);
155     }
156
157     return cell;
158 }
159
160 TKCell* DNQSContainer::createCellWithCellCodeClass(std::string theLocation, std::string theName, TKCellCode *cellCode, std::string customScript)
161 {
162     TKCell *cell = NULL;
163
164     std::string type = cellCode->getCellAPIName();
165
166     if (type == CELLTYPE_JSBASIC)
167     {
168         cell = new DNQSBasicCell(this, theLocation, theName, false);
169     }
170     else if (type == CELLTYPE_BASICSTORAGE)
171     {
172         cell = new DNQSBasicStorageCell(this, theLocation, theName, false);
173     }
174     else if (type == CELLTYPE_OUT)
175     {
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);
179     }
180     else if (type == CELLTYPE_IN)
181     {
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);
185     }    
186     else
187     {
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);
191     }
192
193
194     if (cell)
195     {
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());
199
200         if (cell->isInterface())
201         {
202             mInterfaceCells.insert(TKCellMap::value_type(fqnName, cell));
203         }
204     }
205     else
206     {
207         std::string fqnName = getFQNString(theLocation.c_str(),theName.c_str());
208         std::string message = "Failed to create a cell. ";
209         message += fqnName;
210         message += "\n";
211         message += "Out of memory?";
212         dnNotifyError("Initialization failed", message);
213     }
214
215     return cell;
216 }
217
218 TKCellCode* DNQSContainer::createCellCode(std::string theName, std::string theAPIType, std::string code)
219 {
220     DNQSCellCode *cellCode = new DNQSCellCode(theName, theAPIType, this, code);
221
222     if (cellCode)
223     {
224         mCellCodes.insert( std::map<std::string, TKCellCode*>::value_type(theName, cellCode));
225     }
226     return cellCode;
227 }
228
229 void DNQSContainer::setValue(std::string key, float value)
230 {
231     mQSGlobalObject.setProperty(QString::fromStdString(key), QScriptValue(value));
232 }
233
234 float DNQSContainer::getValue(std::string key)
235 {
236     QScriptValue v = mQSGlobalObject.property(QString::fromStdString(key));
237     if (v.isNumber())
238     {
239         return v.toNumber();
240     }
241     else
242     {
243         return 0;
244     }
245 }