Exemplo n.º 1
0
QObject* CuteNews::createQmlObject(const QString &fileName) {
    initEngine();
    QDeclarativeContext *context = new QDeclarativeContext(m_engine->rootContext());
    QDeclarativeComponent *component = new QDeclarativeComponent(m_engine, fileName, this);
    
    if (QObject *obj = component->create(context)) {
        context->setParent(obj);
        return obj;
    }

    if (component->isError()) {
        foreach (const QDeclarativeError &error, component->errors()) {
            Logger::log("CuteNews::createQmlObject(). Error: " + error.toString());
        }        
    }
// Currently item creation is tightly bound to landmark model. Some day
// this may be leveraged to any user defined model or e.g. XML model.
QDeclarativeGeoMapObject* QDeclarativeGeoMapObjectView::createItem(int modelRow)
{
    if (!delegate_ || !model_)
        return NULL;
    QModelIndex index = model_->index(modelRow, 0); // column 0
    if (!index.isValid()) {
        qWarning() << "QDeclarativeGeoMapObject Index is not valid: " << modelRow;
        return NULL;
    }
    QHashIterator<int, QByteArray> iterator(model_->roleNames());
    QDeclarativeContext *itemContext = new QDeclarativeContext(qmlContext(this));
    while (iterator.hasNext()) {
        iterator.next();
        QVariant modelData = model_->data(index, iterator.key());
        if (!modelData.isValid())
            continue;
        // This call would fail for <QObject*> Need to be figured out why
        // if the model support is leveraged.
        QObject *data_ptr = modelData.value<QDeclarativeLandmark*>();
        if (!data_ptr)
            continue;
        itemContext->setContextProperty(QLatin1String(iterator.value().data()), data_ptr);
        // To avoid name collisions (delegate has same named attribute as model's role)
        // one can add here that the data is accessible also e.g. via 'model'.
        // In case of landmarks, two of the following are then equivalent:
        // latitude : landmark.coordinate.latitude
        // latitude : model.landmark.coordinate.latitude
        // itemContext->setContextProperty(QLatin1String("model."), data_ptr);
        // At the time being, it is however uncertain how to make it from a
        // QtMobility project (QDeclarativeVisualDataModel not available).
        // This however needs to be figured out if model support is generalized.
    }
    QObject* obj = delegate_->create(itemContext);

    if (!obj) {
        qWarning() << "QDeclarativeGeoMapObject map object creation failed.";
        delete itemContext;
        return NULL;
    }
    QDeclarativeGeoMapObject *declMapObj =  qobject_cast<QDeclarativeGeoMapObject*>(obj);
    if (!declMapObj) {
        qWarning() << "QDeclarativeGeoMapObject map object delegate is of unsupported type.";
        delete itemContext;
        return NULL;
    }
    itemContext->setParent(declMapObj);
    return declMapObj;
}
Exemplo n.º 3
0
QDeclarativeWebView* QDeclarativeWebView::createWindow(QWebPage::WebWindowType type)
{
    switch (type) {
    case QWebPage::WebBrowserWindow: {
        if (!d->newWindowComponent && d->newWindowParent)
            qWarning("WebView::newWindowComponent not set - WebView::newWindowParent ignored");
        else if (d->newWindowComponent && !d->newWindowParent)
            qWarning("WebView::newWindowParent not set - WebView::newWindowComponent ignored");
        else if (d->newWindowComponent && d->newWindowParent) {
            QDeclarativeWebView* webview = 0;
            QDeclarativeContext* windowContext = new QDeclarativeContext(qmlContext(this));

            QObject* newObject = d->newWindowComponent->create(windowContext);
            if (newObject) {
                windowContext->setParent(newObject);
                QDeclarativeItem* item = qobject_cast<QDeclarativeItem *>(newObject);
                if (!item)
                    delete newObject;
                else {
                    webview = item->findChild<QDeclarativeWebView*>();
                    if (!webview)
                        delete item;
                    else {
                        newObject->setParent(d->newWindowParent);
                        static_cast<QGraphicsObject*>(item)->setParentItem(d->newWindowParent);
                    }
                }
            } else
                delete windowContext;

            return webview;
        }
    }
    break;
    case QWebPage::WebModalDialog: {
        // Not supported
    }
    }
    return 0;
}
Exemplo n.º 4
0
Cometd::Cometd(QObject *parent)
    : QObject(parent)
    , m_binding(new Binding(this))
{
    QDeclarativeEngine *engine = CometdPlugin::engine();
    QDeclarativeContext *rootContext = engine->rootContext();
    QDeclarativeContext *cometdContext = new QDeclarativeContext(rootContext);

    // Cometd-JavaScript depends on window.setTimeout and friends
    cometdContext->setContextProperty("timerWindow", new TimerWindow(this));

    // Allow the implementation to pass back the forward-function
    cometdContext->setContextProperty("binding", m_binding);

    cometdContext->setContextProperty("application", qApp);

    QDeclarativeComponent component(engine, QUrl("qrc:/cometd.qml"));
    QObject *implementation = component.create(cometdContext);
    cometdContext->setParent(implementation);
    implementation->setParent(this);

    QMetaObject::invokeMethod(implementation, "initialize");
}
Exemplo n.º 5
0
void TimelineView::updateTimeline(bool updateStartX)
{
    if (!m_delegate)
        return;

    if (!m_ranges.isArray())
        return;

    int length = m_ranges.property("length").toInt32();

    qreal startValue = m_ranges.property(0).property("start").toNumber();
    qreal endValue = m_ranges.property(length-1).property("start").toNumber() + m_ranges.property(length-1).property("duration").toNumber();

    qreal totalRange = endValue - startValue;
    qreal window = m_endTime - m_startTime;

    if (window == 0)    //###
        return;

    qreal spacing = width() / window;
    qreal oldtw = m_totalWidth;
    m_totalWidth = totalRange * spacing;

    // Find region samples
    int minsample = 0;
    int maxsample = 0;

    for (int i = 0; i < length; ++i) {
        if (m_ends.at(i) >= m_startTime)
            break;
        minsample = i;
    }

    for (int i = minsample + 1; i < length; ++i) {
        maxsample = i;
        if (m_starts.at(i) > m_endTime)
            break;
    }

    //### overkill (if we can expose whether or not data is nested)
    for (int i = maxsample + 1; i < length; ++i) {
        if (m_starts.at(i) < m_endTime)
            maxsample = i;
    }

    //qDebug() << maxsample - minsample;

    if (updateStartX) {
        qreal oldStartX = m_startX;
        m_startX = qRound(m_startTime * spacing);
        if (m_startX != oldStartX) {
            emit startXChanged(m_startX);
        }
    }

    //### emitting this before startXChanged was causing issues
    if (m_totalWidth != oldtw)
        emit totalWidthChanged(m_totalWidth);

    //clear items no longer in view
    while (prevMin < minsample) {
        delete m_items.take(prevMin);
        ++prevMin;
    }
    while (prevMax > maxsample) {
        delete m_items.take(prevMax);
        --prevMax;
    }

    // Show items
    int z = 0;
    for (int i = maxsample-1; i >= minsample; --i) {
        QDeclarativeItem *item = 0;
        item = m_items.value(i);
        bool creating = false;
        if (!item) {
            QDeclarativeContext *ctxt = new QDeclarativeContext(qmlContext(this));
            item = qobject_cast<QDeclarativeItem*>(m_delegate->beginCreate(ctxt));
            m_items.insert(i, item);
            creating = true;

            int type = m_ranges.property(i).property("type").toNumber();

            ctxt->setParent(item); //### QDeclarative_setParent_noEvent(ctxt, item); instead?
            ctxt->setContextProperty("duration", qMax(qRound(m_ranges.property(i).property("duration").toNumber()/qreal(1000)),1));
            ctxt->setContextProperty("fileName", m_ranges.property(i).property("fileName").toString());
            ctxt->setContextProperty("line", m_ranges.property(i).property("line").toNumber());
            QString label;
            QVariantList list = m_ranges.property(i).property("label").toVariant().value<QVariantList>();
            for (int i = 0; i < list.size(); ++i) {
                if (i > 0)
                    label += QLatin1Char('\n');
                QString sub = list.at(i).toString();

                //### only do rewrite for bindings...
                if (type == 3) {
                    //### don't construct in loop
                    QRegExp rewrite("\\(function \\$(\\w+)\\(\\) \\{ return (.+) \\}\\)");
                    bool match = rewrite.exactMatch(sub);
                    if (match)
                        sub = rewrite.cap(1) + ": " + rewrite.cap(2);
                }

                label += sub;
            }
            ctxt->setContextProperty("label", label);
            ctxt->setContextProperty("type", type);
            item->setParentItem(this);
        }
        if (item) {
            item->setX(m_starts.at(i)*spacing);
            item->setWidth((m_ends.at(i)-m_starts.at(i)) * spacing);
            item->setZValue(++z);
        }
        if (creating)
            m_delegate->completeCreate();
    }

    prevMin = minsample;
    prevMax = maxsample;
}