grids test application added, to compare the diefferent layout
implementations
This commit is contained in:
parent
569bd3f5a6
commit
5432d5f5b0
79
playground/grids/GridAccessor.cpp
Normal file
79
playground/grids/GridAccessor.cpp
Normal file
@ -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 <QSize>
|
||||||
|
|
||||||
|
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 );
|
||||||
|
}
|
53
playground/grids/GridAccessor.h
Normal file
53
playground/grids/GridAccessor.h
Normal file
@ -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 <Qt>
|
||||||
|
|
||||||
|
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
|
204
playground/grids/GridGraphics.cpp
Normal file
204
playground/grids/GridGraphics.cpp
Normal file
@ -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 <QGraphicsGridLayout>
|
||||||
|
#include <QGraphicsWidget>
|
||||||
|
#include <QGraphicsScene>
|
||||||
|
#include <QDebug>
|
||||||
|
|
||||||
|
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() );
|
||||||
|
}
|
40
playground/grids/GridGraphics.h
Normal file
40
playground/grids/GridGraphics.h
Normal file
@ -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 <QGraphicsView>
|
||||||
|
|
||||||
|
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
|
126
playground/grids/GridSkinny.cpp
Normal file
126
playground/grids/GridSkinny.cpp
Normal file
@ -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 <QskControl.h>
|
||||||
|
#include <QskGridBox.h>
|
||||||
|
#include <QskQuick.h>
|
||||||
|
|
||||||
|
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 );
|
||||||
|
|
||||||
|
}
|
38
playground/grids/GridSkinny.h
Normal file
38
playground/grids/GridSkinny.h
Normal file
@ -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 <QQuickWidget>
|
||||||
|
|
||||||
|
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
|
197
playground/grids/GridWidgets.cpp
Normal file
197
playground/grids/GridWidgets.cpp
Normal file
@ -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 <QGridLayout>
|
||||||
|
#include <QDebug>
|
||||||
|
|
||||||
|
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 );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
38
playground/grids/GridWidgets.h
Normal file
38
playground/grids/GridWidgets.h
Normal file
@ -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 <QWidget>
|
||||||
|
|
||||||
|
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
|
120
playground/grids/TestBox.cpp
Normal file
120
playground/grids/TestBox.cpp
Normal file
@ -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 );
|
||||||
|
}
|
48
playground/grids/TestBox.h
Normal file
48
playground/grids/TestBox.h
Normal file
@ -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 <QWidget>
|
||||||
|
|
||||||
|
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
|
25
playground/grids/grids.pro
Normal file
25
playground/grids/grids.pro
Normal file
@ -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
|
||||||
|
}
|
||||||
|
}
|
45
playground/grids/main.cpp
Normal file
45
playground/grids/main.cpp
Normal file
@ -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 <QApplication>
|
||||||
|
#include <QskSizePolicy.h>
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
@ -11,3 +11,9 @@ qtHaveModule(webengine) {
|
|||||||
SUBDIRS += \
|
SUBDIRS += \
|
||||||
webview
|
webview
|
||||||
}
|
}
|
||||||
|
|
||||||
|
qtHaveModule(quickwidgets) {
|
||||||
|
|
||||||
|
SUBDIRS += \
|
||||||
|
grids
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user