From eb614457ee165450354404003ef0c24ef023a289 Mon Sep 17 00:00:00 2001 From: Marco Bubke Date: Tue, 26 Jan 2021 17:05:27 +0100 Subject: [PATCH] QmlDesigner: Do only clean image cache if the target changed Task-number: QDS-3388 Change-Id: I159f70b2bc2f874203392befea566e4c52de7347 Reviewed-by: Thomas Hartmann --- .../itemlibrary/itemlibraryview.cpp | 44 +++++++++++++++---- .../itemlibrary/itemlibrarywidget.cpp | 7 ++- .../imagecache/imagecachecollector.cpp | 5 +++ .../imagecache/imagecachecollector.h | 1 + 4 files changed, 45 insertions(+), 12 deletions(-) diff --git a/src/plugins/qmldesigner/components/itemlibrary/itemlibraryview.cpp b/src/plugins/qmldesigner/components/itemlibrary/itemlibraryview.cpp index fe82bddb4bf..2117eb9e952 100644 --- a/src/plugins/qmldesigner/components/itemlibrary/itemlibraryview.cpp +++ b/src/plugins/qmldesigner/components/itemlibrary/itemlibraryview.cpp @@ -39,6 +39,8 @@ #include #include #include +#include +#include #include #include #include @@ -49,6 +51,16 @@ namespace QmlDesigner { +namespace { +ProjectExplorer::Target *activeTarget(ProjectExplorer::Project *project) +{ + if (project) + return project->activeTarget(); + + return {}; +} +} // namespace + class ImageCacheData { public: @@ -72,9 +84,32 @@ ItemLibraryView::ItemLibraryView(QObject* parent) { m_imageCacheData = std::make_unique(); + + auto setTargetInImageCache = + [imageCacheData = m_imageCacheData.get()](ProjectExplorer::Target *target) { + if (target == imageCacheData->collector.target()) + return; + + if (target) + imageCacheData->cache.clean(); + + imageCacheData->collector.setTarget(target); + }; + + if (auto project = ProjectExplorer::SessionManager::startupProject(); project) { + m_imageCacheData->collector.setTarget(project->activeTarget()); + connect(project, &ProjectExplorer::Project::activeTargetChanged, this, setTargetInImageCache); + } + + connect(ProjectExplorer::SessionManager::instance(), + &ProjectExplorer::SessionManager::startupProjectChanged, + this, + [=](ProjectExplorer::Project *project) { setTargetInImageCache(activeTarget(project)); }); } -ItemLibraryView::~ItemLibraryView() = default; +ItemLibraryView::~ItemLibraryView() +{ +} bool ItemLibraryView::hasWidget() const { @@ -101,11 +136,6 @@ WidgetInfo ItemLibraryView::widgetInfo() void ItemLibraryView::modelAttached(Model *model) { AbstractView::modelAttached(model); - auto target = QmlDesignerPlugin::instance()->currentDesignDocument()->currentTarget(); - m_imageCacheData->cache.clean(); - - if (target) - m_imageCacheData->collector.setTarget(target); m_widget->clearSearchFilter(); m_widget->setModel(model); @@ -120,8 +150,6 @@ void ItemLibraryView::modelAboutToBeDetached(Model *model) { model->detachView(m_importManagerView); - m_imageCacheData->collector.setTarget({}); - AbstractView::modelAboutToBeDetached(model); m_widget->setModel(nullptr); diff --git a/src/plugins/qmldesigner/components/itemlibrary/itemlibrarywidget.cpp b/src/plugins/qmldesigner/components/itemlibrary/itemlibrarywidget.cpp index 87ca95f9f83..bbd43986d9e 100644 --- a/src/plugins/qmldesigner/components/itemlibrary/itemlibrarywidget.cpp +++ b/src/plugins/qmldesigner/components/itemlibrary/itemlibrarywidget.cpp @@ -268,6 +268,9 @@ ItemLibraryWidget::ItemLibraryWidget(AsynchronousImageCache &imageCache, connect(dropSupport, &Utils::DropSupport::filesDropped, this, &ItemLibraryWidget::importDroppedFiles); + m_itemViewQuickWidget->engine()->addImageProvider("itemlibrary_preview", + new ItemLibraryIconImageProvider{m_imageCache}); + // init the first load of the QML UI elements reloadQmlSource(); } @@ -337,14 +340,10 @@ void ItemLibraryWidget::delayedUpdateModel() void ItemLibraryWidget::setModel(Model *model) { - m_itemViewQuickWidget->engine()->removeImageProvider("itemlibrary_preview"); m_model = model; if (!model) return; - m_itemViewQuickWidget->engine()->addImageProvider("itemlibrary_preview", - new ItemLibraryIconImageProvider{m_imageCache}); - setItemLibraryInfo(model->metaInfo().itemLibraryInfo()); } diff --git a/src/plugins/qmldesigner/designercore/imagecache/imagecachecollector.cpp b/src/plugins/qmldesigner/designercore/imagecache/imagecachecollector.cpp index 777f4c45063..b9b99d8a9a5 100644 --- a/src/plugins/qmldesigner/designercore/imagecache/imagecachecollector.cpp +++ b/src/plugins/qmldesigner/designercore/imagecache/imagecachecollector.cpp @@ -153,4 +153,9 @@ void ImageCacheCollector::setTarget(ProjectExplorer::Target *target) m_target = target; } +ProjectExplorer::Target *ImageCacheCollector::target() const +{ + return m_target; +} + } // namespace QmlDesigner diff --git a/src/plugins/qmldesigner/designercore/imagecache/imagecachecollector.h b/src/plugins/qmldesigner/designercore/imagecache/imagecachecollector.h index 99f6ecb70c4..a9deaf9bf7d 100644 --- a/src/plugins/qmldesigner/designercore/imagecache/imagecachecollector.h +++ b/src/plugins/qmldesigner/designercore/imagecache/imagecachecollector.h @@ -67,6 +67,7 @@ public: const ImageCache::AuxiliaryData &auxiliaryData) override; void setTarget(ProjectExplorer::Target *target); + ProjectExplorer::Target *target() const; private: ImageCacheConnectionManager &m_connectionManager;