From a813897825ebfcf7bcd7078c4ea75c3a6d6e1bbc Mon Sep 17 00:00:00 2001 From: Miina Puuronen Date: Thu, 24 Jun 2021 14:08:40 +0300 Subject: [PATCH] QmlDesigner: Disable Asset Library context menu when not needed Assets Library context menus options are now enabled only when needed and disabled otherwise. Also some renaming etc. Task-number: QDS-4628 Change-Id: I5b2c4209f8389e51cf3f5e470bc14e5f9ffc1f29 Reviewed-by: Mahmoud Badri Reviewed-by: Miikka Heikkinen --- .../itemLibraryQmlSources/Assets.qml | 4 +++ .../itemlibrary/itemlibraryassetsmodel.cpp | 30 ++++++++++++++++--- .../itemlibrary/itemlibraryassetsmodel.h | 14 +++++++-- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/share/qtcreator/qmldesigner/itemLibraryQmlSources/Assets.qml b/share/qtcreator/qmldesigner/itemLibraryQmlSources/Assets.qml index 6632f07d57b..10a44e3ed79 100644 --- a/share/qtcreator/qmldesigner/itemLibraryQmlSources/Assets.qml +++ b/share/qtcreator/qmldesigner/itemLibraryQmlSources/Assets.qml @@ -33,6 +33,7 @@ import StudioTheme 1.0 as StudioTheme Item { property var selectedAssets: ({}) + property int allExpandedState: 0 DropArea { id: dropArea @@ -81,11 +82,13 @@ Item { StudioControls.MenuItem { text: qsTr("Expand All") + enabled: allExpandedState !== 1 onTriggered: assetsModel.toggleExpandAll(true) } StudioControls.MenuItem { text: qsTr("Collapse All") + enabled: allExpandedState !== 2 onTriggered: assetsModel.toggleExpandAll(false) } } @@ -118,6 +121,7 @@ Item { dirExpanded = !dirExpanded } onShowContextMenu: { + allExpandedState = assetsModel.getAllExpandedState() contextMenu.popup() } diff --git a/src/plugins/qmldesigner/components/itemlibrary/itemlibraryassetsmodel.cpp b/src/plugins/qmldesigner/components/itemlibrary/itemlibraryassetsmodel.cpp index 9b6bd7c3cca..5429264733a 100644 --- a/src/plugins/qmldesigner/components/itemlibrary/itemlibraryassetsmodel.cpp +++ b/src/plugins/qmldesigner/components/itemlibrary/itemlibraryassetsmodel.cpp @@ -44,14 +44,35 @@ namespace QmlDesigner { -void ItemLibraryAssetsModel::saveExpandedState(bool expanded, const QString §ionName) +void ItemLibraryAssetsModel::saveExpandedState(bool expanded, const QString &assetPath) { - m_expandedStateHash.insert(sectionName, expanded); + m_expandedStateHash.insert(assetPath, expanded); } -bool ItemLibraryAssetsModel::loadExpandedState(const QString §ionName) +bool ItemLibraryAssetsModel::loadExpandedState(const QString &assetPath) { - return m_expandedStateHash.value(sectionName, true); + return m_expandedStateHash.value(assetPath, true); +} + +ItemLibraryAssetsModel::DirExpandState ItemLibraryAssetsModel::getAllExpandedState() const +{ + const auto keys = m_expandedStateHash.keys(); + bool allExpanded = true; + bool allCollapsed = true; + for (const QString &assetPath : keys) { + bool expanded = m_expandedStateHash.value(assetPath); + + if (expanded) + allCollapsed = false; + if (!expanded) + allExpanded = false; + + if (!allCollapsed && !allExpanded) + break; + } + + return allExpanded ? DirExpandState::AllExpanded : allCollapsed ? DirExpandState::AllCollapsed + : DirExpandState::SomeExpanded; } void ItemLibraryAssetsModel::toggleExpandAll(bool expand) @@ -200,6 +221,7 @@ void ItemLibraryAssetsModel::setRootPath(const QString &path) ItemLibraryAssetsDir *assetsDir = new ItemLibraryAssetsDir(subDir.path(), currDepth, loadExpandedState(subDir.path()), currAssetsDir); currAssetsDir->addDir(assetsDir); + saveExpandedState(loadExpandedState(assetsDir->dirPath()), assetsDir->dirPath()); isEmpty &= parseDirRecursive(assetsDir, currDepth + 1); } diff --git a/src/plugins/qmldesigner/components/itemlibrary/itemlibraryassetsmodel.h b/src/plugins/qmldesigner/components/itemlibrary/itemlibraryassetsmodel.h index 34d5fa2bc78..75c7132df4f 100644 --- a/src/plugins/qmldesigner/components/itemlibrary/itemlibraryassetsmodel.h +++ b/src/plugins/qmldesigner/components/itemlibrary/itemlibraryassetsmodel.h @@ -69,10 +69,18 @@ public: const QSet &supportedSuffixes() const; const QSet &previewableSuffixes() const; - static void saveExpandedState(bool expanded, const QString §ionName); - static bool loadExpandedState(const QString §ionName); + static void saveExpandedState(bool expanded, const QString &assetPath); + static bool loadExpandedState(const QString &assetPath); + + enum class DirExpandState { + SomeExpanded, + AllExpanded, + AllCollapsed + }; + Q_ENUM(DirExpandState) Q_INVOKABLE void toggleExpandAll(bool expand); + Q_INVOKABLE DirExpandState getAllExpandedState() const; private: SynchronousImageCache &m_fontImageCache; @@ -83,7 +91,7 @@ private: ItemLibraryAssetsDir *m_assetsDir = nullptr; QHash m_roleNames; - inline static QHash m_expandedStateHash; + inline static QHash m_expandedStateHash; // }; } // namespace QmlDesigner