From beb44e400492448070716bcd708ed1716a057a41 Mon Sep 17 00:00:00 2001 From: Uwe Rathmann Date: Thu, 25 Jul 2019 18:39:50 +0200 Subject: [PATCH] more test scenarios --- playground/grids/GridAccessor.cpp | 20 ++- playground/grids/GridAccessor.h | 9 +- playground/grids/GridGraphics.cpp | 16 ++ playground/grids/GridGraphics.h | 3 + playground/grids/GridQuick.cpp | 37 ++++- playground/grids/GridQuick.h | 3 + playground/grids/GridSkinny.cpp | 24 +-- playground/grids/GridSkinny.h | 4 +- playground/grids/GridWidgets.cpp | 17 +++ playground/grids/GridWidgets.h | 4 +- playground/grids/TestBox.cpp | 163 ++++++++++++++++----- playground/grids/TestBox.h | 61 ++++---- playground/grids/main.cpp | 233 ++++++++++++++++++++++-------- 13 files changed, 448 insertions(+), 146 deletions(-) diff --git a/playground/grids/GridAccessor.cpp b/playground/grids/GridAccessor.cpp index 87d706a6..d93b02d2 100644 --- a/playground/grids/GridAccessor.cpp +++ b/playground/grids/GridAccessor.cpp @@ -91,7 +91,25 @@ void GridAccessor::setMaximumSizeAt( int index, const QSize& size ) setMaximumHeightAt( index, size.height() ); } -void GridAccessor::setSizePolicyAt( +void GridAccessor::setFixedWidthAt( int index, int hint ) +{ + setMinimumWidthAt( index, hint ); + setMaximumWidthAt( index, hint ); +} + +void GridAccessor::setFixedHeightAt( int index, int hint ) +{ + setMinimumHeightAt( index, hint ); + setMaximumHeightAt( index, hint ); +} + +void GridAccessor::setFixedSizeAt( int index, const QSize& size ) +{ + setMinimumSizeAt( index, size ); + setMaximumSizeAt( index, size ); +} + +void GridAccessor::setSizePoliciesAt( int index, int horizontalPolicy, int verticalPolicy ) { setSizePolicyAt( index, Qt::Horizontal, horizontalPolicy ); diff --git a/playground/grids/GridAccessor.h b/playground/grids/GridAccessor.h index 8c064e27..f6f0da81 100644 --- a/playground/grids/GridAccessor.h +++ b/playground/grids/GridAccessor.h @@ -37,7 +37,7 @@ class GridAccessor virtual void setSizeHintAt( int index, Qt::Orientation, Qt::SizeHint, int hint ) = 0; virtual void setSizePolicyAt( int index, Qt::Orientation, int policy ) = 0; - void setSizePolicyAt( int index, int horizontalPolicy, int verticalPolicy ); + void setSizePoliciesAt( int index, int horizontalPolicy, int verticalPolicy ); void setMinimumWidthAt( int index, int hint ); void setMinimumHeightAt( int index, int hint ); @@ -51,8 +51,15 @@ class GridAccessor void setMaximumHeightAt( int index, int hint ); void setMaximumSizeAt( int index, const QSize& ); + void setFixedWidthAt( int index, int hint ); + void setFixedHeightAt( int index, int hint ); + void setFixedSizeAt( int index, const QSize& ); + virtual void setAlignmentAt( int index, Qt::Alignment ) = 0; virtual void setRetainSizeWhenHiddenAt( int index, bool on ) = 0; + virtual void setVisibleAt( int index, bool on ) = 0; + + virtual QSize preferredSize() const = 0; }; #endif diff --git a/playground/grids/GridGraphics.cpp b/playground/grids/GridGraphics.cpp index 633799de..6652eb69 100644 --- a/playground/grids/GridGraphics.cpp +++ b/playground/grids/GridGraphics.cpp @@ -69,7 +69,9 @@ GridGraphics::GridGraphics( QWidget* parent ) setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff ); auto grid = new Grid(); + m_layout = new QGraphicsGridLayout( grid ); + m_layout->setSpacing( 5 ); auto scene = new QGraphicsScene(); scene->addItem( grid ); @@ -220,6 +222,20 @@ void GridGraphics::setRetainSizeWhenHiddenAt( int index, bool on ) } } +void GridGraphics::setVisibleAt( int index, bool on ) +{ + if ( auto layoutItem = m_layout->itemAt( index ) ) + { + if ( auto item = layoutItem->graphicsItem() ) + item->setVisible( on ); + } +} + +QSize GridGraphics::preferredSize() const +{ + return m_layout->preferredSize().toSize(); +} + void GridGraphics::resizeEvent( QResizeEvent* ) { auto sceneRect = contentsRect(); diff --git a/playground/grids/GridGraphics.h b/playground/grids/GridGraphics.h index c3e2d8f2..fcb9ceab 100644 --- a/playground/grids/GridGraphics.h +++ b/playground/grids/GridGraphics.h @@ -29,6 +29,9 @@ class GridGraphics : public QGraphicsView, public GridAccessor void setSizePolicyAt( int index, Qt::Orientation, int policy ) override; void setAlignmentAt( int index, Qt::Alignment ) override; void setRetainSizeWhenHiddenAt( int index, bool on ) override; + void setVisibleAt( int index, bool on ) override; + + QSize preferredSize() const override; protected: void resizeEvent( QResizeEvent* ) override; diff --git a/playground/grids/GridQuick.cpp b/playground/grids/GridQuick.cpp index decca2c8..01c8c235 100644 --- a/playground/grids/GridQuick.cpp +++ b/playground/grids/GridQuick.cpp @@ -17,6 +17,15 @@ static QQuickItem* createQml( const char* qmlCode ) return qobject_cast< QQuickItem* >( component.create() ); } +static QQuickItem* itemAt( const QQuickItem* grid, int index ) +{ + const auto children = grid->childItems(); + if ( ( index >= 0 ) && ( index < children.count() ) ) + return children.at( index ); + + return nullptr; +} + static QObject* attachedProperties( const QQuickItem* item ) { for ( auto child : item->children() ) @@ -30,15 +39,12 @@ static QObject* attachedProperties( const QQuickItem* item ) static QObject* attachedPropertiesAt( const QQuickItem* grid, int index ) { - const auto children = grid->childItems(); - - if ( ( index >= 0 ) && ( index < children.count() ) ) - return attachedProperties( children.at( index ) ); + if ( auto item = itemAt( grid, index ) ) + return attachedProperties( item ); return nullptr; } - GridQuick::GridQuick( QWidget* parent ) : QQuickWidget( parent ) { @@ -50,6 +56,8 @@ GridQuick::GridQuick( QWidget* parent ) setContent( QUrl(), nullptr, contentItem ); m_grid = contentItem->childItems().first(); + m_grid->setProperty( "rowSpacing", 5 ); + m_grid->setProperty( "columnSpacing", 5 ); } GridQuick::~GridQuick() @@ -127,7 +135,7 @@ void GridQuick::setSizeHintAt( int index, Qt::Orientation orientation, if ( orientation == Qt::Horizontal ) props->setProperty( "maximumWidth", size ); else - props->setProperty( "maximumWidth", size ); + props->setProperty( "maximumHeight", size ); break; } @@ -167,12 +175,25 @@ void GridQuick::setRetainSizeWhenHiddenAt( int, bool ) qWarning() << "setRetainSizeWhenHidden is not supported by Quick Layouts."; } +void GridQuick::setVisibleAt( int index, bool on ) +{ + if ( auto item = itemAt( m_grid, index ) ) + item->setVisible( on ); +} + +static const qreal margin = 10.0; + +QSize GridQuick::preferredSize() const +{ + return QSize( + m_grid->implicitWidth() + 2 * margin, + m_grid->implicitHeight() + 2 * margin ); +} + void GridQuick::resizeEvent( QResizeEvent* event ) { QQuickWidget::resizeEvent( event ); - const qreal margin = 10.0; - m_grid->setX( margin ); m_grid->setY( margin ); m_grid->setWidth( width() - 2 * margin ); diff --git a/playground/grids/GridQuick.h b/playground/grids/GridQuick.h index 5b7db37b..760556f4 100644 --- a/playground/grids/GridQuick.h +++ b/playground/grids/GridQuick.h @@ -29,6 +29,9 @@ class GridQuick : public QQuickWidget, public GridAccessor void setSizePolicyAt( int index, Qt::Orientation, int policy ) override; void setAlignmentAt( int index, Qt::Alignment ) override; void setRetainSizeWhenHiddenAt( int index, bool on ) override; + void setVisibleAt( int index, bool on ) override; + + QSize preferredSize() const override; protected: void resizeEvent( QResizeEvent* ) override; diff --git a/playground/grids/GridSkinny.cpp b/playground/grids/GridSkinny.cpp index 6f9cd5e4..ebbe97ef 100644 --- a/playground/grids/GridSkinny.cpp +++ b/playground/grids/GridSkinny.cpp @@ -34,17 +34,6 @@ namespace private: QByteArray m_colorName; }; - - class Grid : public QskGridBox - { - public: - Grid( QQuickItem* parent = nullptr ) - : QskGridBox( parent ) - { - setBackgroundColor( Qt::white ); - setMargins( 10 ); - } - }; } GridSkinny::GridSkinny( QWidget* parent ) @@ -56,6 +45,7 @@ GridSkinny::GridSkinny( QWidget* parent ) m_grid = new QskGridBox(); m_grid->setBackgroundColor( Qt::white ); m_grid->setMargins( 10 ); + m_grid->setSpacing( 5 ); setContent( QUrl(), nullptr, m_grid ); } @@ -130,5 +120,15 @@ void GridSkinny::setRetainSizeWhenHiddenAt( int index, bool on ) { if ( auto item = m_grid->itemAtIndex( index ) ) m_grid->setRetainSizeWhenHidden( item, on ); - +} + +void GridSkinny::setVisibleAt( int index, bool on ) +{ + if ( auto item = m_grid->itemAtIndex( index ) ) + item->setVisible( on ); +} + +QSize GridSkinny::preferredSize() const +{ + return m_grid->preferredSize().toSize(); } diff --git a/playground/grids/GridSkinny.h b/playground/grids/GridSkinny.h index 77f25237..7cb562fa 100644 --- a/playground/grids/GridSkinny.h +++ b/playground/grids/GridSkinny.h @@ -27,9 +27,11 @@ class GridSkinny : public QQuickWidget, public GridAccessor void setSizeHintAt( int index, Qt::Orientation, Qt::SizeHint, int hint ) override; void setSizePolicyAt( int index, Qt::Orientation, int policy ) override; - void setAlignmentAt( int index, Qt::Alignment ) override; void setRetainSizeWhenHiddenAt( int index, bool on ) override; + void setVisibleAt( int index, bool on ) override; + + QSize preferredSize() const override; private: QskGridBox* m_grid; diff --git a/playground/grids/GridWidgets.cpp b/playground/grids/GridWidgets.cpp index 20cffe27..2dc071c2 100644 --- a/playground/grids/GridWidgets.cpp +++ b/playground/grids/GridWidgets.cpp @@ -60,6 +60,8 @@ GridWidgets::GridWidgets( QWidget* parent ) setContentsMargins( QMargins() ); m_layout = new QGridLayout(); + m_layout->setSpacing( 5 ); + setLayout( m_layout ); } @@ -197,3 +199,18 @@ void GridWidgets::setRetainSizeWhenHiddenAt( int index, bool on ) } } } + +void GridWidgets::setVisibleAt( int index, bool on ) +{ + if ( auto layoutItem = m_layout->itemAt( index ) ) + { + if ( auto widget = layoutItem->widget() ) + widget->setVisible( on ); + } +} + +QSize GridWidgets::preferredSize() const +{ + return sizeHint(); +} + diff --git a/playground/grids/GridWidgets.h b/playground/grids/GridWidgets.h index ebc69f9d..d2f56746 100644 --- a/playground/grids/GridWidgets.h +++ b/playground/grids/GridWidgets.h @@ -27,9 +27,11 @@ class GridWidgets : public QWidget, public GridAccessor void setSizeHintAt( int index, Qt::Orientation, Qt::SizeHint, int hint ) override; void setSizePolicyAt( int index, Qt::Orientation, int policy ) override; - void setAlignmentAt( int index, Qt::Alignment ) override; void setRetainSizeWhenHiddenAt( int index, bool on ) override; + void setVisibleAt( int index, bool on ) override; + + QSize preferredSize() const override; private: QGridLayout* m_layout; diff --git a/playground/grids/TestBox.cpp b/playground/grids/TestBox.cpp index 4b965a9b..d8f6b555 100644 --- a/playground/grids/TestBox.cpp +++ b/playground/grids/TestBox.cpp @@ -8,6 +8,7 @@ #include "GridSkinny.h" #include "GridWidgets.h" #include "GridQuick.h" +#include TestBox::TestBox( QWidget* parent ) : QWidget( parent ) @@ -17,16 +18,29 @@ TestBox::TestBox( QWidget* parent ) setContentsMargins( QMargins( 10, 10, 10, 10 ) ); - grids[Skinny] = new GridSkinny( this ); - grids[Widgets] = new GridWidgets( this ); - grids[Graphics] = new GridGraphics( this ); - grids[Quick] = new GridQuick( this ); + m_grids[Skinny] = new GridSkinny( this ); + m_labels[Skinny] = new QLabel( "Skinny", this ); + + m_grids[Widgets] = new GridWidgets( this ); + m_labels[Widgets] = new QLabel( "Widget", this ); + + m_grids[Graphics] = new GridGraphics( this ); + m_labels[Graphics] = new QLabel( "Graphic", this ); + + m_grids[Quick] = new GridQuick( this ); + m_labels[Quick] = new QLabel( "Quick", this ); } TestBox::~TestBox() { } +void TestBox::enableGrid( int index, bool on ) +{ + m_grids[index]->setVisible( on ); + m_labels[index]->setVisible( on ); +} + void TestBox::setColumns( int columnCount ) { m_columnCount = qBound( 1, columnCount, static_cast< int >( GridCount ) ); @@ -38,81 +52,144 @@ void TestBox::setColumns( int columnCount ) void TestBox::insert( const QByteArray& colorName, int row, int column, int rowSpan, int columnSpan ) { - for ( auto grid : grids ) + for ( auto grid : m_grids ) { - auto accessor = dynamic_cast< GridAccessor* >( grid ); - accessor->insert( colorName, - row, column, rowSpan, columnSpan ); + if ( grid->isVisibleTo( this ) ) + { + auto accessor = dynamic_cast< GridAccessor* >( grid ); + accessor->insert( colorName, + row, column, rowSpan, columnSpan ); + } } } void TestBox::setSpacing( Qt::Orientations orientations, int spacing ) { - for ( auto grid : grids ) + for ( auto grid : m_grids ) { - auto accessor = dynamic_cast< GridAccessor* >( grid ); - accessor->setSpacing( orientations, spacing ); + if ( grid->isVisibleTo( this ) ) + { + auto accessor = dynamic_cast< GridAccessor* >( grid ); + accessor->setSpacing( orientations, spacing ); + } } } void TestBox::setSizeHint( int pos, Qt::Orientation orientation, Qt::SizeHint which, int hint ) { - for ( auto grid : grids ) + for ( auto grid : m_grids ) { - auto accessor = dynamic_cast< GridAccessor* >( grid ); - accessor->setSizeHint( pos, orientation, which, hint ); + if ( grid->isVisibleTo( this ) ) + { + auto accessor = dynamic_cast< GridAccessor* >( grid ); + accessor->setSizeHint( pos, orientation, which, hint ); + } } } void TestBox::setStretchFactor( int pos, Qt::Orientation orientation, int stretch ) { - for ( auto grid : grids ) + for ( auto grid : m_grids ) { - auto accessor = dynamic_cast< GridAccessor* >( grid ); - accessor->setStretchFactor( pos, orientation, stretch ); + if ( grid->isVisibleTo( this ) ) + { + auto accessor = dynamic_cast< GridAccessor* >( grid ); + accessor->setStretchFactor( pos, orientation, stretch ); + } } } void TestBox::setSizeHintAt( int index, Qt::Orientation orientation, Qt::SizeHint which, int hint ) { - for ( auto grid : grids ) + for ( auto grid : m_grids ) { - auto accessor = dynamic_cast< GridAccessor* >( grid ); - accessor->setSizeHintAt( index, orientation, which, hint ); + if ( grid->isVisibleTo( this ) ) + { + auto accessor = dynamic_cast< GridAccessor* >( grid ); + accessor->setSizeHintAt( index, orientation, which, hint ); + } } } void TestBox::setSizePolicyAt( int index, Qt::Orientation orientation, int policy ) { - for ( auto grid : grids ) + for ( auto grid : m_grids ) { - auto accessor = dynamic_cast< GridAccessor* >( grid ); - accessor->setSizePolicyAt( index, orientation, policy ); + if ( grid->isVisibleTo( this ) ) + { + auto accessor = dynamic_cast< GridAccessor* >( grid ); + accessor->setSizePolicyAt( index, orientation, policy ); + } } } void TestBox::setAlignmentAt( int index, Qt::Alignment alignment ) { - for ( auto grid : grids ) + for ( auto grid : m_grids ) { - auto accessor = dynamic_cast< GridAccessor* >( grid ); - accessor->setAlignmentAt( index, alignment ); + if ( grid->isVisibleTo( this ) ) + { + auto accessor = dynamic_cast< GridAccessor* >( grid ); + accessor->setAlignmentAt( index, alignment ); + } } } void TestBox::setRetainSizeWhenHiddenAt( int index, bool on ) { - for ( auto grid : grids ) + for ( auto grid : m_grids ) { - auto accessor = dynamic_cast< GridAccessor* >( grid ); - accessor->setRetainSizeWhenHiddenAt( index, on ); + if ( grid->isVisibleTo( this ) ) + { + auto accessor = dynamic_cast< GridAccessor* >( grid ); + accessor->setRetainSizeWhenHiddenAt( index, on ); + } } } +void TestBox::setVisibleAt( int index, bool on ) +{ + for ( auto grid : m_grids ) + { + if ( grid->isVisibleTo( this ) ) + { + auto accessor = dynamic_cast< GridAccessor* >( grid ); + accessor->setVisibleAt( index, on ); + } + } + +} + +QSize TestBox::preferredSize() const +{ + return QSize( -1, -1 ); +} + +bool TestBox::event( QEvent* event ) +{ + if ( event->type() == QEvent::Polish ) + { + for ( int i = 0; i < GridCount; i++ ) + { + auto accessor = dynamic_cast< GridAccessor* >( m_grids[i] ); + auto label = m_labels[i]; + + const auto hint = accessor->preferredSize(); + + QString text = label->text(); + text += QString( ": ( %1x%2 )" ).arg( hint.width() ).arg( hint.height() ); + + label->setText( text ); + } + } + + return QWidget::event( event ); +} + void TestBox::resizeEvent( QResizeEvent* ) { layoutGrids(); @@ -127,13 +204,21 @@ void TestBox::layoutGrids() const auto r = contentsRect(); - int rowCount = GridCount / m_columnCount; - if ( rowCount * m_columnCount < GridCount ) + int gridCount = 0; + for ( int i = 0; i < GridCount; i++ ) + { + if ( m_grids[i]->isVisibleTo( this ) ) + gridCount++; + } + + int columnCount = qMin( gridCount, m_columnCount ); + int rowCount = gridCount / columnCount; + if ( rowCount * columnCount < gridCount ) rowCount++; const int spacing = 5; - const int width = ( r.width() - ( m_columnCount - 1 ) * spacing ) / m_columnCount; + const int width = ( r.width() - ( columnCount - 1 ) * spacing ) / columnCount; const int height = ( r.height() - ( rowCount - 1 ) * spacing ) / rowCount; int row = 0; @@ -141,12 +226,22 @@ void TestBox::layoutGrids() for ( int i = 0; i < GridCount; i++ ) { + if ( !m_grids[i]->isVisibleTo( this ) ) + continue; + const int x = r.left() + col * ( spacing + width ); const int y = r.top() + row * ( spacing + height ); - grids[i]->setGeometry( x, y, width, height ); + const QRect rect( x, y, width, height ); + m_grids[i]->setGeometry( rect ); - if ( ++col >= m_columnCount ) + const auto sz = m_labels[i]->sizeHint(); + + m_labels[i]->setGeometry( + rect.right() - sz.width() - 10, rect.top() + 10, + sz.width(), sz.height() ); + + if ( ++col >= columnCount ) { col = 0; row++; diff --git a/playground/grids/TestBox.h b/playground/grids/TestBox.h index b56bf3d0..c7a9cd79 100644 --- a/playground/grids/TestBox.h +++ b/playground/grids/TestBox.h @@ -9,34 +9,11 @@ #include "GridAccessor.h" #include +class QLabel; + class TestBox : public QWidget, public GridAccessor { public: - TestBox( QWidget* parent = nullptr ); - ~TestBox() override; - - void setColumns( int ); - - void insert( const QByteArray& colorName, - int row, int column, int rowSpan, int columnSpan ) override; - - void setSpacing( Qt::Orientations, int spacing ) override; - - void setStretchFactor( int pos, Qt::Orientation, int stretch ) override; - void setSizeHint( int pos, Qt::Orientation, Qt::SizeHint, int hint ) override; - - void setSizeHintAt( int index, Qt::Orientation, Qt::SizeHint, int hint ) override; - void setSizePolicyAt( int index, Qt::Orientation, int policy ) override; - - void setAlignmentAt( int index, Qt::Alignment ) override; - void setRetainSizeWhenHiddenAt( int index, bool on ) override; - - protected: - void resizeEvent( QResizeEvent* ) override; - - private: - void layoutGrids(); - enum { Skinny, @@ -47,7 +24,39 @@ class TestBox : public QWidget, public GridAccessor GridCount }; - QWidget* grids[ GridCount ]; + TestBox( QWidget* parent = nullptr ); + ~TestBox() override; + + void setColumns( int ); + void enableGrid( int, bool on ); + + void insert( const QByteArray& colorName, + int row, int column, int rowSpan, int columnSpan ) override; + + void setSpacing( Qt::Orientations, int spacing ) override; + using GridAccessor::setSpacing; + + void setStretchFactor( int pos, Qt::Orientation, int stretch ) override; + void setSizeHint( int pos, Qt::Orientation, Qt::SizeHint, int hint ) override; + + void setSizeHintAt( int index, Qt::Orientation, Qt::SizeHint, int hint ) override; + void setSizePolicyAt( int index, Qt::Orientation, int policy ) override; + void setAlignmentAt( int index, Qt::Alignment ) override; + void setRetainSizeWhenHiddenAt( int index, bool on ) override; + void setVisibleAt( int index, bool on ) override; + + QSize preferredSize() const override; + + protected: + bool event( QEvent* ) override; + void resizeEvent( QResizeEvent* ) override; + + private: + void layoutGrids(); + + QWidget* m_grids[ GridCount ]; + QLabel* m_labels[ GridCount ]; + int m_columnCount = 2; }; diff --git a/playground/grids/main.cpp b/playground/grids/main.cpp index 05b05377..fe501174 100644 --- a/playground/grids/main.cpp +++ b/playground/grids/main.cpp @@ -4,93 +4,110 @@ *****************************************************************************/ #include "TestBox.h" -#include + #include +#include +#include class MainBox : public TestBox { public: MainBox( int id ) { - switch( id ) + using Testcase = void (MainBox::*)(); + + const Testcase tests[] = { - case 0: - setup0(); - break; + &MainBox::test0, &MainBox::test1, &MainBox::test2, + &MainBox::test3, &MainBox::test4, &MainBox::test5, + &MainBox::test6, &MainBox::test7, &MainBox::test8 + }; - case 1: - setup1(); - break; + const int count = static_cast( sizeof( tests ) / sizeof( tests[0] ) ); - case 2: - setup2(); - break; + if ( id < 0 || id >= count ) + id = 0; - case 3: - setup3(); - break; - } + ( this->*tests[id] )(); } + void insert( const char* colorName, int row, int column ) + { + insert( colorName, row, column, 1, 1 ); + } + + using TestBox::insert; + private: - void setup0(); - void setup1(); - void setup2(); - void setup3(); - - void setSizePolicyAt( int index, - QskSizePolicy::Policy horizontalPolicy, - QskSizePolicy::Policy verticalPolicy ) - { - TestBox::setSizePolicyAt( index, Qt::Horizontal, horizontalPolicy ); - TestBox::setSizePolicyAt( index, Qt::Vertical, verticalPolicy ); - } + void test0(); + void test1(); + void test2(); + void test3(); + void test4(); + void test5(); + void test6(); + void test7(); + void test8(); }; -void MainBox::setup0() +void MainBox::test0() { // something, that works with all layouts - insert( "PaleVioletRed", 0, 0, 1, 1 ); - insert( "DarkSeaGreen", 1, 1, 1, 1 ); - insert( "SkyBlue", 2, 1, 1, 1 ); - insert( "Coral", 2, 2, 1, 1 ); + insert( "PaleVioletRed", 0, 0 ); + insert( "DarkSeaGreen", 1, 1 ); + insert( "SkyBlue", 2, 1 ); + insert( "Coral", 2, 2 ); insert( "NavajoWhite", 3, 0, 1, 3 ); } -void MainBox::setup1() +void MainBox::test1() { /* - The Graphics layout adds the extra space to the stretchable + Graphics layout adds the extra space to the stretchables while the other layouts use the extra space for reaching a ratio according to the stretch factor first, That leads to column 0 being too large. - */ setColumns( 1 ); - insert( "PaleVioletRed", 0, 0, 1, 1 ); - insert( "DarkSeaGreen", 1, 1, 1, 1 ); - insert( "SkyBlue", 2, 1, 1, 1 ); - insert( "Coral", 2, 2, 1, 1 ); - insert( "NavajoWhite", 3, 0, 1, 3 ); - - setSizePolicyAt( 0, QskSizePolicy::Expanding, QskSizePolicy::Preferred ); - setSizePolicyAt( 1, QskSizePolicy::Fixed, QskSizePolicy::Preferred ); - setSizePolicyAt( 2, QskSizePolicy::Expanding, QskSizePolicy::Preferred ); - -#if 1 // Quick layouts do not support row/column hints + enableGrid( Quick, false ); + + insert( "PaleVioletRed", 0, 0 ); + insert( "DarkSeaGreen", 1, 1 ); + insert( "SkyBlue", 2, 1 ); + insert( "Coral", 2, 2 ); + + setSizePolicyAt( 0, Qt::Horizontal, QskSizePolicy::Expanding ); + setSizePolicyAt( 1, Qt::Horizontal, QskSizePolicy::Fixed ); + setSizePolicyAt( 2, Qt::Horizontal, QskSizePolicy::Expanding ); + setSizePolicyAt( 3, Qt::Horizontal, QskSizePolicy::Fixed ); + setColumnSizeHint( 1, Qt::MinimumSize, 100 ); -#else - for ( int i = 0; i < 4; i++ ) - setMinimumWidthAt( i, 100 ); -#endif } -void MainBox::setup2() +void MainBox::test2() +{ + setColumns( 1 ); + + insert( "PaleVioletRed", 0, 0 ); + insert( "DarkSeaGreen", 1, 1 ); + insert( "SkyBlue", 2, 1 ); + insert( "Coral", 2, 2 ); + + setSizePolicyAt( 0, Qt::Horizontal, QskSizePolicy::Expanding ); + setSizePolicyAt( 1, Qt::Horizontal, QskSizePolicy::Fixed ); + setSizePolicyAt( 2, Qt::Horizontal, QskSizePolicy::Expanding ); + setSizePolicyAt( 3, Qt::Horizontal, QskSizePolicy::Fixed ); + + setMinimumWidthAt( 1, 100 ); + setMinimumWidthAt( 2, 100 ); +} + +void MainBox::test3() { /* The Graphics layout uses a "magic" formula for how to apply @@ -100,16 +117,19 @@ void MainBox::setup2() larger than twice of columns 0. */ - setColumns( 4 ); + setColumns( 3 ); - insert( "PaleVioletRed", 0, 0, 1, 1 ); - insert( "DarkSeaGreen", 1, 0, 1, 1 ); + // Quick layouts do not support row/column hints + enableGrid( Quick, false ); + + insert( "PaleVioletRed", 0, 0 ); + insert( "DarkSeaGreen", 1, 0 ); setRowStretchFactor( 0, 1 ); setRowStretchFactor( 1, 2 ); } -void MainBox::setup3() +void MainBox::test4() { /* When setting a maximum size together with an alignment the expected result @@ -125,22 +145,111 @@ void MainBox::setup3() */ setColumns( 1 ); - insert( "PaleVioletRed", 0, 0, 1, 1 ); + insert( "PaleVioletRed", 0, 0 ); - setPreferredWidthAt( 0, 10 ); - setPreferredHeightAt( 0, 10 ); - - setMaximumWidthAt( 0, 200 ); - setMaximumHeightAt( 0, 200 ); + setPreferredSizeAt( 0, QSize( 10, 10 ) ); + setMaximumSizeAt( 0, QSize( 200, 200 ) ); setAlignmentAt( 0, Qt::AlignCenter ); } +void MainBox::test5() +{ + /* + QGridLayoutEngine does not work correctly when putting more + than one element into the same cell. For the specific situation + below we have a wrong preferredSize for Quick and Graphic as only the + last element being inserted one cell goes into the calculation. + + The reason behind this limitation is a second list for the elements + organized according to row/column that can only store one + element per cell. Unfortunately the implementation iterates in several + situations over rows/columns using this list. + Iterating over the elements instead avoids this limitation and would + be more efficient as well. + + Actually the row/column organized list is ony useful for faster lookups, + when retrieving an item at a specific cell - beside that it + complicates the code without offering extra benefits. + + The position of the rectangles differs because the systems have + default alignments. + */ + + insert( "PaleVioletRed", 0, 0 ); + insert( "DarkSeaGreen", 0, 0 ); + + setFixedSizeAt( 0, QSize( 100, 100 ) ); + setFixedSizeAt( 1, QSize( 50, 50 ) ); +} + +void MainBox::test6() +{ + /* + QGridLayoutEngine ignores the first column coming from + the multicell element at the bottom + */ + insert( "DarkSeaGreen", 1, 1 ); + insert( "Coral", 2, 2 ); + insert( "NavajoWhite", 3, 0, 1, 3 ); + + setSpacing( 0 ); +} + +void MainBox::test7() +{ + /* + This test is very similar to test6, but here we can see a + difference between Quick and Graphic. The hidden element has + an effect on the layout for Graphic, what is actually wrong, when + setRetainSizeWhenHidden is not set. The preferred width also + includes a wrong contribution from the hidden element. + */ + insert( "PaleVioletRed", 0, 0 ); + insert( "DarkSeaGreen", 1, 1 ); + insert( "Coral", 2, 2 ); + insert( "NavajoWhite", 3, 0, 1, 3 ); + + setVisibleAt( 0, false ); + //setRetainSizeWhenHiddenAt( 0, true ); +} + +void MainBox::test8() +{ + /* + This test creates a situation, where we have more space than + the minimum, but not enough for preferred. For this situation + all layout engines use a different algorithm how to distribute + the extra space. All of them are based on the difference between + preferred and minimum. + + - Skinny simply uses the ratio of the differences + - Widgets seems to do something that works exponatially + ( need to check the code ). + - Graphic/Quick ( QGridLayoutEngine ) levels the impact + of the differences down. + + Hard to say what a user expects to happen. + */ + insert( "PaleVioletRed", 0, 0 ); + insert( "DarkSeaGreen", 1, 1 ); + + for ( int i = 0; i < 2; i++ ) + { + setMinimumSizeAt( i, QSize( 20, 20 ) ); + setPreferredSizeAt( i, ( i + 1 ) * QSize( 2000, 2000 ) ); + } +} + int main( int argc, char** argv ) { QApplication a( argc, argv ); - MainBox box( 0 ); + int testcase = 0; + if ( argc == 2 ) + testcase = atoi( argv[1] ); + + MainBox box( testcase ); box.resize( 600, 600 ); box.show();