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()
{
if (m_ownsItems)
qDeleteAll(m_items);
m_items.clear();
clear();
}
void ListModel::appendItems(const QList<ListItem *> &items)
@@ -161,6 +159,15 @@ const QList<ListItem *> ListModel::items() const
return m_items;
}
void ListModel::clear()
{
beginResetModel();
if (m_ownsItems)
qDeleteAll(m_items);
m_items.clear();
endResetModel();
}
int ListModel::rowCount(const QModelIndex &) const
{
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);
auto sectionLabel = new QLabel(section.name);
m_sectionLabels.append(sectionLabel);
sectionLabel->setContentsMargins(0, Core::WelcomePageHelpers::ItemGap, 0, 0);
sectionLabel->setFont(Core::WelcomePageHelpers::brandFont());
auto scrollArea = qobject_cast<QScrollArea *>(widget(0));
@@ -713,4 +721,16 @@ ListModel *SectionedGridView::addSection(const Section &section, const QList<Lis
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

View File

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

View File

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

View File

@@ -117,12 +117,6 @@ signals:
private:
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;
};