From ad643fdd30047be8855cc14db7d9a78f8655d0d2 Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Mon, 16 Jan 2023 18:44:54 +0100 Subject: [PATCH] Welcome/Qt: Use separate model instances for examples and tutorials And get rid of the need to filter the model in a special way. Change-Id: I42dd80e3b8b122dcd2d5f454d0acde1facf556bd Reviewed-by: Reviewed-by: Christian Stenger --- src/plugins/qtsupport/exampleslistmodel.cpp | 100 +++++++----------- src/plugins/qtsupport/exampleslistmodel.h | 20 +--- .../qtsupport/gettingstartedwelcomepage.cpp | 31 +++--- 3 files changed, 61 insertions(+), 90 deletions(-) diff --git a/src/plugins/qtsupport/exampleslistmodel.cpp b/src/plugins/qtsupport/exampleslistmodel.cpp index cdfc6c872db..a1424cdcb8f 100644 --- a/src/plugins/qtsupport/exampleslistmodel.cpp +++ b/src/plugins/qtsupport/exampleslistmodel.cpp @@ -263,16 +263,25 @@ static QPixmap fetchPixmapAndUpdatePixmapCache(const QString &url) return pixmap; } -ExamplesListModel::ExamplesListModel(QObject *parent) +ExamplesListModel::ExamplesListModel(ExampleSetModel *exampleSetModel, + bool isExamples, + QObject *parent) : Core::ListModel(parent) + , m_exampleSetModel(exampleSetModel) + , m_isExamples(isExamples) { - connect(&m_exampleSetModel, &ExampleSetModel::selectedExampleSetChanged, - this, &ExamplesListModel::updateExamples); + if (isExamples) { + connect(m_exampleSetModel, + &ExampleSetModel::selectedExampleSetChanged, + this, + &ExamplesListModel::updateExamples); + } connect(Core::HelpManager::Signals::instance(), &Core::HelpManager::Signals::documentationChanged, this, &ExamplesListModel::updateExamples); setPixmapFunction(fetchPixmapAndUpdatePixmapCache); + updateExamples(); } static QString fixStringForTags(const QString &string) @@ -332,11 +341,11 @@ static bool isValidExampleOrDemo(ExampleItem *item) return ok || debugExamples(); } -static QList parseExamples(QXmlStreamReader *reader, - const QString &projectsOffset, - const QString &examplesInstallPath) +static QList parseExamples(QXmlStreamReader *reader, + const QString &projectsOffset, + const QString &examplesInstallPath) { - QList result; + QList result; std::unique_ptr item; const QChar slash = QLatin1Char('/'); while (!reader->atEnd()) { @@ -389,11 +398,11 @@ static QList parseExamples(QXmlStreamReader *reader, return result; } -static QList parseDemos(QXmlStreamReader *reader, - const QString &projectsOffset, - const QString &demosInstallPath) +static QList parseDemos(QXmlStreamReader *reader, + const QString &projectsOffset, + const QString &demosInstallPath) { - QList result; + QList result; std::unique_ptr item; const QChar slash = QLatin1Char('/'); while (!reader->atEnd()) { @@ -437,9 +446,9 @@ static QList parseDemos(QXmlStreamReader *reader, return result; } -static QList parseTutorials(QXmlStreamReader *reader, const QString &projectsOffset) +static QList parseTutorials(QXmlStreamReader *reader, const QString &projectsOffset) { - QList result; + QList result; std::unique_ptr item; const QChar slash = QLatin1Char('/'); while (!reader->atEnd()) { @@ -489,12 +498,12 @@ void ExamplesListModel::updateExamples() QString examplesInstallPath; QString demosInstallPath; - const QStringList sources = m_exampleSetModel.exampleSources(&examplesInstallPath, - &demosInstallPath); + const QStringList sources = m_exampleSetModel->exampleSources(&examplesInstallPath, + &demosInstallPath); clear(); - QList items; + QList items; for (const QString &exampleSource : sources) { QFile exampleFile(exampleSource); if (!exampleFile.open(QIODevice::ReadOnly)) { @@ -514,11 +523,11 @@ void ExamplesListModel::updateExamples() while (!reader.atEnd()) switch (reader.readNext()) { case QXmlStreamReader::StartElement: - if (reader.name() == QLatin1String("examples")) + if (m_isExamples && reader.name() == QLatin1String("examples")) items += parseExamples(&reader, examplesDir.path(), examplesInstallPath); - else if (reader.name() == QLatin1String("demos")) + else if (m_isExamples && reader.name() == QLatin1String("demos")) items += parseDemos(&reader, demosDir.path(), demosInstallPath); - else if (reader.name() == QLatin1String("tutorials")) + else if (!m_isExamples && reader.name() == QLatin1String("tutorials")) items += parseTutorials(&reader, examplesDir.path()); break; default: // nothing @@ -531,7 +540,18 @@ void ExamplesListModel::updateExamples() << ": " << reader.errorString(); } } - appendItems(items); + if (m_isExamples) { + if (m_exampleSetModel->selectedQtSupports(Android::Constants::ANDROID_DEVICE_TYPE)) { + items = Utils::filtered(items, [](ExampleItem *item) { + return item->tags.contains("android"); + }); + } else if (m_exampleSetModel->selectedQtSupports(Ios::Constants::IOS_DEVICE_TYPE)) { + items = Utils::filtered(items, + [](ExampleItem *item) { return item->tags.contains("ios"); }); + } + } + + appendItems(static_container_cast(items)); } void ExampleSetModel::updateQtVersionList() @@ -694,45 +714,5 @@ void ExampleSetModel::tryToInitialize() updateQtVersionList(); } - -ExamplesListModelFilter::ExamplesListModelFilter(ExamplesListModel *sourceModel, bool showTutorialsOnly, QObject *parent) : - Core::ListModelFilter(sourceModel, parent), - m_showTutorialsOnly(showTutorialsOnly), - m_examplesListModel(sourceModel) -{ -} - -bool ExamplesListModelFilter::leaveFilterAcceptsRowBeforeFiltering(const Core::ListItem *item, - bool *earlyExitResult) const -{ - QTC_ASSERT(earlyExitResult, return false); - - const bool isTutorial = static_cast(item)->type == Tutorial; - - if (m_showTutorialsOnly) { - *earlyExitResult = isTutorial; - return !isTutorial; - } - - if (isTutorial) { - *earlyExitResult = false; - return true; - } - - if (m_examplesListModel->exampleSetModel()->selectedQtSupports(Android::Constants::ANDROID_DEVICE_TYPE) - && !item->tags.contains("android")) { - *earlyExitResult = false; - return true; - } - - if (m_examplesListModel->exampleSetModel()->selectedQtSupports(Ios::Constants::IOS_DEVICE_TYPE) - && !item->tags.contains("ios")) { - *earlyExitResult = false; - return true; - } - - return false; -} - } // namespace Internal } // namespace QtSupport diff --git a/src/plugins/qtsupport/exampleslistmodel.h b/src/plugins/qtsupport/exampleslistmodel.h index b1d59436e06..158e1063214 100644 --- a/src/plugins/qtsupport/exampleslistmodel.h +++ b/src/plugins/qtsupport/exampleslistmodel.h @@ -104,27 +104,13 @@ class ExamplesListModel : public Core::ListModel { Q_OBJECT public: - explicit ExamplesListModel(QObject *parent); + explicit ExamplesListModel(ExampleSetModel *exampleSetModel, bool isExamples, QObject *parent); void updateExamples(); - ExampleSetModel *exampleSetModel() { return &m_exampleSetModel; } - private: - ExampleSetModel m_exampleSetModel; -}; - -class ExamplesListModelFilter : public Core::ListModelFilter -{ -public: - ExamplesListModelFilter(ExamplesListModel *sourceModel, bool showTutorialsOnly, QObject *parent); - -protected: - bool leaveFilterAcceptsRowBeforeFiltering(const Core::ListItem *item, - bool *earlyExitResult) const override; -private: - const bool m_showTutorialsOnly; - ExamplesListModel *m_examplesListModel = nullptr; + ExampleSetModel *m_exampleSetModel; + bool m_isExamples; }; } // namespace Internal diff --git a/src/plugins/qtsupport/gettingstartedwelcomepage.cpp b/src/plugins/qtsupport/gettingstartedwelcomepage.cpp index 443c4f3047d..312c3708af4 100644 --- a/src/plugins/qtsupport/gettingstartedwelcomepage.cpp +++ b/src/plugins/qtsupport/gettingstartedwelcomepage.cpp @@ -46,6 +46,8 @@ namespace Internal { const char C_FALLBACK_ROOT[] = "ProjectsFallbackRoot"; +Q_GLOBAL_STATIC(ExampleSetModel, s_exampleSetModel) + ExamplesWelcomePage::ExamplesWelcomePage(bool showExamples) : m_showExamples(showExamples) { @@ -258,10 +260,9 @@ public: : m_isExamples(isExamples) { m_exampleDelegate.setShowExamples(isExamples); - static auto s_examplesModel = new ExamplesListModel(this); - m_examplesModel = s_examplesModel; - auto filteredModel = new ExamplesListModelFilter(m_examplesModel, !m_isExamples, this); + auto examplesModel = new ExamplesListModel(s_exampleSetModel, isExamples, this); + auto filteredModel = new ListModelFilter(examplesModel, this); auto searchBox = new SearchBox(this); m_searcher = searchBox->m_lineEdit; @@ -284,13 +285,16 @@ public: exampleSetSelector->setPalette(pal); exampleSetSelector->setMinimumWidth(Core::ListItemDelegate::GridItemWidth); exampleSetSelector->setMaximumWidth(Core::ListItemDelegate::GridItemWidth); - ExampleSetModel *exampleSetModel = m_examplesModel->exampleSetModel(); - exampleSetSelector->setModel(exampleSetModel); - exampleSetSelector->setCurrentIndex(exampleSetModel->selectedExampleSet()); - connect(exampleSetSelector, &QComboBox::activated, - exampleSetModel, &ExampleSetModel::selectExampleSet); - connect(exampleSetModel, &ExampleSetModel::selectedExampleSetChanged, - exampleSetSelector, &QComboBox::setCurrentIndex); + exampleSetSelector->setModel(s_exampleSetModel); + exampleSetSelector->setCurrentIndex(s_exampleSetModel->selectedExampleSet()); + connect(exampleSetSelector, + &QComboBox::activated, + s_exampleSetModel, + &ExampleSetModel::selectExampleSet); + connect(s_exampleSetModel, + &ExampleSetModel::selectedExampleSetChanged, + exampleSetSelector, + &QComboBox::setCurrentIndex); hbox->setSpacing(Core::WelcomePageHelpers::HSpacing); hbox->addWidget(exampleSetSelector); @@ -311,8 +315,10 @@ public: connect(&m_exampleDelegate, &ExampleDelegate::tagClicked, this, &ExamplesPageWidget::onTagClicked); - connect(m_searcher, &QLineEdit::textChanged, - filteredModel, &ExamplesListModelFilter::setSearchString); + connect(m_searcher, + &QLineEdit::textChanged, + filteredModel, + &ListModelFilter::setSearchString); } void onTagClicked(const QString &tag) @@ -324,7 +330,6 @@ public: const bool m_isExamples; ExampleDelegate m_exampleDelegate; - QPointer m_examplesModel; QLineEdit *m_searcher; };