From 86f546dc658106eab9ef489b5473a5407ff8c3c0 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Tue, 25 Apr 2017 17:05:57 +0200 Subject: QQuickIconImage: fix file selectors for named theme icons QQuickIconImage needs to be associated with a QML context to be able to call QQmlContext::resolvedUrl(), which in turn passes it to the URL interceptor aka. QQmlFileSelector. Change-Id: Iff34fb8316c765ac0ff04d5d7a6ab23002b31385 Reviewed-by: Mitch Curtis --- src/quickcontrols2/qquickiconimage.cpp | 12 +++++++-- src/quickcontrols2/qquickiconlabel.cpp | 1 + tests/auto/qquickiconimage/data/fileSelectors.qml | 16 ++++++++++++ .../actions/+testselector/appointment-new.png | Bin 0 -> 1146 bytes .../actions/+testselector/appointment-new@2x.png | Bin 0 -> 256 bytes .../actions/+testselector/appointment-new.png | Bin 0 -> 256 bytes tests/auto/qquickiconimage/resources.qrc | 3 +++ tests/auto/qquickiconimage/tst_qquickiconimage.cpp | 28 +++++++++++++++++++++ 8 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 tests/auto/qquickiconimage/data/fileSelectors.qml create mode 100644 tests/auto/qquickiconimage/icons/testtheme/22x22/actions/+testselector/appointment-new.png create mode 100644 tests/auto/qquickiconimage/icons/testtheme/22x22/actions/+testselector/appointment-new@2x.png create mode 100644 tests/auto/qquickiconimage/icons/testtheme/22x22@2/actions/+testselector/appointment-new.png diff --git a/src/quickcontrols2/qquickiconimage.cpp b/src/quickcontrols2/qquickiconimage.cpp index d392b72a..629fc0cb 100644 --- a/src/quickcontrols2/qquickiconimage.cpp +++ b/src/quickcontrols2/qquickiconimage.cpp @@ -80,8 +80,16 @@ void QQuickIconImagePrivate::updateIcon() const qreal dpr = calculateDevicePixelRatio(); const QIconLoaderEngineEntry *entry = QIconLoaderEngine::entryForSize(icon, size * dpr, qCeil(dpr)); - url = entry ? QUrl::fromLocalFile(entry->filename) : source; - isThemeIcon = entry != nullptr; + + if (entry) { + QQmlContext *context = qmlContext(q); + const QUrl entryUrl = QUrl::fromLocalFile(entry->filename); + url = context ? context->resolvedUrl(entryUrl) : entryUrl; + isThemeIcon = true; + } else { + url = source; + isThemeIcon = false; + } q->load(); updatingIcon = false; diff --git a/src/quickcontrols2/qquickiconlabel.cpp b/src/quickcontrols2/qquickiconlabel.cpp index d1ac8349..61151f28 100644 --- a/src/quickcontrols2/qquickiconlabel.cpp +++ b/src/quickcontrols2/qquickiconlabel.cpp @@ -96,6 +96,7 @@ bool QQuickIconLabelPrivate::createImage() image->setSource(icon->source()); image->setSourceSize(QSize(icon->width(), icon->height())); image->setColor(icon->color()); + QQmlEngine::setContextForObject(image, qmlContext(q)); if (componentComplete) completeComponent(image); return true; diff --git a/tests/auto/qquickiconimage/data/fileSelectors.qml b/tests/auto/qquickiconimage/data/fileSelectors.qml new file mode 100644 index 00000000..e3ed3857 --- /dev/null +++ b/tests/auto/qquickiconimage/data/fileSelectors.qml @@ -0,0 +1,16 @@ +import QtQuick 2.9 +import QtQuick.Controls 2.3 +import QtQuick.Controls.impl 2.3 + +Row { + width: 200 + height: 200 + + IconImage { + name: "appointment-new" + sourceSize: Qt.size(22, 22) + } + Image { + source: "qrc:/icons/testtheme/22x22/actions/appointment-new.png" + } +} diff --git a/tests/auto/qquickiconimage/icons/testtheme/22x22/actions/+testselector/appointment-new.png b/tests/auto/qquickiconimage/icons/testtheme/22x22/actions/+testselector/appointment-new.png new file mode 100644 index 00000000..c6ceca43 Binary files /dev/null and b/tests/auto/qquickiconimage/icons/testtheme/22x22/actions/+testselector/appointment-new.png differ diff --git a/tests/auto/qquickiconimage/icons/testtheme/22x22/actions/+testselector/appointment-new@2x.png b/tests/auto/qquickiconimage/icons/testtheme/22x22/actions/+testselector/appointment-new@2x.png new file mode 100644 index 00000000..f380ebb6 Binary files /dev/null and b/tests/auto/qquickiconimage/icons/testtheme/22x22/actions/+testselector/appointment-new@2x.png differ diff --git a/tests/auto/qquickiconimage/icons/testtheme/22x22@2/actions/+testselector/appointment-new.png b/tests/auto/qquickiconimage/icons/testtheme/22x22@2/actions/+testselector/appointment-new.png new file mode 100644 index 00000000..f380ebb6 Binary files /dev/null and b/tests/auto/qquickiconimage/icons/testtheme/22x22@2/actions/+testselector/appointment-new.png differ diff --git a/tests/auto/qquickiconimage/resources.qrc b/tests/auto/qquickiconimage/resources.qrc index 76757d4e..6558b039 100644 --- a/tests/auto/qquickiconimage/resources.qrc +++ b/tests/auto/qquickiconimage/resources.qrc @@ -4,6 +4,9 @@ icons/testtheme/22x22/actions/appointment-new.png icons/testtheme/22x22/actions/appointment-new@2x.png icons/testtheme/22x22@2/actions/appointment-new.png + icons/testtheme/22x22/actions/+testselector/appointment-new.png + icons/testtheme/22x22/actions/+testselector/appointment-new@2x.png + icons/testtheme/22x22@2/actions/+testselector/appointment-new.png icons/testtheme/index.theme icons/testtheme/appointment-new.svg icons/testtheme/22x22/actions/color-test-original.png diff --git a/tests/auto/qquickiconimage/tst_qquickiconimage.cpp b/tests/auto/qquickiconimage/tst_qquickiconimage.cpp index 86bd2f2c..4e4afb13 100644 --- a/tests/auto/qquickiconimage/tst_qquickiconimage.cpp +++ b/tests/auto/qquickiconimage/tst_qquickiconimage.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -65,6 +66,7 @@ private slots: void svgNoSizes(); void svgSourceBindingSourceSize(); void color(); + void fileSelectors(); private: void setTheme(); @@ -452,6 +454,32 @@ void tst_qquickiconimage::changeSourceSize() iconImage->setSourceSize(sourceSize); } + +void tst_qquickiconimage::fileSelectors() +{ + SKIP_IF_DPR_TOO_HIGH(); + + QQuickView view; + QQmlFileSelector* fileSelector = new QQmlFileSelector(view.engine()); + fileSelector->setExtraSelectors(QStringList() << "testselector"); + view.setSource(testFileUrl("fileSelectors.qml")); + QCOMPARE(view.status(), QQuickView::Ready); + view.show(); + view.requestActivate(); + QVERIFY(QTest::qWaitForWindowActive(&view)); + + QQuickIconImage *iconImage = qobject_cast(view.rootObject()->childItems().at(0)); + QVERIFY(iconImage); + + QQuickItem *image = view.rootObject()->childItems().at(1); + QVERIFY(image); + + QImage iconImageWindowGrab = grabItemToImage(iconImage); + QCOMPARE(iconImageWindowGrab, grabItemToImage(image)); + + QCOMPARE(iconImageWindowGrab.pixelColor(iconImageWindowGrab.width() / 2, iconImageWindowGrab.height() / 2), QColor(Qt::blue)); +} + int main(int argc, char *argv[]) { QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps); -- cgit v1.2.3