ExamplesListModel: Remove direct access of model items variable

Change-Id: I8dc2833f61fe0267953acfa8746151cea893c7f3
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
Eike Ziller
2023-01-16 17:20:56 +01:00
parent e3acf9262b
commit 6c6277bc7d
4 changed files with 55 additions and 28 deletions

View File

@@ -144,9 +144,7 @@ ListModel::ListModel(QObject *parent)
ListModel::~ListModel() ListModel::~ListModel()
{ {
if (m_ownsItems) clear();
qDeleteAll(m_items);
m_items.clear();
} }
void ListModel::appendItems(const QList<ListItem *> &items) void ListModel::appendItems(const QList<ListItem *> &items)
@@ -161,6 +159,15 @@ const QList<ListItem *> ListModel::items() const
return m_items; return m_items;
} }
void ListModel::clear()
{
beginResetModel();
if (m_ownsItems)
qDeleteAll(m_items);
m_items.clear();
endResetModel();
}
int ListModel::rowCount(const QModelIndex &) const int ListModel::rowCount(const QModelIndex &) const
{ {
return m_items.size(); return m_items.size();
@@ -695,6 +702,7 @@ ListModel *SectionedGridView::addSection(const Section &section, const QList<Lis
const auto it = m_gridViews.insert(section, gridView); const auto it = m_gridViews.insert(section, gridView);
auto sectionLabel = new QLabel(section.name); auto sectionLabel = new QLabel(section.name);
m_sectionLabels.append(sectionLabel);
sectionLabel->setContentsMargins(0, Core::WelcomePageHelpers::ItemGap, 0, 0); sectionLabel->setContentsMargins(0, Core::WelcomePageHelpers::ItemGap, 0, 0);
sectionLabel->setFont(Core::WelcomePageHelpers::brandFont()); sectionLabel->setFont(Core::WelcomePageHelpers::brandFont());
auto scrollArea = qobject_cast<QScrollArea *>(widget(0)); auto scrollArea = qobject_cast<QScrollArea *>(widget(0));
@@ -713,4 +721,16 @@ ListModel *SectionedGridView::addSection(const Section &section, const QList<Lis
return model; return model;
} }
void SectionedGridView::clear()
{
auto allProducts = static_cast<ListModel *>(m_filteredAllItemsModel->sourceModel());
allProducts->clear();
qDeleteAll(m_sectionModels);
qDeleteAll(m_sectionLabels);
qDeleteAll(m_gridViews);
m_sectionModels.clear();
m_sectionLabels.clear();
m_gridViews.clear();
}
} // namespace Core } // namespace Core

View File

@@ -79,6 +79,7 @@ public:
void appendItems(const QList<ListItem *> &items); void appendItems(const QList<ListItem *> &items);
const QList<ListItem *> items() const; const QList<ListItem *> items() const;
void clear();
int rowCount(const QModelIndex &parent = QModelIndex()) const final; int rowCount(const QModelIndex &parent = QModelIndex()) const final;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
@@ -88,7 +89,7 @@ public:
void setOwnsItems(bool owns); void setOwnsItems(bool owns);
protected: private:
QList<ListItem *> m_items; QList<ListItem *> m_items;
PixmapFunction m_fetchPixmapAndUpdatePixmapCache; PixmapFunction m_fetchPixmapAndUpdatePixmapCache;
bool m_ownsItems = true; bool m_ownsItems = true;
@@ -192,8 +193,11 @@ public:
Core::ListModel *addSection(const Section &section, const QList<Core::ListItem *> &items); Core::ListModel *addSection(const Section &section, const QList<Core::ListItem *> &items);
void clear();
private: private:
QMap<Section, Core::ListModel *> m_sectionModels; QMap<Section, Core::ListModel *> m_sectionModels;
QList<QWidget *> m_sectionLabels;
QMap<Section, Core::GridView *> m_gridViews; QMap<Section, Core::GridView *> m_gridViews;
Core::GridView *m_allItemsView = nullptr; Core::GridView *m_allItemsView = nullptr;
Core::ListModelFilter *m_filteredAllItemsModel = nullptr; Core::ListModelFilter *m_filteredAllItemsModel = nullptr;

View File

@@ -29,6 +29,7 @@
#include <algorithm> #include <algorithm>
#include <memory> #include <memory>
using namespace Core;
using namespace Utils; using namespace Utils;
namespace QtSupport { namespace QtSupport {
@@ -331,9 +332,11 @@ static bool isValidExampleOrDemo(ExampleItem *item)
return ok || debugExamples(); return ok || debugExamples();
} }
void ExamplesListModel::parseExamples(QXmlStreamReader *reader, static QList<ListItem *> parseExamples(QXmlStreamReader *reader,
const QString &projectsOffset, const QString &examplesInstallPath) const QString &projectsOffset,
const QString &examplesInstallPath)
{ {
QList<ListItem *> result;
std::unique_ptr<ExampleItem> item; std::unique_ptr<ExampleItem> item;
const QChar slash = QLatin1Char('/'); const QChar slash = QLatin1Char('/');
while (!reader->atEnd()) { while (!reader->atEnd()) {
@@ -374,20 +377,23 @@ void ExamplesListModel::parseExamples(QXmlStreamReader *reader,
case QXmlStreamReader::EndElement: case QXmlStreamReader::EndElement:
if (reader->name() == QLatin1String("example")) { if (reader->name() == QLatin1String("example")) {
if (isValidExampleOrDemo(item.get())) if (isValidExampleOrDemo(item.get()))
m_items.push_back(item.release()); result.push_back(item.release());
} else if (reader->name() == QLatin1String("examples")) { } else if (reader->name() == QLatin1String("examples")) {
return; return result;
} }
break; break;
default: // nothing default: // nothing
break; break;
} }
} }
return result;
} }
void ExamplesListModel::parseDemos(QXmlStreamReader *reader, static QList<ListItem *> parseDemos(QXmlStreamReader *reader,
const QString &projectsOffset, const QString &demosInstallPath) const QString &projectsOffset,
const QString &demosInstallPath)
{ {
QList<ListItem *> result;
std::unique_ptr<ExampleItem> item; std::unique_ptr<ExampleItem> item;
const QChar slash = QLatin1Char('/'); const QChar slash = QLatin1Char('/');
while (!reader->atEnd()) { while (!reader->atEnd()) {
@@ -419,19 +425,21 @@ void ExamplesListModel::parseDemos(QXmlStreamReader *reader,
case QXmlStreamReader::EndElement: case QXmlStreamReader::EndElement:
if (reader->name() == QLatin1String("demo")) { if (reader->name() == QLatin1String("demo")) {
if (isValidExampleOrDemo(item.get())) if (isValidExampleOrDemo(item.get()))
m_items.push_back(item.release()); result.push_back(item.release());
} else if (reader->name() == QLatin1String("demos")) { } else if (reader->name() == QLatin1String("demos")) {
return; return result;
} }
break; break;
default: // nothing default: // nothing
break; break;
} }
} }
return result;
} }
void ExamplesListModel::parseTutorials(QXmlStreamReader *reader, const QString &projectsOffset) static QList<ListItem *> parseTutorials(QXmlStreamReader *reader, const QString &projectsOffset)
{ {
QList<ListItem *> result;
std::unique_ptr<ExampleItem> item; std::unique_ptr<ExampleItem> item;
const QChar slash = QLatin1Char('/'); const QChar slash = QLatin1Char('/');
while (!reader->atEnd()) { while (!reader->atEnd()) {
@@ -465,14 +473,15 @@ void ExamplesListModel::parseTutorials(QXmlStreamReader *reader, const QString &
break; break;
case QXmlStreamReader::EndElement: case QXmlStreamReader::EndElement:
if (reader->name() == QLatin1String("tutorial")) if (reader->name() == QLatin1String("tutorial"))
m_items.push_back(item.release()); result.push_back(item.release());
else if (reader->name() == QLatin1String("tutorials")) else if (reader->name() == QLatin1String("tutorials"))
return; return result;
break; break;
default: // nothing default: // nothing
break; break;
} }
} }
return result;
} }
void ExamplesListModel::updateExamples() void ExamplesListModel::updateExamples()
@@ -482,10 +491,10 @@ void ExamplesListModel::updateExamples()
const QStringList sources = m_exampleSetModel.exampleSources(&examplesInstallPath, const QStringList sources = m_exampleSetModel.exampleSources(&examplesInstallPath,
&demosInstallPath); &demosInstallPath);
beginResetModel();
qDeleteAll(m_items);
m_items.clear();
clear();
QList<ListItem *> items;
for (const QString &exampleSource : sources) { for (const QString &exampleSource : sources) {
QFile exampleFile(exampleSource); QFile exampleFile(exampleSource);
if (!exampleFile.open(QIODevice::ReadOnly)) { if (!exampleFile.open(QIODevice::ReadOnly)) {
@@ -506,11 +515,11 @@ void ExamplesListModel::updateExamples()
switch (reader.readNext()) { switch (reader.readNext()) {
case QXmlStreamReader::StartElement: case QXmlStreamReader::StartElement:
if (reader.name() == QLatin1String("examples")) if (reader.name() == QLatin1String("examples"))
parseExamples(&reader, examplesDir.path(), examplesInstallPath); items += parseExamples(&reader, examplesDir.path(), examplesInstallPath);
else if (reader.name() == QLatin1String("demos")) else if (reader.name() == QLatin1String("demos"))
parseDemos(&reader, demosDir.path(), demosInstallPath); items += parseDemos(&reader, demosDir.path(), demosInstallPath);
else if (reader.name() == QLatin1String("tutorials")) else if (reader.name() == QLatin1String("tutorials"))
parseTutorials(&reader, examplesDir.path()); items += parseTutorials(&reader, examplesDir.path());
break; break;
default: // nothing default: // nothing
break; break;
@@ -522,7 +531,7 @@ void ExamplesListModel::updateExamples()
<< ": " << reader.errorString(); << ": " << reader.errorString();
} }
} }
endResetModel(); appendItems(items);
} }
void ExampleSetModel::updateQtVersionList() void ExampleSetModel::updateQtVersionList()

View File

@@ -117,12 +117,6 @@ signals:
private: private:
void updateSelectedQtVersion(); void updateSelectedQtVersion();
void parseExamples(QXmlStreamReader *reader, const QString &projectsOffset,
const QString &examplesInstallPath);
void parseDemos(QXmlStreamReader *reader, const QString &projectsOffset,
const QString &demosInstallPath);
void parseTutorials(QXmlStreamReader *reader, const QString &projectsOffset);
ExampleSetModel m_exampleSetModel; ExampleSetModel m_exampleSetModel;
}; };