2011-04-27 10:05:43 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2016-01-15 12:36:27 +00:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** This file is part of the test suite of the Qt Toolkit.
|
|
|
|
**
|
2016-01-15 12:36:27 +00:00
|
|
|
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
|
2012-09-19 12:28:29 +00:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2015-01-28 08:44:43 +00:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
2016-01-15 12:36:27 +00:00
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2012-09-19 12:28:29 +00:00
|
|
|
**
|
2016-01-15 12:36:27 +00:00
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
#include <QtTest/QtTest>
|
|
|
|
|
|
|
|
#include <qcoreapplication.h>
|
|
|
|
#include <qdebug.h>
|
|
|
|
#include <qboxlayout.h>
|
|
|
|
#include <qmenubar.h>
|
|
|
|
#include <qdialog.h>
|
|
|
|
#include <qsizegrip.h>
|
|
|
|
#include <qlabel.h>
|
2011-05-23 14:49:06 +00:00
|
|
|
#include <QtWidgets/QFrame>
|
2012-11-23 14:13:41 +00:00
|
|
|
#include <QtWidgets/QStyleFactory>
|
2011-05-23 14:49:06 +00:00
|
|
|
#include <QtWidgets/QSizePolicy>
|
2012-03-16 06:00:45 +00:00
|
|
|
#include <QtWidgets/QComboBox>
|
2011-04-27 10:05:43 +00:00
|
|
|
#include <QPushButton>
|
|
|
|
#include <QRadioButton>
|
|
|
|
#include <private/qlayoutengine_p.h>
|
|
|
|
|
2017-10-30 12:45:18 +00:00
|
|
|
#include <QtTest/private/qtesthelpers_p.h>
|
|
|
|
|
|
|
|
using namespace QTestPrivate;
|
2012-12-28 16:35:54 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
class tst_QLayout : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
tst_QLayout();
|
|
|
|
virtual ~tst_QLayout();
|
|
|
|
|
|
|
|
private slots:
|
2015-06-11 10:11:22 +00:00
|
|
|
void cleanup() { QVERIFY(QApplication::topLevelWidgets().isEmpty()); }
|
2011-04-27 10:05:43 +00:00
|
|
|
void getSetCheck();
|
|
|
|
void geometry();
|
|
|
|
void smartMaxSize();
|
|
|
|
void setLayoutBugs();
|
|
|
|
void setContentsMargins();
|
|
|
|
void layoutItemRect();
|
|
|
|
void warnIfWrongParent();
|
|
|
|
void controlTypes();
|
2012-03-16 06:00:45 +00:00
|
|
|
void controlTypes2();
|
2011-04-27 10:05:43 +00:00
|
|
|
void adjustSizeShouldMakeSureLayoutIsActivated();
|
2012-08-10 13:24:33 +00:00
|
|
|
void testRetainSizeWhenHidden();
|
2011-04-27 10:05:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
tst_QLayout::tst_QLayout()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
tst_QLayout::~tst_QLayout()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// Testing get/set functions
|
|
|
|
void tst_QLayout::getSetCheck()
|
|
|
|
{
|
|
|
|
QBoxLayout obj1(QBoxLayout::LeftToRight);
|
|
|
|
// QWidget * QLayout::menuBar()
|
|
|
|
// void QLayout::setMenuBar(QWidget *)
|
|
|
|
QMenuBar *var1 = new QMenuBar();
|
|
|
|
obj1.setMenuBar(var1);
|
|
|
|
QCOMPARE(static_cast<QWidget *>(var1), obj1.menuBar());
|
|
|
|
obj1.setMenuBar((QWidget *)0);
|
|
|
|
QCOMPARE((QWidget *)0, obj1.menuBar());
|
|
|
|
delete var1;
|
|
|
|
}
|
|
|
|
|
|
|
|
class SizeHinterFrame : public QFrame
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SizeHinterFrame(const QSize &sh, const QSize &msh = QSize())
|
|
|
|
: QFrame(0), sh(sh), msh(msh) {
|
|
|
|
setFrameStyle(QFrame::Box | QFrame::Plain);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void setSizeHint(const QSize &s) { sh = s; }
|
|
|
|
QSize sizeHint() const { return sh; }
|
|
|
|
QSize minimumSizeHint() const { return msh; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
QSize sh;
|
|
|
|
QSize msh;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void tst_QLayout::geometry()
|
|
|
|
{
|
|
|
|
// For QWindowsStyle we know that QWidgetItem::geometry() and QWidget::geometry()
|
|
|
|
// should be the same.
|
2012-11-23 14:13:41 +00:00
|
|
|
QApplication::setStyle(QStyleFactory::create(QLatin1String("Windows")));
|
2011-04-27 10:05:43 +00:00
|
|
|
QWidget topLevel;
|
2012-12-28 16:35:54 +00:00
|
|
|
setFrameless(&topLevel);
|
2011-04-27 10:05:43 +00:00
|
|
|
QWidget w(&topLevel);
|
|
|
|
QVBoxLayout layout(&w);
|
|
|
|
SizeHinterFrame widget(QSize(100,100));
|
|
|
|
layout.addWidget(&widget);
|
|
|
|
QLayoutItem *item = layout.itemAt(0);
|
|
|
|
topLevel.show();
|
|
|
|
QApplication::processEvents();
|
|
|
|
QCOMPARE(item->geometry().size(), QSize(100,100));
|
|
|
|
|
|
|
|
widget.setMinimumSize(QSize(110,110));
|
|
|
|
QCOMPARE(item->geometry().size(), QSize(110,110));
|
|
|
|
|
|
|
|
widget.setMinimumSize(QSize(0,0));
|
|
|
|
widget.setMaximumSize(QSize(90,90));
|
|
|
|
widget.setSizeHint(QSize(100,100));
|
|
|
|
QCOMPARE(item->geometry().size(), QSize(90,90));
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QLayout::smartMaxSize()
|
|
|
|
{
|
2013-03-14 23:42:15 +00:00
|
|
|
QVector<int> expectedWidths;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-06-19 08:22:46 +00:00
|
|
|
QFile f(QFINDTESTDATA("baseline/smartmaxsize"));
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QCOMPARE(f.open(QIODevice::ReadOnly | QIODevice::Text), true);
|
|
|
|
|
|
|
|
QTextStream stream(&f);
|
|
|
|
|
|
|
|
while(!stream.atEnd()) {
|
|
|
|
QString line = stream.readLine(200);
|
|
|
|
expectedWidths.append(line.section(QLatin1Char(' '), 6, -1, QString::SectionSkipEmpty).toInt());
|
|
|
|
}
|
|
|
|
f.close();
|
|
|
|
|
|
|
|
int sizeCombinations[] = { 0, 10, 20, QWIDGETSIZE_MAX};
|
2013-03-14 23:42:15 +00:00
|
|
|
QSizePolicy::Policy policies[] = { QSizePolicy::Fixed,
|
|
|
|
QSizePolicy::Minimum,
|
|
|
|
QSizePolicy::Maximum,
|
2011-04-27 10:05:43 +00:00
|
|
|
QSizePolicy::Preferred,
|
|
|
|
QSizePolicy::Expanding,
|
|
|
|
QSizePolicy::MinimumExpanding,
|
|
|
|
QSizePolicy::Ignored
|
|
|
|
};
|
|
|
|
Qt::Alignment alignments[] = { 0,
|
|
|
|
Qt::AlignLeft,
|
|
|
|
Qt::AlignRight,
|
|
|
|
Qt::AlignHCenter
|
|
|
|
};
|
|
|
|
|
|
|
|
int expectedIndex = 0;
|
|
|
|
int regressionCount = 0;
|
2011-05-19 14:19:01 +00:00
|
|
|
for (size_t p = 0; p < sizeof(policies)/sizeof(QSizePolicy::Policy); ++p) {
|
2011-04-27 10:05:43 +00:00
|
|
|
QSizePolicy sizePolicy;
|
|
|
|
sizePolicy.setHorizontalPolicy(policies[p]);
|
2011-05-19 14:19:01 +00:00
|
|
|
for (size_t min = 0; min < sizeof(sizeCombinations)/sizeof(int); ++min) {
|
2011-04-27 10:05:43 +00:00
|
|
|
int minSize = sizeCombinations[min];
|
2011-05-19 14:19:01 +00:00
|
|
|
for (size_t max = 0; max < sizeof(sizeCombinations)/sizeof(int); ++max) {
|
2011-04-27 10:05:43 +00:00
|
|
|
int maxSize = sizeCombinations[max];
|
2011-05-19 14:19:01 +00:00
|
|
|
for (size_t sh = 0; sh < sizeof(sizeCombinations)/sizeof(int); ++sh) {
|
2011-04-27 10:05:43 +00:00
|
|
|
int sizeHint = sizeCombinations[sh];
|
2011-05-19 14:19:01 +00:00
|
|
|
for (size_t a = 0; a < sizeof(alignments)/sizeof(int); ++a) {
|
2011-04-27 10:05:43 +00:00
|
|
|
Qt::Alignment align = alignments[a];
|
|
|
|
QSize sz = qSmartMaxSize(QSize(sizeHint, 1), QSize(minSize, 1), QSize(maxSize, 1), sizePolicy, align);
|
|
|
|
int width = sz.width();
|
|
|
|
int expectedWidth = expectedWidths[expectedIndex];
|
|
|
|
if (width != expectedWidth) {
|
2015-10-13 07:46:56 +00:00
|
|
|
qDebug() << "error at index" << expectedIndex << ':' << sizePolicy.horizontalPolicy() << align << minSize << sizeHint << maxSize << width;
|
2011-04-27 10:05:43 +00:00
|
|
|
++regressionCount;
|
|
|
|
}
|
|
|
|
++expectedIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
QCOMPARE(regressionCount, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QLayout::setLayoutBugs()
|
|
|
|
{
|
|
|
|
QWidget widget(0);
|
|
|
|
QHBoxLayout *hBoxLayout = new QHBoxLayout(&widget);
|
|
|
|
|
|
|
|
for(int i = 0; i < 6; ++i) {
|
|
|
|
QPushButton *pushButton = new QPushButton("Press me!", &widget);
|
|
|
|
hBoxLayout->addWidget(pushButton);
|
|
|
|
}
|
|
|
|
|
|
|
|
widget.setLayout(hBoxLayout);
|
2015-07-30 13:16:36 +00:00
|
|
|
QCOMPARE(widget.layout(), hBoxLayout);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QWidget containerWidget(0);
|
|
|
|
containerWidget.setLayout(widget.layout());
|
2015-07-30 13:16:36 +00:00
|
|
|
QVERIFY(!widget.layout());
|
|
|
|
QCOMPARE(containerWidget.layout(), hBoxLayout);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class MyLayout : public QLayout
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MyLayout() : invalidated(false) {}
|
|
|
|
virtual void invalidate() {invalidated = true;}
|
|
|
|
bool invalidated;
|
|
|
|
QSize sizeHint() const {return QSize();}
|
|
|
|
void addItem(QLayoutItem*) {}
|
|
|
|
QLayoutItem* itemAt(int) const {return 0;}
|
|
|
|
QLayoutItem* takeAt(int) {return 0;}
|
|
|
|
int count() const {return 0;}
|
|
|
|
};
|
|
|
|
|
|
|
|
void tst_QLayout::setContentsMargins()
|
|
|
|
{
|
|
|
|
MyLayout layout;
|
|
|
|
layout.invalidated = false;
|
|
|
|
int left, top, right, bottom;
|
2013-03-14 23:42:15 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
layout.setContentsMargins(52, 53, 54, 55);
|
|
|
|
QVERIFY(layout.invalidated);
|
|
|
|
layout.invalidated = false;
|
|
|
|
|
|
|
|
layout.getContentsMargins(&left, &top, &right, &bottom);
|
|
|
|
QCOMPARE(left, 52);
|
|
|
|
QCOMPARE(top, 53);
|
|
|
|
QCOMPARE(right, 54);
|
|
|
|
QCOMPARE(bottom, 55);
|
2013-03-14 23:42:15 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
layout.setContentsMargins(52, 53, 54, 55);
|
|
|
|
QVERIFY(!layout.invalidated);
|
|
|
|
}
|
|
|
|
|
|
|
|
class EventReceiver : public QObject
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool eventFilter(QObject *watched, QEvent *event)
|
|
|
|
{
|
|
|
|
if (event->type() == QEvent::Show) {
|
|
|
|
geom = static_cast<QWidget*>(watched)->geometry();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
QRect geom;
|
|
|
|
};
|
|
|
|
|
|
|
|
void tst_QLayout::layoutItemRect()
|
|
|
|
{
|
2012-01-16 10:49:53 +00:00
|
|
|
#ifdef Q_OS_MAC
|
2012-11-16 23:52:26 +00:00
|
|
|
if (QApplication::style()->inherits("QMacStyle")) {
|
2011-04-27 10:05:43 +00:00
|
|
|
QWidget *window = new QWidget;
|
|
|
|
QRadioButton *radio = new QRadioButton(window);
|
|
|
|
QWidgetItem item(radio);
|
|
|
|
EventReceiver eventReceiver;
|
|
|
|
radio->installEventFilter(&eventReceiver);
|
|
|
|
|
|
|
|
radio->show();
|
|
|
|
QApplication::processEvents();
|
|
|
|
QApplication::processEvents();
|
|
|
|
QSize s = item.sizeHint();
|
|
|
|
|
|
|
|
item.setAlignment(Qt::AlignVCenter);
|
|
|
|
item.setGeometry(QRect(QPoint(0, 0), s));
|
|
|
|
|
|
|
|
QCOMPARE(radio->geometry().size(), radio->sizeHint());
|
|
|
|
delete radio;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QLayout::warnIfWrongParent()
|
|
|
|
{
|
|
|
|
QWidget root;
|
|
|
|
QHBoxLayout lay;
|
|
|
|
lay.setParent(&root);
|
|
|
|
QTest::ignoreMessage(QtWarningMsg, "QLayout::parentWidget: A layout can only have another layout as a parent.");
|
2016-09-30 13:16:55 +00:00
|
|
|
QCOMPARE(lay.parentWidget(), nullptr);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QLayout::controlTypes()
|
|
|
|
{
|
|
|
|
QVBoxLayout layout;
|
|
|
|
QCOMPARE(layout.controlTypes(), QSizePolicy::DefaultType);
|
|
|
|
QSizePolicy p;
|
|
|
|
QCOMPARE(p.controlType(),QSizePolicy::DefaultType);
|
2012-03-16 06:00:45 +00:00
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-03-16 06:00:45 +00:00
|
|
|
void tst_QLayout::controlTypes2()
|
|
|
|
{
|
|
|
|
QWidget main;
|
|
|
|
QVBoxLayout *const layout = new QVBoxLayout(&main);
|
|
|
|
layout->setMargin(0);
|
|
|
|
QComboBox *combo = new QComboBox(&main);
|
|
|
|
layout->addWidget(combo);
|
|
|
|
QCOMPARE(layout->controlTypes(), QSizePolicy::ComboBox);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QLayout::adjustSizeShouldMakeSureLayoutIsActivated()
|
|
|
|
{
|
|
|
|
QWidget main;
|
|
|
|
|
|
|
|
QVBoxLayout *const layout = new QVBoxLayout(&main);
|
|
|
|
layout->setMargin(0);
|
|
|
|
SizeHinterFrame *frame = new SizeHinterFrame(QSize(200, 10), QSize(200, 8));
|
|
|
|
frame->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
|
|
|
|
layout->addWidget(frame);
|
|
|
|
|
|
|
|
SizeHinterFrame *frame2 = new SizeHinterFrame(QSize(200, 10), QSize(200, 8));
|
|
|
|
frame2->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
|
|
|
|
layout->addWidget(frame2);
|
|
|
|
|
|
|
|
main.show();
|
|
|
|
|
|
|
|
frame2->hide();
|
|
|
|
main.adjustSize();
|
|
|
|
QCOMPARE(main.size(), QSize(200, 10));
|
|
|
|
}
|
|
|
|
|
2012-08-10 13:24:33 +00:00
|
|
|
void tst_QLayout::testRetainSizeWhenHidden()
|
|
|
|
{
|
2017-06-02 09:53:23 +00:00
|
|
|
#if defined(Q_OS_ANDROID) && !defined(Q_OS_ANDROID_EMBEDDED)
|
2015-01-13 15:13:34 +00:00
|
|
|
QSKIP("Test does not work on platforms which default to showMaximized()");
|
|
|
|
#endif
|
|
|
|
|
2012-08-10 13:24:33 +00:00
|
|
|
QWidget widget;
|
|
|
|
QBoxLayout layout(QBoxLayout::TopToBottom, &widget);
|
|
|
|
|
|
|
|
QLabel *label1 = new QLabel("label1 text", &widget);
|
|
|
|
layout.addWidget(label1);
|
|
|
|
QLabel *label2 = new QLabel("label2 text", &widget);
|
|
|
|
layout.addWidget(label2);
|
|
|
|
|
|
|
|
widget.show();
|
|
|
|
QVERIFY(QTest::qWaitForWindowExposed(&widget));
|
|
|
|
int normalHeight = widget.height();
|
|
|
|
|
|
|
|
// a. Verify that a removed visible will mean lesser size after adjust
|
|
|
|
label1->hide();
|
|
|
|
widget.adjustSize();
|
|
|
|
int heightWithoutLabel1 = widget.height();
|
|
|
|
QVERIFY(heightWithoutLabel1 < normalHeight);
|
|
|
|
|
|
|
|
// b restore with verify that the size is the same
|
|
|
|
label1->show();
|
|
|
|
QCOMPARE(widget.sizeHint().height(), normalHeight);
|
|
|
|
|
|
|
|
// c verify that a policy with retainSizeWhenHidden is respected
|
|
|
|
QSizePolicy sp_remove = label1->sizePolicy();
|
|
|
|
QSizePolicy sp_retain = label1->sizePolicy();
|
|
|
|
sp_retain.setRetainSizeWhenHidden(true);
|
|
|
|
|
|
|
|
label1->setSizePolicy(sp_retain);
|
|
|
|
label1->hide();
|
|
|
|
QCOMPARE(widget.sizeHint().height(), normalHeight);
|
|
|
|
|
|
|
|
// d check that changing the policy to not wanting size will result in lesser size
|
|
|
|
label1->setSizePolicy(sp_remove);
|
|
|
|
QCOMPARE(widget.sizeHint().height(), heightWithoutLabel1);
|
|
|
|
|
|
|
|
// e verify that changing back the hidden widget to want the hidden size will ensure that it gets more size
|
|
|
|
label1->setSizePolicy(sp_retain);
|
|
|
|
QCOMPARE(widget.sizeHint().height(), normalHeight);
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
QTEST_MAIN(tst_QLayout)
|
|
|
|
#include "tst_qlayout.moc"
|