QCAD Application Framework
CAD Application Development and Automation.
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
REcmaHelper.h
Go to the documentation of this file.
1 #ifndef RECMAHELPER_H
2 #define RECMAHELPER_H
3 
4 #include <QtScript>
5 #include <QList>
6 
7 #include "RVector.h"
8 #include "RSpline.h"
9 #include "REntity.h"
10 #include "RLineweight.h"
11 
12 class RGraphicsScene;
13 class RGraphicsView;
14 
15 
16 class REcmaHelper {
17 public:
18 
19 // tries to convert the given value to all possible inheritance strains
20 // if multiple inheritance is used:
21 template<class T>
22 static T* scriptValueTo(QScriptValue v) {
23  // try to cast:
24  T* ret = qscriptvalue_cast<T*>(v);
25  //qDebug() << "scriptValueTo: ret: 1: " << (long int)ret;
26  if (ret!=NULL) {
27  return ret;
28  }
29 
30  // try to convert to QObject:
31  /*
32  ret = qobject_cast<T*>(v.toQObject());
33  qDebug() << "scriptValueTo: QObject: " << (long int)v.toQObject();
34  if (ret!=NULL) {
35  return ret;
36  }
37  */
38 
39  // try to cast prototype
40  // TODO: check if this makes sense
41  if (ret==NULL && !v.isNull()) {
42  QScriptValue prototype = v.prototype();
43  ret = qscriptvalue_cast<T*>(prototype);
44  //qDebug() << "scriptValueTo: ret: prototype: " << (long int)ret;
45  }
46 
47  // try to cast prototypes of base classes:
48  if (ret==NULL && !v.isNull()) {
49  QScriptValue fBaseClasses = v.property("getBaseClasses");
50  if (!fBaseClasses.isValid()) {
51  return NULL;
52  }
53  QScriptValue baseClasses = fBaseClasses.call(v);
54 
55  // skip first base class (primary base class):
56  int i=1;
57  while(true) {
58  QScriptValue baseClass = baseClasses.property(i);
59  if (baseClass.toString().isEmpty()) {
60  break;
61  }
62 
63  //qDebug() << "base class[" << i << "]: " << baseClass.toString();
64 
65  ret = qscriptvalue_cast<T*>(v.property(QString("get") + baseClass.toString()).call(v));
66 
67  if (ret!=NULL) {
68  break;
69  }
70 
71  i++;
72  }
73  }
74 
75  return ret;
76 }
77 
78 // try to convert the given value to a QObject.
79 /*
80 template<class T>
81 static T* scriptValueToQObject(QScriptValue v) {
82  // try to convert to QObject:
83  T* ret = qobject_cast<T*>(v.toQObject());
84  qDebug() << "scriptValueToQObject: toQObject: " << v.toQObject();
85  qDebug() << "scriptValueToQObject: ret: 1: " << ret;
86 
87  if (ret==NULL && !v.isNull()) {
88  QScriptValue prototype = v.prototype();
89  ret = qobject_cast<T*>(prototype.toQObject());
90  qDebug() << "scriptValueToQObject: ret: 2: " << ret;
91  }
92 
93  return ret;
94 }
95 */
96 
97 static QScriptValue throwError(const QString& message, QScriptContext* context);
98 
99 static QScriptValue toScriptValue(QScriptEngine* engine, const QMap<QString, QString>& cppValue);
100 
101 //static bool isRVector(QScriptValue& sv);
102 
103 template<class T1, class T2>
104 static QScriptValue pairListToScriptValue(QScriptEngine* engine, const QList<QPair<T1, T2> >& cppValue) {
105  QVariantList vl;
106 
107  for (int i=0; i<cppValue.size(); ++i) {
108  QVariantList vlp;
109  QVariant v;
110  QPair<T1, T2> item = cppValue.at(i);
111 // if (QString(item.first.typeName())=="RLineweight::Lineweight") {
112 // v.setValue((int)item.first.value<RLineweight::Lineweight>());
113 // }
114 // else {
115  v.setValue(item.first);
116  //}
117  vlp.append(v);
118  v.setValue(item.second);
119  vlp.append(v);
120 
121  v.setValue(vlp);
122  vl.append(v);
123  }
124 
125  return qScriptValueFromValue(engine, vl);
126 }
127 
128 //template<class T1, class T2>
129 //static QScriptValue pairToScriptValue(QScriptEngine* engine, const QPair<T1, T2>& cppValue) {
130 // QVariantList vl;
131 // QVariant v;
132 // if (QString(cppValue.first.typeName())=="RLineweight::Lineweight") {
133 // v.setValue((int)cppValue.first.value<RLineweight::Lineweight>());
134 // }
135 // else {
136 // v.setValue(cppValue.first);
137 // }
138 // vl.append(v);
139 // v.setValue(cppValue.second);
140 // vl.append(v);
141 // return qScriptValueFromValue(engine, vl);
142 //}
143 
144 static QScriptValue listToScriptValue(QScriptEngine* engine, const QList<QSharedPointer<RShape> >& cppValue);
145 
146 template<class T>
147 static QScriptValue listToScriptValue(QScriptEngine* engine, const QList<T>& cppValue) {
148  QVariantList vl;
149  for (int i = 0; i < cppValue.size(); ++i) {
150  QVariant v;
151  v.setValue(cppValue.at(i));
152  vl.append(v);
153  }
154  return qScriptValueFromValue(engine, vl);
155 }
156 
157 template<class T>
158 static QScriptValue setToScriptValue(QScriptEngine* engine, const QSet<T>& cppValue) {
159  QVariantList vl;
160  typename QSet<T>::const_iterator it;
161  for (it=cppValue.begin(); it!=cppValue.end(); it++) {
162  QVariant v;
163  v.setValue(*it);
164  vl.append(v);
165  }
166  return qScriptValueFromValue(engine, vl);
167 }
168 
169 /*
170 static QScriptValue toScriptValue(QScriptEngine* engine, const QList<double>& cppValue);
171 static QScriptValue toScriptValue(QScriptEngine* engine, const QList<RVector>& cppValue);
172 static QScriptValue toScriptValue(QScriptEngine* engine, const QList<RSpline>& cppValue);
173 static QScriptValue toScriptValue(QScriptEngine* engine, const QList<QKeySequence>& cppValue);
174 static QScriptValue toScriptValue(QScriptEngine* engine, const QList<RGraphicsScene*>& cppValue);
175 static QScriptValue toScriptValue(QScriptEngine* engine, const QList<RGraphicsView*>& cppValue);
176 */
177 static QScriptValue toScriptValue(QScriptEngine* engine, RGraphicsView* cppValue);
178 static QScriptValue toScriptValue(QScriptEngine* engine, RGraphicsScene* cppValue);
179 static QScriptValue toScriptValue(QScriptEngine* engine, RShape* cppValue);
180 static QScriptValue toScriptValue(QScriptEngine* engine, QSharedPointer<RShape>& cppValue);
181 static QScriptValue toScriptValue(QScriptEngine* engine, QSharedPointer<REntity>& cppValue);
182 static QScriptValue toScriptValue(QScriptEngine* engine, REntity* cppValue);
183 static QScriptValue toScriptValue(QScriptEngine* engine, QSharedPointer<REntityData>& cppValue);
184 
185 static QScriptValue toScriptValue(QScriptEngine* engine, const QVariant& cppValue);
186 
187 template<class T>
188 static QScriptValue tryCast(QScriptEngine* engine, QSharedPointer<RShape>& cppValue) {
189  QSharedPointer<T> shape = cppValue.dynamicCast<T>();
190  if (!shape.isNull()) {
191  return qScriptValueFromValue(engine, shape);
192  }
193  else {
194  return QScriptValue();
195  }
196 }
197 
198 template<class T>
199 static QScriptValue tryCast(QScriptEngine* engine, RShape* cppValue) {
200  T* shape = dynamic_cast<T*>(cppValue);
201  if (shape!=NULL) {
202  return qScriptValueFromValue(engine, shape);
203  }
204  else {
205  return QScriptValue();
206  }
207 }
208 
209 template<class T>
210 static QScriptValue tryCast(QScriptEngine* engine, QSharedPointer<REntity>& cppValue) {
211  QSharedPointer<T> entity = cppValue.dynamicCast<T>();
212  if (!entity.isNull()) {
213  return qScriptValueFromValue(engine, entity);
214  }
215  else {
216  return QScriptValue();
217  }
218 }
219 
220 template<class T>
221 static QScriptValue tryCast(QScriptEngine* engine, REntity* cppValue) {
222  T* entity = dynamic_cast<T*>(cppValue);
223  if (entity!=NULL) {
224  return qScriptValueFromValue(engine, entity);
225  }
226  else {
227  return QScriptValue();
228  }
229 }
230 
231 template<class T>
232 static QScriptValue tryCast(QScriptEngine* engine, QSharedPointer<REntityData>& cppValue) {
233  QSharedPointer<T> entity = cppValue.dynamicCast<T>();
234  if (!entity.isNull()) {
235  return qScriptValueFromValue(engine, entity);
236  }
237  else {
238  return QScriptValue();
239  }
240 }
241 
242 static void fromScriptValue(QScriptEngine* engine, QScriptValue scriptValue, QList<QSharedPointer<RShape> >& cppValue);
243 static QVariant toVariant(const QSharedPointer<RShape>& cppValue);
244 
245 /*
246 template<class T>
247 static void fromScriptValueSP(QScriptEngine* engine, QScriptValue scriptValue, QList<QSharedPointer<T> >& cppValue) {
248  QVariantList vl = engine->fromScriptValue<QVariantList>(scriptValue);
249  for (int i = 0; i < vl.size(); ++i) {
250  qDebug() << "fromScriptValueSP: " << i;
251  QSharedPointer<T> v = vl.at(i).value<QSharedPointer<T> >();
252  if (!v.isNull()) {
253  qDebug() << "fromScriptValueSP: shared pointer";
254  cppValue.append(v);
255  }
256  else {
257  qDebug() << "fromScriptValueSP: no shared pointer";
258  T* p = vl.at(i).value<T*>();
259  if (p!=NULL) {
260  qDebug() << "fromScriptValueSP: normal pointer";
261  QSharedPointer<T> q(p->clone());
262  cppValue.append(q);
263  }
264  }
265  }
266 }
267 */
268 
269 template<class T>
270 static void fromScriptValue(QScriptEngine* engine, QScriptValue scriptValue, QList<T>& cppValue) {
271  QVariantList vl = engine->fromScriptValue<QVariantList>(scriptValue);
272  for (int i = 0; i < vl.size(); ++i) {
273  cppValue.append(vl.at(i).template value<T>());
274  }
275 }
276 
277 template<class T>
278 static void fromScriptValue(QScriptEngine* engine, QScriptValue scriptValue, QSet<T>& cppValue) {
279  QVariantList vl = engine->fromScriptValue<QVariantList>(scriptValue);
280  for (int i = 0; i < vl.size(); ++i) {
281  cppValue.insert(vl.at(i).template value<T>());
282  }
283 }
284 
285 static void registerFunction(QScriptEngine* engine, QScriptValue* proto,
286  QScriptEngine::FunctionSignature fun,
287  const QString& name);
288 
289 static void functionStart(const QString& name, QScriptContext *context, QScriptEngine *engine);
290 static void functionEnd(const QString& name, QScriptContext *context, QScriptEngine *engine);
291 
292 static void shellFunctionStart(const QString& name, QScriptEngine *engine);
293 static void shellFunctionEnd(const QString& name, QScriptEngine *engine);
294 
296 };
297 
298 #endif