From 5432d5f5b01feb62e8c565671a97050d04831a72 Mon Sep 17 00:00:00 2001 From: Uwe Rathmann Date: Mon, 22 Jul 2019 17:21:33 +0200 Subject: [PATCH] grids test application added, to compare the diefferent layout implementations --- playground/grids/GridAccessor.cpp | 79 ++++++++++++ playground/grids/GridAccessor.h | 53 ++++++++ playground/grids/GridGraphics.cpp | 204 ++++++++++++++++++++++++++++++ playground/grids/GridGraphics.h | 40 ++++++ playground/grids/GridSkinny.cpp | 126 ++++++++++++++++++ playground/grids/GridSkinny.h | 38 ++++++ playground/grids/GridWidgets.cpp | 197 +++++++++++++++++++++++++++++ playground/grids/GridWidgets.h | 38 ++++++ playground/grids/TestBox.cpp | 120 ++++++++++++++++++ playground/grids/TestBox.h | 48 +++++++ playground/grids/grids.pro | 25 ++++ playground/grids/main.cpp | 45 +++++++ playground/playground.pro | 6 + 13 files changed, 1019 insertions(+) create mode 100644 playground/grids/GridAccessor.cpp create mode 100644 playground/grids/GridAccessor.h create mode 100644 playground/grids/GridGraphics.cpp create mode 100644 playground/grids/GridGraphics.h create mode 100644 playground/grids/GridSkinny.cpp create mode 100644 playground/grids/GridSkinny.h create mode 100644 playground/grids/GridWidgets.cpp create mode 100644 playground/grids/GridWidgets.h create mode 100644 playground/grids/TestBox.cpp create mode 100644 playground/grids/TestBox.h create mode 100644 playground/grids/grids.pro create mode 100644 playground/grids/main.cpp diff --git a/playground/grids/GridAccessor.cpp b/playground/grids/GridAccessor.cpp new file mode 100644 index 00000000..36d11cad --- /dev/null +++ b/playground/grids/GridAccessor.cpp @@ -0,0 +1,79 @@ +/****************************************************************************** + * QSkinny - Copyright (C) 2016 Uwe Rathmann + * This file may be used under the terms of the 3-clause BSD License + *****************************************************************************/ + +#include "GridAccessor.h" +#include + +void GridAccessor::setSpacing( int spacing ) +{ + setSpacing( Qt::Vertical | Qt::Horizontal, spacing ); +} + +void GridAccessor::setRowFixedHeight( int row, qreal height ) +{ + setRowSizeHint( row, Qt::MinimumSize, height ); + setRowSizeHint( row, Qt::MaximumSize, height ); +} + +void GridAccessor::setColumnFixedWidth( int column, qreal width ) +{ + setColumnSizeHint( column, Qt::MinimumSize, width ); + setColumnSizeHint( column, Qt::MaximumSize, width ); +} + +void GridAccessor::setMinimumWidth( int index, int hint ) +{ + setSizeHint( index, Qt::Horizontal, Qt::MinimumSize, hint ); +} + +void GridAccessor::setMinimumHeight( int index, int hint ) +{ + setSizeHint( index, Qt::Vertical, Qt::MinimumSize, hint ); +} + +void GridAccessor::setMinimumSize( int index, const QSize& size ) +{ + setMinimumWidth( index, size.width() ); + setMinimumHeight( index, size.height() ); +} + +void GridAccessor::setPreferredWidth( int index, int hint ) +{ + setSizeHint( index, Qt::Horizontal, Qt::PreferredSize, hint ); +} + +void GridAccessor::setPreferredHeight( int index, int hint ) +{ + setSizeHint( index, Qt::Vertical, Qt::PreferredSize, hint ); +} + +void GridAccessor::setPreferredSize( int index, const QSize& size ) +{ + setPreferredWidth( index, size.width() ); + setPreferredHeight( index, size.height() ); +} + +void GridAccessor::setMaximumWidth( int index, int hint ) +{ + setSizeHint( index, Qt::Horizontal, Qt::MaximumSize, hint ); +} + +void GridAccessor::setMaximumHeight( int index, int hint ) +{ + setSizeHint( index, Qt::Vertical, Qt::MaximumSize, hint ); +} + +void GridAccessor::setMaximumSize( int index, const QSize& size ) +{ + setMaximumWidth( index, size.width() ); + setMaximumHeight( index, size.height() ); +} + +void GridAccessor::setSizePolicy( + int index, int horizontalPolicy, int verticalPolicy ) +{ + setSizePolicy( index, Qt::Horizontal, horizontalPolicy ); + setSizePolicy( index, Qt::Vertical, verticalPolicy ); +} diff --git a/playground/grids/GridAccessor.h b/playground/grids/GridAccessor.h new file mode 100644 index 00000000..f27677d0 --- /dev/null +++ b/playground/grids/GridAccessor.h @@ -0,0 +1,53 @@ +/****************************************************************************** + * QSkinny - Copyright (C) 2016 Uwe Rathmann + * This file may be used under the terms of the 3-clause BSD License + *****************************************************************************/ + +#ifndef GRID_ACCESSOR_H +#define GRID_ACCESSOR_H + +#include + +class QColor; +class QSize; +class QByteArray; + +class GridAccessor +{ + public: + virtual ~GridAccessor() = default; + + virtual void insert( const QByteArray& colorName, + int row, int column, int rowSpan, int columnSpan ) = 0; + + void setSpacing( int spacing ); + virtual void setSpacing( Qt::Orientations, int spacing ) = 0; + + virtual void setRowSizeHint( int row, Qt::SizeHint, int height ) = 0; + virtual void setColumnSizeHint( int column, Qt::SizeHint, int width ) = 0; + + void setRowFixedHeight( int row, qreal height ); + void setColumnFixedWidth( int column, qreal width ); + + virtual void setSizeHint( int index, Qt::Orientation, Qt::SizeHint, int hint ) = 0; + virtual void setSizePolicy(int index, Qt::Orientation, int policy ) = 0; + + void setSizePolicy( int index, int horizontalPolicy, int verticalPolicy ); + + void setMinimumWidth( int index, int hint ); + void setMinimumHeight( int index, int hint ); + void setMinimumSize( int index, const QSize& ); + + void setPreferredWidth( int index, int hint ); + void setPreferredHeight( int index, int hint ); + void setPreferredSize( int index, const QSize& ); + + void setMaximumWidth( int index, int hint ); + void setMaximumHeight( int index, int hint ); + void setMaximumSize( int index, const QSize& ); + + virtual void setAlignment( int index, Qt::Alignment ) = 0; + virtual void setRetainSizeWhenHidden( int, bool on ) = 0; +}; + +#endif diff --git a/playground/grids/GridGraphics.cpp b/playground/grids/GridGraphics.cpp new file mode 100644 index 00000000..cd4f3f36 --- /dev/null +++ b/playground/grids/GridGraphics.cpp @@ -0,0 +1,204 @@ +/****************************************************************************** + * QSkinny - Copyright (C) 2016 Uwe Rathmann + * This file may be used under the terms of the 3-clause BSD License + *****************************************************************************/ + +#include "GridGraphics.h" + +#include +#include +#include +#include + +namespace +{ + class Rectangle : public QGraphicsWidget + { + public: + Rectangle( const QByteArray& colorName ) + : m_colorName( colorName ) + { + setObjectName( colorName ); + + setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred ); + setPreferredSize( 50, 50 ); + + setPalette( QColor( colorName.constData() ) ); + setAutoFillBackground( true ); + } + + void resizeEvent( QGraphicsSceneResizeEvent* event ) override + { + QGraphicsWidget::resizeEvent( event ); + //qDebug() << m_color << size(); + } + + private: + const QByteArray m_colorName; + }; +} + +GridGraphics::GridGraphics( QWidget* parent ) + : QGraphicsView( parent ) +{ + setFrameStyle( QFrame::NoFrame ); + setContentsMargins( QMargins() ); + + auto grid = new QGraphicsWidget(); + m_layout = new QGraphicsGridLayout( grid ); + + auto scene = new QGraphicsScene(); + scene->addItem( grid ); + + setScene( scene ); +} + +GridGraphics::~GridGraphics() +{ +} + +void GridGraphics::insert( const QByteArray& colorName, + int row, int column, int rowSpan, int columnSpan ) +{ + m_layout->addItem( new Rectangle( colorName ), + row, column, rowSpan, columnSpan ); +} + +void GridGraphics::setSpacing( Qt::Orientations orientations, int spacing ) +{ + if ( orientations & Qt::Horizontal ) + m_layout->setHorizontalSpacing( spacing ); + + if ( orientations & Qt::Vertical ) + m_layout->setVerticalSpacing( spacing ); +} + +void GridGraphics::setRowSizeHint( int row, Qt::SizeHint which, int height ) +{ + switch( static_cast< int >( which ) ) + { + case Qt::MinimumSize: + { + m_layout->setRowMinimumHeight( row, height ); + break; + } + case Qt::PreferredSize: + { + m_layout->setRowPreferredHeight( row, height ); + break; + } + case Qt::MaximumSize: + { + m_layout->setRowMaximumHeight( row, height ); + break; + } + } +} + +void GridGraphics::setColumnSizeHint( int column, Qt::SizeHint which, int width ) +{ + switch( static_cast< int >( which ) ) + { + case Qt::MinimumSize: + { + m_layout->setColumnMinimumWidth( column, width ); + break; + } + case Qt::PreferredSize: + { + m_layout->setColumnPreferredWidth( column, width ); + break; + } + case Qt::MaximumSize: + { + m_layout->setColumnMaximumWidth( column, width ); + break; + } + } + +} + +void GridGraphics::setSizeHint( + int index, Qt::Orientation orientation, Qt::SizeHint which, int hint ) +{ + if ( auto layoutItem = m_layout->itemAt( index ) ) + { + switch( static_cast< int >( which ) ) + { + case Qt::MinimumSize: + { + if ( orientation == Qt::Horizontal ) + layoutItem->setMinimumWidth( hint ); + else + layoutItem->setMinimumHeight( hint ); + break; + } + case Qt::PreferredSize: + { + if ( orientation == Qt::Horizontal ) + layoutItem->setPreferredWidth( hint ); + else + layoutItem->setPreferredHeight( hint ); + + break; + } + case Qt::MaximumSize: + { + if ( orientation == Qt::Horizontal ) + layoutItem->setMaximumWidth( hint ); + else + layoutItem->setMaximumHeight( hint ); + + break; + } + } + + } +} + +void GridGraphics::setSizePolicy( int index, Qt::Orientation orientation, int policy ) +{ + if ( auto layoutItem = m_layout->itemAt( index ) ) + { + const auto qPolicy = static_cast< QSizePolicy::Policy >( policy & 0xF ); + + const bool isConstrained = policy & ( 1 << 4 ); + + auto sizePolicy = layoutItem->sizePolicy(); + + if ( orientation == Qt::Horizontal ) + { + sizePolicy.setHorizontalPolicy( qPolicy ); + sizePolicy.setWidthForHeight( isConstrained ); + } + else + { + sizePolicy.setVerticalPolicy( qPolicy ); + sizePolicy.setHeightForWidth( isConstrained ); + } + + layoutItem->setSizePolicy( sizePolicy ); + } +} + +void GridGraphics::setAlignment( int index, Qt::Alignment alignment ) +{ + if ( auto layoutItem = m_layout->itemAt( index ) ) + m_layout->setAlignment( layoutItem, alignment ); +} + +void GridGraphics::setRetainSizeWhenHidden( int index, bool on ) +{ + if ( auto layoutItem = m_layout->itemAt( index ) ) + { + auto sizePolicy = layoutItem->sizePolicy(); + sizePolicy.setRetainSizeWhenHidden( on ); + layoutItem->setSizePolicy( sizePolicy ); + } +} + +void GridGraphics::resizeEvent( QResizeEvent* ) +{ + auto grid = static_cast< QGraphicsWidget* >( scene()->items().last() ); + grid->resize( contentsRect().size() ); +} diff --git a/playground/grids/GridGraphics.h b/playground/grids/GridGraphics.h new file mode 100644 index 00000000..b34d7b5e --- /dev/null +++ b/playground/grids/GridGraphics.h @@ -0,0 +1,40 @@ +/****************************************************************************** + * QSkinny - Copyright (C) 2016 Uwe Rathmann + * This file may be used under the terms of the 3-clause BSD License + *****************************************************************************/ + +#ifndef GRID_GRAPHICS_H +#define GRID_GRAPHICS_H + +#include "GridAccessor.h" +#include + +class QGraphicsGridLayout; + +class GridGraphics : public QGraphicsView, public GridAccessor +{ + public: + GridGraphics( QWidget* parent = nullptr ); + ~GridGraphics() override; + + void insert( const QByteArray& colorName, + int row, int column, int rowSpan, int columnSpan ) override; + + void setSpacing( Qt::Orientations, int spacing ) override; + + void setRowSizeHint( int row, Qt::SizeHint, int height ) override; + void setColumnSizeHint( int column, Qt::SizeHint, int width ) override; + + void setSizeHint( int index, Qt::Orientation, Qt::SizeHint, int hint ) override; + void setSizePolicy( int index, Qt::Orientation, int policy ) override; + void setAlignment( int index, Qt::Alignment ) override; + void setRetainSizeWhenHidden( int index, bool on ) override; + + protected: + void resizeEvent( QResizeEvent* ) override; + + private: + QGraphicsGridLayout* m_layout; +}; + +#endif diff --git a/playground/grids/GridSkinny.cpp b/playground/grids/GridSkinny.cpp new file mode 100644 index 00000000..66d0d0dc --- /dev/null +++ b/playground/grids/GridSkinny.cpp @@ -0,0 +1,126 @@ +/****************************************************************************** + * QSkinny - Copyright (C) 2016 Uwe Rathmann + * This file may be used under the terms of the 3-clause BSD License + *****************************************************************************/ + +#include "GridSkinny.h" +#include +#include +#include + +namespace +{ + class Rectangle : public QskControl + { + public: + Rectangle( const QByteArray& colorName ) + : m_colorName( colorName ) + { + setObjectName( colorName ); + + initSizePolicy( QskSizePolicy::Preferred, QskSizePolicy::Preferred ); + setPreferredSize( 50, 50 ); + + setBackgroundColor( colorName.constData() ); + } + + protected: + void geometryChangeEvent( QskGeometryChangeEvent* event ) override + { + QskControl::geometryChangeEvent( event ); + //qDebug() << colorName() << size(); + } + + private: + QByteArray m_colorName; + }; + + class Grid : public QskGridBox + { + public: + Grid( QQuickItem* parent = nullptr ) + : QskGridBox( parent ) + { + setBackgroundColor( Qt::white ); + setMargins( 10 ); + } + }; +} + +GridSkinny::GridSkinny( QWidget* parent ) + : QQuickWidget( parent ) +{ + setContentsMargins( QMargins() ); + setResizeMode( QQuickWidget::SizeRootObjectToView ); + + m_grid = new QskGridBox(); + m_grid->setBackgroundColor( Qt::white ); + m_grid->setMargins( 10 ); + + setContent( QUrl(), nullptr, m_grid ); +} + +GridSkinny::~GridSkinny() +{ +} + +void GridSkinny::insert( const QByteArray& colorName, + int row, int column, int rowSpan, int columnSpan ) +{ + m_grid->addItem( new Rectangle( colorName ), + row, column, rowSpan, columnSpan ); +} + +void GridSkinny::setSpacing( Qt::Orientations orientations, int spacing ) +{ + m_grid->setSpacing( orientations, spacing ); +} + +void GridSkinny::setRowSizeHint( int row, Qt::SizeHint which, int height ) +{ + m_grid->setRowSizeHint( row, which, height ); +} + +void GridSkinny::setColumnSizeHint( int column, Qt::SizeHint which, int width ) +{ + m_grid->setColumnSizeHint( column, which, width ); +} + +void GridSkinny::setSizeHint( int index, Qt::Orientation orientation, + Qt::SizeHint which, int hint ) +{ + if ( auto control = qobject_cast< QskControl* >( m_grid->itemAtIndex( index ) ) ) + { + auto size = control->explicitSizeHint( which ); + + if ( orientation == Qt::Horizontal ) + size.setWidth( hint ); + else + size.setHeight( hint ); + + control->setExplicitSizeHint( which, size ); + } +} + +void GridSkinny::setSizePolicy( + int index, Qt::Orientation orientation, int policy ) +{ + if ( auto control = qobject_cast< QskControl* >( m_grid->itemAtIndex( index ) ) ) + { + control->setSizePolicy( orientation, + static_cast< QskSizePolicy::Policy >( policy ) ); + } +} + +void GridSkinny::setAlignment( int index, Qt::Alignment alignment ) +{ + if ( auto item = m_grid->itemAtIndex( index ) ) + m_grid->setAlignment( item, alignment ); +} + +void GridSkinny::setRetainSizeWhenHidden( int index, bool on ) +{ + if ( auto item = m_grid->itemAtIndex( index ) ) + m_grid->setRetainSizeWhenHidden( item, on ); + +} diff --git a/playground/grids/GridSkinny.h b/playground/grids/GridSkinny.h new file mode 100644 index 00000000..1d3d24dc --- /dev/null +++ b/playground/grids/GridSkinny.h @@ -0,0 +1,38 @@ +/****************************************************************************** + * QSkinny - Copyright (C) 2016 Uwe Rathmann + * This file may be used under the terms of the 3-clause BSD License + *****************************************************************************/ + +#ifndef GRID_SKINNY_H +#define GRID_SKINNY_H + +#include "GridAccessor.h" +#include + +class QskGridBox; + +class GridSkinny : public QQuickWidget, public GridAccessor +{ + public: + GridSkinny( QWidget* parent = nullptr ); + ~GridSkinny() override; + + void insert( const QByteArray& colorName, + int row, int column, int rowSpan, int columnSpan ) override; + + void setSpacing( Qt::Orientations, int spacing ) override; + + void setRowSizeHint( int row, Qt::SizeHint, int height ) override; + void setColumnSizeHint( int column, Qt::SizeHint, int width ) override; + + void setSizeHint( int index, Qt::Orientation, Qt::SizeHint, int hint ) override; + void setSizePolicy( int index, Qt::Orientation, int policy ) override; + + void setAlignment( int index, Qt::Alignment ) override; + void setRetainSizeWhenHidden( int, bool on ) override; + + private: + QskGridBox* m_grid; +}; + +#endif diff --git a/playground/grids/GridWidgets.cpp b/playground/grids/GridWidgets.cpp new file mode 100644 index 00000000..6421a68d --- /dev/null +++ b/playground/grids/GridWidgets.cpp @@ -0,0 +1,197 @@ +/****************************************************************************** + * QSkinny - Copyright (C) 2016 Uwe Rathmann + * This file may be used under the terms of the 3-clause BSD License + *****************************************************************************/ + +#include "GridWidgets.h" +#include +#include + +namespace +{ + class Rectangle : public QWidget + { + public: + Rectangle( const QByteArray& colorName ) + : m_colorName( colorName ) + { + setObjectName( colorName ); + + setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred ); + + setPalette( QColor( colorName.constData() ) ); + setAutoFillBackground( true ); + } + + void setPreferredWidth( qreal width ) + { + m_preferredSize.setWidth( width ); + } + + void setPreferredHeight( qreal height ) + { + m_preferredSize.setHeight( height ); + } + + QSize sizeHint() const override + { + return m_preferredSize; + } + + protected: + void resizeEvent( QResizeEvent* event ) override + { + QWidget::resizeEvent( event ); + //qDebug() << m_color << size(); + } + + private: + const QByteArray m_colorName; + QSize m_preferredSize = { 50, 50 }; + }; +} + +GridWidgets::GridWidgets( QWidget* parent ) + : QWidget( parent ) +{ + setPalette( Qt::white ); + setAutoFillBackground( true ); + + setContentsMargins( QMargins() ); + + m_layout = new QGridLayout(); + setLayout( m_layout ); +} + +GridWidgets::~GridWidgets() +{ +} + +void GridWidgets::insert( const QByteArray& colorName, + int row, int column, int rowSpan, int columnSpan ) +{ + m_layout->addWidget( new Rectangle( colorName ), + row, column, rowSpan, columnSpan ); +} + +void GridWidgets::setSpacing( Qt::Orientations orientations, int spacing ) +{ + if ( orientations & Qt::Horizontal ) + m_layout->setHorizontalSpacing( spacing ); + + if ( orientations & Qt::Vertical ) + m_layout->setVerticalSpacing( spacing ); +} + +void GridWidgets::setRowSizeHint( int row, Qt::SizeHint which, int height ) +{ + if ( which != Qt::MinimumSize ) + { + qWarning() << "setRowSizeHint" << which << "is not supported by QGridLayout."; + return; + } + + m_layout->setRowMinimumHeight( row, height ); +} + +void GridWidgets::setColumnSizeHint( int column, Qt::SizeHint which, int width ) +{ + if ( which != Qt::MinimumSize ) + { + qWarning() << "setColumnSizeHint" << which << "is not supported by QGridLayout."; + return; + } + + m_layout->setColumnMinimumWidth( column, width ); +} + +void GridWidgets::setSizeHint( + int index, Qt::Orientation orientation, Qt::SizeHint which, int hint ) +{ + Rectangle* rectangle = nullptr; + + if ( auto layoutItem = m_layout->itemAt( index ) ) + rectangle = static_cast< Rectangle* >( layoutItem->widget() ); + + if ( rectangle == nullptr ) + return; + + switch( static_cast< int >( which ) ) + { + case Qt::MinimumSize: + { + if ( orientation == Qt::Horizontal ) + rectangle->setMinimumWidth( hint ); + else + rectangle->setMinimumHeight( hint ); + + break; + } + case Qt::PreferredSize: + { + if ( orientation == Qt::Horizontal ) + rectangle->setPreferredWidth( hint ); + else + rectangle->setPreferredHeight( hint ); + + break; + } + case Qt::MaximumSize: + { + if ( orientation == Qt::Horizontal ) + rectangle->setMaximumWidth( hint ); + else + rectangle->setMaximumHeight( hint ); + + break; + } + } +} + +void GridWidgets::setSizePolicy( int index, Qt::Orientation orientation, int policy ) +{ + QWidget* widget = nullptr; + + if ( auto layoutItem = m_layout->itemAt( index ) ) + widget = layoutItem->widget(); + + if ( widget == nullptr ) + return; + + const auto qPolicy = static_cast< QSizePolicy::Policy >( policy & 0xF ); + const bool isConstrained = policy & ( 1 << 4 ); + + auto sizePolicy = widget->sizePolicy(); + + if ( orientation == Qt::Horizontal ) + { + sizePolicy.setHorizontalPolicy( qPolicy ); + sizePolicy.setWidthForHeight( isConstrained ); + } + else + { + sizePolicy.setVerticalPolicy( qPolicy ); + sizePolicy.setHeightForWidth( isConstrained ); + } + + widget->setSizePolicy( sizePolicy ); +} + +void GridWidgets::setAlignment( int index, Qt::Alignment alignment ) +{ + if ( auto layoutItem = m_layout->itemAt( index ) ) + layoutItem->setAlignment( alignment ); +} + +void GridWidgets::setRetainSizeWhenHidden( int index, bool on ) +{ + if ( auto layoutItem = m_layout->itemAt( index ) ) + { + if ( auto widget = layoutItem->widget() ) + { + auto sizePolicy = widget->sizePolicy(); + sizePolicy.setRetainSizeWhenHidden( on ); + widget->setSizePolicy( sizePolicy ); + } + } +} diff --git a/playground/grids/GridWidgets.h b/playground/grids/GridWidgets.h new file mode 100644 index 00000000..ef80683a --- /dev/null +++ b/playground/grids/GridWidgets.h @@ -0,0 +1,38 @@ +/****************************************************************************** + * QSkinny - Copyright (C) 2016 Uwe Rathmann + * This file may be used under the terms of the 3-clause BSD License + *****************************************************************************/ + +#ifndef GRID_WIDGETS_H +#define GRID_WIDGETS_H + +#include "GridAccessor.h" +#include + +class QGridLayout; + +class GridWidgets : public QWidget, public GridAccessor +{ + public: + GridWidgets( QWidget* parent = nullptr ); + ~GridWidgets() override; + + void insert( const QByteArray& colorName, + int row, int column, int rowSpan, int columnSpan ) override; + + void setSpacing( Qt::Orientations, int spacing ) override; + + void setRowSizeHint( int row, Qt::SizeHint, int height ) override; + void setColumnSizeHint( int column, Qt::SizeHint, int width ) override; + + void setSizeHint( int index, Qt::Orientation, Qt::SizeHint, int hint ) override; + void setSizePolicy( int index, Qt::Orientation, int policy ) override; + + void setAlignment( int index, Qt::Alignment ) override; + void setRetainSizeWhenHidden( int, bool on ) override; + + private: + QGridLayout* m_layout; +}; + +#endif diff --git a/playground/grids/TestBox.cpp b/playground/grids/TestBox.cpp new file mode 100644 index 00000000..f9e91141 --- /dev/null +++ b/playground/grids/TestBox.cpp @@ -0,0 +1,120 @@ +/****************************************************************************** + * QSkinny - Copyright (C) 2016 Uwe Rathmann + * This file may be used under the terms of the 3-clause BSD License + *****************************************************************************/ + +#include "TestBox.h" +#include "GridGraphics.h" +#include "GridSkinny.h" +#include "GridWidgets.h" + +TestBox::TestBox( QWidget* parent ) + : QWidget( parent ) +{ + setPalette( QColor( "Lavender" ) ); + setAutoFillBackground( true ); + + setContentsMargins( QMargins( 10, 10, 10, 10 ) ); + + grids[Skinny] = new GridSkinny( this ); + grids[Widgets] = new GridWidgets( this ); + grids[Graphics] = new GridGraphics( this ); +} + +TestBox::~TestBox() +{ +} + +void TestBox::insert( const QByteArray& colorName, + int row, int column, int rowSpan, int columnSpan ) +{ + for ( auto grid : grids ) + { + 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 ) + { + auto accessor = dynamic_cast< GridAccessor* >( grid ); + accessor->setSpacing( orientations, spacing ); + } +} + +void TestBox::setRowSizeHint( int row, Qt::SizeHint which, int height ) +{ + for ( auto grid : grids ) + { + auto accessor = dynamic_cast< GridAccessor* >( grid ); + accessor->setRowSizeHint( row, which, height ); + } +} + +void TestBox::setColumnSizeHint( int column, Qt::SizeHint which, int width ) +{ + for ( auto grid : grids ) + { + auto accessor = dynamic_cast< GridAccessor* >( grid ); + accessor->setColumnSizeHint( column, which, width ); + } +} + +void TestBox::setSizeHint( + int index, Qt::Orientation orientation, Qt::SizeHint which, int hint ) +{ + for ( auto grid : grids ) + { + auto accessor = dynamic_cast< GridAccessor* >( grid ); + accessor->setSizeHint( index, orientation, which, hint ); + } +} + +void TestBox::setSizePolicy( + int index, Qt::Orientation orientation, int policy ) +{ + for ( auto grid : grids ) + { + auto accessor = dynamic_cast< GridAccessor* >( grid ); + accessor->setSizePolicy( index, orientation, policy ); + } +} + +void TestBox::setAlignment( int index, Qt::Alignment alignment ) +{ + for ( auto grid : grids ) + { + auto accessor = dynamic_cast< GridAccessor* >( grid ); + accessor->setAlignment( index, alignment ); + } +} + +void TestBox::setRetainSizeWhenHidden( int index, bool on ) +{ + for ( auto grid : grids ) + { + auto accessor = dynamic_cast< GridAccessor* >( grid ); + accessor->setRetainSizeWhenHidden( index, on ); + } +} + +void TestBox::resizeEvent( QResizeEvent* ) +{ + /* + Not using layouts here to avoid confusion + while debugging. + */ + + const auto r = contentsRect(); + const auto sz = 0.5 * r.size() - QSize( 5, 5 ); + + grids[ 0 ]->move( r.left(), r.top() ); + grids[ 1 ]->move( r.right() - sz.width(), r.top() ); + grids[ 2 ]->move( r.left(), r.bottom() - sz.height() ); + + for ( auto grid : grids ) + grid->resize( sz ); +} diff --git a/playground/grids/TestBox.h b/playground/grids/TestBox.h new file mode 100644 index 00000000..a917c3e0 --- /dev/null +++ b/playground/grids/TestBox.h @@ -0,0 +1,48 @@ +/****************************************************************************** + * QSkinny - Copyright (C) 2016 Uwe Rathmann + * This file may be used under the terms of the 3-clause BSD License + *****************************************************************************/ + +#ifndef TEST_BOX_H +#define TEST_BOX_H + +#include "GridAccessor.h" +#include + +class TestBox : public QWidget, public GridAccessor +{ + public: + TestBox( QWidget* parent = nullptr ); + ~TestBox() override; + + void insert( const QByteArray& colorName, + int row, int column, int rowSpan, int columnSpan ) override; + + void setSpacing( Qt::Orientations, int spacing ) override; + + void setRowSizeHint( int row, Qt::SizeHint, int height ) override; + void setColumnSizeHint( int column, Qt::SizeHint, int width ) override; + + void setSizeHint( int index, Qt::Orientation, Qt::SizeHint, int hint ) override; + void setSizePolicy( int index, Qt::Orientation, int policy ) override; + + void setAlignment( int index, Qt::Alignment ) override; + void setRetainSizeWhenHidden( int, bool on ) override; + + protected: + void resizeEvent( QResizeEvent* ) override; + + private: + enum + { + Skinny, + Widgets, + Graphics, + + GridCount + }; + + QWidget* grids[ GridCount ]; +}; + +#endif diff --git a/playground/grids/grids.pro b/playground/grids/grids.pro new file mode 100644 index 00000000..9b0d53db --- /dev/null +++ b/playground/grids/grids.pro @@ -0,0 +1,25 @@ +CONFIG += qskexample +QT += widgets quickwidgets + +HEADERS += \ + GridAccessor.h \ + GridSkinny.h \ + GridWidgets.h \ + GridGraphics.h \ + TestBox.h + +SOURCES += \ + GridAccessor.cpp \ + GridSkinny.cpp \ + GridWidgets.cpp \ + GridGraphics.cpp \ + TestBox.cpp \ + main.cpp + +linux { + + pedantic { + QMAKE_CXXFLAGS += -isystem $$[QT_INSTALL_HEADERS]/QtQuickWidgets + QMAKE_CXXFLAGS += -isystem $$[QT_INSTALL_HEADERS]/QtWidgets + } +} diff --git a/playground/grids/main.cpp b/playground/grids/main.cpp new file mode 100644 index 00000000..e11c4de0 --- /dev/null +++ b/playground/grids/main.cpp @@ -0,0 +1,45 @@ +/****************************************************************************** + * QSkinny - Copyright (C) 2016 Uwe Rathmann + * This file may be used under the terms of the 3-clause BSD License + *****************************************************************************/ + +#include "TestBox.h" +#include +#include + +class MainBox : public TestBox +{ + public: + MainBox() + { + 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 ); + + setSizePolicy( 1, QskSizePolicy::Fixed, QskSizePolicy::Preferred ); + setSizePolicy( 2, QskSizePolicy::Fixed, QskSizePolicy::Preferred ); + setColumnSizeHint( 1, Qt::MinimumSize, 100 ); + } + + private: + void setSizePolicy( int index, + QskSizePolicy::Policy horizontalPolicy, + QskSizePolicy::Policy verticalPolicy ) + { + TestBox::setSizePolicy( index, Qt::Horizontal, horizontalPolicy ); + TestBox::setSizePolicy( index, Qt::Vertical, verticalPolicy ); + } +}; + +int main( int argc, char** argv ) +{ + QApplication a( argc, argv ); + + MainBox box; + box.resize( 600, 600 ); + box.show(); + + return a.exec(); +} diff --git a/playground/playground.pro b/playground/playground.pro index e0aff6d8..33a92754 100644 --- a/playground/playground.pro +++ b/playground/playground.pro @@ -11,3 +11,9 @@ qtHaveModule(webengine) { SUBDIRS += \ webview } + +qtHaveModule(quickwidgets) { + + SUBDIRS += \ + grids +}