From af2b27d1b89b57c857c848e448722cae27d6d7fe Mon Sep 17 00:00:00 2001 From: David Rosca Date: Sun, 26 Feb 2017 11:27:58 +0100 Subject: [PATCH] IconItem: Add roundToIconSize property Disabling this property makes it possible to show icon of arbitrary size. Differential Revision: https://phabricator.kde.org/D4689 --- autotests/iconitemtest.cpp | 20 ++++++++++++ autotests/iconitemtest.h | 1 + src/declarativeimports/core/iconitem.cpp | 39 +++++++++++++++++++++--- src/declarativeimports/core/iconitem.h | 10 ++++++ 4 files changed, 66 insertions(+), 4 deletions(-) diff --git a/autotests/iconitemtest.cpp b/autotests/iconitemtest.cpp index bc7221ce4..16fb9e812 100644 --- a/autotests/iconitemtest.cpp +++ b/autotests/iconitemtest.cpp @@ -514,5 +514,25 @@ void IconItemTest::implicitSize() QCOMPARE(item->implicitHeight(), qreal(64)); } +void IconItemTest::roundToIconSize() +{ + QQuickItem *item = createIconItem(); + + item->setWidth(25); + item->setHeight(25); + QVERIFY(item->property("paintedWidth").toInt() != 25); + QVERIFY(item->property("paintedHeight").toInt() != 25); + + QSignalSpy paintedSizeSpy(item, SIGNAL(paintedSizeChanged())); + QSignalSpy roundToIconSizeSpy(item, SIGNAL(roundToIconSizeChanged())); + + item->setProperty("roundToIconSize", false); + + QTRY_COMPARE(paintedSizeSpy.count(), 1); + QTRY_COMPARE(roundToIconSizeSpy.count(), 1); + QVERIFY(item->property("paintedWidth").toInt() == 25); + QVERIFY(item->property("paintedHeight").toInt() == 25); +} + QTEST_MAIN(IconItemTest) diff --git a/autotests/iconitemtest.h b/autotests/iconitemtest.h index fcf7003a5..842682932 100644 --- a/autotests/iconitemtest.h +++ b/autotests/iconitemtest.h @@ -56,6 +56,7 @@ private Q_SLOTS: void windowChanged(); void paintedSize(); void implicitSize(); + void roundToIconSize(); private: QQuickItem *createIconItem(); diff --git a/src/declarativeimports/core/iconitem.cpp b/src/declarativeimports/core/iconitem.cpp index 4b2480834..1fa13bb14 100644 --- a/src/declarativeimports/core/iconitem.cpp +++ b/src/declarativeimports/core/iconitem.cpp @@ -44,6 +44,7 @@ IconItem::IconItem(QQuickItem *parent) m_active(false), m_animated(true), m_usesPlasmaTheme(true), + m_roundToIconSize(true), m_textureChanged(false), m_sizeChanged(false), m_allowNextAnimation(false), @@ -305,6 +306,29 @@ void IconItem::setUsesPlasmaTheme(bool usesPlasmaTheme) emit usesPlasmaThemeChanged(); } +bool IconItem::roundToIconSize() const +{ + return m_roundToIconSize; +} + +void IconItem::setRoundToIconSize(bool roundToIconSize) +{ + if (m_roundToIconSize == roundToIconSize) { + return; + } + + const QSize oldPaintedSize = paintedSize(); + + m_roundToIconSize = roundToIconSize; + emit roundToIconSizeChanged(); + + if (oldPaintedSize != paintedSize()) { + emit paintedSizeChanged(); + } + + schedulePixmapUpdate(); +} + bool IconItem::isValid() const { return !m_icon.isNull() || m_svgIcon || !m_imageIcon.isNull(); @@ -330,17 +354,21 @@ QSize IconItem::paintedSize(const QSizeF &containerSize) const const int height = paintedSize.height(); if (width == height) { - return QSize(Units::roundToIconSize(width), Units::roundToIconSize(height)); + if (m_roundToIconSize) { + return QSize(Units::roundToIconSize(width), Units::roundToIconSize(height)); + } else { + return QSize(width, height); + } } // if we don't have a square image, we still want it to be rounded to icon size // but we cannot just blindly round both as we might erroneously change a 50x45 image to be 48x32 // instead, round the bigger of the two and then downscale the smaller with the ratio if (width > height) { - const int roundedWidth = Units::roundToIconSize(width); + const int roundedWidth = m_roundToIconSize ? Units::roundToIconSize(width) : width; return QSize(roundedWidth, qRound(height * (roundedWidth / static_cast(width)))); } else { - const int roundedHeight = Units::roundToIconSize(height); + const int roundedHeight = m_roundToIconSize ? Units::roundToIconSize(height) : height; return QSize(qRound(width * (roundedHeight / static_cast(height))), roundedHeight); } } @@ -452,7 +480,10 @@ void IconItem::loadPixmap() return; } - const int size = Units::roundToIconSize(qMin(qRound(width()), qRound(height()))); + int size = qMin(qRound(width()), qRound(height())); + if (m_roundToIconSize) { + size = Units::roundToIconSize(size); + } //final pixmap to paint QPixmap result; diff --git a/src/declarativeimports/core/iconitem.h b/src/declarativeimports/core/iconitem.h index f30d0473d..e4b2a959d 100644 --- a/src/declarativeimports/core/iconitem.h +++ b/src/declarativeimports/core/iconitem.h @@ -97,6 +97,11 @@ class IconItem : public QQuickItem */ Q_PROPERTY(bool usesPlasmaTheme READ usesPlasmaTheme WRITE setUsesPlasmaTheme NOTIFY usesPlasmaThemeChanged) + /** + * If set, icon will round the painted size to defined icon sizes. Default is true. + */ + Q_PROPERTY(bool roundToIconSize READ roundToIconSize WRITE setRoundToIconSize NOTIFY roundToIconSizeChanged) + /** * True if a valid icon is set. False otherwise. */ @@ -139,6 +144,9 @@ public: bool usesPlasmaTheme() const; void setUsesPlasmaTheme(bool usesPlasmaTheme); + bool roundToIconSize() const; + void setRoundToIconSize(bool roundToIconSize); + bool isValid() const; int paintedWidth() const; @@ -163,6 +171,7 @@ Q_SIGNALS: void smoothChanged(); void animatedChanged(); void usesPlasmaThemeChanged(); + void roundToIconSizeChanged(); void validChanged(); void colorGroupChanged(); void paintedSizeChanged(); @@ -195,6 +204,7 @@ private: bool m_active; bool m_animated; bool m_usesPlasmaTheme; + bool m_roundToIconSize; bool m_textureChanged; bool m_sizeChanged;