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:
parent
c95f6ada5e
commit
af2b27d1b8
@ -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)
|
||||
|
||||
|
@ -56,6 +56,7 @@ private Q_SLOTS:
|
||||
void windowChanged();
|
||||
void paintedSize();
|
||||
void implicitSize();
|
||||
void roundToIconSize();
|
||||
|
||||
private:
|
||||
QQuickItem *createIconItem();
|
||||
|
@ -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<qreal>(width))));
|
||||
} 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);
|
||||
}
|
||||
}
|
||||
@ -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;
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user