From a2f9ee870e6b17d8526f365a56bc7621abda15dc Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Wed, 1 Feb 2012 13:15:08 +0100 Subject: [PATCH] Delay looking for examples until the information is needed. E.g. when showing the example or tutorial browser. Change-Id: I938d38872c455bbfc6f28b759701e84cd57bf354 Reviewed-by: Daniel Molkentin --- src/plugins/qtsupport/exampleslistmodel.cpp | 44 ++++++++++++++++--- src/plugins/qtsupport/exampleslistmodel.h | 14 ++++-- .../qtsupport/gettingstartedwelcomepage.cpp | 3 +- 3 files changed, 50 insertions(+), 11 deletions(-) diff --git a/src/plugins/qtsupport/exampleslistmodel.cpp b/src/plugins/qtsupport/exampleslistmodel.cpp index 74d388137fb..669429d0317 100644 --- a/src/plugins/qtsupport/exampleslistmodel.cpp +++ b/src/plugins/qtsupport/exampleslistmodel.cpp @@ -54,7 +54,9 @@ namespace Internal { ExamplesListModel::ExamplesListModel(QObject *parent) : QAbstractListModel(parent), - m_updateOnQtVersionsChanged(false) + m_updateOnQtVersionsChanged(false), + m_initialized(false), + m_helpInitialized(false) { QHash roleNames; roleNames[Name] = "name"; @@ -382,12 +384,13 @@ void ExamplesListModel::addItems(const QList &newItems) int ExamplesListModel::rowCount(const QModelIndex &) const { + ensureInitialized(); return exampleItems.size(); } QVariant ExamplesListModel::data(const QModelIndex &index, int role) const { - + ensureInitialized(); if (!index.isValid() || index.row()+1 > exampleItems.count()) { qDebug() << Q_FUNC_INFO << "invalid index requested"; return QVariant(); @@ -433,16 +436,33 @@ QVariant ExamplesListModel::data(const QModelIndex &index, int role) const } -void ExamplesListModel::helpInitialized() +QStringList ExamplesListModel::tags() const { - updateExamples(); + ensureInitialized(); + return m_tags; } +void ExamplesListModel::helpInitialized() +{ + m_helpInitialized = true; + if (m_initialized) // if we are already initialized we need to update nevertheless + updateExamples(); +} -ExamplesListModelFilter::ExamplesListModelFilter(QObject *parent) : - QSortFilterProxyModel(parent), m_showTutorialsOnly(true) +void ExamplesListModel::ensureInitialized() const +{ + if (m_initialized || !m_helpInitialized) + return; + ExamplesListModel *that = const_cast(this); + that->m_initialized = true; + that->updateExamples(); +} + +ExamplesListModelFilter::ExamplesListModelFilter(ExamplesListModel *sourceModel, QObject *parent) : + QSortFilterProxyModel(parent), m_showTutorialsOnly(true), m_sourceModel(sourceModel) { connect(this, SIGNAL(showTutorialsOnlyChanged()), SLOT(updateFilter())); + setSourceModel(m_sourceModel); } void ExamplesListModelFilter::updateFilter() @@ -510,6 +530,18 @@ bool ExamplesListModelFilter::filterAcceptsRow(int sourceRow, const QModelIndex return true; } +int ExamplesListModelFilter::rowCount(const QModelIndex &parent) const +{ + m_sourceModel->ensureInitialized(); + return QSortFilterProxyModel::rowCount(parent); +} + +QVariant ExamplesListModelFilter::data(const QModelIndex &index, int role) const +{ + m_sourceModel->ensureInitialized(); + return QSortFilterProxyModel::data(index, role); +} + void ExamplesListModelFilter::setShowTutorialsOnly(bool showTutorialsOnly) { m_showTutorialsOnly = showTutorialsOnly; diff --git a/src/plugins/qtsupport/exampleslistmodel.h b/src/plugins/qtsupport/exampleslistmodel.h index 295ba2039ee..ac7989aaf85 100644 --- a/src/plugins/qtsupport/exampleslistmodel.h +++ b/src/plugins/qtsupport/exampleslistmodel.h @@ -69,14 +69,15 @@ class ExamplesListModel : public QAbstractListModel { Q_OBJECT public: explicit ExamplesListModel(QObject *parent); - void addItems(const QList &items); virtual int rowCount(const QModelIndex &parent = QModelIndex()) const; virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; - QStringList tags() const { return m_tags; } + QStringList tags() const; + void ensureInitialized() const; + signals: void tagsUpdated(); @@ -86,6 +87,7 @@ public slots: void helpInitialized(); private: + void addItems(const QList &items); QList parseExamples(QXmlStreamReader* reader, const QString& projectsOffset); QList parseDemos(QXmlStreamReader* reader, const QString& projectsOffset); QList parseTutorials(QXmlStreamReader* reader, const QString& projectsOffset); @@ -95,6 +97,8 @@ private: QList exampleItems; QStringList m_tags; bool m_updateOnQtVersionsChanged; + bool m_initialized; + bool m_helpInitialized; }; class ExamplesListModelFilter : public QSortFilterProxyModel { @@ -104,7 +108,7 @@ public: Q_PROPERTY(QStringList filterTags READ filterTags WRITE setFilterTags NOTIFY filterTagsChanged) Q_PROPERTY(QStringList searchStrings READ searchStrings WRITE setSearchStrings NOTIFY searchStrings) - explicit ExamplesListModelFilter(QObject *parent); + explicit ExamplesListModelFilter(ExamplesListModel *sourceModel, QObject *parent); bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const; @@ -112,6 +116,9 @@ public: QStringList filterTags() const { return m_filterTags; } QStringList searchStrings() const { return m_searchString; } + virtual int rowCount(const QModelIndex &parent = QModelIndex()) const; + virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; + public slots: void setFilterTags(const QStringList& arg) { @@ -145,6 +152,7 @@ private: bool m_showTutorialsOnly; QStringList m_filterTags; QStringList m_searchString; + ExamplesListModel *m_sourceModel; }; } // namespace Internal diff --git a/src/plugins/qtsupport/gettingstartedwelcomepage.cpp b/src/plugins/qtsupport/gettingstartedwelcomepage.cpp index 2758ead5bb4..2741e320713 100644 --- a/src/plugins/qtsupport/gettingstartedwelcomepage.cpp +++ b/src/plugins/qtsupport/gettingstartedwelcomepage.cpp @@ -267,8 +267,7 @@ void ExamplesWelcomePage::facilitateQml(QDeclarativeEngine *engine) m_engine = engine; m_engine->addImageProvider(QLatin1String("helpimage"), new HelpImageProvider); connect (examplesModel(), SIGNAL(tagsUpdated()), SLOT(updateTagsModel())); - ExamplesListModelFilter *proxy = new ExamplesListModelFilter(this); - proxy->setSourceModel(examplesModel()); + ExamplesListModelFilter *proxy = new ExamplesListModelFilter(examplesModel(), this); proxy->setDynamicSortFilter(true); proxy->sort(0);