qskinny/playground/grids/GridSkinny.cpp

135 lines
3.6 KiB
C++
Raw Normal View History

/******************************************************************************
* 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
{
2020-09-22 13:21:26 +02:00
class ColoredRectangle : public QskControl
{
public:
2020-09-22 13:21:26 +02:00
ColoredRectangle( 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;
};
}
GridSkinny::GridSkinny( QWidget* parent )
: QQuickWidget( parent )
{
setContentsMargins( QMargins() );
setResizeMode( QQuickWidget::SizeRootObjectToView );
m_grid = new QskGridBox();
m_grid->setBackgroundColor( Qt::white );
m_grid->setMargins( 10 );
2019-07-25 18:39:50 +02:00
m_grid->setSpacing( 5 );
setContent( QUrl(), nullptr, m_grid );
}
GridSkinny::~GridSkinny()
{
}
void GridSkinny::insert( const QByteArray& colorName,
int row, int column, int rowSpan, int columnSpan )
{
2020-09-22 13:21:26 +02:00
m_grid->addItem( new ColoredRectangle( colorName ),
row, column, rowSpan, columnSpan );
}
void GridSkinny::setSpacing( Qt::Orientations orientations, int spacing )
{
m_grid->setSpacing( orientations, spacing );
}
2019-07-23 18:35:51 +02:00
void GridSkinny::setSizeHint(
int pos, Qt::Orientation orientation, Qt::SizeHint which, int hint )
{
2019-07-23 18:35:51 +02:00
if ( orientation == Qt::Vertical )
m_grid->setRowSizeHint( pos, which, hint );
else
m_grid->setColumnSizeHint( pos, which, hint );
}
2019-07-23 18:35:51 +02:00
void GridSkinny::setStretchFactor(
int pos, Qt::Orientation orientation, int stretch )
{
2019-07-23 18:35:51 +02:00
if ( orientation == Qt::Vertical )
m_grid->setRowStretchFactor( pos, stretch );
else
m_grid->setColumnStretchFactor( pos, stretch );
}
2019-07-23 18:35:51 +02:00
void GridSkinny::setSizeHintAt( int index, Qt::Orientation orientation,
Qt::SizeHint which, int hint )
{
if ( auto control = qskControlCast( m_grid->itemAtIndex( index ) ) )
{
auto size = control->explicitSizeHint( which );
if ( orientation == Qt::Horizontal )
size.setWidth( hint );
else
size.setHeight( hint );
control->setExplicitSizeHint( which, size );
}
}
2019-07-23 18:35:51 +02:00
void GridSkinny::setSizePolicyAt(
int index, Qt::Orientation orientation, int policy )
{
if ( auto control = qskControlCast( m_grid->itemAtIndex( index ) ) )
{
control->setSizePolicy( orientation,
static_cast< QskSizePolicy::Policy >( policy ) );
}
}
2019-07-23 18:35:51 +02:00
void GridSkinny::setAlignmentAt( int index, Qt::Alignment alignment )
{
if ( auto control = qskControlCast( m_grid->itemAtIndex( index ) ) )
control->setLayoutAlignmentHint( alignment );
}
2019-07-23 18:35:51 +02:00
void GridSkinny::setRetainSizeWhenHiddenAt( int index, bool on )
{
if ( auto control = qskControlCast( m_grid->itemAtIndex( index ) ) )
control->setLayoutHint( QskControl::RetainSizeWhenHidden, on );
2019-07-25 18:39:50 +02:00
}
void GridSkinny::setVisibleAt( int index, bool on )
{
if ( auto item = m_grid->itemAtIndex( index ) )
item->setVisible( on );
}
2019-07-25 18:39:50 +02:00
QSize GridSkinny::preferredSize() const
{
return m_grid->preferredSize().toSize();
}