void StatusIndicatorMenuDropDownView::setPannabilityAndLayout() { QGraphicsWidget *pannableWidget = pannableViewport->widget(); // Enable pannability if there is too much content to fit on the screen bool viewportShouldBePannable = pannableWidget->effectiveSizeHint(Qt::PreferredSize).height() > pannableViewport->geometry().height(); pannableViewport->setVerticalPanningPolicy(viewportShouldBePannable ? MPannableWidget::PanningAsNeeded : MPannableWidget::PanningAlwaysOff); // Appear or disappear the close button overlay based on close area position const QGraphicsWidget *closeButtonRow = static_cast<PannedWidgetController *>(pannableViewport->widget())->bottommostWidget(); qreal closeButtonRowBottomYPos = closeButtonRow->mapToItem(controller, QPointF(0, closeButtonRow->geometry().height())).y(); if (controller->sceneManager()) { qreal screenHeight = controller->sceneManager()->visibleSceneSize().height(); if (closeButtonRowBottomYPos <= screenHeight) { controller->sceneManager()->disappearSceneWindowNow(closeButtonOverlay); } else { controller->sceneManager()->appearSceneWindowNow(closeButtonOverlay); } } // Make the pannable area background window extend from the top of the pannable viewport halfway to the bottom of the close button row qreal viewPortYPos = pannableViewport->mapToItem(controller, QPointF()).y(); qreal backgroundHeight = (closeButtonRowBottomYPos - viewPortYPos) / 2; if (backgroundHeight < 0) { backgroundHeight = 0; } backgroundWidget->setMinimumHeight(backgroundHeight); backgroundWidget->setMaximumHeight(backgroundHeight); }
void tst_QGraphicsLayout::sizeHints() { QGraphicsView view; QGraphicsScene scene; QGraphicsWidget *window = new QGraphicsWidget(); scene.addItem(window); QGraphicsLinearLayout *lout = new QGraphicsLinearLayout(window); lout->setContentsMargins(0,0,0,0); QGraphicsWidget *gw = new QGraphicsWidget(window); gw->setMinimumSize(QSizeF(10,10)); gw->setPreferredSize(QSizeF(100,100)); gw->setMaximumSize(QSizeF(500,500)); lout->addItem(gw); QCOMPARE(lout->effectiveSizeHint(Qt::MinimumSize), gw->effectiveSizeHint(Qt::MinimumSize)); QCOMPARE(lout->effectiveSizeHint(Qt::PreferredSize), gw->effectiveSizeHint(Qt::PreferredSize)); QCOMPARE(lout->effectiveSizeHint(Qt::MaximumSize), gw->effectiveSizeHint(Qt::MaximumSize)); }
void tst_QGraphicsLinearLayout::heightForWidth() { QFETCH(bool, hfw); QFETCH(bool, nested); QGraphicsScene scene; QGraphicsWidget *form = new QGraphicsWidget; scene.addItem(form); QGraphicsLinearLayout *outerlayout = 0; if (nested) { outerlayout = new QGraphicsLinearLayout(form); for (int i = 0; i < 8; i++) { QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(Qt::Vertical); outerlayout->addItem(layout); outerlayout = layout; } } QGraphicsLinearLayout *qlayout = 0; qlayout = new QGraphicsLinearLayout(Qt::Vertical); if (nested) outerlayout->addItem(qlayout); else form->setLayout(qlayout); MySquareWidget *widget = new MySquareWidget; for (int i = 0; i < 1; i++) { widget = new MySquareWidget; QSizePolicy sizepolicy = widget->sizePolicy(); sizepolicy.setHeightForWidth(hfw); widget->setSizePolicy(sizepolicy); qlayout->addItem(widget); } // make sure only one iteration is done. // run with tst_QGraphicsLinearLayout.exe "heightForWidth" -tickcounter -iterations 6 // this will iterate 6 times the whole test, (not only the benchmark) // which should reduce warmup time and give a realistic picture of the performance of // effectiveSizeHint() QSizeF constraint(hfw ? 100 : -1, -1); QBENCHMARK { (void)form->effectiveSizeHint(Qt::PreferredSize, constraint); } }
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; }
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); } }