more test scenarios

This commit is contained in:
Uwe Rathmann 2019-07-25 18:39:50 +02:00
parent 50d85a524e
commit beb44e4004
13 changed files with 448 additions and 146 deletions

View File

@ -91,7 +91,25 @@ void GridAccessor::setMaximumSizeAt( int index, const QSize& size )
setMaximumHeightAt( index, size.height() ); 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 ) int index, int horizontalPolicy, int verticalPolicy )
{ {
setSizePolicyAt( index, Qt::Horizontal, horizontalPolicy ); setSizePolicyAt( index, Qt::Horizontal, horizontalPolicy );

View File

@ -37,7 +37,7 @@ class GridAccessor
virtual void setSizeHintAt( int index, Qt::Orientation, Qt::SizeHint, int hint ) = 0; virtual void setSizeHintAt( int index, Qt::Orientation, Qt::SizeHint, int hint ) = 0;
virtual void setSizePolicyAt( int index, Qt::Orientation, int policy ) = 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 setMinimumWidthAt( int index, int hint );
void setMinimumHeightAt( int index, int hint ); void setMinimumHeightAt( int index, int hint );
@ -51,8 +51,15 @@ class GridAccessor
void setMaximumHeightAt( int index, int hint ); void setMaximumHeightAt( int index, int hint );
void setMaximumSizeAt( int index, const QSize& ); 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 setAlignmentAt( int index, Qt::Alignment ) = 0;
virtual void setRetainSizeWhenHiddenAt( int index, bool on ) = 0; virtual void setRetainSizeWhenHiddenAt( int index, bool on ) = 0;
virtual void setVisibleAt( int index, bool on ) = 0;
virtual QSize preferredSize() const = 0;
}; };
#endif #endif

View File

