IconItem: Add roundToIconSize property

Disabling this property makes it possible to show icon of arbitrary size.

Differential Revision: https://phabricator.kde.org/D4689
This commit is contained in:
David Rosca 2017-02-26 11:27:58 +01:00
parent c95f6ada5e
commit af2b27d1b8
4 changed files with 66 additions and 4 deletions

View File

@ -514,5 +514,25 @@ void IconItemTest::implicitSize()
QCOMPARE(item->implicitHeight(), qreal(64)); 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) QTEST_MAIN(IconItemTest)

View File

@ -56,6 +56,7 @@ private Q_SLOTS:
void windowChanged(); void windowChanged();
void paintedSize(); void paintedSize();
void implicitSize(); void implicitSize();
void roundToIconSize();
private: private:
QQuickItem *createIconItem(); QQuickItem *createIconItem();

View File

@ -44,6 +44,7 @@ IconItem::IconItem(QQuickItem *parent)
m_active(false), m_active(false),
m_animated(true), m_animated(true),
m_usesPlasmaTheme(true), m_usesPlasmaTheme(true),
m_roundToIconSize(true),
m_textureChanged(false), m_textureChanged(false),
m_sizeChanged(false), m_sizeChanged(false),
m_allowNextAnimation(false), m_allowNextAnimation(false),
@ -305,6 +306,29 @@ void IconItem::setUsesPlasmaTheme(bool usesPlasmaTheme)
emit usesPlasmaThemeChanged(); 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 bool IconItem::isValid() const
{ {
return !m_icon.isNull() || m_svgIcon || !m_imageIcon.isNull(); 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(); const int height = paintedSize.height();
if (width == 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 // 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 // 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 // instead, round the bigger of the two and then downscale the smaller with the ratio
if (width > height) { 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<qreal>(width)))); return QSize(roundedWidth, qRound(height * (roundedWidth / static_cast<qreal>(width))));
} else { } else {
const int roundedHeight = Units::roundToIconSize(height); const int roundedHeight = m_roundToIconSize ? Units::roundToIconSize(height) : height;
return QSize(qRound(width * (roundedHeight / static_cast<qreal>(height))), roundedHeight); return QSize(qRound(width * (roundedHeight / static_cast<qreal>(height))), roundedHeight);
} }
} }
@ -452,7 +480,10 @@ void IconItem::loadPixmap()
return; 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 //final pixmap to paint
QPixmap result; QPixmap result;

View File

@ -97,6 +97,11 @@ class IconItem : public QQuickItem
*/ */
Q_PROPERTY(bool usesPlasmaTheme READ usesPlasmaTheme WRITE setUsesPlasmaTheme NOTIFY usesPlasmaThemeChanged) 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. * True if a valid icon is set. False otherwise.
*/ */
@ -139,6 +144,9 @@ public:
bool usesPlasmaTheme() const; bool usesPlasmaTheme() const;
void setUsesPlasmaTheme(bool usesPlasmaTheme); void setUsesPlasmaTheme(bool usesPlasmaTheme);
bool roundToIconSize() const;
void setRoundToIconSize(bool roundToIconSize);
bool isValid() const; bool isValid() const;
int paintedWidth() const; int paintedWidth() const;
@ -163,6 +171,7 @@ Q_SIGNALS:
void smoothChanged(); void smoothChanged();
void animatedChanged(); void animatedChanged();
void usesPlasmaThemeChanged(); void usesPlasmaThemeChanged();
void roundToIconSizeChanged();
void validChanged(); void validChanged();
void colorGroupChanged(); void colorGroupChanged();
void paintedSizeChanged(); void paintedSizeChanged();
@ -195,6 +204,7 @@ private:
bool m_active; bool m_active;
bool m_animated; bool m_animated;
bool m_usesPlasmaTheme; bool m_usesPlasmaTheme;
bool m_roundToIconSize;
bool m_textureChanged; bool m_textureChanged;
bool m_sizeChanged; bool m_sizeChanged;