From 3afe94777c9a359a01de93a40bfed15144601770 Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Fri, 17 Mar 2023 15:18:59 +0100 Subject: [PATCH] Examples/Marketplace: Create grid view with all items on demand No need to create that at startup. Change-Id: Iff4cc634777fdd6cc9920a60e3260ee6d5a1a619 Reviewed-by: Christian Stenger Reviewed-by: --- src/plugins/coreplugin/welcomepagehelper.cpp | 53 ++++++++++++-------- src/plugins/coreplugin/welcomepagehelper.h | 9 ++-- 2 files changed, 37 insertions(+), 25 deletions(-) diff --git a/src/plugins/coreplugin/welcomepagehelper.cpp b/src/plugins/coreplugin/welcomepagehelper.cpp index d5f09a8c5c6..0e8f1211bf2 100644 --- a/src/plugins/coreplugin/welcomepagehelper.cpp +++ b/src/plugins/coreplugin/welcomepagehelper.cpp @@ -633,11 +633,9 @@ void ListItemDelegate::goon() SectionedGridView::SectionedGridView(QWidget *parent) : QStackedWidget(parent) - , m_allItemsView(new Core::GridView(this)) { - auto allItemsModel = new ListModel(this); - allItemsModel->setPixmapFunction(m_pixmapFunction); - m_filteredAllItemsModel = new Core::ListModelFilter(allItemsModel, this); + m_allItemsModel.reset(new ListModel); + m_allItemsModel->setPixmapFunction(m_pixmapFunction); auto area = new QScrollArea(this); area->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); @@ -653,16 +651,18 @@ SectionedGridView::SectionedGridView(QWidget *parent) area->setWidget(sectionedView); addWidget(area); - - m_allItemsView->setModel(m_filteredAllItemsModel); - addWidget(m_allItemsView); } -SectionedGridView::~SectionedGridView() = default; +SectionedGridView::~SectionedGridView() +{ + clear(); +} void SectionedGridView::setItemDelegate(QAbstractItemDelegate *delegate) { - m_allItemsView->setItemDelegate(delegate); + m_itemDelegate = delegate; + if (m_allItemsView) + m_allItemsView->setItemDelegate(delegate); for (GridView *view : std::as_const(m_gridViews)) view->setItemDelegate(delegate); } @@ -670,18 +670,30 @@ void SectionedGridView::setItemDelegate(QAbstractItemDelegate *delegate) void SectionedGridView::setPixmapFunction(const Core::ListModel::PixmapFunction &pixmapFunction) { m_pixmapFunction = pixmapFunction; - auto allProducts = static_cast(m_filteredAllItemsModel->sourceModel()); - allProducts->setPixmapFunction(pixmapFunction); + m_allItemsModel->setPixmapFunction(pixmapFunction); for (ListModel *model : std::as_const(m_sectionModels)) model->setPixmapFunction(pixmapFunction); } void SectionedGridView::setSearchString(const QString &searchString) { - int view = searchString.isEmpty() ? 0 // sectioned view - : 1; // search view - setCurrentIndex(view); - m_filteredAllItemsModel->setSearchString(searchString); + if (searchString.isEmpty()) { + // back to sectioned view + setCurrentIndex(0); + return; + } + if (!m_allItemsView) { + // We don't have a grid set for searching yet. + // Create all items view for filtering. + m_allItemsView.reset(new GridView); + m_allItemsView->setModel(new ListModelFilter(m_allItemsModel.get(), m_allItemsView.get())); + if (m_itemDelegate) + m_allItemsView->setItemDelegate(m_itemDelegate); + addWidget(m_allItemsView.get()); + } + setCurrentWidget(m_allItemsView.get()); + auto filterModel = static_cast(m_allItemsView.get()->model()); + filterModel->setSearchString(searchString); } ListModel *SectionedGridView::addSection(const Section §ion, const QList &items) @@ -695,7 +707,7 @@ ListModel *SectionedGridView::addSection(const Section §ion, const QListappendItems(items); auto gridView = new SectionGridView(this); - gridView->setItemDelegate(m_allItemsView->itemDelegate()); + gridView->setItemDelegate(m_itemDelegate); gridView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); gridView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); gridView->setModel(model); @@ -717,12 +729,11 @@ ListModel *SectionedGridView::addSection(const Section §ion, const QListinsertWidget(position + 1, gridView); // add the items also to the all products model to be able to search correctly - auto allProducts = static_cast(m_filteredAllItemsModel->sourceModel()); - const QSet allItems = toSet(allProducts->items()); + const QSet allItems = toSet(m_allItemsModel->items()); const QList newItems = filtered(items, [&allItems](ListItem *item) { return !allItems.contains(item); }); - allProducts->appendItems(newItems); + m_allItemsModel->appendItems(newItems); // only show section label(s) if there is more than one section m_sectionLabels.at(0)->setVisible(m_sectionLabels.size() > 1); @@ -732,14 +743,14 @@ ListModel *SectionedGridView::addSection(const Section §ion, const QList(m_filteredAllItemsModel->sourceModel()); - allProducts->clear(); + m_allItemsModel->clear(); qDeleteAll(m_sectionModels); qDeleteAll(m_sectionLabels); qDeleteAll(m_gridViews); m_sectionModels.clear(); m_sectionLabels.clear(); m_gridViews.clear(); + m_allItemsView.reset(); } } // namespace Core diff --git a/src/plugins/coreplugin/welcomepagehelper.h b/src/plugins/coreplugin/welcomepagehelper.h index 77760000e9e..cf9f438aced 100644 --- a/src/plugins/coreplugin/welcomepagehelper.h +++ b/src/plugins/coreplugin/welcomepagehelper.h @@ -40,7 +40,7 @@ public: class CORE_EXPORT GridView : public QListView { public: - explicit GridView(QWidget *parent); + explicit GridView(QWidget *parent = nullptr); protected: void leaveEvent(QEvent *) final; @@ -74,7 +74,7 @@ public: using PixmapFunction = std::function; - explicit ListModel(QObject *parent); + explicit ListModel(QObject *parent = nullptr); ~ListModel() override; void appendItems(const QList &items); @@ -199,9 +199,10 @@ private: QMap m_sectionModels; QList m_sectionLabels; QMap m_gridViews; - Core::GridView *m_allItemsView = nullptr; - Core::ListModelFilter *m_filteredAllItemsModel = nullptr; + std::unique_ptr m_allItemsModel; + std::unique_ptr m_allItemsView; Core::ListModel::PixmapFunction m_pixmapFunction; + QAbstractItemDelegate *m_itemDelegate = nullptr; }; } // namespace Core