diff --git a/playground/grids/GridAccessor.cpp b/playground/grids/GridAccessor.cpp index 36d11cad..87d706a6 100644 --- a/playground/grids/GridAccessor.cpp +++ b/playground/grids/GridAccessor.cpp @@ -11,6 +11,16 @@ void GridAccessor::setSpacing( int spacing ) setSpacing( Qt::Vertical | Qt::Horizontal, spacing ); } +void GridAccessor::setRowSizeHint( int row, Qt::SizeHint which, int height ) +{ + setSizeHint( row, Qt::Vertical, which, height ); +} + +void GridAccessor::setColumnSizeHint( int column, Qt::SizeHint which, int width ) +{ + setSizeHint( column, Qt::Horizontal, which, width ); +} + void GridAccessor::setRowFixedHeight( int row, qreal height ) { setRowSizeHint( row, Qt::MinimumSize, height ); @@ -23,57 +33,67 @@ void GridAccessor::setColumnFixedWidth( int column, qreal width ) setColumnSizeHint( column, Qt::MaximumSize, width ); } -void GridAccessor::setMinimumWidth( int index, int hint ) +void GridAccessor::setRowStretchFactor( int row, int stretch ) { - setSizeHint( index, Qt::Horizontal, Qt::MinimumSize, hint ); + setStretchFactor( row, Qt::Vertical, stretch ); } -void GridAccessor::setMinimumHeight( int index, int hint ) +void GridAccessor::setColumnStretchFactor( int column, int stretch ) { - setSizeHint( index, Qt::Vertical, Qt::MinimumSize, hint ); + setStretchFactor( column, Qt::Vertical, stretch ); } -void GridAccessor::setMinimumSize( int index, const QSize& size ) +void GridAccessor::setMinimumWidthAt( int index, int hint ) { - setMinimumWidth( index, size.width() ); - setMinimumHeight( index, size.height() ); + setSizeHintAt( index, Qt::Horizontal, Qt::MinimumSize, hint ); } -void GridAccessor::setPreferredWidth( int index, int hint ) +void GridAccessor::setMinimumHeightAt( int index, int hint ) { - setSizeHint( index, Qt::Horizontal, Qt::PreferredSize, hint ); + setSizeHintAt( index, Qt::Vertical, Qt::MinimumSize, hint ); } -void GridAccessor::setPreferredHeight( int index, int hint ) +void GridAccessor::setMinimumSizeAt( int index, const QSize& size ) { - setSizeHint( index, Qt::Vertical, Qt::PreferredSize, hint ); + setMinimumWidthAt( index, size.width() ); + setMinimumHeightAt( index, size.height() ); } -void GridAccessor::setPreferredSize( int index, const QSize& size ) +void GridAccessor::setPreferredWidthAt( int index, int hint ) { - setPreferredWidth( index, size.width() ); - setPreferredHeight( index, size.height() ); + setSizeHintAt( index, Qt::Horizontal, Qt::PreferredSize, hint ); } -void GridAccessor::setMaximumWidth( int index, int hint ) +void GridAccessor::setPreferredHeightAt( int index, int hint ) { - setSizeHint( index, Qt::Horizontal, Qt::MaximumSize, hint ); + setSizeHintAt( index, Qt::Vertical, Qt::PreferredSize, hint ); } -void GridAccessor::setMaximumHeight( int index, int hint ) +void GridAccessor::setPreferredSizeAt( int index, const QSize& size ) { - setSizeHint( index, Qt::Vertical, Qt::MaximumSize, hint ); + setPreferredWidthAt( index, size.width() ); + setPreferredHeightAt( index, size.height() ); } -void GridAccessor::setMaximumSize( int index, const QSize& size ) +void GridAccessor::setMaximumWidthAt( int index, int hint ) { - setMaximumWidth( index, size.width() ); - setMaximumHeight( index, size.height() ); + setSizeHintAt( index, Qt::Horizontal, Qt::MaximumSize, hint ); } -void GridAccessor::setSizePolicy( +void GridAccessor::setMaximumHeightAt( int index, int hint ) +{ + setSizeHintAt( index, Qt::Vertical, Qt::MaximumSize, hint ); +} + +void GridAccessor::setMaximumSizeAt( int index, const QSize& size ) +{ + setMaximumWidthAt( index, size.width() ); + setMaximumHeightAt( index, size.height() ); +} + +void GridAccessor::setSizePolicyAt( int index, int horizontalPolicy, int verticalPolicy ) { - setSizePolicy( index, Qt::Horizontal, horizontalPolicy ); - setSizePolicy( index, Qt::Vertical, verticalPolicy ); + setSizePolicyAt( index, Qt::Horizontal, horizontalPolicy ); + setSizePolicyAt( index, Qt::Vertical, verticalPolicy ); } diff --git a/playground/grids/GridAccessor.h b/playground/grids/GridAccessor.h index f27677d0..8c064e27 100644 --- a/playground/grids/GridAccessor.h +++ b/playground/grids/GridAccessor.h @@ -23,31 +23,36 @@ class GridAccessor 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; + virtual void setSizeHint( int pos, Qt::Orientation, Qt::SizeHint, int height ) = 0; + void setRowSizeHint( int row, Qt::SizeHint, int height ); + void setColumnSizeHint( int column, Qt::SizeHint, int width ); 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; + virtual void setStretchFactor( int pos, Qt::Orientation, int stretch ) = 0; + void setRowStretchFactor( int row, int stretch ); + void setColumnStretchFactor( int row, int stretch ); - void setSizePolicy( int index, int horizontalPolicy, int verticalPolicy ); + virtual void setSizeHintAt( int index, Qt::Orientation, Qt::SizeHint, int hint ) = 0; + virtual void setSizePolicyAt( int index, Qt::Orientation, int policy ) = 0; - void setMinimumWidth( int index, int hint ); - void setMinimumHeight( int index, int hint ); - void setMinimumSize( int index, const QSize& ); + void setSizePolicyAt( int index, int horizontalPolicy, int verticalPolicy ); - void setPreferredWidth( int index, int hint ); - void setPreferredHeight( int index, int hint ); - void setPreferredSize( int index, const QSize& ); + void setMinimumWidthAt( int index, int hint ); + void setMinimumHeightAt( int index, int hint ); + void setMinimumSizeAt( int index, const QSize& ); - void setMaximumWidth( int index, int hint ); - void setMaximumHeight( int index, int hint ); - void setMaximumSize( int index, const QSize& ); + void setPreferredWidthAt( int index, int hint ); + void setPreferredHeightAt( int index, int hint ); + void setPreferredSizeAt( int index, const QSize& ); - virtual void setAlignment( int index, Qt::Alignment ) = 0; - virtual void setRetainSizeWhenHidden( int, bool on ) = 0; + void setMaximumWidthAt( int index, int hint ); + void setMaximumHeightAt( int index, int hint ); + void setMaximumSizeAt( int index, const QSize& ); + + virtual void setAlignmentAt( int index, Qt::Alignment ) = 0; + virtual void setRetainSizeWhenHiddenAt( int index, bool on ) = 0; }; #endif diff --git a/playground/grids/GridGraphics.cpp b/playground/grids/GridGraphics.cpp index cd4f3f36..633799de 100644 --- a/playground/grids/GridGraphics.cpp +++ b/playground/grids/GridGraphics.cpp @@ -27,24 +27,48 @@ namespace setAutoFillBackground( true ); } + protected: void resizeEvent( QGraphicsSceneResizeEvent* event ) override { QGraphicsWidget::resizeEvent( event ); - //qDebug() << m_color << size(); +#if 0 + qDebug() << m_colorName << size(); +#endif } private: const QByteArray m_colorName; }; + + class Grid : public QGraphicsWidget + { + public: + Grid() + { +#if 0 + setPalette( QColor( "LightCyan" ) ); + setAutoFillBackground( true ); +#endif + } + + protected: + QSizeF sizeHint( Qt::SizeHint, const QSizeF& ) const override + { + // we don't want to derive our size hints from the items + return QSizeF( -1, -1 ); + } + }; } GridGraphics::GridGraphics( QWidget* parent ) : QGraphicsView( parent ) { setFrameStyle( QFrame::NoFrame ); - setContentsMargins( QMargins() ); - auto grid = new QGraphicsWidget(); + setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff ); + setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff ); + + auto grid = new Grid(); m_layout = new QGraphicsGridLayout( grid ); auto scene = new QGraphicsScene(); @@ -73,52 +97,51 @@ void GridGraphics::setSpacing( Qt::Orientations orientations, int spacing ) 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 pos, Qt::Orientation orientation, Qt::SizeHint which, int hint ) +{ + switch( static_cast< int >( which ) ) + { + case Qt::MinimumSize: + { + if ( orientation == Qt::Vertical ) + m_layout->setRowMinimumHeight( pos, hint ); + else + m_layout->setColumnMinimumWidth( pos, hint ); + + break; + } + case Qt::PreferredSize: + { + if ( orientation == Qt::Vertical ) + m_layout->setRowPreferredHeight( pos, hint ); + else + m_layout->setColumnPreferredWidth( pos, hint ); + + break; + } + case Qt::MaximumSize: + { + if ( orientation == Qt::Vertical ) + m_layout->setRowMaximumHeight( pos, hint ); + else + m_layout->setColumnMaximumWidth( pos, hint ); + + break; + } + } +} + +void GridGraphics::setStretchFactor( + int pos, Qt::Orientation orientation, int stretch ) +{ + if ( orientation == Qt::Vertical ) + m_layout->setRowStretchFactor( pos, stretch ); + else + m_layout->setColumnStretchFactor( pos, stretch ); +} + +void GridGraphics::setSizeHintAt( int index, Qt::Orientation orientation, Qt::SizeHint which, int hint ) { if ( auto layoutItem = m_layout->itemAt( index ) ) @@ -156,7 +179,7 @@ void GridGraphics::setSizeHint( } } -void GridGraphics::setSizePolicy( int index, Qt::Orientation orientation, int policy ) +void GridGraphics::setSizePolicyAt( int index, Qt::Orientation orientation, int policy ) { if ( auto layoutItem = m_layout->itemAt( index ) ) { @@ -181,13 +204,13 @@ void GridGraphics::setSizePolicy( int index, Qt::Orientation orientation, int po } } -void GridGraphics::setAlignment( int index, Qt::Alignment alignment ) +void GridGraphics::setAlignmentAt( int index, Qt::Alignment alignment ) { if ( auto layoutItem = m_layout->itemAt( index ) ) m_layout->setAlignment( layoutItem, alignment ); } -void GridGraphics::setRetainSizeWhenHidden( int index, bool on ) +void GridGraphics::setRetainSizeWhenHiddenAt( int index, bool on ) { if ( auto layoutItem = m_layout->itemAt( index ) ) { @@ -199,6 +222,12 @@ void GridGraphics::setRetainSizeWhenHidden( int index, bool on ) void GridGraphics::resizeEvent( QResizeEvent* ) { + auto sceneRect = contentsRect(); +#if 0 + sceneRect = sceneRect.adjusted( 10, 10, -10, -10 ); +#endif + scene()->setSceneRect( QRectF( QPointF(), sceneRect.size() ) ); + auto grid = static_cast< QGraphicsWidget* >( scene()->items().last() ); - grid->resize( contentsRect().size() ); + grid->resize( sceneRect.size() ); } diff --git a/playground/grids/GridGraphics.h b/playground/grids/GridGraphics.h index b34d7b5e..c3e2d8f2 100644 --- a/playground/grids/GridGraphics.h +++ b/playground/grids/GridGraphics.h @@ -22,13 +22,13 @@ class GridGraphics : public QGraphicsView, public GridAccessor 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 setStretchFactor( int pos, Qt::Orientation, int stretch ) override; + void setSizeHint( int pos, Qt::Orientation, Qt::SizeHint, int hint ) 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; + 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; diff --git a/playground/grids/GridQuick.cpp b/playground/grids/GridQuick.cpp new file mode 100644 index 00000000..decca2c8 --- /dev/null +++ b/playground/grids/GridQuick.cpp @@ -0,0 +1,181 @@ +/****************************************************************************** + * QSkinny - Copyright (C) 2016 Uwe Rathmann + * This file may be used under the terms of the 3-clause BSD License + *****************************************************************************/ + +#include "GridQuick.h" +#include +#include + +static QQuickItem* createQml( const char* qmlCode ) +{ + QQmlEngine engine( nullptr ); + + QQmlComponent component( &engine ); + component.setData( qmlCode, QUrl() ); + + return qobject_cast< QQuickItem* >( component.create() ); +} + +static QObject* attachedProperties( const QQuickItem* item ) +{ + for ( auto child : item->children() ) + { + if ( child->inherits( "QQuickLayoutAttached" ) ) + return child; + } + + return nullptr; +} + +static QObject* attachedPropertiesAt( const QQuickItem* grid, int index ) +{ + const auto children = grid->childItems(); + + if ( ( index >= 0 ) && ( index < children.count() ) ) + return attachedProperties( children.at( index ) ); + + return nullptr; +} + + +GridQuick::GridQuick( QWidget* parent ) + : QQuickWidget( parent ) +{ + setContentsMargins( QMargins() ); + setResizeMode( QQuickWidget::SizeRootObjectToView ); + + auto contentItem = + createQml( "import QtQuick 2.0\nimport QtQuick.Layouts 1.1\nItem { GridLayout {} }" ); + setContent( QUrl(), nullptr, contentItem ); + + m_grid = contentItem->childItems().first(); +} + +GridQuick::~GridQuick() +{ +} + +void GridQuick::insert( const QByteArray& colorName, + int row, int column, int rowSpan, int columnSpan ) +{ + auto rectangle = createQml( "import QtQuick 2.0\nRectangle {}" ); + rectangle->setProperty( "color", QColor( colorName.constData() ) ); + rectangle->setParent( m_grid ); + rectangle->setParentItem( m_grid ); + + rectangle->setImplicitWidth( 50 ); + rectangle->setImplicitHeight( 50 ); + + auto props = attachedProperties( rectangle ); + props->setProperty( "row", row ); + props->setProperty( "column", column ); + props->setProperty( "rowSpan", rowSpan ); + props->setProperty( "columnSpan", columnSpan ); + props->setProperty( "fillWidth", true ); + props->setProperty( "fillHeight", true ); +} + +void GridQuick::setSpacing( Qt::Orientations orientations, int spacing ) +{ + if ( orientations & Qt::Vertical ) + m_grid->setProperty( "rowSpacing", spacing ); + + if ( orientations & Qt::Horizontal ) + m_grid->setProperty( "columnSpacing", spacing ); +} + +void GridQuick::setSizeHint( int, Qt::Orientation, Qt::SizeHint, int ) +{ + qWarning() << "setSizeHint is not supported by Quick Layouts."; +} + +void GridQuick::setStretchFactor( int, Qt::Orientation, int ) +{ + qWarning() << "setStretchFactor is not supported by Quick Layouts."; +} + +void GridQuick::setSizeHintAt( int index, Qt::Orientation orientation, + Qt::SizeHint which, int hint ) +{ + if ( auto props = attachedPropertiesAt( m_grid, index ) ) + { + const qreal size = hint; + + switch( static_cast< int >( which ) ) + { + case Qt::MinimumSize: + { + if ( orientation == Qt::Horizontal ) + props->setProperty( "minimumWidth", size ); + else + props->setProperty( "minimumHeight", size ); + + break; + } + case Qt::PreferredSize: + { + if ( orientation == Qt::Horizontal ) + props->setProperty( "preferredWidth", size ); + else + props->setProperty( "preferredHeight", size ); + + break; + } + case Qt::MaximumSize: + { + if ( orientation == Qt::Horizontal ) + props->setProperty( "maximumWidth", size ); + else + props->setProperty( "maximumWidth", size ); + + break; + } + } + } +} + +void GridQuick::setSizePolicyAt( + int index, Qt::Orientation orientation, int policy ) +{ + const auto qPolicy = static_cast< QSizePolicy::Policy >( policy & 0xF ); + +#if 0 + const bool isConstrained = policy & ( 1 << 4 ); +#endif + + const bool doFill = ( qPolicy & QSizePolicy::GrowFlag ) + || ( qPolicy & QSizePolicy::ExpandFlag ); + + if ( auto props = attachedPropertiesAt( m_grid, index ) ) + { + if ( orientation == Qt::Horizontal ) + props->setProperty( "fillWidth", doFill ); + else + props->setProperty( "fillHeight", doFill ); + } +} + +void GridQuick::setAlignmentAt( int index, Qt::Alignment alignment ) +{ + if ( auto props = attachedPropertiesAt( m_grid, index ) ) + props->setProperty( "alignment", QVariant::fromValue( alignment ) ); +} + +void GridQuick::setRetainSizeWhenHiddenAt( int, bool ) +{ + qWarning() << "setRetainSizeWhenHidden is not supported by Quick Layouts."; +} + +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 ); + m_grid->setHeight( height() - 2 * margin ); +} + diff --git a/playground/grids/GridQuick.h b/playground/grids/GridQuick.h new file mode 100644 index 00000000..5b7db37b --- /dev/null +++ b/playground/grids/GridQuick.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_QUICK_H +#define GRID_QUICK_H + +#include "GridAccessor.h" +#include + +class QGraphicsGridLayout; + +class GridQuick : public QQuickWidget, public GridAccessor +{ + public: + GridQuick( QWidget* parent = nullptr ); + ~GridQuick() override; + + 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: + QQuickItem* m_grid; +}; + +#endif diff --git a/playground/grids/GridSkinny.cpp b/playground/grids/GridSkinny.cpp index 66d0d0dc..6f9cd5e4 100644 --- a/playground/grids/GridSkinny.cpp +++ b/playground/grids/GridSkinny.cpp @@ -76,17 +76,25 @@ void GridSkinny::setSpacing( Qt::Orientations orientations, int spacing ) m_grid->setSpacing( orientations, spacing ); } -void GridSkinny::setRowSizeHint( int row, Qt::SizeHint which, int height ) +void GridSkinny::setSizeHint( + int pos, Qt::Orientation orientation, Qt::SizeHint which, int hint ) { - m_grid->setRowSizeHint( row, which, height ); + if ( orientation == Qt::Vertical ) + m_grid->setRowSizeHint( pos, which, hint ); + else + m_grid->setColumnSizeHint( pos, which, hint ); } -void GridSkinny::setColumnSizeHint( int column, Qt::SizeHint which, int width ) +void GridSkinny::setStretchFactor( + int pos, Qt::Orientation orientation, int stretch ) { - m_grid->setColumnSizeHint( column, which, width ); + if ( orientation == Qt::Vertical ) + m_grid->setRowStretchFactor( pos, stretch ); + else + m_grid->setColumnStretchFactor( pos, stretch ); } -void GridSkinny::setSizeHint( int index, Qt::Orientation orientation, +void GridSkinny::setSizeHintAt( int index, Qt::Orientation orientation, Qt::SizeHint which, int hint ) { if ( auto control = qobject_cast< QskControl* >( m_grid->itemAtIndex( index ) ) ) @@ -102,7 +110,7 @@ void GridSkinny::setSizeHint( int index, Qt::Orientation orientation, } } -void GridSkinny::setSizePolicy( +void GridSkinny::setSizePolicyAt( int index, Qt::Orientation orientation, int policy ) { if ( auto control = qobject_cast< QskControl* >( m_grid->itemAtIndex( index ) ) ) @@ -112,13 +120,13 @@ void GridSkinny::setSizePolicy( } } -void GridSkinny::setAlignment( int index, Qt::Alignment alignment ) +void GridSkinny::setAlignmentAt( int index, Qt::Alignment alignment ) { if ( auto item = m_grid->itemAtIndex( index ) ) m_grid->setAlignment( item, alignment ); } -void GridSkinny::setRetainSizeWhenHidden( int index, bool on ) +void GridSkinny::setRetainSizeWhenHiddenAt( 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 index 1d3d24dc..77f25237 100644 --- a/playground/grids/GridSkinny.h +++ b/playground/grids/GridSkinny.h @@ -22,14 +22,14 @@ class GridSkinny : public QQuickWidget, public GridAccessor 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 setStretchFactor( int pos, Qt::Orientation, int stretch ) override; + void setSizeHint( int pos, Qt::Orientation, Qt::SizeHint, int hint ) override; - void setSizeHint( int index, Qt::Orientation, Qt::SizeHint, int hint ) override; - void setSizePolicy( int index, Qt::Orientation, int policy ) override; + void setSizeHintAt( int index, Qt::Orientation, Qt::SizeHint, int hint ) override; + void setSizePolicyAt( int index, Qt::Orientation, int policy ) override; - void setAlignment( int index, Qt::Alignment ) override; - void setRetainSizeWhenHidden( int, bool on ) override; + void setAlignmentAt( int index, Qt::Alignment ) override; + void setRetainSizeWhenHiddenAt( int index, bool on ) override; private: QskGridBox* m_grid; diff --git a/playground/grids/GridWidgets.cpp b/playground/grids/GridWidgets.cpp index 6421a68d..20cffe27 100644 --- a/playground/grids/GridWidgets.cpp +++ b/playground/grids/GridWidgets.cpp @@ -83,29 +83,31 @@ void GridWidgets::setSpacing( Qt::Orientations orientations, int spacing ) 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 pos, Qt::Orientation orientation, Qt::SizeHint which, int hint ) +{ + if ( which != Qt::MinimumSize ) + { + qWarning() << "setSizeHint" << which << "is not supported by QGridLayout."; + return; + } + + if ( orientation == Qt::Vertical ) + m_layout->setRowMinimumHeight( pos, hint ); + else + m_layout->setColumnMinimumWidth( pos, hint ); +} + +void GridWidgets::setStretchFactor( + int pos, Qt::Orientation orientation, int stretch ) +{ + if ( orientation == Qt::Vertical ) + m_layout->setRowStretch( pos, stretch ); + else + m_layout->setColumnStretch( pos, stretch ); +} + +void GridWidgets::setSizeHintAt( int index, Qt::Orientation orientation, Qt::SizeHint which, int hint ) { Rectangle* rectangle = nullptr; @@ -148,7 +150,7 @@ void GridWidgets::setSizeHint( } } -void GridWidgets::setSizePolicy( int index, Qt::Orientation orientation, int policy ) +void GridWidgets::setSizePolicyAt( int index, Qt::Orientation orientation, int policy ) { QWidget* widget = nullptr; @@ -177,13 +179,13 @@ void GridWidgets::setSizePolicy( int index, Qt::Orientation orientation, int pol widget->setSizePolicy( sizePolicy ); } -void GridWidgets::setAlignment( int index, Qt::Alignment alignment ) +void GridWidgets::setAlignmentAt( int index, Qt::Alignment alignment ) { if ( auto layoutItem = m_layout->itemAt( index ) ) layoutItem->setAlignment( alignment ); } -void GridWidgets::setRetainSizeWhenHidden( int index, bool on ) +void GridWidgets::setRetainSizeWhenHiddenAt( int index, bool on ) { if ( auto layoutItem = m_layout->itemAt( index ) ) { diff --git a/playground/grids/GridWidgets.h b/playground/grids/GridWidgets.h index ef80683a..ebc69f9d 100644 --- a/playground/grids/GridWidgets.h +++ b/playground/grids/GridWidgets.h @@ -22,14 +22,14 @@ class GridWidgets : public QWidget, public GridAccessor 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 setStretchFactor( int pos, Qt::Orientation, int stretch ) override; + void setSizeHint( int pos, Qt::Orientation, Qt::SizeHint, int hint ) override; - void setSizeHint( int index, Qt::Orientation, Qt::SizeHint, int hint ) override; - void setSizePolicy( int index, Qt::Orientation, int policy ) override; + void setSizeHintAt( int index, Qt::Orientation, Qt::SizeHint, int hint ) override; + void setSizePolicyAt( int index, Qt::Orientation, int policy ) override; - void setAlignment( int index, Qt::Alignment ) override; - void setRetainSizeWhenHidden( int, bool on ) override; + void setAlignmentAt( int index, Qt::Alignment ) override; + void setRetainSizeWhenHiddenAt( int index, bool on ) override; private: QGridLayout* m_layout; diff --git a/playground/grids/TestBox.cpp b/playground/grids/TestBox.cpp index f9e91141..4b965a9b 100644 --- a/playground/grids/TestBox.cpp +++ b/playground/grids/TestBox.cpp @@ -7,6 +7,7 @@ #include "GridGraphics.h" #include "GridSkinny.h" #include "GridWidgets.h" +#include "GridQuick.h" TestBox::TestBox( QWidget* parent ) : QWidget( parent ) @@ -19,12 +20,21 @@ TestBox::TestBox( QWidget* parent ) grids[Skinny] = new GridSkinny( this ); grids[Widgets] = new GridWidgets( this ); grids[Graphics] = new GridGraphics( this ); + grids[Quick] = new GridQuick( this ); } TestBox::~TestBox() { } +void TestBox::setColumns( int columnCount ) +{ + m_columnCount = qBound( 1, columnCount, static_cast< int >( GridCount ) ); + + if ( testAttribute( Qt::WA_WState_Polished ) ) + layoutGrids(); +} + void TestBox::insert( const QByteArray& colorName, int row, int column, int rowSpan, int columnSpan ) { @@ -45,63 +55,70 @@ void TestBox::setSpacing( Qt::Orientations orientations, int 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 pos, Qt::Orientation orientation, Qt::SizeHint which, int hint ) +{ + for ( auto grid : grids ) + { + 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 ) + { + 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 ) { auto accessor = dynamic_cast< GridAccessor* >( grid ); - accessor->setSizeHint( index, orientation, which, hint ); + accessor->setSizeHintAt( index, orientation, which, hint ); } } -void TestBox::setSizePolicy( +void TestBox::setSizePolicyAt( int index, Qt::Orientation orientation, int policy ) { for ( auto grid : grids ) { auto accessor = dynamic_cast< GridAccessor* >( grid ); - accessor->setSizePolicy( index, orientation, policy ); + accessor->setSizePolicyAt( index, orientation, policy ); } } -void TestBox::setAlignment( int index, Qt::Alignment alignment ) +void TestBox::setAlignmentAt( int index, Qt::Alignment alignment ) { for ( auto grid : grids ) { auto accessor = dynamic_cast< GridAccessor* >( grid ); - accessor->setAlignment( index, alignment ); + accessor->setAlignmentAt( index, alignment ); } } -void TestBox::setRetainSizeWhenHidden( int index, bool on ) +void TestBox::setRetainSizeWhenHiddenAt( int index, bool on ) { for ( auto grid : grids ) { auto accessor = dynamic_cast< GridAccessor* >( grid ); - accessor->setRetainSizeWhenHidden( index, on ); + accessor->setRetainSizeWhenHiddenAt( index, on ); } } void TestBox::resizeEvent( QResizeEvent* ) +{ + layoutGrids(); +} + +void TestBox::layoutGrids() { /* Not using layouts here to avoid confusion @@ -109,12 +126,30 @@ void TestBox::resizeEvent( QResizeEvent* ) */ 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() ); + int rowCount = GridCount / m_columnCount; + if ( rowCount * m_columnCount < GridCount ) + rowCount++; - for ( auto grid : grids ) - grid->resize( sz ); + const int spacing = 5; + + const int width = ( r.width() - ( m_columnCount - 1 ) * spacing ) / m_columnCount; + const int height = ( r.height() - ( rowCount - 1 ) * spacing ) / rowCount; + + int row = 0; + int col = 0; + + for ( int i = 0; i < GridCount; i++ ) + { + const int x = r.left() + col * ( spacing + width ); + const int y = r.top() + row * ( spacing + height ); + + grids[i]->setGeometry( x, y, width, height ); + + if ( ++col >= m_columnCount ) + { + col = 0; + row++; + } + } } diff --git a/playground/grids/TestBox.h b/playground/grids/TestBox.h index a917c3e0..b56bf3d0 100644 --- a/playground/grids/TestBox.h +++ b/playground/grids/TestBox.h @@ -15,34 +15,40 @@ class TestBox : public QWidget, public GridAccessor 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 setRowSizeHint( int row, Qt::SizeHint, int height ) override; - void setColumnSizeHint( int column, Qt::SizeHint, int width ) override; + void setStretchFactor( int pos, Qt::Orientation, int stretch ) override; + void setSizeHint( int pos, Qt::Orientation, Qt::SizeHint, int hint ) override; - void setSizeHint( int index, Qt::Orientation, Qt::SizeHint, int hint ) override; - void setSizePolicy( int index, Qt::Orientation, int policy ) override; + void setSizeHintAt( int index, Qt::Orientation, Qt::SizeHint, int hint ) override; + void setSizePolicyAt( int index, Qt::Orientation, int policy ) override; - void setAlignment( int index, Qt::Alignment ) override; - void setRetainSizeWhenHidden( int, bool on ) 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, Widgets, Graphics, + Quick, GridCount }; QWidget* grids[ GridCount ]; + int m_columnCount = 2; }; #endif diff --git a/playground/grids/grids.pro b/playground/grids/grids.pro index 9b0d53db..3c85b881 100644 --- a/playground/grids/grids.pro +++ b/playground/grids/grids.pro @@ -6,6 +6,7 @@ HEADERS += \ GridSkinny.h \ GridWidgets.h \ GridGraphics.h \ + GridQuick.h \ TestBox.h SOURCES += \ @@ -13,6 +14,7 @@ SOURCES += \ GridSkinny.cpp \ GridWidgets.cpp \ GridGraphics.cpp \ + GridQuick.cpp \ TestBox.cpp \ main.cpp diff --git a/playground/grids/main.cpp b/playground/grids/main.cpp index e11c4de0..05b05377 100644 --- a/playground/grids/main.cpp +++ b/playground/grids/main.cpp @@ -10,34 +10,138 @@ class MainBox : public TestBox { public: - MainBox() + MainBox( int id ) { - 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 ); + switch( id ) + { + case 0: + setup0(); + break; - setSizePolicy( 1, QskSizePolicy::Fixed, QskSizePolicy::Preferred ); - setSizePolicy( 2, QskSizePolicy::Fixed, QskSizePolicy::Preferred ); - setColumnSizeHint( 1, Qt::MinimumSize, 100 ); + case 1: + setup1(); + break; + + case 2: + setup2(); + break; + + case 3: + setup3(); + break; + } } private: - void setSizePolicy( int index, + + void setup0(); + void setup1(); + void setup2(); + void setup3(); + + void setSizePolicyAt( int index, QskSizePolicy::Policy horizontalPolicy, QskSizePolicy::Policy verticalPolicy ) { - TestBox::setSizePolicy( index, Qt::Horizontal, horizontalPolicy ); - TestBox::setSizePolicy( index, Qt::Vertical, verticalPolicy ); + TestBox::setSizePolicyAt( index, Qt::Horizontal, horizontalPolicy ); + TestBox::setSizePolicyAt( index, Qt::Vertical, verticalPolicy ); } }; +void MainBox::setup0() +{ + // 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( "NavajoWhite", 3, 0, 1, 3 ); +} + +void MainBox::setup1() +{ + /* + The Graphics layout adds the extra space to the stretchable + 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 + setColumnSizeHint( 1, Qt::MinimumSize, 100 ); +#else + for ( int i = 0; i < 4; i++ ) + setMinimumWidthAt( i, 100 ); +#endif +} + +void MainBox::setup2() +{ + /* + The Graphics layout uses a "magic" formula for how to apply + the stretch factors, while the other layouts result in + predictable sizes being calculated according to the factors. + In the specific situation column 1 in the Graphics layout is + larger than twice of columns 0. + */ + + setColumns( 4 ); + + insert( "PaleVioletRed", 0, 0, 1, 1 ); + insert( "DarkSeaGreen", 1, 0, 1, 1 ); + + setRowStretchFactor( 0, 1 ); + setRowStretchFactor( 1, 2 ); +} + +void MainBox::setup3() +{ + /* + When setting a maximum size together with an alignment the expected result + would be a cell that can grow beyond the maximum, while the content stays + at its maximum, being aligned inside the cell. + + But with widgets, the cell size gets always bounded to the preferred size + as soon as an alignment has been set. In ths + + With graphics we have the effect, that the cells get bounded to the + maximum size and we always end up with being aligned Top | Left. + + */ + setColumns( 1 ); + + insert( "PaleVioletRed", 0, 0, 1, 1 ); + + setPreferredWidthAt( 0, 10 ); + setPreferredHeightAt( 0, 10 ); + + setMaximumWidthAt( 0, 200 ); + setMaximumHeightAt( 0, 200 ); + + setAlignmentAt( 0, Qt::AlignCenter ); +} + int main( int argc, char** argv ) { QApplication a( argc, argv ); - MainBox box; + MainBox box( 0 ); + box.resize( 600, 600 ); box.show();