[Icon Item] Fix updating implicit size when icon sizes change

The old code was connecting the KIconLoader signal to implicitWidth/HeightChanged
but never actually set a new implicit size.
Also, while at it use "setImplicitSize" - it's also marked internal but public
like setSize and uses qreal instead of QSizeF.

Differential Revision: https://phabricator.kde.org/D4011
This commit is contained in:
Kai Uwe Broulik 2017-01-08 16:16:59 +01:00
parent 4b55493568
commit 8875a04a1b
4 changed files with 44 additions and 7 deletions

View File

@ -480,5 +480,37 @@ void IconItemTest::paintedSize()
QCOMPARE(item->property("paintedHeight").toInt(), 400); QCOMPARE(item->property("paintedHeight").toInt(), 400);
} }
void IconItemTest::implicitSize()
{
KConfigGroup cg(KSharedConfig::openConfig(), "DialogIcons");
cg.writeEntry("Size", 22);
cg.sync();
KIconLoader::global()->reconfigure(QString());
QQuickItem *item = createIconItem();
// qreal cast needed as QTest::qCompare<double, int> fails to link
QCOMPARE(item->implicitWidth(), qreal(22));
QCOMPARE(item->implicitHeight(), qreal(22));
QSignalSpy widthSpy(item, &QQuickItem::implicitWidthChanged);
QVERIFY(widthSpy.isValid());
QSignalSpy heightSpy(item, &QQuickItem::implicitHeightChanged);
QVERIFY(heightSpy.isValid());
cg.writeEntry("Size", 64);
cg.sync();
KIconLoader::global()->reconfigure(QString());
// merely changing the setting and calling reconfigure won't emit this signal,
// the KCM uses a method "newIconLoader" method which does that but it's deprecated
emit KIconLoader::global()->iconLoaderSettingsChanged();
QCOMPARE(widthSpy.count(), 1);
QCOMPARE(heightSpy.count(), 1);
QCOMPARE(item->implicitWidth(), qreal(64));
QCOMPARE(item->implicitHeight(), qreal(64));
}
QTEST_MAIN(IconItemTest) QTEST_MAIN(IconItemTest)

View File

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

View File

@ -62,10 +62,8 @@ IconItem::IconItem(QQuickItem *parent)
setFlag(ItemHasContents, true); setFlag(ItemHasContents, true);
connect(KIconLoader::global(), SIGNAL(iconLoaderSettingsChanged()), connect(KIconLoader::global(), &KIconLoader::iconLoaderSettingsChanged,
this, SIGNAL(implicitWidthChanged())); this, &IconItem::updateImplicitSize);
connect(KIconLoader::global(), SIGNAL(iconLoaderSettingsChanged()),
this, SIGNAL(implicitHeightChanged()));
connect(this, &QQuickItem::enabledChanged, connect(this, &QQuickItem::enabledChanged,
this, &IconItem::enabledChanged); this, &IconItem::enabledChanged);
@ -76,15 +74,20 @@ IconItem::IconItem(QQuickItem *parent)
connect(this, SIGNAL(overlaysChanged()), connect(this, SIGNAL(overlaysChanged()),
this, SLOT(schedulePixmapUpdate())); this, SLOT(schedulePixmapUpdate()));
//initialize implicit size to the Dialog size updateImplicitSize();
setImplicitWidth(KIconLoader::global()->currentSize(KIconLoader::Dialog));
setImplicitHeight(KIconLoader::global()->currentSize(KIconLoader::Dialog));
} }
IconItem::~IconItem() IconItem::~IconItem()
{ {
} }
void IconItem::updateImplicitSize()
{
//initialize implicit size to the Dialog size
const int implicitSize = KIconLoader::global()->currentSize(KIconLoader::Dialog);
setImplicitSize(implicitSize, implicitSize);
}
void IconItem::setSource(const QVariant &source) void IconItem::setSource(const QVariant &source)
{ {
if (source == m_source) { if (source == m_source) {

View File

@ -177,6 +177,7 @@ private Q_SLOTS:
private: private:
void loadPixmap(); void loadPixmap();
QSize paintedSize(const QSizeF &containerSize = QSizeF()) const; QSize paintedSize(const QSizeF &containerSize = QSizeF()) const;
void updateImplicitSize();
//all the ways we can set an source. Only one of them will be valid //all the ways we can set an source. Only one of them will be valid
QIcon m_icon; QIcon m_icon;