sourceSize members replaced by strutSizeHints to make icons sizes

configurable in the skin
This commit is contained in:
Uwe Rathmann 2022-07-28 11:37:14 +02:00
parent 5db2f636f3
commit 2737bc0be3
8 changed files with 62 additions and 77 deletions

View File

@ -30,7 +30,7 @@ RoundButton::RoundButton( QskAspect::Placement placement, QQuickItem* parent )
setGraphicSource( "down" );
}
setGraphicSourceSize( graphic().defaultSize() * 1.2 );
setGraphicStrutSize( graphic().defaultSize() * 1.2 );
}
#include "moc_RoundButton.cpp"

View File

@ -19,7 +19,6 @@ class QskGraphicLabel::PrivateData
public:
PrivateData( const QUrl& sourceUrl )
: source( sourceUrl )
, sourceSize( -1, -1 )
, fillMode( QskGraphicLabel::PreserveAspectFit )
, mirror( false )
, isSourceDirty( !sourceUrl.isEmpty() )
@ -28,8 +27,6 @@ class QskGraphicLabel::PrivateData
}
QUrl source;
QSize sourceSize;
QskGraphic graphic;
uint fillMode : 2;
@ -126,7 +123,7 @@ void QskGraphicLabel::setGraphic( const QskGraphic& graphic )
{
if ( m_data->graphic != graphic )
{
const bool keepImplicitSize = m_data->sourceSize.isValid()
const bool keepImplicitSize = graphicStrutSize().isValid()
|| ( m_data->graphic.defaultSize() == graphic.defaultSize() );
m_data->graphic = graphic;
@ -178,7 +175,7 @@ void QskGraphicLabel::setMirror( bool on )
{
m_data->mirror = on;
if ( !( m_data->sourceSize.isEmpty() || m_data->graphic.isEmpty() ) )
if ( !( graphicStrutSize().isEmpty() || m_data->graphic.isEmpty() ) )
update();
Q_EMIT mirrorChanged();
@ -190,35 +187,28 @@ bool QskGraphicLabel::mirror() const
return m_data->mirror;
}
void QskGraphicLabel::setSourceSize( const QSize& size )
void QskGraphicLabel::setGraphicStrutSize( const QSizeF& size )
{
QSize sz = size;
auto newSize = size;
if ( newSize.width() < 0.0 )
newSize.setWidth( -1.0 );
if ( sz.width() < 0 )
sz.setWidth( -1 );
if ( newSize.height() < 0.0 )
newSize.setHeight( -1.0 );
if ( sz.height() < 0 )
sz.setHeight( -1 );
if ( m_data->sourceSize != sz )
{
m_data->sourceSize = sz;
resetImplicitSize();
update();
Q_EMIT sourceSizeChanged();
}
if ( setStrutSizeHint( Graphic, newSize ) )
Q_EMIT graphicStrutSizeChanged();
}
QSize QskGraphicLabel::sourceSize() const
QSizeF QskGraphicLabel::graphicStrutSize() const
{
return m_data->sourceSize;
return strutSizeHint( Graphic );
}
void QskGraphicLabel::resetSourceSize()
void QskGraphicLabel::resetGraphicStrutSize()
{
setSourceSize( QSize( -1, -1 ) );
if ( resetStrutSizeHint( Graphic ) )
Q_EMIT graphicStrutSizeChanged();
}
void QskGraphicLabel::setFillMode( FillMode mode )
@ -271,12 +261,12 @@ void QskGraphicLabel::updateResources()
QSizeF QskGraphicLabel::effectiveSourceSize() const
{
const auto& sourceSize = m_data->sourceSize;
const auto strutSize = graphicStrutSize();
if ( sourceSize.width() >= 0 && sourceSize.height() >= 0 )
if ( strutSize.width() >= 0 && strutSize.height() >= 0 )
{
// the size has been explicitly set
return sourceSize;
return strutSize;
}
if ( !m_data->source.isEmpty() && m_data->isSourceDirty )
@ -291,23 +281,23 @@ QSizeF QskGraphicLabel::effectiveSourceSize() const
{
const QSizeF defaultSize = m_data->graphic.defaultSize();
if ( sourceSize.width() <= 0 && sourceSize.height() <= 0 )
if ( strutSize.width() <= 0 && strutSize.height() <= 0 )
{
// size is derived from the default size
sz = defaultSize;
}
else if ( sourceSize.width() <= 0 )
else if ( strutSize.width() <= 0 )
{
// only the height has been given
const qreal f = sourceSize.height() / defaultSize.height();
const qreal f = strutSize.height() / defaultSize.height();
sz.setWidth( f * defaultSize.width() );
sz.setHeight( sourceSize.height() );
sz.setHeight( strutSize.height() );
}
else
{
// only the width has been given
const qreal f = sourceSize.width() / defaultSize.width();
sz.setWidth( sourceSize.width() );
const qreal f = strutSize.width() / defaultSize.width();
sz.setWidth( strutSize.width() );
sz.setHeight( f * defaultSize.height() );
}
}

View File

@ -19,8 +19,9 @@ class QSK_EXPORT QskGraphicLabel : public QskControl
Q_PROPERTY( bool mirror READ mirror WRITE setMirror NOTIFY mirrorChanged )
Q_PROPERTY( QSize sourceSize READ sourceSize
WRITE setSourceSize RESET resetSourceSize NOTIFY sourceSizeChanged )
Q_PROPERTY( QSizeF graphicStrutSize READ graphicStrutSize
WRITE setGraphicStrutSize RESET resetGraphicStrutSize
NOTIFY graphicStrutSizeChanged )
Q_PROPERTY( int graphicRole READ graphicRole
WRITE setGraphicRole RESET resetGraphicRole NOTIFY graphicRoleChanged )
@ -66,9 +67,10 @@ class QSK_EXPORT QskGraphicLabel : public QskControl
void setSource( const QString& source );
void setSource( const QUrl& url );
void setSourceSize( const QSize& size );
void resetSourceSize();
QSize sourceSize() const;
void setGraphicStrutSize( const QSizeF& size );
QSizeF graphicStrutSize() const;
void resetGraphicStrutSize();
QSizeF effectiveSourceSize() const;
void setMirror( bool on );
@ -93,7 +95,7 @@ class QSK_EXPORT QskGraphicLabel : public QskControl
Q_SIGNALS:
void sourceChanged();
void mirrorChanged();
void sourceSizeChanged();
void graphicStrutSizeChanged();
void graphicRoleChanged( int );
void alignmentChanged( Qt::Alignment );
void fillModeChanged( FillMode );

View File

@ -49,8 +49,6 @@ class QskPushButton::PrivateData
QUrl graphicSource;
QskGraphic graphic;
QSizeF graphicSourceSize;
bool isCheckable : 1;
bool isGraphicSourceDirty : 1;
};
@ -140,15 +138,16 @@ QskTextOptions QskPushButton::textOptions() const
QFont QskPushButton::font() const
{
return effectiveFont( QskPushButton::Text );
return effectiveFont( Text );
}
void QskPushButton::resetGraphicSourceSize()
void QskPushButton::resetGraphicStrutSize()
{
setGraphicSourceSize( QSizeF( -1.0, -1.0 ) );
if ( resetStrutSizeHint( Graphic ) )
Q_EMIT graphicStrutSizeChanged();
}
void QskPushButton::setGraphicSourceSize( const QSizeF& size )
void QskPushButton::setGraphicStrutSize( const QSizeF& size )
{
auto newSize = size;
if ( newSize.width() < 0.0 )
@ -157,21 +156,13 @@ void QskPushButton::setGraphicSourceSize( const QSizeF& size )
if ( newSize.height() < 0.0 )
newSize.setHeight( -1.0 );
if ( size != m_data->graphicSourceSize )
{
m_data->graphicSourceSize = size;
resetImplicitSize();
polish();
update();
Q_EMIT graphicSourceSizeChanged();
}
if ( setStrutSizeHint( Graphic, newSize ) )
Q_EMIT graphicStrutSizeChanged();
}
QSizeF QskPushButton::graphicSourceSize() const
QSizeF QskPushButton::graphicStrutSize() const
{
return m_data->graphicSourceSize;
return strutSizeHint( Graphic );
}
void QskPushButton::setGraphicSource( const QUrl& url )

View File

@ -29,9 +29,9 @@ class QSK_EXPORT QskPushButton : public QskAbstractButton
Q_PROPERTY( QskGraphic graphic READ graphic
WRITE setGraphic NOTIFY graphicChanged FINAL )
Q_PROPERTY( QSizeF graphicSourceSize READ graphicSourceSize
WRITE setGraphicSourceSize RESET resetGraphicSourceSize
NOTIFY graphicSourceSizeChanged FINAL )
Q_PROPERTY( QSizeF graphicStrutSize READ graphicStrutSize
WRITE setGraphicStrutSize RESET resetGraphicStrutSize
NOTIFY graphicStrutSizeChanged FINAL )
Q_PROPERTY( bool checkable READ isCheckable
WRITE setCheckable NOTIFY checkableChanged FINAL )
@ -61,13 +61,14 @@ class QSK_EXPORT QskPushButton : public QskAbstractButton
void setTextOptions( const QskTextOptions& );
QskTextOptions textOptions() const;
void setGraphicStrutSize( const QSizeF& );
QSizeF graphicStrutSize() const;
void resetGraphicStrutSize();
QUrl graphicSource() const;
QSizeF graphicSourceSize() const;
QskGraphic graphic() const;
bool hasGraphic() const;
void resetGraphicSourceSize();
QFont font() const;
QRectF layoutRectForSize( const QSizeF& ) const override;
@ -77,16 +78,17 @@ class QSK_EXPORT QskPushButton : public QskAbstractButton
void setGraphicSource( const QUrl& );
void setGraphicSource( const QString& );
void setGraphic( const QskGraphic& );
void setGraphicSourceSize( const QSizeF& );
Q_SIGNALS:
void checkableChanged( bool );
void shapeChanged();
void textChanged();
void textOptionsChanged();
void graphicChanged();
void graphicSourceChanged();
void graphicSourceSizeChanged();
void graphicStrutSizeChanged();
protected:
void changeEvent( QEvent* ) override;

View File

@ -148,7 +148,7 @@ QRectF QskPushButtonSkinlet::graphicRect(
r.setBottom( r.bottom() - h );
}
const auto maxSize = button->graphicSourceSize();
const auto maxSize = button->strutSizeHint( Q::Graphic );
if ( maxSize.width() >= 0 || maxSize.height() >= 0 )
{
@ -278,7 +278,7 @@ QSizeF QskPushButtonSkinlet::sizeHint( const QskSkinnable* skinnable,
if ( button->hasGraphic() )
{
const auto sz = button->graphicSourceSize();
const auto sz = button->strutSizeHint( Q::Graphic );
qreal w = sz.width();
qreal h = sz.height();

View File

@ -42,25 +42,25 @@ namespace
initSizePolicy( QskSizePolicy::Fixed, QskSizePolicy::Fixed );
setAlignment( Qt::AlignTop | Qt::AlignHCenter );
updateSourceSize();
updatePreferredSize();
}
protected:
void changeEvent( QEvent* event ) override
{
if ( event->type() == QEvent::FontChange )
updateSourceSize();
updatePreferredSize();
QskGraphicLabel::changeEvent( event );
}
private:
void updateSourceSize()
void updatePreferredSize()
{
// when there is no explicit size known,
// we always adjust the icon according to the font
if ( sourceSize().isEmpty() )
if ( graphicStrutSize().isEmpty() )
{
const QFont font = effectiveFont( QskTextLabel::Text );
setPreferredSize( -1.0, 1.5 * QFontMetricsF( font ).height() );

View File

@ -43,25 +43,25 @@ namespace
initSizePolicy( QskSizePolicy::Fixed, QskSizePolicy::Fixed );
setAlignment( Qt::AlignTop | Qt::AlignHCenter );
updateSourceSize();
updatePreferredSize();
}
protected:
void changeEvent( QEvent* event ) override
{
if ( event->type() == QEvent::FontChange )
updateSourceSize();
updatePreferredSize();
QskGraphicLabel::changeEvent( event );
}
private:
void updateSourceSize()
void updatePreferredSize()
{
// when there is no explicit size known,
// we always adjust the icon according to the font
if ( sourceSize().isEmpty() )
if ( graphicStrutSize().isEmpty() )
{
const QFont font = effectiveFont( QskTextLabel::Text );
setPreferredSize( -1.0, 1.5 * QFontMetricsF( font ).height() );