forked from qt-creator/qt-creator
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:
@@ -139,7 +139,10 @@ ScrollView {
|
||||
bottomPadding: 0
|
||||
expanded: importExpanded
|
||||
expandOnClick: false
|
||||
onToggleExpand: importExpanded = !importExpanded
|
||||
onToggleExpand: {
|
||||
if (categoryModel.rowCount() > 0)
|
||||
importExpanded = !importExpanded
|
||||
}
|
||||
onShowContextMenu: {
|
||||
importToRemove = importUsed ? "" : importUrl
|
||||
contextMenu.popup()
|
||||
|
@@ -96,6 +96,18 @@ QHash<int, QByteArray> ItemLibraryCategoriesModel::roleNames() const
|
||||
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)
|
||||
{
|
||||
m_categoryList.append(category);
|
||||
|
@@ -48,6 +48,7 @@ public:
|
||||
QHash<int, QByteArray> roleNames() const override;
|
||||
|
||||
void addCategory(ItemLibraryCategory *category);
|
||||
void expandCategories(bool expand = true);
|
||||
|
||||
const QList<QPointer<ItemLibraryCategory>> &categorySections() const;
|
||||
|
||||
|
@@ -78,6 +78,10 @@ bool ItemLibraryCategory::updateItemVisibility(const QString &searchText, bool *
|
||||
hasVisibleItems = true;
|
||||
}
|
||||
|
||||
// expand category if it has an item matching search criteria
|
||||
if (hasVisibleItems && !categoryExpanded())
|
||||
setExpanded(true);
|
||||
|
||||
return hasVisibleItems;
|
||||
}
|
||||
|
||||
|
@@ -77,25 +77,28 @@ QObject *ItemLibraryImport::categoryModel()
|
||||
return &m_categoryModel;
|
||||
}
|
||||
|
||||
void ItemLibraryImport::expandCategories(bool expand)
|
||||
{
|
||||
m_categoryModel.expandCategories(expand);
|
||||
}
|
||||
|
||||
bool ItemLibraryImport::updateCategoryVisibility(const QString &searchText, bool *changed)
|
||||
{
|
||||
bool hasVisibleItems = false;
|
||||
|
||||
bool hasVisibleCategories = false;
|
||||
*changed = false;
|
||||
|
||||
for (const auto &category : m_categoryModel.categorySections()) {
|
||||
bool categoryChanged = false;
|
||||
hasVisibleItems = category->updateItemVisibility(searchText, &categoryChanged);
|
||||
bool hasVisibleItems = category->updateItemVisibility(searchText, &categoryChanged);
|
||||
categoryChanged |= category->setVisible(hasVisibleItems);
|
||||
|
||||
*changed |= categoryChanged;
|
||||
*changed |= hasVisibleItems;
|
||||
|
||||
if (hasVisibleItems)
|
||||
hasVisibleCategories = true;
|
||||
}
|
||||
|
||||
if (*changed)
|
||||
m_categoryModel.resetModel();
|
||||
|
||||
return hasVisibleItems;
|
||||
return hasVisibleCategories;
|
||||
}
|
||||
|
||||
Import ItemLibraryImport::importEntry() const
|
||||
|
@@ -63,6 +63,7 @@ public:
|
||||
void setImportUsed(bool importUsed);
|
||||
void sortCategorySections();
|
||||
void setImportExpanded(bool expanded = true);
|
||||
void expandCategories(bool expand = true);
|
||||
|
||||
static QString userComponentsTitle();
|
||||
|
||||
|
@@ -60,34 +60,28 @@ bool ItemLibraryModel::loadExpandedState(const QString §ionName)
|
||||
|
||||
void ItemLibraryModel::expandAll()
|
||||
{
|
||||
bool changed = false;
|
||||
int i = 0;
|
||||
for (const QPointer<ItemLibraryImport> &import : std::as_const(m_importList)) {
|
||||
if (import->hasCategories() && !import->importExpanded()) {
|
||||
changed = true;
|
||||
if (!import->importExpanded()) {
|
||||
import->setImportExpanded();
|
||||
emit dataChanged(index(i), index(i), {m_roleNames.key("importExpanded")});
|
||||
saveExpandedState(true, import->importUrl());
|
||||
}
|
||||
}
|
||||
|
||||
if (changed) {
|
||||
beginResetModel();
|
||||
endResetModel();
|
||||
import->expandCategories(true);
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
||||
void ItemLibraryModel::collapseAll()
|
||||
{
|
||||
bool changed = false;
|
||||
int i = 0;
|
||||
for (const QPointer<ItemLibraryImport> &import : std::as_const(m_importList)) {
|
||||
if (import->hasCategories() && import->importExpanded()) {
|
||||
changed = true;
|
||||
import->setImportExpanded(false);
|
||||
emit dataChanged(index(i), index(i), {m_roleNames.key("importExpanded")});
|
||||
saveExpandedState(false, import->importUrl());
|
||||
}
|
||||
}
|
||||
if (changed) {
|
||||
beginResetModel();
|
||||
endResetModel();
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -324,9 +318,18 @@ void ItemLibraryModel::updateVisibility(bool *changed)
|
||||
{
|
||||
for (ItemLibraryImport *import : std::as_const(m_importList)) {
|
||||
bool categoryChanged = false;
|
||||
import->updateCategoryVisibility(m_searchText, &categoryChanged);
|
||||
bool hasVisibleItems = import->updateCategoryVisibility(m_searchText, &categoryChanged);
|
||||
|
||||
*changed |= categoryChanged;
|
||||
|
||||
// expand import if it has an item matching search criteria
|
||||
if (hasVisibleItems && !import->importExpanded())
|
||||
import->setImportExpanded();
|
||||
}
|
||||
|
||||
if (changed) {
|
||||
beginResetModel();
|
||||
endResetModel();
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -268,6 +268,7 @@ void ItemLibraryWidget::handleAddImport(int index)
|
||||
m_model->changeImports({import}, {});
|
||||
QmlDesignerPlugin::instance()->currentDesignDocument()->updateSubcomponentManager();
|
||||
m_stackedWidget->setCurrentIndex(0); // switch to the Components view after import is added
|
||||
updateSearch();
|
||||
}
|
||||
|
||||
void ItemLibraryWidget::delayedUpdateModel()
|
||||
|
Reference in New Issue
Block a user