From 74e036c35576b5e7285100efd8d6a782ab6acf63 Mon Sep 17 00:00:00 2001 From: Peter Hartmann Date: Wed, 15 Jun 2022 10:34:51 +0200 Subject: [PATCH] QskPushButton: respect the graphics alignment (#193) Also, respect the padding hint of the graphic Resolves #22 --- skins/material/QskMaterialSkin.cpp | 3 ++ skins/squiek/QskSquiekSkin.cpp | 5 +- src/common/QskAspect.h | 1 + src/controls/QskPushButtonSkinlet.cpp | 77 ++++++++++++++++++++++----- 4 files changed, 72 insertions(+), 14 deletions(-) diff --git a/skins/material/QskMaterialSkin.cpp b/skins/material/QskMaterialSkin.cpp index 4c8433f2..ac13de2c 100644 --- a/skins/material/QskMaterialSkin.cpp +++ b/skins/material/QskMaterialSkin.cpp @@ -428,6 +428,7 @@ void Editor::setupPushButton() using namespace QskRgb; using Q = QskPushButton; + setFlagHint( Q::Panel | QskAspect::Direction, Qsk::LeftToRight ); setStrutSize( Q::Panel, qskDpiScaled( 75.0 ), qskDpiScaled( 23.0 ) ); setSpacing( Q::Panel, 4 ); @@ -474,6 +475,8 @@ void Editor::setupPushButton() } } + setPadding( Q::Graphic, 5 ); + setAnimation( Q::Panel | A::Color, qskDuration ); setAnimation( Q::Panel | A::Metric, qskDuration ); setAnimation( Q::Text | A::Color, qskDuration ); diff --git a/skins/squiek/QskSquiekSkin.cpp b/skins/squiek/QskSquiekSkin.cpp index 5a729df0..8d9a6173 100644 --- a/skins/squiek/QskSquiekSkin.cpp +++ b/skins/squiek/QskSquiekSkin.cpp @@ -566,7 +566,7 @@ void Editor::setupPushButton() using Q = QskPushButton; // Panel - + setFlagHint( Q::Panel | QskAspect::Direction, Qsk::TopToBottom ); setStrutSize( Q::Panel, qskDpiScaled( 75.0 ), qskDpiScaled( 23.0 ) ); setMargin( Q::Panel, 0 ); @@ -594,6 +594,9 @@ void Editor::setupPushButton() setColor( Q::Text, m_pal.themeForeground ); setColor( Q::Text | Q::Disabled, m_pal.darker200 ); + + // Graphic + setPadding( Q::Graphic, 2 ); } void Editor::setupDialogButton() diff --git a/src/common/QskAspect.h b/src/common/QskAspect.h index 4b5f03bc..2819ae2b 100644 --- a/src/common/QskAspect.h +++ b/src/common/QskAspect.h @@ -32,6 +32,7 @@ class QSK_EXPORT QskAspect NoPrimitive = 0, Alignment, + Direction, Style, GraphicRole, FontRole, diff --git a/src/controls/QskPushButtonSkinlet.cpp b/src/controls/QskPushButtonSkinlet.cpp index 6063d9eb..b7fd445c 100644 --- a/src/controls/QskPushButtonSkinlet.cpp +++ b/src/controls/QskPushButtonSkinlet.cpp @@ -77,11 +77,24 @@ QRectF QskPushButtonSkinlet::textRect( if ( button->hasGraphic() ) { - // in case of having text + graphic we put the text at the bottom + const auto orientation = button->flagHint( QskPushButton::Panel | QskAspect::Direction, Qsk::LeftToRight ); - qreal h = button->effectiveFontHeight( QskPushButton::Text ); - if ( h < r.height() ) - r.setTop( r.bottom() - h ); + switch( orientation ) + { + case Qsk::LeftToRight: + { + const auto graphicsRect = subControlRect( button, contentsRect, QskPushButton::Graphic ); + const auto spacing = button->metric( QskPushButton::Panel | QskAspect::Spacing ); + r.setX( r.x() + graphicsRect.width() + spacing ); + break; + } + default: // Qsk::TopToBottom, the other ones are not handled yet + { + qreal h = button->effectiveFontHeight( QskPushButton::Text ); + if ( h < r.height() ) + r.setTop( r.bottom() - h ); + } + } } return r; @@ -94,7 +107,10 @@ QRectF QskPushButtonSkinlet::graphicRect( auto r = button->subControlContentsRect( contentsRect, QskPushButton::Panel ); - if ( !button->text().isEmpty() ) + const auto orientation = button->flagHint( QskPushButton::Panel + | QskAspect::Direction, Qsk::LeftToRight ); + + if ( !button->text().isEmpty() && orientation == Qsk::TopToBottom ) { const auto textRect = subControlRect( button, contentsRect, QskPushButton::Text ); qreal h = textRect.height() + button->spacingHint( QskPushButton::Panel ); @@ -106,6 +122,7 @@ QRectF QskPushButtonSkinlet::graphicRect( } const auto maxSize = button->graphicSourceSize(); + if ( maxSize.width() >= 0 || maxSize.height() >= 0 ) { // limiting the size by graphicSize @@ -134,15 +151,34 @@ QRectF QskPushButtonSkinlet::graphicRect( const double scaleFactor = qMin( r.width() / sz.width(), r.height() / sz.height() ); - // early aligning to avoid pointless operations, that finally will - // have no effect, when drawing to an integer based paint device - const int w = qFloor( scaleFactor * sz.width() ); const int h = qFloor( scaleFactor * sz.height() ); - const int x = qFloor( r.center().x() - 0.5 * w ); - const int y = qFloor( r.center().y() - 0.5 * h ); + int x, y; + + const auto orientation = button->flagHint( QskPushButton::Panel + | QskAspect::Direction, Qsk::LeftToRight ); + + switch( orientation ) + { + case Qsk::LeftToRight: + { + x = r.left(); + y = r.top(); + break; + } + default: // TopToBottom + { + // early aligning to avoid pointless operations, that finally will + // have no effect, when drawing to an integer based paint device + + x = qFloor( r.center().x() - 0.5 * w ); + y = qFloor( r.center().y() - 0.5 * h ); + } + } r = QRectF( x, y, w, h ); + const auto padding = button->paddingHint( QskPushButton::Graphic ); + r = r.marginsRemoved( padding ); } return r; @@ -213,10 +249,25 @@ QSizeF QskPushButtonSkinlet::sizeHint( const QskSkinnable* skinnable, } } - const qreal padding = 2.0; // paddingHint( Graphic ) ??? + const QMarginsF padding = button->paddingHint( QskPushButton::Graphic ); - size.rheight() += 2 * padding + h; - size.rwidth() = qMax( size.width(), w ); + const auto orientation = button->flagHint( QskPushButton::Panel + | QskAspect::Direction, Qsk::LeftToRight ); + + switch( orientation ) + { + case Qsk::LeftToRight: + { + size.rwidth() += padding.left() + w + padding.right(); + size.rheight() = qMax( size.height(), h ); + break; + } + default: // TopToBottom + { + size.rheight() += padding.top() + h + padding.bottom(); + size.rwidth() = qMax( size.width(), w ); + } + } } size = size.expandedTo( button->strutSizeHint( QskPushButton::Panel ) );