Example #1
0
void
Context::Applet::cleanUpAndDelete()
{
    QGraphicsWidget *parent = dynamic_cast<QGraphicsWidget *>( parentItem() );
    //it probably won't matter, but right now if there are applethandles, *they* are the parent.
    //not the containment.

    //is the applet in a containment and is the containment have a layout? if yes, we remove the applet in the layout
    if ( parent && parent->layout() )
    {
        QGraphicsLayout *l = parent->layout();
        for ( int i = 0; i < l->count(); ++i )
        {
            if ( this == l->itemAt( i ) )
            {
                l->removeAt( i );
                break;
            }
        }
    }

    if ( Plasma::Applet::configScheme() ) {
        Plasma::Applet::configScheme()->setDefaults();
    }
    Plasma::Applet::scene()->removeItem( this );
    Plasma::Applet::deleteLater();
}
Example #2
0
//    Q_DECLARE_METATYPE(QGraphicsLayoutItem*)
QGraphicsLayoutItem *extractLayoutItem(QScriptContext *ctx, int index = 0, bool noExistingLayout = false)
{
    QScriptValue v = ctx->argument(index);
    if (ctx->argumentCount() == 0 || v.isQObject()) {
        QObject *object = v.toQObject();
        QGraphicsWidget *w = qobject_cast<QGraphicsWidget *>(object);
        if (!w) {
            AppletInterface *interface = qobject_cast<AppletInterface*>(object);
            if (!interface) {
                interface =  qobject_cast<AppletInterface*>(ctx->engine()->globalObject().property("plasmoid").toQObject());
            }

            if (interface) {
                w = interface->applet();
            }
        }

        if (noExistingLayout && w->layout()) {
            return 0;
        }

        return w;
    }

    QVariant variant = v.toVariant();
    QGraphicsLayoutItem *item = variant.value<QGraphicsLayoutItem *>();
    //this is horribly ugly code, but when a QLinearLayout* is stuffed into a QVariant,
    //QVariant does not know that it is a QGraphicsLayoutItem. repeat for all subclasses
    //of same
    if (!item) {
        item = variant.value<QGraphicsLayout *>();

        if (!item) {
            item = variant.value<QGraphicsLinearLayout *>();

            if (!item) {
                item = variant.value<QGraphicsGridLayout *>();

                if (!item) {
                    item = variant.value<QGraphicsAnchorLayout *>();
                }
            }
        }
    }

    QGraphicsWidget *w = dynamic_cast<QGraphicsWidget *>(item);
    if (noExistingLayout && w && w->layout()) {
        return 0;
    }

    return item;
}
/*!
    \internal

    This function is called from subclasses to add graphics widget \a w to
    a layout. If the layout has a parentWidget, it will set that parent to be the parent of \a w.
    If \a w is already in a layout, it will remove \a w from that layout.
*/
void QGraphicsLayoutPrivate::addChildWidget(QGraphicsWidget *w)
{
    Q_Q(QGraphicsLayout);

    QGraphicsWidget *lw = parentWidget();
    QGraphicsWidget *pw = w->parentWidget();
    if (pw == lw || !lw)
        return;
    if (pw) {
        QGraphicsLayout *pl = pw->layout();
        if (pl) {
            removeWidgetFromLayout(pl, w);
        }
    }

#ifdef QT_DEBUG
    if (pw) {
        qWarning("QGraphicsLayout::addChildWidget: %s \"%s\" in wrong parent; moved to correct parent",
            w->metaObject()->className(), w->objectName().toLocal8Bit().constData());
    }
#endif

    w->setParentLayoutItem(q);
    w->setParentItem(lw);
}
Example #4
0
bool QGraphicsLayoutItemPrivate::hasHeightForWidth() const
{
    Q_Q(const QGraphicsLayoutItem);
    if (isLayout) {
        const QGraphicsLayout *l = static_cast<const QGraphicsLayout *>(q);
        for (int i = l->count() - 1; i >= 0; --i) {
            if (QGraphicsLayoutItemPrivate::get(l->itemAt(i))->hasHeightForWidth())
                return true;
        }
    } else if (QGraphicsItem *item = q->graphicsItem()) {
        if (item->isWidget()) {
            QGraphicsWidget *w = static_cast<QGraphicsWidget *>(item);
            if (w->layout()) {
                return QGraphicsLayoutItemPrivate::get(w->layout())->hasHeightForWidth();
            }
        }
    }
    return q->sizePolicy().hasHeightForWidth();
}
Example #5
0
bool QGraphicsLayoutItemPrivate::hasWidthForHeight() const
{
    // enable this code when we add QSizePolicy::hasWidthForHeight() (For 4.8)
#if 1
    return false;
#else
    Q_Q(const QGraphicsLayoutItem);
    if (isLayout) {
        const QGraphicsLayout *l = static_cast<const QGraphicsLayout *>(q);
        for (int i = l->count() - 1; i >= 0; --i) {
            if (QGraphicsLayoutItemPrivate::get(l->itemAt(i))->hasWidthForHeight())
                return true;
        }
    } else if (QGraphicsItem *item = q->graphicsItem()) {
        if (item->isWidget()) {
            QGraphicsWidget *w = static_cast<QGraphicsWidget *>(item);
            if (w->layout()) {
                return QGraphicsLayoutItemPrivate::get(w->layout())->hasWidthForHeight();
            }
        }
    }
    return q->sizePolicy().hasWidthForHeight();
#endif
}
Example #6
0
void tst_QGraphicsLayout::invalidateAndMove()
{
    // Check that if we set the position of an item and invalidate its layout at the same
    // time, the widget keeps its correct size
    QFETCH(bool, instantInvalidatePropagation);
    QGraphicsLayout::setInstantInvalidatePropagation(instantInvalidatePropagation);
    QGraphicsScene scene;

    QGraphicsWidget *widget = new QGraphicsWidget;
    new QGraphicsLinearLayout(widget);

    widget->setMinimumSize(1,1);
    widget->setPreferredSize(34,34);
    widget->setMaximumSize(100,100);
    widget->resize(widget->preferredSize());

    scene.addItem(widget);

    qApp->processEvents();

    /* Invalidate and reactivate.  The size should not have changed */
    widget->layout()->invalidate();
    widget->layout()->activate();

    QCOMPARE(widget->geometry().size(), widget->preferredSize());
    QCOMPARE(widget->layout()->geometry().size(), widget->preferredSize());
    qApp->processEvents();
    QCOMPARE(widget->geometry().size(), widget->preferredSize());
    QCOMPARE(widget->layout()->geometry().size(), widget->preferredSize());

    widget->layout()->invalidate();
    widget->setX(1);  //Change just the position using setX
    QCOMPARE(widget->geometry().size(), widget->preferredSize());
    QCOMPARE(widget->layout()->geometry().size(), widget->preferredSize());
    qApp->processEvents();
    QCOMPARE(widget->geometry().size(), widget->preferredSize());
    QCOMPARE(widget->layout()->geometry().size(), widget->preferredSize());

    widget->layout()->invalidate();
    widget->setGeometry(1,1,34,34);  //Change just the position using setGeometry
    QCOMPARE(widget->geometry().size(), widget->preferredSize());
    QCOMPARE(widget->layout()->geometry().size(), widget->preferredSize());
    qApp->processEvents();
    QCOMPARE(widget->geometry().size(), widget->preferredSize());
    QCOMPARE(widget->layout()->geometry().size(), widget->preferredSize());

    widget->layout()->invalidate();
    widget->setGeometry(1,1,60,60);  //Change just the size using setGeometry
    QCOMPARE(widget->geometry().size(), QSizeF(60,60));
    QCOMPARE(widget->layout()->geometry().size(), QSizeF(60,60));
    qApp->processEvents();
    QCOMPARE(widget->geometry().size(), QSizeF(60,60));
    QCOMPARE(widget->layout()->geometry().size(), QSizeF(60,60));

    widget->layout()->invalidate();
    widget->setGeometry(0,0,34,34); //Change the size and position using setGeometry
    QCOMPARE(widget->geometry().size(), widget->preferredSize());
    QCOMPARE(widget->layout()->geometry().size(), widget->preferredSize());
    qApp->processEvents();
    QCOMPARE(widget->geometry().size(), widget->preferredSize());
    QCOMPARE(widget->layout()->geometry().size(), widget->preferredSize());

    QGraphicsLayout::setInstantInvalidatePropagation(false);
}