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" ); setGraphicSource( "down" );
} }
setGraphicSourceSize( graphic().defaultSize() * 1.2 ); setGraphicStrutSize( graphic().defaultSize() * 1.2 );
} }
#include "moc_RoundButton.cpp" #include "moc_RoundButton.cpp"

View File

@ -19,7 +19,6 @@ class QskGraphicLabel::PrivateData
public: public:
PrivateData( const QUrl& sourceUrl ) PrivateData( const QUrl& sourceUrl )
: source( sourceUrl ) : source( sourceUrl )
, sourceSize( -1, -1 )
, fillMode( QskGraphicLabel::PreserveAspectFit ) , fillMode( QskGraphicLabel::PreserveAspectFit )
, mirror( false ) , mirror( false )
, isSourceDirty( !sourceUrl.isEmpty() ) , isSourceDirty( !sourceUrl.isEmpty() )
@ -28,8 +27,6 @@ class QskGraphicLabel::PrivateData
} }
QUrl source; QUrl source;
QSize sourceSize;
QskGraphic graphic; QskGraphic graphic;
uint fillMode : 2; uint fillMode : 2;
@ -126,7 +123,7 @@ void QskGraphicLabel::setGraphic( const QskGraphic& graphic )
{ {
if ( m_data->graphic != 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.defaultSize() == graphic.defaultSize() );
m_data->graphic = graphic; m_data->graphic = graphic;
@ -178,7 +175,7 @@ void QskGraphicLabel::setMirror( bool on )
{ {
m_data->mirror = on; m_data->mirror = on;
if ( !( m_data->sourceSize.isEmpty() || m_data->graphic.isEmpty() ) ) if ( !( graphicStrutSize().isEmpty() || m_data->graphic.isEmpty() ) )
update(); update();
Q_EMIT mirrorChanged(); Q_EMIT mirrorChanged();
@ -190,35 +187,28 @@ bool QskGraphicLabel::mirror() const
return m_data->mirror; 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 ) if ( newSize.height() < 0.0 )
sz.setWidth( -1 ); newSize.setHeight( -1.0 );
if ( sz.height() < 0 ) if ( setStrutSizeHint( Graphic, newSize ) )
sz.setHeight( -1 ); Q_EMIT graphicStrutSizeChanged();
if ( m_data->sourceSize != sz )
{
m_data->sourceSize = sz;
resetImplicitSize();
update();
Q_EMIT sourceSizeChanged();
}
} }
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 ) void QskGraphicLabel::setFillMode( FillMode mode )
@ -271,12 +261,12 @@ void QskGraphicLabel::updateResources()
QSizeF QskGraphicLabel::effectiveSourceSize() const 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 // the size has been explicitly set
return sourceSize; return strutSize;
} }
if ( !m_data->source.isEmpty() && m_data->isSourceDirty ) if ( !m_data->source.isEmpty() && m_data->isSourceDirty )
@ -291,23 +281,23 @@ QSizeF QskGraphicLabel::effectiveSourceSize() const
{ {
const QSizeF defaultSize = m_data->graphic.defaultSize(); 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 // size is derived from the default size
sz = defaultSize; sz = defaultSize;
} }
else if ( sourceSize.width() <= 0 ) else if ( strutSize.width() <= 0 )
{ {
// only the height has been given // 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.setWidth( f * defaultSize.width() );
sz.setHeight( sourceSize.height() ); sz.setHeight( strutSize.height() );
} }
else else
{ {
// only the width has been given // only the width has been given
const qreal f = sourceSize.width() / defaultSize.width(); const qreal f = strutSize.width() / defaultSize.width();
sz.setWidth( sourceSize.width() ); sz.setWidth( strutSize.width() );
sz.setHeight( f * defaultSize.height() ); 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( bool mirror READ mirror WRITE setMirror NOTIFY mirrorChanged )
Q_PROPERTY( QSize sourceSize READ sourceSize Q_PROPERTY( QSizeF graphicStrutSize READ graphicStrutSize
WRITE setSourceSize RESET resetSourceSize NOTIFY sourceSizeChanged ) WRITE setGraphicStrutSize RESET resetGraphicStrutSize
NOTIFY graphicStrutSizeChanged )
Q_PROPERTY( int graphicRole READ graphicRole Q_PROPERTY( int graphicRole READ graphicRole
WRITE setGraphicRole RESET resetGraphicRole NOTIFY graphicRoleChanged ) WRITE setGraphicRole RESET resetGraphicRole NOTIFY graphicRoleChanged )
@ -66,9 +67,10 @@ class QSK_EXPORT QskGraphicLabel : public QskControl
void setSource( const QString& source ); void setSource( const QString& source );
void setSource( const QUrl& url ); void setSource( const QUrl& url );
void setSourceSize( const QSize& size ); void setGraphicStrutSize( const QSizeF& size );
void resetSourceSize(); QSizeF graphicStrutSize() const;
QSize sourceSize() const; void resetGraphicStrutSize();
QSizeF effectiveSourceSize() const; QSizeF effectiveSourceSize() const;
void setMirror( bool on ); void setMirror( bool on );
@ -93,7 +95,7 @@ class QSK_EXPORT QskGraphicLabel : public QskControl
Q_SIGNALS: Q_SIGNALS:
void sourceChanged(); void sourceChanged();
void mirrorChanged(); void mirrorChanged();
void sourceSizeChanged(); void graphicStrutSizeChanged();
void graphicRoleChanged( int ); void graphicRoleChanged( int );
void alignmentChanged( Qt::Alignment ); void alignmentChanged( Qt::Alignment );
void fillModeChanged( FillMode ); void fillModeChanged( FillMode );

View File

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

View File

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

View File

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

View File

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

View File

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