2022-05-10 10:06:48 +00:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2024-02-02 13:36:10 +00:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
#include <qtest.h>
|
2011-05-23 14:49:06 +00:00
|
|
|
|
|
|
|
#include <QtWidgets/QLayout>
|
|
|
|
#include <QtGui/QPainter>
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
static void processEvents()
|
|
|
|
{
|
|
|
|
QApplication::processEvents();
|
|
|
|
QApplication::processEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
class UpdateWidget : public QWidget
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
UpdateWidget(int rows, int columns)
|
|
|
|
: QWidget(0), rowCount(0), columnCount(0), opaqueChildren(false)
|
|
|
|
{
|
|
|
|
fill(rows, columns);
|
|
|
|
}
|
|
|
|
|
2020-10-07 11:05:48 +00:00
|
|
|
UpdateWidget(QWidget *parent = nullptr)
|
2011-04-27 10:05:43 +00:00
|
|
|
: QWidget(parent), rowCount(0), columnCount(0), opaqueChildren(false) {}
|
|
|
|
|
|
|
|
void fill(int rows, int columns)
|
|
|
|
{
|
|
|
|
if (rows == rowCount && columns == columnCount)
|
|
|
|
return;
|
|
|
|
delete layout();
|
|
|
|
QGridLayout *layout = new QGridLayout;
|
|
|
|
rowCount = rows;
|
|
|
|
columnCount = columns;
|
|
|
|
for (int row = 0; row < rowCount; ++row) {
|
|
|
|
for (int column = 0; column < columnCount; ++column) {
|
|
|
|
UpdateWidget *widget = new UpdateWidget;
|
|
|
|
widget->setFixedSize(20, 20);
|
|
|
|
layout->addWidget(widget, row, column);
|
|
|
|
children.append(widget);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setLayout(layout);
|
|
|
|
adjustSize();
|
|
|
|
QTest::qWait(250);
|
|
|
|
processEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
void setOpaqueChildren(bool enable)
|
|
|
|
{
|
|
|
|
if (opaqueChildren != enable) {
|
2023-08-05 17:32:09 +00:00
|
|
|
for (QWidget *w : std::as_const(children))
|
2011-04-27 10:05:43 +00:00
|
|
|
w->setAttribute(Qt::WA_OpaquePaintEvent, enable);
|
|
|
|
opaqueChildren = enable;
|
|
|
|
processEvents();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-24 14:31:47 +00:00
|
|
|
void paintEvent(QPaintEvent *) override
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
static int color = Qt::black;
|
|
|
|
|
|
|
|
QPainter painter(this);
|
|
|
|
painter.fillRect(rect(), Qt::GlobalColor(color));
|
|
|
|
|
|
|
|
if (++color > Qt::darkYellow)
|
|
|
|
color = Qt::black;
|
|
|
|
}
|
|
|
|
|
|
|
|
QRegion updateRegion;
|
|
|
|
QList<UpdateWidget*> children;
|
|
|
|
int rowCount;
|
|
|
|
int columnCount;
|
|
|
|
bool opaqueChildren;
|
|
|
|
};
|
|
|
|
|
|
|
|
class tst_QWidget : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public slots:
|
|
|
|
void initTestCase();
|
|
|
|
void init();
|
|
|
|
|
|
|
|
private slots:
|
|
|
|
void update_data();
|
|
|
|
void update();
|
|
|
|
void updatePartial_data();
|
|
|
|
void updatePartial();
|
|
|
|
void updateComplex_data();
|
|
|
|
void updateComplex();
|
|
|
|
|
|
|
|
private:
|
|
|
|
UpdateWidget widget;
|
|
|
|
};
|
|
|
|
|
|
|
|
void tst_QWidget::initTestCase()
|
|
|
|
{
|
|
|
|
widget.show();
|
2017-09-08 16:42:25 +00:00
|
|
|
QVERIFY(QTest::qWaitForWindowExposed(&widget));
|
2011-04-27 10:05:43 +00:00
|
|
|
QTest::qWait(300);
|
|
|
|
processEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QWidget::init()
|
|
|
|
{
|
|
|
|
QVERIFY(widget.isVisible());
|
|
|
|
for (int i = 0; i < 3; ++i)
|
|
|
|
processEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QWidget::update_data()
|
|
|
|
{
|
|
|
|
QTest::addColumn<int>("rows");
|
|
|
|
QTest::addColumn<int>("columns");
|
|
|
|
QTest::addColumn<int>("numUpdates");
|
|
|
|
QTest::addColumn<bool>("opaque");
|
|
|
|
|
|
|
|
QTest::newRow("10x10x1 transparent") << 10 << 10 << 1 << false;
|
|
|
|
QTest::newRow("10x10x10 transparent") << 10 << 10 << 10 << false;
|
|
|
|
QTest::newRow("10x10x100 transparent") << 10 << 10 << 100 << false;
|
|
|
|
QTest::newRow("10x10x1 opaque") << 10 << 10 << 1 << true;
|
|
|
|
QTest::newRow("10x10x10 opaque") << 10 << 10 << 10 << true;
|
|
|
|
QTest::newRow("10x10x100 opaque") << 10 << 10 << 100 << true;
|
|
|
|
QTest::newRow("25x25x1 transparent ") << 25 << 25 << 1 << false;
|
|
|
|
QTest::newRow("25x25x10 transparent") << 25 << 25 << 10 << false;
|
|
|
|
QTest::newRow("25x25x100 transparent") << 25 << 25 << 100 << false;
|
|
|
|
QTest::newRow("25x25x1 opaque") << 25 << 25 << 1 << true;
|
|
|
|
QTest::newRow("25x25x10 opaque") << 25 << 25 << 10 << true;
|
|
|
|
QTest::newRow("25x25x100 opaque") << 25 << 25 << 100 << true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QWidget::update()
|
|
|
|
{
|
|
|
|
QFETCH(int, rows);
|
|
|
|
QFETCH(int, columns);
|
|
|
|
QFETCH(int, numUpdates);
|
|
|
|
QFETCH(bool, opaque);
|
|
|
|
|
|
|
|
widget.fill(rows, columns);
|
|
|
|
widget.setOpaqueChildren(opaque);
|
|
|
|
|
|
|
|
QBENCHMARK {
|
|
|
|
for (int i = 0; i < widget.children.size(); ++i) {
|
|
|
|
for (int j = 0; j < numUpdates; ++j)
|
|
|
|
widget.children.at(i)->update();
|
|
|
|
QApplication::processEvents();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 14:07:06 +00:00
|
|
|
QApplication::processEvents();
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QWidget::updatePartial_data()
|
|
|
|
{
|
|
|
|
update_data();
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QWidget::updatePartial()
|
|
|
|
{
|
|
|
|
QFETCH(int, rows);
|
|
|
|
QFETCH(int, columns);
|
|
|
|
QFETCH(int, numUpdates);
|
|
|
|
QFETCH(bool, opaque);
|
|
|
|
|
|
|
|
widget.fill(rows, columns);
|
|
|
|
widget.setOpaqueChildren(opaque);
|
|
|
|
|
|
|
|
QBENCHMARK {
|
|
|
|
for (int i = 0; i < widget.children.size(); ++i) {
|
|
|
|
QWidget *w = widget.children[i];
|
|
|
|
const int x = w->width() / 2;
|
|
|
|
const int y = w->height() / 2;
|
|
|
|
for (int j = 0; j < numUpdates; ++j) {
|
|
|
|
w->update(0, 0, x, y);
|
|
|
|
w->update(x, 0, x, y);
|
|
|
|
w->update(0, y, x, y);
|
|
|
|
w->update(x, y, x, y);
|
|
|
|
}
|
|
|
|
QApplication::processEvents();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QWidget::updateComplex_data()
|
|
|
|
{
|
|
|
|
update_data();
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QWidget::updateComplex()
|
|
|
|
{
|
|
|
|
QFETCH(int, rows);
|
|
|
|
QFETCH(int, columns);
|
|
|
|
QFETCH(int, numUpdates);
|
|
|
|
QFETCH(bool, opaque);
|
|
|
|
|
|
|
|
widget.fill(rows, columns);
|
|
|
|
widget.setOpaqueChildren(opaque);
|
|
|
|
|
|
|
|
QBENCHMARK {
|
|
|
|
for (int i = 0; i < widget.children.size(); ++i) {
|
|
|
|
QWidget *w = widget.children[i];
|
|
|
|
const int x = w->width() / 2;
|
|
|
|
const int y = w->height() / 2;
|
|
|
|
QRegion r1(0, 0, x, y, QRegion::Ellipse);
|
|
|
|
QRegion r2(x, y, x, y, QRegion::Ellipse);
|
|
|
|
for (int j = 0; j < numUpdates; ++j) {
|
|
|
|
w->update(r1);
|
|
|
|
w->update(r2);
|
|
|
|
}
|
|
|
|
QApplication::processEvents();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QTEST_MAIN(tst_QWidget)
|
|
|
|
|
|
|
|
#include "tst_qwidget.moc"
|