forked from qt-creator/qt-creator
Examples/Marketplace: Create grid view with all items on demand
No need to create that at startup. Change-Id: Iff4cc634777fdd6cc9920a60e3260ee6d5a1a619 Reviewed-by: Christian Stenger <christian.stenger@qt.io> Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
@@ -633,11 +633,9 @@ void ListItemDelegate::goon()
|
|||||||
|
|
||||||
SectionedGridView::SectionedGridView(QWidget *parent)
|
SectionedGridView::SectionedGridView(QWidget *parent)
|
||||||
: QStackedWidget(parent)
|
: QStackedWidget(parent)
|
||||||
, m_allItemsView(new Core::GridView(this))
|
|
||||||
{
|
{
|
||||||
auto allItemsModel = new ListModel(this);
|
m_allItemsModel.reset(new ListModel);
|
||||||
allItemsModel->setPixmapFunction(m_pixmapFunction);
|
m_allItemsModel->setPixmapFunction(m_pixmapFunction);
|
||||||
m_filteredAllItemsModel = new Core::ListModelFilter(allItemsModel, this);
|
|
||||||
|
|
||||||
auto area = new QScrollArea(this);
|
auto area = new QScrollArea(this);
|
||||||
area->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
area->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
||||||
@@ -653,15 +651,17 @@ SectionedGridView::SectionedGridView(QWidget *parent)
|
|||||||
area->setWidget(sectionedView);
|
area->setWidget(sectionedView);
|
||||||
|
|
||||||
addWidget(area);
|
addWidget(area);
|
||||||
|
|
||||||
m_allItemsView->setModel(m_filteredAllItemsModel);
|
|
||||||
addWidget(m_allItemsView);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SectionedGridView::~SectionedGridView() = default;
|
SectionedGridView::~SectionedGridView()
|
||||||
|
{
|
||||||
|
clear();
|
||||||
|
}
|
||||||
|
|
||||||
void SectionedGridView::setItemDelegate(QAbstractItemDelegate *delegate)
|
void SectionedGridView::setItemDelegate(QAbstractItemDelegate *delegate)
|
||||||
{
|
{
|
||||||
|
m_itemDelegate = delegate;
|
||||||
|
if (m_allItemsView)
|
||||||
m_allItemsView->setItemDelegate(delegate);
|
m_allItemsView->setItemDelegate(delegate);
|
||||||
for (GridView *view : std::as_const(m_gridViews))
|
for (GridView *view : std::as_const(m_gridViews))
|
||||||
view->setItemDelegate(delegate);
|
view->setItemDelegate(delegate);
|
||||||
@@ -670,18 +670,30 @@ void SectionedGridView::setItemDelegate(QAbstractItemDelegate *delegate)
|
|||||||
void SectionedGridView::setPixmapFunction(const Core::ListModel::PixmapFunction &pixmapFunction)
|
void SectionedGridView::setPixmapFunction(const Core::ListModel::PixmapFunction &pixmapFunction)
|
||||||
{
|
{
|
||||||
m_pixmapFunction = pixmapFunction;
|
m_pixmapFunction = pixmapFunction;
|
||||||
auto allProducts = static_cast<ListModel *>(m_filteredAllItemsModel->sourceModel());
|
m_allItemsModel->setPixmapFunction(pixmapFunction);
|
||||||
allProducts->setPixmapFunction(pixmapFunction);
|
|
||||||
for (ListModel *model : std::as_const(m_sectionModels))
|
for (ListModel *model : std::as_const(m_sectionModels))
|
||||||
model->setPixmapFunction(pixmapFunction);
|
model->setPixmapFunction(pixmapFunction);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SectionedGridView::setSearchString(const QString &searchString)
|
void SectionedGridView::setSearchString(const QString &searchString)
|
||||||
{
|
{
|
||||||
int view = searchString.isEmpty() ? 0 // sectioned view
|
if (searchString.isEmpty()) {
|
||||||
: 1; // search view
|
// back to sectioned view
|
||||||
setCurrentIndex(view);
|
setCurrentIndex(0);
|
||||||
m_filteredAllItemsModel->setSearchString(searchString);
|
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<ListModelFilter *>(m_allItemsView.get()->model());
|
||||||
|
filterModel->setSearchString(searchString);
|
||||||
}
|
}
|
||||||
|
|
||||||
ListModel *SectionedGridView::addSection(const Section §ion, const QList<ListItem *> &items)
|
ListModel *SectionedGridView::addSection(const Section §ion, const QList<ListItem *> &items)
|
||||||
@@ -695,7 +707,7 @@ ListModel *SectionedGridView::addSection(const Section §ion, const QList<Lis
|
|||||||
model->appendItems(items);
|
model->appendItems(items);
|
||||||
|
|
||||||
auto gridView = new SectionGridView(this);
|
auto gridView = new SectionGridView(this);
|
||||||
gridView->setItemDelegate(m_allItemsView->itemDelegate());
|
gridView->setItemDelegate(m_itemDelegate);
|
||||||
gridView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
gridView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
||||||
gridView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
gridView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
||||||
gridView->setModel(model);
|
gridView->setModel(model);
|
||||||
@@ -717,12 +729,11 @@ ListModel *SectionedGridView::addSection(const Section §ion, const QList<Lis
|
|||||||
vbox->insertWidget(position + 1, gridView);
|
vbox->insertWidget(position + 1, gridView);
|
||||||
|
|
||||||
// add the items also to the all products model to be able to search correctly
|
// add the items also to the all products model to be able to search correctly
|
||||||
auto allProducts = static_cast<ListModel *>(m_filteredAllItemsModel->sourceModel());
|
const QSet<ListItem *> allItems = toSet(m_allItemsModel->items());
|
||||||
const QSet<ListItem *> allItems = toSet(allProducts->items());
|
|
||||||
const QList<ListItem *> newItems = filtered(items, [&allItems](ListItem *item) {
|
const QList<ListItem *> newItems = filtered(items, [&allItems](ListItem *item) {
|
||||||
return !allItems.contains(item);
|
return !allItems.contains(item);
|
||||||
});
|
});
|
||||||
allProducts->appendItems(newItems);
|
m_allItemsModel->appendItems(newItems);
|
||||||
|
|
||||||
// only show section label(s) if there is more than one section
|
// only show section label(s) if there is more than one section
|
||||||
m_sectionLabels.at(0)->setVisible(m_sectionLabels.size() > 1);
|
m_sectionLabels.at(0)->setVisible(m_sectionLabels.size() > 1);
|
||||||
@@ -732,14 +743,14 @@ ListModel *SectionedGridView::addSection(const Section §ion, const QList<Lis
|
|||||||
|
|
||||||
void SectionedGridView::clear()
|
void SectionedGridView::clear()
|
||||||
{
|
{
|
||||||
auto allProducts = static_cast<ListModel *>(m_filteredAllItemsModel->sourceModel());
|
m_allItemsModel->clear();
|
||||||
allProducts->clear();
|
|
||||||
qDeleteAll(m_sectionModels);
|
qDeleteAll(m_sectionModels);
|
||||||
qDeleteAll(m_sectionLabels);
|
qDeleteAll(m_sectionLabels);
|
||||||
qDeleteAll(m_gridViews);
|
qDeleteAll(m_gridViews);
|
||||||
m_sectionModels.clear();
|
m_sectionModels.clear();
|
||||||
m_sectionLabels.clear();
|
m_sectionLabels.clear();
|
||||||
m_gridViews.clear();
|
m_gridViews.clear();
|
||||||
|
m_allItemsView.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace Core
|
} // namespace Core
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ public:
|
|||||||
class CORE_EXPORT GridView : public QListView
|
class CORE_EXPORT GridView : public QListView
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit GridView(QWidget *parent);
|
explicit GridView(QWidget *parent = nullptr);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void leaveEvent(QEvent *) final;
|
void leaveEvent(QEvent *) final;
|
||||||
@@ -74,7 +74,7 @@ public:
|
|||||||
|
|
||||||
using PixmapFunction = std::function<QPixmap(QString)>;
|
using PixmapFunction = std::function<QPixmap(QString)>;
|
||||||
|
|
||||||
explicit ListModel(QObject *parent);
|
explicit ListModel(QObject *parent = nullptr);
|
||||||
~ListModel() override;
|
~ListModel() override;
|
||||||
|
|
||||||
void appendItems(const QList<ListItem *> &items);
|
void appendItems(const QList<ListItem *> &items);
|
||||||
@@ -199,9 +199,10 @@ private:
|
|||||||
QMap<Section, Core::ListModel *> m_sectionModels;
|
QMap<Section, Core::ListModel *> m_sectionModels;
|
||||||
QList<QWidget *> m_sectionLabels;
|
QList<QWidget *> m_sectionLabels;
|
||||||
QMap<Section, Core::GridView *> m_gridViews;
|
QMap<Section, Core::GridView *> m_gridViews;
|
||||||
Core::GridView *m_allItemsView = nullptr;
|
std::unique_ptr<Core::ListModel> m_allItemsModel;
|
||||||
Core::ListModelFilter *m_filteredAllItemsModel = nullptr;
|
std::unique_ptr<Core::GridView> m_allItemsView;
|
||||||
Core::ListModel::PixmapFunction m_pixmapFunction;
|
Core::ListModel::PixmapFunction m_pixmapFunction;
|
||||||
|
QAbstractItemDelegate *m_itemDelegate = nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Core
|
} // namespace Core
|
||||||
|
|||||||
Reference in New Issue
Block a user