QmlDesigner: Expand collapsed component categories when searching

- Expand collapsed categories in the following 2 cases:
  - When searching.
  - When choosing "Expand all" from the context menu.
- Update only changed data when expand state changes rather than
resetting the whole model.
- Update search after adding a new QML import and switching to
the components view.

Task-number: QDS-3781
Task-number: QDS-3784
Task-number: QDS-3789
Change-Id: I09e6f1f97171cd9172cadf4202dd8d02cbb78513
Reviewed-by: Miikka Heikkinen <miikka.heikkinen@qt.io>
This commit is contained in:
Mahmoud Badri
2021-02-18 21:14:23 +02:00
parent 01caf29221
commit 80dfaf3be3
8 changed files with 52 additions and 24 deletions

View File

@@ -139,7 +139,10 @@ ScrollView {
bottomPadding: 0 bottomPadding: 0
expanded: importExpanded expanded: importExpanded
expandOnClick: false expandOnClick: false
onToggleExpand: importExpanded = !importExpanded onToggleExpand: {
if (categoryModel.rowCount() > 0)
importExpanded = !importExpanded
}
onShowContextMenu: { onShowContextMenu: {
importToRemove = importUsed ? "" : importUrl importToRemove = importUsed ? "" : importUrl
contextMenu.popup() contextMenu.popup()

View File

@@ -96,6 +96,18 @@ QHash<int, QByteArray> ItemLibraryCategoriesModel::roleNames() const
return m_roleNames; return m_roleNames;
} }
void ItemLibraryCategoriesModel::expandCategories(bool expand)
{
int i = 0;
for (const auto &category : std::as_const(m_categoryList)) {
if (category->categoryExpanded() != expand) {
category->setExpanded(expand);
emit dataChanged(index(i), index(i), {m_roleNames.key("categoryExpanded")});
}
++i;
}
}
void ItemLibraryCategoriesModel::addCategory(ItemLibraryCategory *category) void ItemLibraryCategoriesModel::addCategory(ItemLibraryCategory *category)
{ {
m_categoryList.append(category); m_categoryList.append(category);

View File

@@ -48,6 +48,7 @@ public:
QHash<int, QByteArray> roleNames() const override; QHash<int, QByteArray> roleNames() const override;
void addCategory(ItemLibraryCategory *category); void addCategory(ItemLibraryCategory *category);
void expandCategories(bool expand = true);
const QList<QPointer<ItemLibraryCategory>> &categorySections() const; const QList<QPointer<ItemLibraryCategory>> &categorySections() const;

View File

@@ -78,6 +78,10 @@ bool ItemLibraryCategory::updateItemVisibility(const QString &searchText, bool *
hasVisibleItems = true; hasVisibleItems = true;
} }
// expand category if it has an item matching search criteria
if (hasVisibleItems && !categoryExpanded())
setExpanded(true);
return hasVisibleItems; return hasVisibleItems;
} }

View File

@@ -77,25 +77,28 @@ QObject *ItemLibraryImport::categoryModel()
return &m_categoryModel; return &m_categoryModel;
} }
void ItemLibraryImport::expandCategories(bool expand)
{
m_categoryModel.expandCategories(expand);
}
bool ItemLibraryImport::updateCategoryVisibility(const QString &searchText, bool *changed) bool ItemLibraryImport::updateCategoryVisibility(const QString &searchText, bool *changed)
{ {
bool hasVisibleItems = false; bool hasVisibleCategories = false;
*changed = false; *changed = false;
for (const auto &category : m_categoryModel.categorySections()) { for (const auto &category : m_categoryModel.categorySections()) {
bool categoryChanged = false; bool categoryChanged = false;
hasVisibleItems = category->updateItemVisibility(searchText, &categoryChanged); bool hasVisibleItems = category->updateItemVisibility(searchText, &categoryChanged);
categoryChanged |= category->setVisible(hasVisibleItems); categoryChanged |= category->setVisible(hasVisibleItems);
*changed |= categoryChanged; *changed |= categoryChanged;
*changed |= hasVisibleItems;
if (hasVisibleItems)
hasVisibleCategories = true;
} }
if (*changed) return hasVisibleCategories;
m_categoryModel.resetModel();
return hasVisibleItems;
} }
Import ItemLibraryImport::importEntry() const Import ItemLibraryImport::importEntry() const

