예제 #1
0
void tst_QGraphicsLayout::moveAndResizeWidgetInWidget()
{
    QFETCH(bool, instantInvalidatePropagation);

    QGraphicsLayout::setInstantInvalidatePropagation(instantInvalidatePropagation);
    QGraphicsScene scene;

    QGraphicsWidget *widget = new QGraphicsWidget;
    QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(widget);
    layout->setContentsMargins(0,0,0,0);
    WidgetToTestResizeEvents *innerWidget = new WidgetToTestResizeEvents;
    QGraphicsLinearLayout *innerLayout = new QGraphicsLinearLayout(innerWidget);
    innerLayout->setContentsMargins(0,0,0,0);
    QCOMPARE(widget->maximumSize(), QSizeF(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
    layout->addItem(innerWidget);
    widget->setMinimumSize(1,1);
    widget->setPreferredSize(1000,1000);
    widget->setMaximumSize(2000,2000);
    widget->resize(widget->preferredSize());
    innerWidget->setMinimumSize(1,1);
    qApp->processEvents();
    innerWidget->resizeEventCalled = false;

    QCOMPARE(widget->size(), QSizeF(1000, 1000));
    QCOMPARE(layout->geometry().size(), QSizeF(1000, 1000));
    QCOMPARE(innerWidget->size(), QSizeF(1000, 1000));

    innerLayout->invalidate();
    widget->setMaximumHeight(500);
    widget->setX(1);
    qApp->processEvents();
    QCOMPARE(widget->size(), QSizeF(1000, 500));
    QCOMPARE(innerWidget->size(), QSizeF(1000, 500));
    QVERIFY(innerWidget->resizeEventCalled);
}
예제 #2
0
QGraphicsWidget *DesignerActionManager::createFormEditorToolBar(QGraphicsItem *parent)
{
    QList<ActionInterface* > actions = Utils::filtered(designerActions(),
                                                       [](ActionInterface *action) {
            return action->type() ==  ActionInterface::FormEditorAction
                && action->action()->isVisible();
    });

    Utils::sort(actions, [](ActionInterface *l, ActionInterface *r) {
        return l->priority() > r->priority();
    });

    QGraphicsWidget *toolbar = new QGraphicsWidget(parent);

    auto layout = new QGraphicsLinearLayout;
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setSpacing(0);
    toolbar->setLayout(layout);

    for (ActionInterface *action : actions) {
        auto button = new FormEditorToolButton(action->action(), toolbar);
        layout->addItem(button);
    }

    toolbar->resize(toolbar->preferredSize());

    layout->invalidate();
    layout->activate();

    toolbar->update();

    return toolbar;
}
void Ut_MGridLayoutPolicy::testHeightForWidthInSubLayout()
{
    QFETCH(bool, useMLayout);
    QFETCH(bool, useInnerMLayout);
    QFETCH(bool, putInnerWidgetInWidget);

    QGraphicsWidget *form = new QGraphicsWidget;


    MGridLayoutPolicy *mpolicy = NULL;
    QGraphicsGridLayout *qlayout = NULL;

    if (useMLayout) {
        MLayout *mlayout = new MLayout(form);
        mlayout->setContentsMargins(0, 0, 0, 0);
        mpolicy = new MGridLayoutPolicy(mlayout);
        mpolicy->setSpacing(0);
    } else {
        qlayout = new QGraphicsGridLayout(form);
        qlayout->setContentsMargins(0, 0, 0, 0);
        qlayout->setSpacing(0);
    }

    QGraphicsWidget *topSpacer = createSpacer();
    QGraphicsWidget *leftSpacer = createSpacer();
    QGraphicsWidget *rightSpacer = createSpacer();
    leftSpacer->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
    rightSpacer->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);

    QGraphicsWidget *square = new SquareWidget;

    QGraphicsLayout *innerLayout = NULL;
    if (useInnerMLayout) {
        innerLayout = new MLayout();
        MLinearLayoutPolicy *policy = new MLinearLayoutPolicy(static_cast<MLayout *>(innerLayout), Qt::Horizontal);
        policy->addItem(square);
    } else {
        innerLayout = new QGraphicsLinearLayout(Qt::Horizontal);
        static_cast<QGraphicsLinearLayout *>(innerLayout)->addItem(square);
    }
    innerLayout->setContentsMargins(0,0,0,0);

    QGraphicsLayoutItem *innerItem;
    if (putInnerWidgetInWidget) {
        QGraphicsWidget *innerWidget = new QGraphicsWidget;
        innerWidget->setLayout(innerLayout);
        innerItem = innerWidget;
    } else {
        innerItem = innerLayout;
    }

    if (useMLayout) {
        mpolicy->addItem(topSpacer, 0, 1);
        mpolicy->addItem(leftSpacer, 1, 0);
        mpolicy->addItem(rightSpacer, 1, 2);
        mpolicy->addItem(innerItem, 1, 1);
    } else {
        qlayout->addItem(topSpacer, 0, 1);
        qlayout->addItem(leftSpacer, 1, 0);
        qlayout->addItem(rightSpacer, 1, 2);
        qlayout->addItem(innerItem, 1, 1);
    }


    QCOMPARE(form->preferredSize(), QSizeF(500,500));
    QCOMPARE(form->effectiveSizeHint(Qt::PreferredSize, QSizeF(100,-1)), QSizeF(100,100));

    delete form;
}
예제 #4
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);
}
void Ut_MPannableViewport::testSizeHint()
{
    struct SquareWidget : public QGraphicsWidget
    {
        SquareWidget() {
            QSizePolicy policy = sizePolicy();
            policy.setHeightForWidth(true);
            setSizePolicy(policy);
        }
        QSizeF sizeHint ( Qt::SizeHint which, const QSizeF & constraint = QSizeF() ) const {
           if (which != Qt::PreferredSize || constraint.width() == -1)
              return QGraphicsWidget::sizeHint(which, constraint);
           else
              return QSizeF( constraint.width(), constraint.width() );
        }
    };

    QFETCH(QString, cssName);
    QFETCH(qreal, width);
    QFETCH(qreal, height);

    QGraphicsWidget *widget = new SquareWidget();
    widget->setPreferredWidth(100);
    subject->setWidget(widget);
    subject->setObjectName(cssName);

    QCOMPARE(widget->preferredSize(), QSizeF(100,100));
    QCOMPARE(widget->effectiveSizeHint(Qt::PreferredSize, QSizeF(200,-1)), QSizeF(200,200));

    QCOMPARE(subject->layout()->preferredSize(), QSizeF(100,100));
    QCOMPARE(subject->layout()->effectiveSizeHint(Qt::PreferredSize, QSizeF(200,-1)), QSizeF(200,200));

    QCOMPARE(subject->preferredSize(), QSizeF(width,height));
    if (cssName == "noCssSize")
        QCOMPARE(subject->effectiveSizeHint(Qt::PreferredSize, QSizeF(200,-1)), QSizeF(200,200));

    // Test that the minimum and maximum size of the viewport is the same as the widget if we cannot pan in that direction
    widget->setMinimumSize(30,30);
    widget->setMaximumSize(200,200);

    QCOMPARE(subject->maximumSize(), QSizeF(200,200));

    subject->setPanDirection(0); //Can't pan in either direction
    QCOMPARE(subject->minimumSize(), QSizeF(30,30));
    subject->setPanDirection(Qt::Vertical);
    QCOMPARE(subject->minimumSize(), QSizeF(30,0));
    subject->setPanDirection(Qt::Horizontal);
    QCOMPARE(subject->minimumSize(), QSizeF(0,30));
    subject->setPanDirection(Qt::Horizontal | Qt::Vertical);
    QCOMPARE(subject->minimumSize(), QSizeF(0,0));

    for (int panDirection = 0; panDirection <= (Qt::Vertical | Qt::Horizontal); ++panDirection) {
        subject->setPanDirection((Qt::Orientations)panDirection);
        QCOMPARE(subject->preferredSize(), QSizeF(width,height));
        QCOMPARE(subject->maximumSize(), QSizeF(200,200));
        subject->setPreferredWidth(200);
        if (cssName == "noCssSize" || cssName == "width50")
            QCOMPARE(subject->preferredSize(), QSizeF(200,200));
        else
            QCOMPARE(subject->preferredSize(), QSizeF(200,50));
        subject->setPreferredWidth(-1);
    }
}