int JsonDbPartition::find(const QString &query, const QJSValue &options, const QJSValue &callback) { QJSValue actualOptions = options; QJSValue actualCallback = callback; if (options.isCallable()) { if (!callback.isUndefined()) { qWarning() << "Callback should be the last parameter."; return -1; } actualCallback = actualOptions; actualOptions = QJSValue(QJSValue::UndefinedValue); } JsonDbQueryObject *newQuery = new JsonDbQueryObject(); newQuery->setQuery(query); if (!actualOptions.isUndefined()) { QVariantMap opt = actualOptions.toVariant().toMap(); if (opt.contains(QLatin1String("limit"))) newQuery->setLimit(opt.value(QLatin1String("limit")).toInt()); if (opt.contains(QLatin1String("bindings"))) newQuery->setBindings(opt.value(QLatin1String("bindings")).toMap()); } newQuery->setPartition(this); connect(newQuery, SIGNAL(finished()), this, SLOT(queryFinished())); connect(newQuery, SIGNAL(statusChanged(JsonDbQueryObject::Status)), this, SLOT(queryStatusChanged())); findCallbacks.insert(newQuery, actualCallback); newQuery->componentComplete(); int id = newQuery->start(); findIds.insert(newQuery, id); return id; }
QString ReportItemPara::paraText() { qfLogFuncFrame(); QString ret; if(m_getTextJsFn.isCallable()) { QJSValue jsv = m_getTextJsFn.call(); if(jsv.isDate()) { QDateTime dt = jsv.toDateTime(); ret = dt.toString(Qt::ISODate);//.date().toString(Qt::ISODate); } else ret = jsv.toString(); } else if(m_getTextCppFn) { ret = m_getTextCppFn(); } else { ret = text(); } { static QString new_line; if(new_line.isEmpty()) new_line += QChar::LineSeparator; ret.replace("\\n", new_line); ret.replace("\n", new_line); /// jinak nedokazu zadat mezeru mezi dvema <data> elementy nez <data>\s<data> ret.replace("\\s", " "); /// non breaking space ret.replace("\\S", QString(QChar::Nbsp)); } return ret; }
QJSValue EnginioQmlFileOperation::object() const { QJSValue object = g_qmlEngine->newObject(); object.setProperty(QStringLiteral("id"), objectId()); object.setProperty(QStringLiteral("objectType"), objectType()); return object; }
void QStaticFileReader::staticLoad(const QString &file, const QJSValue ¶ms){ bool monitor = false; if ( params.isObject() ){ if ( params.hasOwnProperty("monitor") ){ monitor = params.property("monitor").toBool(); } } if ( m_reader ) disconnect(m_reader, SIGNAL(dataChanged(QByteArray)), this, SLOT(readerDataChanged(QByteArray))); QStaticContainer* container = QStaticContainer::grabFromContext(this); m_reader = container->get<QFileReader>(file); if ( !m_reader ){ m_reader = new QFileReader(); m_reader->setSource(file); m_reader->componentComplete(); container->set<QFileReader>(file, m_reader); connect(m_reader, SIGNAL(dataChanged(QByteArray)), this, SLOT(readerDataChanged(QByteArray))); } m_data = m_reader->data(); emit dataChanged(m_data); m_reader->setMonitor(monitor); }
QJSValue THREEMatrix4::lookAt(QJSValue threeVecEye, QJSValue threeVecTarget, QJSValue threeVecUp) { THREEVector3 *eye = THREEVector3::getAsTHREEVector3(threeVecEye); THREEVector3 *target = THREEVector3::getAsTHREEVector3(threeVecTarget); THREEVector3 *up = THREEVector3::getAsTHREEVector3(threeVecUp); if (!eye) { qDebug().nospace() << "THREEMatrix4::" << __FUNCTION__ << " invalid parameter type for threeVecEye, expected THREEVector3 got " << threeVecEye.toQObject(); return m_engine->newQObject(this); } if (!target) { qDebug().nospace() << "THREEMatrix4::" << __FUNCTION__ << " invalid parameter type for threeVecTarget, expected THREEVector3 got " << threeVecTarget.toQObject(); return m_engine->newQObject(this); } if (!up) { qDebug().nospace() << "THREEMatrix4::" << __FUNCTION__ << " invalid parameter type for threeVecUp, expected THREEVector3 got " << threeVecUp.toQObject(); return m_engine->newQObject(this); } return m_engine->newQObject(_lookAt(eye, target, up)); }
void QDeclarativeGeoRoute::setPath(const QJSValue &value) { if (!value.isArray()) return; QList<QGeoCoordinate> pathList; quint32 length = value.property(QStringLiteral("length")).toUInt(); for (quint32 i = 0; i < length; ++i) { bool ok; QGeoCoordinate c = parseCoordinate(value.property(i), &ok); if (!ok || !c.isValid()) { qmlInfo(this) << "Unsupported path type"; return; } pathList.append(c); } if (route_.path() == pathList) return; route_.setPath(pathList); emit pathChanged(); }
void QQuickWebEngineViewPrivate::didFindText(quint64 requestId, int matchCount) { QJSValue callback = m_callbacks.take(requestId); QJSValueList args; args.append(QJSValue(matchCount)); callback.call(args); }
QJSValue THREEMatrix4::makeBasis(QJSValue xAxisVec, QJSValue yAxisVec, QJSValue zAxisVec) { THREEVector3 *xAxis = THREEVector3::getAsTHREEVector3(xAxisVec); THREEVector3 *yAxis = THREEVector3::getAsTHREEVector3(yAxisVec); THREEVector3 *zAxis = THREEVector3::getAsTHREEVector3(zAxisVec); if (!xAxis) { qDebug().nospace() << "THREEMatrix4::" << __FUNCTION__ << " invalid parameter type for xAxisVec, expected THREEVector3 got " << xAxisVec.toQObject(); return m_engine->newQObject(this); } if (!yAxis) { qDebug().nospace() << "THREEMatrix4::" << __FUNCTION__ << " invalid parameter type for yAxisVec, expected THREEVector3 got " << yAxisVec.toQObject(); return m_engine->newQObject(this); } if (!zAxis) { qDebug().nospace() << "THREEMatrix4::" << __FUNCTION__ << " invalid parameter type for zAxisVec, expected THREEVector3 got " << zAxisVec.toQObject(); return m_engine->newQObject(this); } return m_engine->newQObject(_makeBasis(xAxis, yAxis, zAxis)); }
QJsonValue JsonDbReduceDefinition::addObject(JsonDbReduceDefinition::FunctionNumber functionNumber, const QJsonValue &keyValue, QJsonValue previousValue, JsonDbObject object) { initScriptEngine(); QJSValue svKeyValue = mScriptEngine->toScriptValue(keyValue); if (!mTargetValueName.isEmpty()) previousValue = previousValue.toObject().value(mTargetValueName); QJSValue svPreviousValue = mScriptEngine->toScriptValue(previousValue); QJSValue svObject = mScriptEngine->toScriptValue(static_cast<QJsonObject>(object)); QJSValueList reduceArgs; reduceArgs << svKeyValue << svPreviousValue << svObject; QJSValue reduced = mFunctions[functionNumber].call(reduceArgs); if (!reduced.isUndefined() && !reduced.isError()) { QJsonValue jsonReduced = mScriptEngine->fromScriptValue<QJsonValue>(reduced); QJsonObject jsonReducedObject; if (!mTargetValueName.isEmpty()) jsonReducedObject.insert(mTargetValueName, jsonReduced); else jsonReducedObject = jsonReduced.toObject(); return jsonReducedObject; } else { if (reduced.isError()) setError(QString::fromLatin1("Error executing %1 function: %2") .arg((functionNumber == JsonDbReduceDefinition::Add ? QStringLiteral("add") : QStringLiteral("subtract"))) .arg(reduced.toString())); return QJsonValue(QJsonValue::Undefined); } }
void JSKitXMLHttpRequest::handleReplyFinished() { if (!_reply) { qCDebug(l) << "reply finished too late"; return; } _response = _reply->readAll(); qCDebug(l) << "reply finished, reply text:" << QString::fromUtf8(_response); emit readyStateChanged(); emit statusChanged(); emit statusTextChanged(); emit responseChanged(); emit responseTextChanged(); if (_onload.isCallable()) { qCDebug(l) << "going to call onload handler:" << _onload.toString(); QJSValue result = _onload.callWithInstance(_mgr->engine()->newQObject(this)); if (result.isError()) { qCWarning(l) << "JS error on onload handler:" << JSKitManager::describeError(result); } } else { qCDebug(l) << "No onload set"; } }
void Decision::setOpinions(QJSValue newOpinions) { if (newOpinions.strictlyEquals(opinions())) return; if (!newOpinions.isObject()) { qDebug() << "Cannot set opinions because opinions is not an object."; return; } // The only way I know of to determine how many opinions there are is to iterate them and count int opinionCount = 0; QJSValueIterator itr(newOpinions); while (itr.hasNext()) { itr.next(); ++opinionCount; } itr = newOpinions; // Now we actually allocate space for and set the opinions auto opinionList = m_decision.initOpinions(opinionCount); opinionCount = 0; while (itr.hasNext()) { itr.next(); auto builder = opinionList[opinionCount++]; builder.setContestant(itr.name().toInt()); builder.setOpinion(itr.value().toInt()); } emit opinionsChanged(); }
int JsonDbPartition::create(const QJSValue &object, const QJSValue &options, const QJSValue &callback) { QJSValue actualOptions = options; QJSValue actualCallback = callback; if (options.isCallable()) { if (!callback.isUndefined()) { qWarning() << "Callback should be the last parameter."; return -1; } actualCallback = actualOptions; actualOptions = QJSValue(QJSValue::UndefinedValue); } //#TODO ADD options QVariant obj = qjsvalue_to_qvariant(object); QJsonDbWriteRequest *request(0); if (obj.type() == QVariant::List) { request = new QJsonDbCreateRequest(qvariantlist_to_qjsonobject_list(obj.toList())); } else { request = new QJsonDbCreateRequest(QJsonObject::fromVariantMap(obj.toMap())); } request->setPartition(_name); connect(request, SIGNAL(finished()), this, SLOT(requestFinished())); connect(request, SIGNAL(finished()), request, SLOT(deleteLater())); connect(request, SIGNAL(error(QtJsonDb::QJsonDbRequest::ErrorCode,QString)), this, SLOT(requestError(QtJsonDb::QJsonDbRequest::ErrorCode,QString))); connect(request, SIGNAL(error(QtJsonDb::QJsonDbRequest::ErrorCode,QString)), request, SLOT(deleteLater())); JsonDatabase::sharedConnection().send(request); writeCallbacks.insert(request, actualCallback); return request->property("requestId").toInt(); }
extern "C" void hsqml_get_jval_string( HsQMLJValHandle* hndl, HsQMLStringHandle* strh) { QJSValue* value = reinterpret_cast<QJSValue*>(hndl); QString* string = reinterpret_cast<QString*>(strh); *string = value->toString(); }
QJSValue AlgorithmInterface::getEdges() { int c = 0; Edge *current; QList<Edge*> edges = _context->getEdges(); QJSValue ret = _engine->newArray(edges.length()); for (int i = 0; i < edges.length(); i++) { current = edges.at(i); QJSValue tmp = _engine->newArray(3); if (current->getBidirectional()) { tmp.setProperty(0, QJSValue(current->getEndNode()->getID())); tmp.setProperty(1, QJSValue(current->getBeginNode()->getID())); tmp.setProperty(2, QJSValue(current->getWeight(true))); ret.setProperty(c, tmp); c++; tmp = _engine->newArray(3); } tmp.setProperty(0, QJSValue(current->getBeginNode()->getID())); tmp.setProperty(1, QJSValue(current->getEndNode()->getID())); tmp.setProperty(2, QJSValue(current->getWeight(true))); ret.setProperty(c, tmp); c++; } return ret; }
extern "C" void hsqml_jval_array_set( HsQMLJValHandle* ahndl, unsigned int i, HsQMLJValHandle* hndl) { QJSValue* array = reinterpret_cast<QJSValue*>(ahndl); QJSValue* value = reinterpret_cast<QJSValue*>(hndl); array->setProperty(i, *value); }
void tst_QJSValueIterator::iterateOverObjectFromDeletedEngine() { QJSEngine *engine = new QJSEngine; QJSValue objet = engine->newObject(); // populate object with properties QHash<QString, int> properties; properties.insert("foo",1235); properties.insert("oof",5321); properties.insert("ofo",3521); QHash<QString, int>::const_iterator i = properties.constBegin(); for (; i != properties.constEnd(); ++i) { objet.setProperty(i.key(), i.value()); } // start iterating QJSValueIterator it(objet); it.next(); QVERIFY(properties.contains(it.name())); delete engine; QVERIFY(objet.isUndefined()); QVERIFY(it.name().isEmpty()); QVERIFY(it.value().isUndefined()); QVERIFY(!it.hasNext()); it.next(); QVERIFY(it.name().isEmpty()); QVERIFY(it.value().isUndefined()); }
void DbModel::insert(QString key, QJSValue callbackFunction) { if(m_name.isEmpty() || key.isEmpty() || m_jsonDocument.isNull()) { DEBUG << "cannot insert. name is empty or json document is null"; if(callbackFunction.isCallable()) { callbackFunction.call(QJSValueList() << 1); } return; } QJsonObject root = m_jsonDocument.object(); QJsonValueRef _arrayRef = root.find(m_name).value(); QJsonArray _arrayMain = _arrayRef.toArray(); QVariantMap _obj; _obj.insert("name", key); _arrayMain.append(QJsonValue::fromVariant(_obj)); root.insert(m_name, _arrayMain); m_jsonDocument.setObject(root); qDebug() << m_jsonDocument.toJson(); saveDatabase(); if(callbackFunction.isCallable()) { callbackFunction.call(QJSValueList() << 0); } beginInsertRows(QModelIndex(), _arrayMain.count() - 1, _arrayMain.count() - 1); endInsertRows(); }
void QDeclarativePolylineMapItem::setPath(const QJSValue &value) { if (!value.isArray()) return; QList<QGeoCoordinate> pathList; quint32 length = value.property(QStringLiteral("length")).toUInt(); for (quint32 i = 0; i < length; ++i) { bool ok; QGeoCoordinate c = parseCoordinate(value.property(i), &ok); if (!ok || !c.isValid()) { qmlInfo(this) << "Unsupported path type"; return; } pathList.append(c); } if (path_ == pathList) return; path_ = pathList; geometry_.markSourceDirty(); updateMapItem(); emit pathChanged(); }
void QQuickWebEngineViewPrivate::didRunJavaScript(quint64 requestId, const QVariant &result) { Q_Q(QQuickWebEngineView); QJSValue callback = m_callbacks.take(requestId); QJSValueList args; args.append(qmlEngine(q)->toScriptValue(result)); callback.call(args); }
// get all entries (this is costly call) QJSValue ValueModel::getList() const { QJSValue array = qmlEngine(this)->newArray(m_data.count()); for(int i=0; i<m_data.count(); i++) { array.setProperty(i, m_data.at(i)); } return array; }
QJSValue Image::size() const { QJSValue val = m_engine->newObject(); QSize s = m_reader->size(); val.setProperty("width", s.width()); val.setProperty("height", s.height()); return val; }
void JSKitPebble::getTimelineToken(QJSValue successCallback, QJSValue failureCallback) { getTokenInternal([this,successCallback]()mutable{ if(successCallback.isCallable()) { successCallback.call(QJSValueList({m_timelineToken})); } },failureCallback); }
QJSValue qjsonobject_list_to_qjsvalue(const QList<QJsonObject> &list) { int count = list.count(); QJSValue resultList = g_declEngine->newArray(count); for (int i = 0; i < count; i++) { resultList.setProperty(i, g_declEngine->toScriptValue(list[i])); } return resultList; }
void QPython::setHandler(QString event, QJSValue callback) { if (!callback.isCallable() || callback.isNull() || callback.isUndefined()) { handlers.remove(event); } else { handlers[event] = callback; } }
void QPython::call(QVariant func, QVariant args, QJSValue callback) { QJSValue *cb = 0; if (!callback.isNull() && !callback.isUndefined() && callback.isCallable()) { cb = new QJSValue(callback); } emit process(func, args, cb); }
void QPython::importModule(QString name, QJSValue callback) { QJSValue *cb = 0; if (!callback.isNull() && !callback.isUndefined() && callback.isCallable()) { cb = new QJSValue(callback); } emit import(name, cb); }
Option<Error> jsToJson(QJSValue value, JsonValue::Builder builder) { if (value.isArray()) { std::vector<QJSValue> arr; QJSValueIterator iter(value); while (iter.hasNext()) { iter.next(); // dafuq!? why is this required if (!iter.hasNext()) { break; } arr.push_back(iter.value()); } auto lst = builder.initArray(static_cast<unsigned int>(arr.size())); for (auto k = 0; k < arr.size(); ++k) { auto subvalue = lst[k]; auto opt_err = jsToJson(arr[k], subvalue); if (opt_err.has()) { return opt_err; } } return Option<Error>(); } if (value.isObject()) { std::vector<std::tuple<QString, QJSValue>> arr; QJSValueIterator iter(value); while (iter.hasNext()) { iter.next(); arr.push_back(std::make_tuple(iter.name(), iter.value())); } auto lst = builder.initObject(static_cast<unsigned int>(arr.size())); for (auto k = 0; k < arr.size(); ++k) { auto value_builder = lst[k].initValue(); const char *name = std::get<0>(arr[k]).toUtf8().constData(); lst[k].setName(name); auto opt_err = jsToJson(std::get<1>(arr[k]), value_builder); if (opt_err.has()) { return opt_err; } } return Option<Error>(); } if (value.isBool()) { builder.setBoolean(value.toBool()); return Option<Error>(); } if (value.isNumber()) { builder.setNumber(value.toNumber()); return Option<Error>(); } if (value.isString()) { const char *str = value.toString().toUtf8().constData(); builder.setString(str); return Option<Error>(); } if (value.isNull()) { builder.setNone(); return Option<Error>(); } auto tpname = toStdString(value.toVariant().typeName()); Error err(Error::Kind::ClientSideError, "Invalid type: " + tpname); return err; }
static QJSValue cameraInfoToJSValue(QJSEngine *jsEngine, const QCameraInfo &camera) { QJSValue o = jsEngine->newObject(); o.setProperty(QStringLiteral("deviceId"), camera.deviceName()); o.setProperty(QStringLiteral("displayName"), camera.description()); o.setProperty(QStringLiteral("position"), int(camera.position())); o.setProperty(QStringLiteral("orientation"), camera.orientation()); return o; }
float JSEndpoint::peek() const { QJSValue result = _callable.call(); if (result.isError()) { qCDebug(controllers).noquote() << formatException(result); return 0.0f; } else { return (float)result.toNumber(); } }
QString NatspecExpressionEvaluator::evalExpression(QString const& _expression) { QString call = ""; if (!m_abi.isEmpty() && !m_transaction.isEmpty() && !m_method.isEmpty()) call = ", {abi:" + m_abi + ", transaction:" + m_transaction + ", method: '" + m_method + "' }"; QJSValue result = m_engine.evaluate("natspec.evaluateExpressionSafe(\"" + _expression + "\"" + call + ")"); return result.toString(); }