From b02a8319ec6429b289588539ab3b76e0fd85e00e Mon Sep 17 00:00:00 2001
From: Michael Gregorius <michael.gregorius.git@arcor.de>
Date: Sun, 7 Jan 2024 11:58:51 +0100
Subject: [PATCH 1/2] Fix scaling of PixmapButton in layouts

Fix the scaling of `PixmapButton` when used in layouts. In this case `PixmapButton::sizeHint` is queried which used `devicePixelRatio` to divide the size of the active pixmap. As a result the size is always the same in pixels regardless of the scaling factor of the application. This is fixed by removing the calls.

Example: If the scaling factor is 2 then the pixmap will also report twice the size in pixels and hence request more space in layouts, etc. However, if we divide by the device/pixel ratio then the original size of the image/pixmap will be reported and therefore it will be too small in layouts.

The calls to `devicePixelRatio` have been introduced with pull request #4950 (via commit c3b4d51) which replaced the old Spectrum Analyzer. The pixmaps of the Spectrum Analyzer still look good with this change.

Fixes #7052.
---
 src/gui/widgets/PixmapButton.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/gui/widgets/PixmapButton.cpp b/src/gui/widgets/PixmapButton.cpp
index 13c09c52ee9..23fbd233885 100644
--- a/src/gui/widgets/PixmapButton.cpp
+++ b/src/gui/widgets/PixmapButton.cpp
@@ -131,11 +131,11 @@ QSize PixmapButton::sizeHint() const
 {
 	if( ( model() != nullptr && model()->value() ) || m_pressed )
 	{
-		return m_activePixmap.size() / devicePixelRatio();
+		return m_activePixmap.size();
 	}
 	else 
 	{
-		return m_inactivePixmap.size() / devicePixelRatio();
+		return m_inactivePixmap.size();
 	}
 }
 

From 87062fbb858581849c2d3044d0115e45a5e36c2f Mon Sep 17 00:00:00 2001
From: Michael Gregorius <michael.gregorius.git@arcor.de>
Date: Sun, 7 Jan 2024 12:41:47 +0100
Subject: [PATCH 2/2] Remove duplicated code

Remove duplicate code which checks if the `PixmapButton` is active by introducing the new method `isActive`.

Simplify the drawing code by first determining which pixmap to draw and then to draw the result if it is not null.

Format code in the adjusted areas.
---
 include/PixmapButton.h           |  2 ++
 src/gui/widgets/PixmapButton.cpp | 24 ++++++++++++------------
 2 files changed, 14 insertions(+), 12 deletions(-)

diff --git a/include/PixmapButton.h b/include/PixmapButton.h
index e8f546dc08e..734bd11ae5c 100644
--- a/include/PixmapButton.h
+++ b/include/PixmapButton.h
@@ -56,6 +56,8 @@ class LMMS_EXPORT PixmapButton : public AutomatableButton
 	void mouseReleaseEvent( QMouseEvent * _me ) override;
 	void mouseDoubleClickEvent( QMouseEvent * _me ) override;
 
+private:
+	bool isActive() const;
 
 private:
 	QPixmap m_activePixmap;
diff --git a/src/gui/widgets/PixmapButton.cpp b/src/gui/widgets/PixmapButton.cpp
index 23fbd233885..069acad567c 100644
--- a/src/gui/widgets/PixmapButton.cpp
+++ b/src/gui/widgets/PixmapButton.cpp
@@ -50,20 +50,15 @@ PixmapButton::PixmapButton( QWidget * _parent, const QString & _name ) :
 
 
 
-void PixmapButton::paintEvent( QPaintEvent * )
+void PixmapButton::paintEvent(QPaintEvent*)
 {
-	QPainter p( this );
+	QPainter p(this);
 
-	if( ( model() != nullptr && model()->value() ) || m_pressed )
-	{
-		if( !m_activePixmap.isNull() )
-		{
-			p.drawPixmap( 0, 0, m_activePixmap );
-		}
-	}
-	else if( !m_inactivePixmap.isNull() )
+	QPixmap* pixmapToDraw = isActive() ? &m_activePixmap : &m_inactivePixmap;
+
+	if (!pixmapToDraw->isNull())
 	{
-		p.drawPixmap( 0, 0, m_inactivePixmap );
+		p.drawPixmap(0, 0, *pixmapToDraw);
 	}
 }
 
@@ -129,7 +124,7 @@ void PixmapButton::setInactiveGraphic( const QPixmap & _pm, bool _update )
 
 QSize PixmapButton::sizeHint() const
 {
-	if( ( model() != nullptr && model()->value() ) || m_pressed )
+	if (isActive())
 	{
 		return m_activePixmap.size();
 	}
@@ -140,4 +135,9 @@ QSize PixmapButton::sizeHint() const
 }
 
 
+bool PixmapButton::isActive() const
+{
+	return (model() != nullptr && model()->value()) || m_pressed;
+}
+
 } // namespace lmms::gui