Ejemplo n.º 1
0
void QQuickDialog::setStandardButtons(StandardButtons buttons)
{
    m_enabledButtons = buttons;
    m_standardButtonsLeftModel = QJSValue();
    m_standardButtonsRightModel = QJSValue();
    emit standardButtonsChanged();
}
QJSValue QDeclarativeGalleryQueryModel::get(const QJSValue &index) const
{
    QJSEngine *scriptEngine = index.engine();

    if (!scriptEngine)
       return QJSValue();

    const int i = index.toInt();

    if (i < 0 || i >= m_rowCount || (i != m_resultSet->currentIndex() && !m_resultSet->fetch(i)))
       return QJSValue();

    QJSValue object = scriptEngine->newObject();

    object.setProperty(
            QLatin1String("itemId"), scriptEngine->toScriptValue(m_resultSet->itemId()));
    object.setProperty(
            QLatin1String("itemUrl"), scriptEngine->toScriptValue(m_resultSet->itemUrl()));

    typedef QVector<QPair<int, QString> >::const_iterator iterator;
    for (iterator it = m_propertyNames.constBegin(), end = m_propertyNames.constEnd();
            it != end;
            ++it) {
        QVariant value = m_resultSet->metaData(it->first);

        if (value.isNull())
            value = QVariant(m_resultSet->propertyType(it->first));

        object.setProperty(it->second, scriptEngine->toScriptValue(value));
    }

    return object;
}
Ejemplo n.º 3
0
QJSValue THREEMatrix4::applyVector3Array(QJSValue floatArray, int offset, int length)
{
    if(!floatArray.isArray()) {
        qDebug().nospace() << "THREEMatrix4::" << __FUNCTION__
                           << " invalid parameter type for floatArray, expected JS array";
        return m_engine->newQObject(this);
    }

    static THREEVector3 v1(parent(), m_engine);
    QVariantList list = floatArray.toVariant().toList();

    if (offset < 0)
        offset = 0;

    if (length < 0 || length == 0)
        length = list.length();

    if (offset + length > list.length())
        length = list.length() - offset;

    for ( int i = 0, j = offset; i < length; i += 3, j += 3 ) {
        v1.m_x = float(floatArray.property(j).toNumber());
        v1.m_y = float(floatArray.property(j + 1).toNumber());
        v1.m_z = float(floatArray.property(j + 2).toNumber());

        v1._applyMatrix4( this );

        floatArray.setProperty(j, QJSValue(v1.m_x));
        floatArray.setProperty(j + 1, QJSValue(v1.m_y));
        floatArray.setProperty(j + 2, QJSValue(v1.m_z));
    }

    return floatArray;
}
Ejemplo n.º 4
0
void MyHttpRequest::finished(QNetworkReply *reply)
{
    Data temp = queue_replyData.dequeue ();
    ReplyType type=temp.replyType;
    bool isError = !(reply->error () == QNetworkReply::NoError);
    
    if(type==CallbackFun){
#if(QT_VERSION>=0x050000)
        QJSValueList list;
        list.append (QJSValue(isError));
        list.append (QJSValue(isError?reply->errorString ():reply->readAll ()));
        temp.callbackFun.call (list);
#else
        QScriptValueList list;
        list.append (QScriptValue(isError));
        list.append (QScriptValue(isError?reply->errorString ():reply->readAll ()));
        temp.callbackFun.call (QScriptValue(), list);
#endif
    }else if(type==ConnectSlot){
        QObject* obj = temp.caller;
        QByteArray slotName = temp.slotName;
        
        if(obj!=NULL){
            bool ok;//记录调用槽是否成功
            int parameterCount = obj->metaObject()->method(obj->metaObject()->indexOfMethod(slotName)).parameterTypes().length();
            QRegExp reg("^[^(]+");
            reg.indexIn (slotName);
            slotName = reg.cap (0).toLatin1 ();
            if(parameterCount==0){//如果形参个数为0个
                ok = QMetaObject::invokeMethod(obj, slotName);
            }else if(parameterCount==1){
                ok = QMetaObject::invokeMethod(obj, slotName, Q_ARG(QNetworkReply*, reply));
            }else if(parameterCount==2){
Ejemplo n.º 5
0
QJSValue JSKitLocalStorage::getItem(const QString &key) const
{
    QVariant value = _storage->value(key);
    if (value.isValid()) {
        return QJSValue(value.toString());
    } else {
        return QJSValue(QJSValue::NullValue);
    }
}
Ejemplo n.º 6
0
QJSValue Request::cookie() const
{
    Config::instance()->init(m_engine);

    CookieJar *jar = qobject_cast<CookieJar*>(m_engine->networkAccessManager()->cookieJar());
    if (!jar)
        return QJSValue("");

    return QJSValue(jar->cookies());
}
Ejemplo n.º 7
0
/**
 * @brief QEjdbClientPrivate::remove remove a bson from database collection. Returns true
 * if remove succeed otherwise false.
 *
 */
QJSValue QEjdbClientPrivate::remove(QString collectionName, QJSValue uid)
{
    QEjdbDatabase db = database();
    if (db.containsCollection(collectionName)) {
        QBsonOid oid = QBsonOid(uid.toString());
        if (oid.isValid()) {
            return QJSValue(db.remove(collectionName, uid.toString()));
        }
    }
    return QJSValue(false);
}
Ejemplo n.º 8
0
ServicesBase::CallbackType ServicesBase::fromJSCallback(QJSValue callback)
{
    CallbackType cb = [callback] (int errorType, QString jsonStr) mutable
    {
        if (callback.isCallable())
        {
            QJSValueList args;
            args << QJSValue(errorType) << QJSValue(jsonStr);
            callback.call(args);
        }
    };
    return cb;
}
Ejemplo n.º 9
0
QJSValue QmlIODevice::getChar()
{
    if(this->hasDevice()){
        char c = 0;
        if(this->m_device->getChar(&c)){
            return QJSValue(QString(QChar(c)));
        } else {
            return QJSValue();
        }
    } else {
        return QJSValue();
    }
}
Ejemplo n.º 10
0
/**
 * @brief QEjdbClientPrivate::load load a Json from database stored in collection
 * identified by collectionName. If nothing found en empty Json is returned.
 *
 * @param collectionName name of collection
 * @param uid Bson uid
 *
 * @return Json Object or empty Json if noting found
 */
QJSValue QEjdbClientPrivate::load(QString collectionName, QJSValue uid)
{
    QEjdbDatabase db = database();
    if (db.containsCollection(collectionName)) {
        QBsonOid oid = QBsonOid(uid.toString());
        if (oid.isValid()) {
            QBsonObject bso = db.load(collectionName, uid.toString());
            if (bso.isEmpty()) {
                return QJSValue(QJSValue::NullValue);
            }
            return QJSValue(convert(bso));
        }
    }
    return QJSValue(QJSValue::NullValue);
}
Ejemplo n.º 11
0
void ShotgunField::onRelease(const QVariant context)
{
    info() << "Update with context" << context;
    trace() << ".update(" << context << ")";

    if (source())
        setCount(source()->count());
    else
        setCount(1);

    if (count() > 0) {
        OperationAttached::Status status = OperationAttached::Finished;

        for (int i = 0; i < count(); i++) {
            setIndex(i);

            QVariant value = buildValue(i, context.toMap());

            if (value.isValid()) {
                // TODO: Qt bug? null QVariant converts to a non-null QJSValue
                setDetail(i, "value", value.isNull() ? QJSValue(QJSValue::NullValue) : toScriptValue(value));
            } else {
                error() << "Unable to build value";
                status = OperationAttached::Error;
            }
        }

        emit releaseFinished(status);
    } else {
        debug() << "source" << source() << "has count of" << source()->count();
        emit releaseFinished(OperationAttached::Finished);
    }
}
Ejemplo n.º 12
0
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;
}
Ejemplo n.º 13
0
void QQuickWebEngineViewPrivate::didFindText(quint64 requestId, int matchCount)
{
    QJSValue callback = m_callbacks.take(requestId);
    QJSValueList args;
    args.append(QJSValue(matchCount));
    callback.call(args);
}
Ejemplo n.º 14
0
const QJSValue &QQmlVMEVariant::asQJSValue()
{
    if (type != qMetaTypeId<QJSValue>())
        setValue(QJSValue());

    return *(QJSValue *)(dataPtr());
}
Ejemplo n.º 15
0
QJSValue ConversionOutput::addRawBuffer(const QString &category, const QString &filename, const QJSValue &buffer, bool compressed, int uncompressedSize)
{
    QByteArray data = QBufferModule::getData(buffer);

    if (compressed) {
        /* We need to prepend the uncompressed size so qUncompress will do its work... */
        QByteArray compressedData(data.size() + sizeof(int), Qt::Uninitialized);
        QDataStream dataStream(&compressedData, QIODevice::WriteOnly);
        dataStream << uncompressedSize;
        dataStream.writeRawData(data.data(), data.size());

        data = qUncompress(compressedData);
    }

    QFileInfo fi(filename);
    fi.dir().mkpath(".");

    QFile file(filename);
    if (!file.open(QFile::WriteOnly))
    {
        QString message = QString("Unable to create %1").arg(filename);
        return QJSExceptionUtils::newError(mCommonJsModule->engine(), message);
    }

    file.write(data);

    return QJSValue();
}
Ejemplo n.º 16
0
void QFAppScriptRunnable::release()
{
    if (!m_condition.isNull() &&
            m_condition.isObject() &&
        m_condition.hasProperty("disconnect")) {

        QJSValue disconnect = m_condition.property("disconnect");
        QJSValueList args;
        args << m_callback;

        disconnect.callWithInstance(m_condition,args);
    }

    m_condition = QJSValue();
    m_callback = QJSValue();
}
Ejemplo n.º 17
0
// get the value at a row
QJSValue ValueModel::get(int row) const
{
    if (row < 0 || row >= m_data.count()) {
        return QJSValue();
    }
    return m_data.at(row);
}
Ejemplo n.º 18
0
/*!
 *  \internal
 * used by QJSEngine::toScriptValue
 */
QJSValue QJSEngine::create(int type, const void *ptr)
{
    Q_D(QJSEngine);
    QV4::Scope scope(d->m_v4Engine);
    QV4::ScopedValue v(scope, scope.engine->metaTypeToJS(type, ptr));
    return QJSValue(d->m_v4Engine, v->asReturnedValue());
}
Ejemplo n.º 19
0
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();
}
Ejemplo n.º 20
0
QJSValue QJSEngine::newQMetaObject(const QMetaObject* metaObject) {
    Q_D(QJSEngine);
    QV4::ExecutionEngine *v4 = QV8Engine::getV4(d);
    QV4::Scope scope(v4);
    QV4::ScopedValue v(scope, QV4::QMetaObjectWrapper::create(v4, metaObject));
    return QJSValue(v4, v->asReturnedValue());
}
Ejemplo n.º 21
0
/*!
  Returns this engine's Global Object.

  By default, the Global Object contains the built-in objects that are
  part of \l{ECMA-262}, such as Math, Date and String. Additionally,
  you can set properties of the Global Object to make your own
  extensions available to all script code. Non-local variables in
  script code will be created as properties of the Global Object, as
  well as local variables in global code.
*/
QJSValue QJSEngine::globalObject() const
{
    Q_D(const QJSEngine);
    QV4::Scope scope(d->m_v4Engine);
    QV4::ScopedValue v(scope, d->m_v4Engine->globalObject);
    return QJSValue(d->m_v4Engine, v->asReturnedValue());
}
Ejemplo n.º 22
0
QJSValue OgreModelConverter::convertModel(QJSValue modelDataJs, QJSValue skeletonDataJs)
{
    QByteArray modelData = QBufferModule::getData(modelDataJs);
    QByteArray skeletonData = QBufferModule::getData(skeletonDataJs);

    return QJSValue();

}
Ejemplo n.º 23
0
/*!
  Creates a JavaScript object of class Array with the given \a length.

  \sa newObject()
*/
QJSValue QJSEngine::newArray(uint length)
{
    QV4::Scope scope(d->m_v4Engine);
    QV4::ScopedArrayObject array(scope, d->m_v4Engine->newArrayObject());
    if (length < 0x1000)
        array->arrayReserve(length);
    array->setArrayLengthUnchecked(length);
    return QJSValue(d->m_v4Engine, array.asReturnedValue());
}
Ejemplo n.º 24
0
void JSContext::transform()
{
    QFETCH(QString, jsx);
    QFETCH(QString, output);

    TJSModule *js = TJSLoader("JSXTransformer", "JSXTransformer").load();
    auto result = js->call("JSXTransformer.transform", QJSValue(jsx)).property("code").toString();
    QCOMPARE(result, output);
}
Ejemplo n.º 25
0
/*!
    Returns the value of the last property that was jumped over using
    next().

    \sa name()
*/
QJSValue QJSValueIterator::value() const
{
    QV4::ExecutionEngine *engine = d_ptr->iterator.engine();
    if (!engine)
        return QJSValue();
    QV4::Scope scope(engine);
    QV4::ScopedObject obj(scope, QJSValuePrivate::getValue(&d_ptr->value));
    if (!obj)
        return QJSValue();

    if (!d_ptr->currentName && d_ptr->currentIndex == UINT_MAX)
        return QJSValue();

    QV4::ScopedValue v(scope, obj->getValue(*obj, &d_ptr->currentProperty, d_ptr->currentAttributes));
    if (scope.hasException()) {
        engine->catchException();
        return QJSValue();
    }
    return QJSValue(engine, v->asReturnedValue());
}
Ejemplo n.º 26
0
void DlgJsRoboKey::on_trayMessageClicked()
{
    //TODO: FIX: this stupid trayJsCallback always ends up being undefined.
    //TODO: depending on the action we need to do something
    if (trayJsCallback.isCallable()){
        trayJsCallback.call();
        //it was already called, now make it undefined
    }
    trayJsCallback = QJSValue();
    //m_pjsrobokey->alert("tray clicked!!","");
}
Ejemplo n.º 27
0
/*!
  Creates a JavaScript object that wraps the given QObject \a
  object, using JavaScriptOwnership.

  Signals and slots, properties and children of \a object are
  available as properties of the created QJSValue.

  If \a object is a null pointer, this function returns a null value.

  If a default prototype has been registered for the \a object's class
  (or its superclass, recursively), the prototype of the new script
  object will be set to be that default prototype.

  If the given \a object is deleted outside of the engine's control, any
  attempt to access the deleted QObject's members through the JavaScript
  wrapper object (either by script code or C++) will result in a
  \l{Script Exceptions}{script exception}.

  \sa QJSValue::toQObject()
*/
QJSValue QJSEngine::newQObject(QObject *object)
{
    Q_D(QJSEngine);
    QV4::ExecutionEngine *v4 = QV8Engine::getV4(d);
    QV4::Scope scope(v4);
    if (object) {
        QQmlData *ddata = QQmlData::get(object, true);
        if (!ddata || !ddata->explicitIndestructibleSet)
            QQmlEngine::setObjectOwnership(object, QQmlEngine::JavaScriptOwnership);
    }
    QV4::ScopedValue v(scope, QV4::QObjectWrapper::wrap(v4, object));
    return QJSValue(v4, v->asReturnedValue());
}
Ejemplo n.º 28
0
QJSValue jsonToJs(JsonValue::Reader r, QQmlEngine *engine) {
    switch (r.which()) {
    case JsonValue::ARRAY: {
        auto ret = engine->newArray();
        unsigned int index = 0;
        for (auto v : r.getArray()) {
            ret.setProperty(index, _::jsonToJs(v, engine));
            index++;
        }
        return ret;
    }
    case JsonValue::OBJECT: {
        auto ret = engine->newObject();
        for (auto kv : r.getObject()) {
            // XXX: MSVC bug => call .c_str()
            ret.setProperty(toQString(kv.getName().cStr()),
                            _::jsonToJs(kv.getValue(), engine));
        }
        return ret;
    }
    case JsonValue::STRING: {
        // XXX: MSVC bug => call .c_str()
        return QJSValue(toQString(r.getString().cStr()));
    }
    case JsonValue::BOOLEAN: {
        return QJSValue(r.getBoolean());
    }
    case JsonValue::NUMBER: {
        return QJSValue(r.getNumber());
    }
    case JsonValue::NONE: {
        return QJSValue(QJSValue::NullValue);
    }
    default:
        ASSERT(false, "jsonToJs: Json decoding failed: Invalid type!");
    }
    return QJSValue();
}
Ejemplo n.º 29
0
void tst_QJSValueIterator::iterateArrayAndDoubleElements()
{
    QJSEngine engine;
    QJSValue array = engine.newArray();
    for (int i = 0; i < 20000; ++i)
        array.setProperty(i, i);
    QBENCHMARK {
        QJSValueIterator it(array);
        while (it.hasNext()) {
            it.next();
            it.setValue(QJSValue(&engine, it.value().toNumber() * 2));
        }
    }
}
Ejemplo n.º 30
0
/**
 * @internal
 * @brief QBsonConverter::mapValue map bson properties to jsvalue. Redirects
 * array and object to mapArray and mapObect methods.
 *
 * @param bsonValue
 *
 * @return QJSValue
 */
QJSValue QBsonConverter::mapValue(const QBsonValue &bsonValue)
{
    switch (bsonValue.type()) {
        case QBsonValue::Id:
        case QBsonValue::String:
            return QJSValue(bsonValue.toString());
        case QBsonValue::Long:
        case QBsonValue::Integer:
            return QJSValue(bsonValue.toInt());
        case QBsonValue::Double:
            return QJSValue(bsonValue.toDouble());
        case QBsonValue::Bool:
            return QJSValue(bsonValue.toBool());
        case QBsonValue::DateTime:
            return m_jsEngine->toScriptValue(bsonValue.toDateTime());
        case QBsonValue::Object:
            return mapObject(bsonValue.toObject());
        case QBsonValue::Array:
            return mapArray(bsonValue.toArray());
        default:;
    }
    return QJSValue();
}