OSDN Git Service

[dennco] Changed the name format for plugin cell. The name format is now "value name...
[dennco/dennco.git] / Source / layer3 / 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 "TKAxon.h"
24 #include "TKReceptor.h"
25 #include "TKAxonTerminal.h"
26 #include "TKContainer.h"
27 #include "TKLog.h"
28 #include "DNUtils.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"
37 #include "DNGlobal.h"
38
39 static QScriptValue scriptPrint(QScriptContext *context, QScriptEngine *engine)
40 {
41     if (context->argumentCount() >= 1)
42     {
43         QString value = context->argument(0).toString();
44         TKLog::printf(TKLog::NORMAL, "%s", value.toLocal8Bit().data());
45     }
46     return engine->nullValue();
47 }
48
49 static QScriptValue scriptFQN(QScriptContext *context, QScriptEngine *engine)
50 {
51     if (context->argumentCount() >= 2)
52     {
53         const char *location = context->argument(0).toString().toUtf8().constData();
54         const char *name = context->argument(1).toString().toUtf8().constData();
55
56         std::string fqnString = getFQNString(location, name);
57         return QScriptValue(fqnString.c_str());
58     }
59     else
60     {
61         return engine->nullValue();
62     }
63 }
64
65 //static
66 TKContainer* TKContainer::createContainer()
67 {
68     TKContainer *container = new DNQSContainer();
69     container->init();
70     return container;
71 }
72
73 DNQSContainer::DNQSContainer()
74 {
75     mQSEngine = new QScriptEngine();
76     mQSGlobalObject = mQSEngine->globalObject();
77
78     mQSGlobalObject.setProperty("print",mQSEngine->newFunction(scriptPrint, 1));
79     mQSGlobalObject.setProperty("FQN",mQSEngine->newFunction(scriptFQN, 2));
80
81     QScriptValue global = mQSEngine->newObject();
82     mQSGlobalObject.setProperty("global", global,QScriptValue::Undeletable);
83     global.setProperty("container_version", QScriptValue(CONTAINER_VERSION),QScriptValue::ReadOnly|QScriptValue::Undeletable);
84
85     mQSCellContainer = mQSEngine->newObject();
86     mQSGlobalObject.setProperty("__private_DNCellContainer__",mQSCellContainer);
87 }
88
89 DNQSContainer::~DNQSContainer()
90 {
91     if (mQSEngine)
92         delete mQSEngine;
93 }
94
95 void DNQSContainer::setValue(std::string key, float value)
96 {
97     mQSGlobalObject.setProperty(QString::fromStdString(key), QScriptValue(value));
98 }
99
100 float DNQSContainer::getValue(std::string key) const
101 {
102     QScriptValue v = mQSGlobalObject.property(QString::fromStdString(key));
103     if (v.isNumber())
104     {
105         return v.toNumber();
106     }
107     else
108     {
109         return 0;
110     }
111 }
112
113 TKCell* DNQSContainer::cellFactory(std::string location, std::string name, std::string type, bool canInterfaceIn, bool canInterfaceOut)
114 {
115     TKCell *cell = NULL;
116
117     if (type == CELLTYPE_JSBASIC || type.length() == 0)
118     {
119         cell = new DNQSBasicCell(this, location, name, canInterfaceIn, canInterfaceOut);
120     }
121     else if (type == CELLTYPE_IN)
122     {
123         cell = new DNQSInputCell(this, location, name);
124     }
125     else if (type == CELLTYPE_OUT)
126     {
127         cell = new DNQSOutputCell(this, location, name);
128     }
129     else if (type == CELLTYPE_BASICSTORAGE)
130     {
131         cell = new DNQSBasicStorageCell(this, location, name, canInterfaceIn, canInterfaceOut);
132     }
133
134     if (cell)
135         cell->init();
136
137     return cell;
138 }
139
140 TKCell* DNQSContainer::pluginCellFactory(std::string location, std::string fullName, std::string type, std::string pluginName, std::string pluginValue, bool canInterfaceIn, bool canInterfaceOut)
141 {
142     Q_ASSERT(canInterfaceIn != canInterfaceOut);
143
144     (void)canInterfaceOut;
145
146     TKCell *cell = NULL;
147
148     if (canInterfaceIn)
149     {
150         cell = new DNPluginInputCell(this, location, fullName, pluginName, pluginValue);
151     }
152     else
153     {
154         cell = new DNPluginOutputCell(this, location, fullName, pluginName, pluginValue);
155     }
156
157     if (cell)
158         cell->init();
159
160     return cell;
161 }
162
163 TKAxon* DNQSContainer::axonFactory(TKCell *theOwner)
164 {
165     return new TKAxon(theOwner);
166 }
167
168 TKReceptor* DNQSContainer::receptorFactory(TKCell *theOwner)
169 {
170     return new TKReceptor(theOwner);
171 }
172
173 TKAxonTerminal* DNQSContainer::axonTerminalFactory(TKAxon *theOwner)
174 {
175     return new TKAxonTerminal(theOwner);
176 }
177
178 TKCellCode* DNQSContainer::cellCodeFactory(std::string name, std::string cellapi, std::string code)
179 {
180     return new DNQSCellCode(name, cellapi, this, code);
181 }
182