@ -69,7 +69,9 @@ GridGraphics::GridGraphics( QWidget* parent )
setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff ); setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
auto grid = new Grid(); auto grid = new Grid();
m_layout = new QGraphicsGridLayout( grid ); m_layout = new QGraphicsGridLayout( grid );
m_layout->setSpacing( 5 );
auto scene = new QGraphicsScene(); auto scene = new QGraphicsScene();
scene->addItem( grid ); 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* ) void GridGraphics::resizeEvent( QResizeEvent* )
{ {
auto sceneRect = contentsRect(); auto sceneRect = contentsRect();

View File

@ -29,6 +29,9 @@ class GridGraphics : public QGraphicsView, public GridAccessor
void setSizePolicyAt( int index, Qt::Orientation, int policy ) override; void setSizePolicyAt( int index, Qt::Orientation, int policy ) override;
void setAlignmentAt( int index, Qt::Alignment ) override; void setAlignmentAt( int index, Qt::Alignment ) override;
void setRetainSizeWhenHiddenAt( int index, bool on ) override; void setRetainSizeWhenHiddenAt( int index, bool on ) override;
void setVisibleAt( int index, bool on ) override;
QSize preferredSize() const override;
protected: protected:
void resizeEvent( QResizeEvent* ) override; void resizeEvent( QResizeEvent* ) override;

View File

@ -17,6 +17,15 @@ static QQuickItem* createQml( const char* qmlCode )
return qobject_cast< QQuickItem* >( component.create() ); 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 ) static QObject* attachedProperties( const QQuickItem* item )
{ {
for ( auto child : item->children() ) for ( auto child : item->children() )
@ -30,15 +39,12 @@ static QObject* attachedProperties( const QQuickItem* item )
static QObject* attachedPropertiesAt( const QQuickItem* grid, int index ) static QObject* attachedPropertiesAt( const QQuickItem* grid, int index )
{ {
const auto children = grid->childItems(); if ( auto item = itemAt( grid, index ) )
return attachedProperties( item );
if ( ( index >= 0 ) && ( index < children.count() ) )
return attachedProperties( children.at( index ) );
return nullptr; return nullptr;
} }
GridQuick::GridQuick( QWidget* parent ) GridQuick::GridQuick( QWidget* parent )
: QQuickWidget( parent ) : QQuickWidget( parent )
{ {
@ -50,6 +56,8 @@ GridQuick::GridQuick( QWidget* parent )
setContent( QUrl(), nullptr, contentItem ); setContent( QUrl(), nullptr, contentItem );
m_grid = contentItem->childItems().first(); m_grid = contentItem->childItems().first();
m_grid->setProperty( "rowSpacing", 5 );
m_grid->setProperty( "columnSpacing", 5 );
} }
GridQuick::~GridQuick() GridQuick::~GridQuick()
@ -127,7 +135,7 @@ void GridQuick::setSizeHintAt( int index, Qt::Orientation orientation,
if ( orientation == Qt::Horizontal ) if ( orientation == Qt::Horizontal )
props->setProperty( "maximumWidth", size ); props->setProperty( "maximumWidth", size );
else else
props->setProperty( "maximumWidth", size ); props->setProperty( "maximumHeight", size );
break; break;
} }
@ -167,12 +175,25 @@ void GridQuick::setRetainSizeWhenHiddenAt( int, bool )
qWarning() << "setRetainSizeWhenHidden is not supported by Quick Layouts."; 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 ) void GridQuick::resizeEvent( QResizeEvent* event )
{ {
QQuickWidget::resizeEvent( event ); QQuickWidget::resizeEvent( event );
const qreal margin = 10.0;
m_grid->setX( margin ); m_grid->setX( margin );
m_grid->setY( margin ); m_grid->setY( margin );
m_grid->setWidth( width() - 2 * margin ); m_grid->setWidth( width() - 2 * margin );

View File

@ -29,6 +29,9 @@ class GridQuick : public QQuickWidget, public GridAccessor
void setSizePolicyAt( int index, Qt::Orientation, int policy ) override; void setSizePolicyAt( int index, Qt::Orientation, int policy ) override;
void setAlignmentAt( int index, Qt::Alignment ) override; void setAlignmentAt( int index, Qt::Alignment ) override;
void setRetainSizeWhenHiddenAt( int index, bool on ) override; void setRetainSizeWhenHiddenAt( int index, bool on ) override;
void setVisibleAt( int index, bool on ) override;
QSize preferredSize() const override;
protected: protected:
void resizeEvent( QResizeEvent* ) override; void resizeEvent( QResizeEvent* ) override;

View File

@ -34,17 +34,6 @@ namespace
private: private:
QByteArray m_colorName; QByteArray m_colorName;
}; };
class Grid : public QskGridBox
{
public:
Grid( QQuickItem* parent = nullptr )
: QskGridBox( parent )
{
setBackgroundColor( Qt::white );
setMargins( 10 );
}
};
} }
GridSkinny::GridSkinny( QWidget* parent ) GridSkinny::GridSkinny( QWidget* parent )
@ -56,6 +45,7 @@ GridSkinny::GridSkinny( QWidget* parent )
m_grid = new QskGridBox(); m_grid = new QskGridBox();
m_grid->setBackgroundColor( Qt::white ); m_grid->setBackgroundColor( Qt::white );
m_grid->setMargins( 10 ); m_grid->setMargins( 10 );
m_grid->setSpacing( 5 );
setContent( QUrl(), nullptr, m_grid ); setContent( QUrl(), nullptr, m_grid );
} }
@ -130,5 +120,15 @@ void GridSkinny::setRetainSizeWhenHiddenAt( int index, bool on )
{ {
if ( auto item = m_grid->itemAtIndex( index ) ) if ( auto item = m_grid->itemAtIndex( index ) )
m_grid->setRetainSizeWhenHidden( item, on ); 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();
} }

View File

@ -27,9 +27,11 @@ class GridSkinny : public QQuickWidget, public GridAccessor
void setSizeHintAt( int index, 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 setSizePolicyAt( int index, Qt::Orientation, int policy ) override;
void setAlignmentAt( int index, Qt::Alignment ) override; void setAlignmentAt( int index, Qt::Alignment ) override;
void setRetainSizeWhenHiddenAt( int index, bool on ) override; void setRetainSizeWhenHiddenAt( int index, bool on ) override;
void setVisibleAt( int index, bool on ) override;
QSize preferredSize() const override;
private: private:
QskGridBox* m_grid; QskGridBox* m_grid;

View File

@ -60,6 +60,8 @@ GridWidgets::GridWidgets( QWidget* parent )
setContentsMargins( QMargins() ); setContentsMargins( QMargins() );
m_layout = new QGridLayout(); m_layout = new QGridLayout();
m_layout->setSpacing( 5 );
setLayout( m_layout ); 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();
}

