OSDN Git Service

2b143fe0a765514c9b3b68f9ef7f284ab65fdc43
[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 "TKUICell.h"
35 #include "dnqscellcode.h"
36 #include "DNGlobal.h"
37
38 static QScriptValue scriptPrint(QScriptContext *context, QScriptEngine *engine)
39 {
40     if (context->argumentCount() >= 1)
41     {
42         QString value = context->argument(0).toString();
43         TKLog::printf(TKLog::NORMAL, "%s", value.toLocal8Bit().data());
44     }
45     return engine->nullValue();
46 }
47
48 static QScriptValue scriptFQN(QScriptContext *context, QScriptEngine *engine)
49 {
50     if (context->argumentCount() >= 2)
51     {
52         const char *location = context->argument(0).toString().toUtf8().constData();
53         const char *name = context->argument(1).toString().toUtf8().constData();
54
55         std::string fqnString = getFQNString(location, name);
56         return QScriptValue(fqnString.c_str());
57     }
58     else
59     {
60         return engine->nullValue();
61     }
62 }
63
64 //static
65 TKContainer* TKContainer::createContainer()
66 {
67     TKContainer *container = new DNQSContainer();
68     container->init();
69     return container;
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
88 DNQSContainer::~DNQSContainer()
89 {
90     if (mQSEngine)
91         delete mQSEngine;
92 }
93
94 void DNQSContainer::setValue(std::string key, float value)
95 {
96     mQSGlobalObject.setProperty(QString::fromStdString(key), QScriptValue(value));
97 }
98
99 float DNQSContainer::getValue(std::string key) const
100 {
101     QScriptValue v = mQSGlobalObject.property(QString::fromStdString(key));
102     if (v.isNumber())
103     {
104         return v.toNumber();
105     }
106     else
107     {
108         return 0;
109     }
110 }
111
112 TKCell* DNQSContainer::cellFactory(std::string location, std::string name, std::string type, bool canInterface)
113 {
114     TKCell *cell = NULL;
115
116     if (type == CELLTYPE_JSBASIC || type.length() == 0)
117     {
118         cell = new DNQSBasicCell(this, location, name, canInterface);
119         cell->init();
120     }
121     else if (type == CELLTYPE_IN)
122     {
123         cell = new DNQSInputCell(this, location, name, canInterface);
124         cell->init();
125     }
126     else if (type == CELLTYPE_OUT)
127     {
128         cell = new DNQSOutputCell(this, location, name, canInterface);
129         cell->init();
130     }
131     else if (type == CELLTYPE_BASICSTORAGE)
132     {
133         cell = new DNQSBasicStorageCell(this, location, name, canInterface);
134         cell->init();
135     }
136
137     return cell;
138 }
139
140 TKAxon* DNQSContainer::axonFactory(TKCell *theOwner)
141 {
142     return new TKAxon(theOwner);
143 }
144
145 TKReceptor* DNQSContainer::receptorFactory(TKCell *theOwner)
146 {
147     return new TKReceptor(theOwner);
148 }
149
150 TKAxonTerminal* DNQSContainer::axonTerminalFactory(TKAxon *theOwner)
151 {
152     return new TKAxonTerminal(theOwner);
153 }
154
155 TKCellCode* DNQSContainer::cellCodeFactory(std::string name, std::string cellapi, std::string code)
156 {
157     return new DNQSCellCode(name, cellapi, this, code);
158 }
159