diff --git a/autotests/iconitemtest.h b/autotests/iconitemtest.h --- a/autotests/iconitemtest.h +++ b/autotests/iconitemtest.h @@ -56,6 +56,7 @@ void windowChanged(); void paintedSize(); void implicitSize(); + void roundToIconSize(); private: QQuickItem *createIconItem(); diff --git a/autotests/iconitemtest.cpp b/autotests/iconitemtest.cpp --- a/autotests/iconitemtest.cpp +++ b/autotests/iconitemtest.cpp @@ -512,5 +512,23 @@ 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())); + + item->setProperty("roundToIconSize", false); + + QTRY_COMPARE(paintedSizeSpy.count(), 1); + QVERIFY(item->property("paintedWidth").toInt() == 25); + QVERIFY(item->property("paintedHeight").toInt() == 25); +} + QTEST_MAIN(IconItemTest) diff --git a/src/declarativeimports/core/iconitem.h b/src/declarativeimports/core/iconitem.h --- a/src/declarativeimports/core/iconitem.h +++ b/src/declarativeimports/core/iconitem.h @@ -98,6 +98,11 @@ 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. */ Q_PROPERTY(bool valid READ isValid NOTIFY validChanged) @@ -139,6 +144,9 @@ bool usesPlasmaTheme() const; void setUsesPlasmaTheme(bool usesPlasmaTheme); + bool roundToIconSize() const; + void setRoundToIconSize(bool roundToIconSize); + bool isValid() const; int paintedWidth() const; @@ -163,6 +171,7 @@ void smoothChanged(); void animatedChanged(); void usesPlasmaThemeChanged(); + void roundToIconSizeChanged(); void validChanged(); void colorGroupChanged(); void paintedSizeChanged(); @@ -195,6 +204,7 @@ bool m_active; bool m_animated; bool m_usesPlasmaTheme; + bool m_roundToIconSize; bool m_textureChanged; bool m_sizeChanged; diff --git a/src/declarativeimports/core/iconitem.cpp b/src/declarativeimports/core/iconitem.cpp --- a/src/declarativeimports/core/iconitem.cpp +++ b/src/declarativeimports/core/iconitem.cpp @@ -44,6 +44,7 @@ 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 @@ 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 @@ 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 @@ 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;