View File

@@ -63,6 +63,7 @@ public:
void setImportUsed(bool importUsed); void setImportUsed(bool importUsed);
void sortCategorySections(); void sortCategorySections();
void setImportExpanded(bool expanded = true); void setImportExpanded(bool expanded = true);
void expandCategories(bool expand = true);
static QString userComponentsTitle(); static QString userComponentsTitle();

View File

@@ -60,34 +60,28 @@ bool ItemLibraryModel::loadExpandedState(const QString &sectionName)
void ItemLibraryModel::expandAll() void ItemLibraryModel::expandAll()
{ {
bool changed = false; int i = 0;
for (const QPointer<ItemLibraryImport> &import : std::as_const(m_importList)) { for (const QPointer<ItemLibraryImport> &import : std::as_const(m_importList)) {
if (import->hasCategories() && !import->importExpanded()) { if (!import->importExpanded()) {
changed = true;
import->setImportExpanded(); import->setImportExpanded();
emit dataChanged(index(i), index(i), {m_roleNames.key("importExpanded")});
saveExpandedState(true, import->importUrl()); saveExpandedState(true, import->importUrl());
} }
} import->expandCategories(true);
++i;
if (changed) {
beginResetModel();
endResetModel();
} }
} }
void ItemLibraryModel::collapseAll() void ItemLibraryModel::collapseAll()
{ {
bool changed = false; int i = 0;
for (const QPointer<ItemLibraryImport> &import : std::as_const(m_importList)) { for (const QPointer<ItemLibraryImport> &import : std::as_const(m_importList)) {
if (import->hasCategories() && import->importExpanded()) { if (import->hasCategories() && import->importExpanded()) {
changed = true;
import->setImportExpanded(false); import->setImportExpanded(false);
emit dataChanged(index(i), index(i), {m_roleNames.key("importExpanded")});
saveExpandedState(false, import->importUrl()); saveExpandedState(false, import->importUrl());
} }
} ++i;
if (changed) {
beginResetModel();
endResetModel();
} }
} }
@@ -324,9 +318,18 @@ void ItemLibraryModel::updateVisibility(bool *changed)
{ {
for (ItemLibraryImport *import : std::as_const(m_importList)) { for (ItemLibraryImport *import : std::as_const(m_importList)) {
bool categoryChanged = false; bool categoryChanged = false;
import->updateCategoryVisibility(m_searchText, &categoryChanged); bool hasVisibleItems = import->updateCategoryVisibility(m_searchText, &categoryChanged);
*changed |= categoryChanged; *changed |= categoryChanged;
// expand import if it has an item matching search criteria
if (hasVisibleItems && !import->importExpanded())
import->setImportExpanded();
}
if (changed) {
beginResetModel();
endResetModel();
} }
} }

View File

@@ -268,6 +268,7 @@ void ItemLibraryWidget::handleAddImport(int index)
m_model->changeImports({import}, {}); m_model->changeImports({import}, {});
QmlDesignerPlugin::instance()->currentDesignDocument()->updateSubcomponentManager(); QmlDesignerPlugin::instance()->currentDesignDocument()->updateSubcomponentManager();
m_stackedWidget->setCurrentIndex(0); // switch to the Components view after import is added m_stackedWidget->setCurrentIndex(0); // switch to the Components view after import is added
updateSearch();
} }
void ItemLibraryWidget::delayedUpdateModel() void ItemLibraryWidget::delayedUpdateModel()