View File

@ -27,9 +27,11 @@ class GridWidgets : public QWidget, public GridAccessor
void setSizeHintAt( int index, 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 setSizePolicyAt( int index, Qt::Orientation, int policy ) override;
void setAlignmentAt( int index, Qt::Alignment ) override; void setAlignmentAt( int index, Qt::Alignment ) override;
void setRetainSizeWhenHiddenAt( int index, bool on ) override; void setRetainSizeWhenHiddenAt( int index, bool on ) override;
void setVisibleAt( int index, bool on ) override;
QSize preferredSize() const override;
private: private:
QGridLayout* m_layout; QGridLayout* m_layout;

View File

@ -8,6 +8,7 @@
#include "GridSkinny.h" #include "GridSkinny.h"
#include "GridWidgets.h" #include "GridWidgets.h"
#include "GridQuick.h" #include "GridQuick.h"
#include <QLabel>
TestBox::TestBox( QWidget* parent ) TestBox::TestBox( QWidget* parent )
: QWidget( parent ) : QWidget( parent )
@ -17,16 +18,29 @@ TestBox::TestBox( QWidget* parent )
setContentsMargins( QMargins( 10, 10, 10, 10 ) ); setContentsMargins( QMargins( 10, 10, 10, 10 ) );
grids[Skinny] = new GridSkinny( this ); m_grids[Skinny] = new GridSkinny( this );
grids[Widgets] = new GridWidgets( this ); m_labels[Skinny] = new QLabel( "Skinny", this );
grids[Graphics] = new GridGraphics( this );
grids[Quick] = new GridQuick( 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() TestBox::~TestBox()
{ {
} }
void TestBox::enableGrid( int index, bool on )
{
m_grids[index]->setVisible( on );
m_labels[index]->setVisible( on );
}
void TestBox::setColumns( int columnCount ) void TestBox::setColumns( int columnCount )
{ {
m_columnCount = qBound( 1, columnCount, static_cast< int >( GridCount ) ); m_columnCount = qBound( 1, columnCount, static_cast< int >( GridCount ) );
@ -38,79 +52,142 @@ void TestBox::setColumns( int columnCount )
void TestBox::insert( const QByteArray& colorName, void TestBox::insert( const QByteArray& colorName,
int row, int column, int rowSpan, int columnSpan ) int row, int column, int rowSpan, int columnSpan )
{ {
for ( auto grid : grids ) for ( auto grid : m_grids )
{
if ( grid->isVisibleTo( this ) )
{ {
auto accessor = dynamic_cast< GridAccessor* >( grid ); auto accessor = dynamic_cast< GridAccessor* >( grid );
accessor->insert( colorName, accessor->insert( colorName,
row, column, rowSpan, columnSpan ); row, column, rowSpan, columnSpan );
} }
}
} }
void TestBox::setSpacing( Qt::Orientations orientations, int spacing ) void TestBox::setSpacing( Qt::Orientations orientations, int spacing )
{ {
for ( auto grid : grids ) for ( auto grid : m_grids )
{
if ( grid->isVisibleTo( this ) )
{ {
auto accessor = dynamic_cast< GridAccessor* >( grid ); auto accessor = dynamic_cast< GridAccessor* >( grid );
accessor->setSpacing( orientations, spacing ); accessor->setSpacing( orientations, spacing );
} }
}
} }
void TestBox::setSizeHint( void TestBox::setSizeHint(
int pos, Qt::Orientation orientation, Qt::SizeHint which, int hint ) int pos, Qt::Orientation orientation, Qt::SizeHint which, int hint )
{ {
for ( auto grid : grids ) for ( auto grid : m_grids )
{
if ( grid->isVisibleTo( this ) )
{ {
auto accessor = dynamic_cast< GridAccessor* >( grid ); auto accessor = dynamic_cast< GridAccessor* >( grid );
accessor->setSizeHint( pos, orientation, which, hint ); accessor->setSizeHint( pos, orientation, which, hint );
} }
}
} }
void TestBox::setStretchFactor( void TestBox::setStretchFactor(
int pos, Qt::Orientation orientation, int stretch ) int pos, Qt::Orientation orientation, int stretch )
{ {
for ( auto grid : grids ) for ( auto grid : m_grids )
{
if ( grid->isVisibleTo( this ) )
{ {
auto accessor = dynamic_cast< GridAccessor* >( grid ); auto accessor = dynamic_cast< GridAccessor* >( grid );
accessor->setStretchFactor( pos, orientation, stretch ); accessor->setStretchFactor( pos, orientation, stretch );
} }
}
} }
void TestBox::setSizeHintAt( void TestBox::setSizeHintAt(
int index, Qt::Orientation orientation, Qt::SizeHint which, int hint ) int index, Qt::Orientation orientation, Qt::SizeHint which, int hint )
{ {
for ( auto grid : grids ) for ( auto grid : m_grids )
{
if ( grid->isVisibleTo( this ) )
{ {
auto accessor = dynamic_cast< GridAccessor* >( grid ); auto accessor = dynamic_cast< GridAccessor* >( grid );
accessor->setSizeHintAt( index, orientation, which, hint ); accessor->setSizeHintAt( index, orientation, which, hint );
} }
}
} }
void TestBox::setSizePolicyAt( void TestBox::setSizePolicyAt(
int index, Qt::Orientation orientation, int policy ) int index, Qt::Orientation orientation, int policy )
{ {
for ( auto grid : grids ) for ( auto grid : m_grids )
{
if ( grid->isVisibleTo( this ) )
{ {
auto accessor = dynamic_cast< GridAccessor* >( grid ); auto accessor = dynamic_cast< GridAccessor* >( grid );
accessor->setSizePolicyAt( index, orientation, policy ); accessor->setSizePolicyAt( index, orientation, policy );
} }
}
} }
void TestBox::setAlignmentAt( int index, Qt::Alignment alignment ) void TestBox::setAlignmentAt( int index, Qt::Alignment alignment )
{ {
for ( auto grid : grids ) for ( auto grid : m_grids )
{
if ( grid->isVisibleTo( this ) )
{ {
auto accessor = dynamic_cast< GridAccessor* >( grid ); auto accessor = dynamic_cast< GridAccessor* >( grid );
accessor->setAlignmentAt( index, alignment ); accessor->setAlignmentAt( index, alignment );
} }
}
} }
void TestBox::setRetainSizeWhenHiddenAt( int index, bool on ) void TestBox::setRetainSizeWhenHiddenAt( int index, bool on )
{ {
for ( auto grid : grids ) for ( auto grid : m_grids )
{
if ( grid->isVisibleTo( this ) )
{ {
auto accessor = dynamic_cast< GridAccessor* >( grid ); auto accessor = dynamic_cast< GridAccessor* >( grid );
accessor->setRetainSizeWhenHiddenAt( index, on ); 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* ) void TestBox::resizeEvent( QResizeEvent* )
@ -127,13 +204,21 @@ void TestBox::layoutGrids()
const auto r = contentsRect(); const auto r = contentsRect();
int rowCount = GridCount / m_columnCount; int gridCount = 0;
if ( rowCount * m_columnCount < GridCount ) 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++; rowCount++;
const int spacing = 5; 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; const int height = ( r.height() - ( rowCount - 1 ) * spacing ) / rowCount;
int row = 0; int row = 0;
@ -141,12 +226,22 @@ void TestBox::layoutGrids()
for ( int i = 0; i < GridCount; i++ ) for ( int i = 0; i < GridCount; i++ )
{ {
if ( !m_grids[i]->isVisibleTo( this ) )
continue;
const int x = r.left() + col * ( spacing + width ); const int x = r.left() + col * ( spacing + width );
const int y = r.top() + row * ( spacing + height ); 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; col = 0;
row++; row++;

View File

@ -9,34 +9,11 @@
#include "GridAccessor.h" #include "GridAccessor.h"
#include <QWidget> #include <QWidget>
class QLabel;
class TestBox : public QWidget, public GridAccessor class TestBox : public QWidget, public GridAccessor
{ {
public: 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 enum
{ {
Skinny, Skinny,
@ -47,7 +24,39 @@ class TestBox : public QWidget, public GridAccessor
GridCount 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; int m_columnCount = 2;
}; };

View File

@ -4,93 +4,110 @@
*****************************************************************************/ *****************************************************************************/
#include "TestBox.h" #include "TestBox.h"
#include <QApplication>
#include <QskSizePolicy.h> #include <QskSizePolicy.h>
#include <QApplication>
#include <QDebug>
class MainBox : public TestBox class MainBox : public TestBox
{ {
public: public:
MainBox( int id ) MainBox( int id )
{ {
switch( id ) using Testcase = void (MainBox::*)();
const Testcase tests[] =
{ {
case 0: &MainBox::test0, &MainBox::test1, &MainBox::test2,
setup0(); &MainBox::test3, &MainBox::test4, &MainBox::test5,
break; &MainBox::test6, &MainBox::test7, &MainBox::test8
};
case 1: const int count = static_cast<int>( sizeof( tests ) / sizeof( tests[0] ) );
setup1();
break;
case 2: if ( id < 0 || id >= count )
setup2(); id = 0;
break;
case 3: ( this->*tests[id] )();
setup3();
break;
} }
void insert( const char* colorName, int row, int column )
{
insert( colorName, row, column, 1, 1 );
} }
using TestBox::insert;
private: private:
void setup0(); void test0();
void setup1(); void test1();
void setup2(); void test2();
void setup3(); void test3();
void test4();
void setSizePolicyAt( int index, void test5();
QskSizePolicy::Policy horizontalPolicy, void test6();
QskSizePolicy::Policy verticalPolicy ) void test7();
{ void test8();
TestBox::setSizePolicyAt( index, Qt::Horizontal, horizontalPolicy );
TestBox::setSizePolicyAt( index, Qt::Vertical, verticalPolicy );
}
}; };
void MainBox::setup0() void MainBox::test0()
{ {
// something, that works with all layouts // something, that works with all layouts
insert( "PaleVioletRed", 0, 0, 1, 1 ); insert( "PaleVioletRed", 0, 0 );
insert( "DarkSeaGreen", 1, 1, 1, 1 ); insert( "DarkSeaGreen", 1, 1 );
insert( "SkyBlue", 2, 1, 1, 1 ); insert( "SkyBlue", 2, 1 );
insert( "Coral", 2, 2, 1, 1 ); insert( "Coral", 2, 2 );
insert( "NavajoWhite", 3, 0, 1, 3 ); 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 while the other layouts use the extra space for reaching
a ratio according to the stretch factor first, a ratio according to the stretch factor first,
That leads to column 0 being too large. That leads to column 0 being too large.
*/ */
setColumns( 1 ); 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 // 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 ); 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 The Graphics layout uses a "magic" formula for how to apply
@ -100,16 +117,19 @@ void MainBox::setup2()
larger than twice of columns 0. larger than twice of columns 0.
*/ */
setColumns( 4 ); setColumns( 3 );
insert( "PaleVioletRed", 0, 0, 1, 1 ); // Quick layouts do not support row/column hints
insert( "DarkSeaGreen", 1, 0, 1, 1 ); enableGrid( Quick, false );
insert( "PaleVioletRed", 0, 0 );
insert( "DarkSeaGreen", 1, 0 );
setRowStretchFactor( 0, 1 ); setRowStretchFactor( 0, 1 );
setRowStretchFactor( 1, 2 ); setRowStretchFactor( 1, 2 );
} }
void MainBox::setup3() void MainBox::test4()
{ {
/* /*
When setting a maximum size together with an alignment the expected result When setting a maximum size together with an alignment the expected result
@ -125,22 +145,111 @@ void MainBox::setup3()
*/ */
setColumns( 1 ); setColumns( 1 );
insert( "PaleVioletRed", 0, 0, 1, 1 ); insert( "PaleVioletRed", 0, 0 );
setPreferredWidthAt( 0, 10 ); setPreferredSizeAt( 0, QSize( 10, 10 ) );
setPreferredHeightAt( 0, 10 ); setMaximumSizeAt( 0, QSize( 200, 200 ) );
setMaximumWidthAt( 0, 200 );
setMaximumHeightAt( 0, 200 );
setAlignmentAt( 0, Qt::AlignCenter ); 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 ) int main( int argc, char** argv )
{ {
QApplication a( argc, 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.resize( 600, 600 );
box.show(); box.show();