QtSupport: Simplify welcome page examples model handling further

Use the ExampleSetModel directly in the combobox, rely on normal
model reset behavior behavior for gui updates.

Change-Id: Icdb34ff0f572caaf92259530823e90bfd783b933
Task-number: QTCREATORBUG-17678
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
hjk
2017-01-20 12:24:56 +01:00
parent f16bd2433b
commit 71007df8fd
3 changed files with 178 additions and 269 deletions

View File

@@ -33,11 +33,13 @@
#include <coreplugin/helpmanager.h> #include <coreplugin/helpmanager.h>
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
#include <qtsupport/qtkitinformation.h> #include <qtsupport/qtkitinformation.h>
#include <qtsupport/qtversionmanager.h> #include <qtsupport/qtversionmanager.h>
#include <utils/algorithm.h>
#include <utils/environment.h> #include <utils/environment.h>
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <utils/algorithm.h>
#include <algorithm> #include <algorithm>
@@ -68,22 +70,54 @@ int ExampleSetModel::readCurrentIndexFromSettings() const
return -1; return -1;
} }
ExampleSetModel::ExampleSetModel(ExamplesListModel *examplesModel, QObject *parent) : ExampleSetModel::ExampleSetModel()
QStandardItemModel(parent),
examplesModel(examplesModel)
{ {
// read extra example sets settings
QSettings *settings = Core::ICore::settings();
const QStringList list = settings->value("Help/InstalledExamples", QStringList()).toStringList();
if (debugExamples())
qWarning() << "Reading Help/InstalledExamples from settings:" << list;
for (const QString &item : list) {
const QStringList &parts = item.split(QLatin1Char('|'));
if (parts.size() < 3) {
if (debugExamples())
qWarning() << "Item" << item << "has less than 3 parts (separated by '|'):" << parts;
continue;
}
ExtraExampleSet set;
set.displayName = parts.at(0);
set.manifestPath = parts.at(1);
set.examplesPath = parts.at(2);
QFileInfo fi(set.manifestPath);
if (!fi.isDir() || !fi.isReadable()) {
if (debugExamples())
qWarning() << "Manifest path " << set.manifestPath << "is not a readable directory, ignoring";
continue;
}
m_extraExampleSets.append(set);
if (debugExamples()) {
qWarning() << "Adding examples set displayName=" << set.displayName
<< ", manifestPath=" << set.manifestPath
<< ", examplesPath=" << set.examplesPath;
}
}
connect(QtVersionManager::instance(), &QtVersionManager::qtVersionsLoaded,
this, &ExampleSetModel::qtVersionManagerLoaded);
connect(Core::HelpManager::instance(), &Core::HelpManager::setupFinished,
this, &ExampleSetModel::helpManagerInitialized);
} }
void ExampleSetModel::update() void ExampleSetModel::recreateModel()
{ {
beginResetModel(); beginResetModel();
clear(); clear();
QSet<QString> extraManifestDirs; QSet<QString> extraManifestDirs;
QList<ExamplesListModel::ExtraExampleSet> extraExampleSets = examplesModel->extraExampleSets(); for (int i = 0; i < m_extraExampleSets.size(); ++i) {
for (int i = 0; i < extraExampleSets.size(); ++i) { const ExtraExampleSet &set = m_extraExampleSets.at(i);
ExamplesListModel::ExtraExampleSet set = extraExampleSets.at(i);
QStandardItem *newItem = new QStandardItem(); QStandardItem *newItem = new QStandardItem();
newItem->setData(set.displayName, Qt::DisplayRole);
newItem->setData(set.displayName, Qt::UserRole + 1); newItem->setData(set.displayName, Qt::UserRole + 1);
newItem->setData(QVariant(), Qt::UserRole + 2); newItem->setData(QVariant(), Qt::UserRole + 2);
newItem->setData(i, Qt::UserRole + 3); newItem->setData(i, Qt::UserRole + 3);
@@ -102,6 +136,7 @@ void ExampleSetModel::update()
continue; continue;
} }
QStandardItem *newItem = new QStandardItem(); QStandardItem *newItem = new QStandardItem();
newItem->setData(version->displayName(), Qt::DisplayRole);
newItem->setData(version->displayName(), Qt::UserRole + 1); newItem->setData(version->displayName(), Qt::UserRole + 1);
newItem->setData(version->uniqueId(), Qt::UserRole + 2); newItem->setData(version->uniqueId(), Qt::UserRole + 2);
newItem->setData(QVariant(), Qt::UserRole + 3); newItem->setData(QVariant(), Qt::UserRole + 3);
@@ -122,13 +157,12 @@ int ExampleSetModel::indexForQtVersion(BaseQtVersion *qtVersion) const
if (getType(i) == QtExampleSet && getQtId(i) == qtVersion->uniqueId()) if (getType(i) == QtExampleSet && getQtId(i) == qtVersion->uniqueId())
return i; return i;
} }
// check for extra set // check for extra set
const QList<ExamplesListModel::ExtraExampleSet> &extraExamples
= examplesModel->extraExampleSets();
const QString &documentationPath = qtVersion->documentationPath(); const QString &documentationPath = qtVersion->documentationPath();
for (int i = 0; i < rowCount(); ++i) { for (int i = 0; i < rowCount(); ++i) {
if (getType(i) == ExtraExampleSet if (getType(i) == ExtraExampleSetType
&& extraExamples.at(getExtraExampleSetIndex(i)).manifestPath == documentationPath) && m_extraExampleSets.at(getExtraExampleSetIndex(i)).manifestPath == documentationPath)
return i; return i;
} }
return -1; return -1;
@@ -161,7 +195,7 @@ ExampleSetModel::ExampleSetType ExampleSetModel::getType(int i) const
QVariant variant = data(modelIndex, Qt::UserRole + 2); /*Qt version uniqueId*/ QVariant variant = data(modelIndex, Qt::UserRole + 2); /*Qt version uniqueId*/
if (variant.isValid()) if (variant.isValid())
return QtExampleSet; return QtExampleSet;
return ExtraExampleSet; return ExtraExampleSetType;
} }
int ExampleSetModel::getQtId(int i) const int ExampleSetModel::getQtId(int i) const
@@ -184,40 +218,11 @@ int ExampleSetModel::getExtraExampleSetIndex(int i) const
return variant.toInt(); return variant.toInt();
} }
ExamplesListModel::ExamplesListModel(QObject *parent) : ExamplesListModel::ExamplesListModel(QObject *parent)
QAbstractListModel(parent), : QAbstractListModel(parent)
m_exampleSetModel(new ExampleSetModel(this, this))
{ {
// read extra example sets settings connect(&m_exampleSetModel, &ExampleSetModel::selectedExampleSetChanged,
QSettings *settings = Core::ICore::settings(); this, &ExamplesListModel::updateExamples);
QStringList list = settings->value(QLatin1String("Help/InstalledExamples"),
QStringList()).toStringList();
if (debugExamples())
qWarning() << "Reading Help/InstalledExamples from settings:" << list;
foreach (const QString &item, list) {
const QStringList &parts = item.split(QLatin1Char('|'));
if (parts.size() < 3) {
if (debugExamples())
qWarning() << "Item" << item << "has less than 3 parts (separated by '|'):" << parts;
continue;
}
ExtraExampleSet set;
set.displayName = parts.at(0);
set.manifestPath = parts.at(1);
set.examplesPath = parts.at(2);
QFileInfo fi(set.manifestPath);
if (!fi.isDir() || !fi.isReadable()) {
if (debugExamples())
qWarning() << "Manifest path " << set.manifestPath << "is not a readable directory, ignoring";
continue;
}
m_extraExampleSets.append(set);
if (debugExamples()) {
qWarning() << "Adding examples set displayName=" << set.displayName
<< ", manifestPath=" << set.manifestPath
<< ", examplesPath=" << set.examplesPath;
}
}
} }
static QString fixStringForTags(const QString &string) static QString fixStringForTags(const QString &string)
@@ -431,7 +436,7 @@ void ExamplesListModel::updateExamples()
QString examplesInstallPath; QString examplesInstallPath;
QString demosInstallPath; QString demosInstallPath;
QStringList sources = exampleSources(&examplesInstallPath, &demosInstallPath); QStringList sources = m_exampleSetModel.exampleSources(&examplesInstallPath, &demosInstallPath);
beginResetModel(); beginResetModel();
m_exampleItems.clear(); m_exampleItems.clear();
@@ -472,7 +477,7 @@ void ExamplesListModel::updateExamples()
endResetModel(); endResetModel();
} }
void ExamplesListModel::updateQtVersions() void ExampleSetModel::updateQtVersionList()
{ {
QList<BaseQtVersion*> versions QList<BaseQtVersion*> versions
= QtVersionManager::sortVersions( = QtVersionManager::sortVersions(
@@ -486,35 +491,34 @@ void ExamplesListModel::updateQtVersions()
if (defaultVersion && versions.contains(defaultVersion)) if (defaultVersion && versions.contains(defaultVersion))
versions.move(versions.indexOf(defaultVersion), 0); versions.move(versions.indexOf(defaultVersion), 0);
m_exampleSetModel->update(); recreateModel();
int currentIndex = m_selectedExampleSetIndex; int currentIndex = m_selectedExampleSetIndex;
if (currentIndex < 0) // reset from settings if (currentIndex < 0) // reset from settings
currentIndex = m_exampleSetModel->readCurrentIndexFromSettings(); currentIndex = readCurrentIndexFromSettings();
ExampleSetModel::ExampleSetType currentType ExampleSetModel::ExampleSetType currentType = getType(currentIndex);
= m_exampleSetModel->getType(currentIndex);
if (currentType == ExampleSetModel::InvalidExampleSet) { if (currentType == ExampleSetModel::InvalidExampleSet) {
// select examples corresponding to 'highest' Qt version // select examples corresponding to 'highest' Qt version
BaseQtVersion *highestQt = findHighestQtVersion(); BaseQtVersion *highestQt = findHighestQtVersion();
currentIndex = m_exampleSetModel->indexForQtVersion(highestQt); currentIndex = indexForQtVersion(highestQt);
} else if (currentType == ExampleSetModel::QtExampleSet) { } else if (currentType == ExampleSetModel::QtExampleSet) {
// try to select the previously selected Qt version, or // try to select the previously selected Qt version, or
// select examples corresponding to 'highest' Qt version // select examples corresponding to 'highest' Qt version
int currentQtId = m_exampleSetModel->getQtId(currentIndex); int currentQtId = getQtId(currentIndex);
BaseQtVersion *newQtVersion = QtVersionManager::version(currentQtId); BaseQtVersion *newQtVersion = QtVersionManager::version(currentQtId);
if (!newQtVersion) if (!newQtVersion)
newQtVersion = findHighestQtVersion(); newQtVersion = findHighestQtVersion();
currentIndex = m_exampleSetModel->indexForQtVersion(newQtVersion); currentIndex = indexForQtVersion(newQtVersion);
} // nothing to do for extra example sets } // nothing to do for extra example sets
selectExampleSet(currentIndex); selectExampleSet(currentIndex);
emit selectedExampleSetChanged(currentIndex);
} }
BaseQtVersion *ExamplesListModel::findHighestQtVersion() const BaseQtVersion *ExampleSetModel::findHighestQtVersion() const
{ {
BaseQtVersion *newVersion = nullptr; BaseQtVersion *newVersion = nullptr;
const QList<BaseQtVersion *> versions = QtVersionManager::versions(); const QList<BaseQtVersion *> versions = QtVersionManager::versions();
for (BaseQtVersion *version : versions) { for (BaseQtVersion *version : versions) {
if (!newVersion) { if (!newVersion) {
@@ -535,7 +539,7 @@ BaseQtVersion *ExamplesListModel::findHighestQtVersion() const
return newVersion; return newVersion;
} }
QStringList ExamplesListModel::exampleSources(QString *examplesInstallPath, QString *demosInstallPath) QStringList ExampleSetModel::exampleSources(QString *examplesInstallPath, QString *demosInstallPath)
{ {
QStringList sources; QStringList sources;
@@ -546,16 +550,15 @@ QStringList ExamplesListModel::exampleSources(QString *examplesInstallPath, QStr
QString demosPath; QString demosPath;
QString manifestScanPath; QString manifestScanPath;
ExampleSetModel::ExampleSetType currentType ExampleSetModel::ExampleSetType currentType = getType(m_selectedExampleSetIndex);
= m_exampleSetModel->getType(m_selectedExampleSetIndex); if (currentType == ExampleSetModel::ExtraExampleSetType) {
if (currentType == ExampleSetModel::ExtraExampleSet) { int index = getExtraExampleSetIndex(m_selectedExampleSetIndex);
int index = m_exampleSetModel->getExtraExampleSetIndex(m_selectedExampleSetIndex);
ExtraExampleSet exampleSet = m_extraExampleSets.at(index); ExtraExampleSet exampleSet = m_extraExampleSets.at(index);
manifestScanPath = exampleSet.manifestPath; manifestScanPath = exampleSet.manifestPath;
examplesPath = exampleSet.examplesPath; examplesPath = exampleSet.examplesPath;
demosPath = exampleSet.examplesPath; demosPath = exampleSet.examplesPath;
} else if (currentType == ExampleSetModel::QtExampleSet) { } else if (currentType == ExampleSetModel::QtExampleSet) {
int qtId = m_exampleSetModel->getQtId(m_selectedExampleSetIndex); int qtId = getQtId(m_selectedExampleSetIndex);
foreach (BaseQtVersion *version, QtVersionManager::versions()) { foreach (BaseQtVersion *version, QtVersionManager::versions()) {
if (version->uniqueId() == qtId) { if (version->uniqueId() == qtId) {
manifestScanPath = version->documentationPath(); manifestScanPath = version->documentationPath();
@@ -615,67 +618,54 @@ QVariant ExamplesListModel::data(const QModelIndex &index, int role) const
} }
} }
void ExamplesListModel::update() void ExampleSetModel::selectExampleSet(int index)
{
updateQtVersions();
updateExamples();
}
int ExamplesListModel::selectedExampleSet() const
{
return m_selectedExampleSetIndex;
}
void ExamplesListModel::selectExampleSet(int index)
{ {
if (index != m_selectedExampleSetIndex) { if (index != m_selectedExampleSetIndex) {
m_selectedExampleSetIndex = index; m_selectedExampleSetIndex = index;
m_exampleSetModel->writeCurrentIdToSettings(m_selectedExampleSetIndex); writeCurrentIdToSettings(m_selectedExampleSetIndex);
updateExamples();
emit selectedExampleSetChanged(m_selectedExampleSetIndex); emit selectedExampleSetChanged(m_selectedExampleSetIndex);
} }
} }
QStringList ExamplesListModel::exampleSets() const void ExampleSetModel::qtVersionManagerLoaded()
{ {
return Utils::transform(QtVersionManager::versions(), &BaseQtVersion::displayName); m_qtVersionManagerInitialized = true;
tryToInitialize();
} }
void ExampleSetModel::helpManagerInitialized()
{
m_helpManagerInitialized = true;
tryToInitialize();
}
void ExampleSetModel::tryToInitialize()
{
if (m_initalized || !m_qtVersionManagerInitialized || !m_helpManagerInitialized)
return;
m_initalized = true;
connect(QtVersionManager::instance(), &QtVersionManager::qtVersionsChanged,
this, &ExampleSetModel::updateQtVersionList);
connect(ProjectExplorer::KitManager::instance(), &ProjectExplorer::KitManager::defaultkitChanged,
this, &ExampleSetModel::updateQtVersionList);
updateQtVersionList();
}
ExamplesListModelFilter::ExamplesListModelFilter(ExamplesListModel *sourceModel, bool showTutorialsOnly, QObject *parent) : ExamplesListModelFilter::ExamplesListModelFilter(ExamplesListModel *sourceModel, bool showTutorialsOnly, QObject *parent) :
QSortFilterProxyModel(parent), QSortFilterProxyModel(parent),
m_showTutorialsOnly(showTutorialsOnly), m_showTutorialsOnly(showTutorialsOnly)
m_sourceModel(sourceModel)
{ {
// initialization hooks setSourceModel(sourceModel);
connect(QtVersionManager::instance(), &QtVersionManager::qtVersionsLoaded,
this, &ExamplesListModelFilter::qtVersionManagerLoaded);
connect(Core::HelpManager::instance(), &Core::HelpManager::setupFinished,
this, &ExamplesListModelFilter::helpManagerInitialized);
setSourceModel(m_sourceModel);
setDynamicSortFilter(true); setDynamicSortFilter(true);
setFilterCaseSensitivity(Qt::CaseInsensitive); setFilterCaseSensitivity(Qt::CaseInsensitive);
sort(0); sort(0);
} }
void ExamplesListModelFilter::updateFilter()
{
ExamplesListModel *exampleListModel = qobject_cast<ExamplesListModel*>(sourceModel());
if (exampleListModel) {
exampleListModel->beginReset();
invalidateFilter();
exampleListModel->endReset();
}
}
void ExamplesListModelFilter::setFilterStrings(const QStringList &arg)
{
if (m_filterStrings != arg) {
m_filterStrings = arg;
delayedUpdateFilter();
}
}
bool ExamplesListModelFilter::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const bool ExamplesListModelFilter::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{ {
const ExampleItem item = sourceModel()->index(sourceRow, 0, sourceParent).data(Qt::UserRole).value<ExampleItem>(); const ExampleItem item = sourceModel()->index(sourceRow, 0, sourceParent).data(Qt::UserRole).value<ExampleItem>();
@@ -711,63 +701,6 @@ bool ExamplesListModelFilter::filterAcceptsRow(int sourceRow, const QModelIndex
return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent); return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
} }
int ExamplesListModelFilter::rowCount(const QModelIndex &parent) const
{
exampleDataRequested();
return QSortFilterProxyModel::rowCount(parent);
}
QVariant ExamplesListModelFilter::data(const QModelIndex &index, int role) const
{
exampleDataRequested();
return QSortFilterProxyModel::data(index, role);
}
void ExamplesListModelFilter::setFilterTags(const QStringList &arg)
{
if (m_filterTags != arg) {
m_filterTags = arg;
emit filterTagsChanged(arg);
}
}
void ExamplesListModelFilter::handleQtVersionsChanged()
{
m_sourceModel->update();
}
void ExamplesListModelFilter::qtVersionManagerLoaded()
{
m_qtVersionManagerInitialized = true;
tryToInitialize();
}
void ExamplesListModelFilter::helpManagerInitialized()
{
m_helpManagerInitialized = true;
tryToInitialize();
}
void ExamplesListModelFilter::exampleDataRequested() const
{
ExamplesListModelFilter *that = const_cast<ExamplesListModelFilter *>(this);
that->m_exampleDataRequested = true;
that->tryToInitialize();
}
void ExamplesListModelFilter::tryToInitialize()
{
if (!m_initalized
&& m_qtVersionManagerInitialized && m_helpManagerInitialized && m_exampleDataRequested) {
m_initalized = true;
connect(QtVersionManager::instance(), &QtVersionManager::qtVersionsChanged,
this, &ExamplesListModelFilter::handleQtVersionsChanged);
connect(ProjectExplorer::KitManager::instance(), &ProjectExplorer::KitManager::defaultkitChanged,
this, &ExamplesListModelFilter::handleQtVersionsChanged);
handleQtVersionsChanged();
}
}
void ExamplesListModelFilter::delayedUpdateFilter() void ExamplesListModelFilter::delayedUpdateFilter()
{ {
if (m_timerId != 0) if (m_timerId != 0)
@@ -779,7 +712,7 @@ void ExamplesListModelFilter::delayedUpdateFilter()
void ExamplesListModelFilter::timerEvent(QTimerEvent *timerEvent) void ExamplesListModelFilter::timerEvent(QTimerEvent *timerEvent)
{ {
if (m_timerId == timerEvent->timerId()) { if (m_timerId == timerEvent->timerId()) {
updateFilter(); invalidateFilter();
killTimer(m_timerId); killTimer(m_timerId);
m_timerId = 0; m_timerId = 0;
} }
@@ -871,32 +804,31 @@ void ExamplesListModelFilter::setSearchString(const QString &arg)
{ {
if (m_searchString == arg) if (m_searchString == arg)
return; return;
m_searchString = arg; m_searchString = arg;
emit searchStringChanged(m_searchString); m_filterTags.clear();
m_filterStrings.clear();
// parse and update // parse and update
QStringList tags;
QStringList searchTerms;
SearchStringLexer lex(arg); SearchStringLexer lex(arg);
bool isTag = false; bool isTag = false;
while (int tk = lex()) { while (int tk = lex()) {
if (tk == SearchStringLexer::TAG) { if (tk == SearchStringLexer::TAG) {
isTag = true; isTag = true;
searchTerms.append(lex.yytext); m_filterStrings.append(lex.yytext);
} }
if (tk == SearchStringLexer::STRING_LITERAL) { if (tk == SearchStringLexer::STRING_LITERAL) {
if (isTag) { if (isTag) {
searchTerms.pop_back(); m_filterStrings.pop_back();
tags.append(lex.yytext); m_filterTags.append(lex.yytext);
isTag = false; isTag = false;
} else { } else {
searchTerms.append(lex.yytext); m_filterStrings.append(lex.yytext);
} }
} }
} }
setFilterStrings(searchTerms);
setFilterTags(tags);
delayedUpdateFilter(); delayedUpdateFilter();
} }

View File

@@ -25,12 +25,13 @@
#pragma once #pragma once
#include <qtsupport/baseqtversion.h>
#include <QAbstractListModel> #include <QAbstractListModel>
#include <QSortFilterProxyModel> #include <QSortFilterProxyModel>
#include <QStandardItemModel> #include <QStandardItemModel>
#include <QStringList> #include <QStringList>
#include <QXmlStreamReader> #include <QXmlStreamReader>
#include <qtsupport/baseqtversion.h>
namespace QtSupport { namespace QtSupport {
namespace Internal { namespace Internal {
@@ -42,28 +43,54 @@ class ExampleSetModel : public QStandardItemModel
Q_OBJECT Q_OBJECT
public: public:
ExampleSetModel();
int selectedExampleSet() const { return m_selectedExampleSetIndex; }
void selectExampleSet(int index);
QStringList exampleSources(QString *examplesInstallPath, QString *demosInstallPath);
signals:
void selectedExampleSetChanged(int);
private:
struct ExtraExampleSet {
QString displayName;
QString manifestPath;
QString examplesPath;
};
enum ExampleSetType { enum ExampleSetType {
InvalidExampleSet, InvalidExampleSet,
QtExampleSet, QtExampleSet,
ExtraExampleSet ExtraExampleSetType
}; };
ExampleSetModel(ExamplesListModel *examplesModel, QObject *parent);
void writeCurrentIdToSettings(int currentIndex) const; void writeCurrentIdToSettings(int currentIndex) const;
int readCurrentIndexFromSettings() const; int readCurrentIndexFromSettings() const;
int indexForQtVersion(BaseQtVersion *qtVersion) const;
void update();
QVariant getDisplayName(int index) const; QVariant getDisplayName(int index) const;
QVariant getId(int index) const; QVariant getId(int index) const;
ExampleSetType getType(int i) const; ExampleSetType getType(int i) const;
int getQtId(int index) const; int getQtId(int index) const;
int getExtraExampleSetIndex(int index) const; int getExtraExampleSetIndex(int index) const;
private: BaseQtVersion *findHighestQtVersion() const;
ExamplesListModel *examplesModel;
int indexForQtVersion(BaseQtVersion *qtVersion) const;
void recreateModel();
void updateQtVersionList();
void qtVersionManagerLoaded();
void helpManagerInitialized();
void tryToInitialize();
QList<ExtraExampleSet> m_extraExampleSets;
QList<BaseQtVersion*> m_qtVersions;
int m_selectedExampleSetIndex = -1;
bool m_qtVersionManagerInitialized = false;
bool m_helpManagerInitialized = false;
bool m_initalized = false;
}; };
enum InstructionalType enum InstructionalType
@@ -98,49 +125,30 @@ class ExamplesListModel : public QAbstractListModel
Q_OBJECT Q_OBJECT
public: public:
struct ExtraExampleSet {
QString displayName;
QString manifestPath;
QString examplesPath;
};
explicit ExamplesListModel(QObject *parent); explicit ExamplesListModel(QObject *parent);
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 final; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const final;
void beginReset() { beginResetModel(); } void updateExamples();
void endReset() { endResetModel(); }
void update();
int selectedExampleSet() const;
void selectExampleSet(int index);
QList<ExtraExampleSet> extraExampleSets() const { return m_extraExampleSets; }
QStringList exampleSets() const; QStringList exampleSets() const;
ExampleSetModel *exampleSetModel() { return &m_exampleSetModel; }
signals: signals:
void selectedExampleSetChanged(int); void selectedExampleSetChanged(int);
private: private:
void updateQtVersions();
void updateExamples();
void updateSelectedQtVersion(); void updateSelectedQtVersion();
BaseQtVersion *findHighestQtVersion() const;
void parseExamples(QXmlStreamReader *reader, const QString &projectsOffset, void parseExamples(QXmlStreamReader *reader, const QString &projectsOffset,
const QString &examplesInstallPath); const QString &examplesInstallPath);
void parseDemos(QXmlStreamReader *reader, const QString &projectsOffset, void parseDemos(QXmlStreamReader *reader, const QString &projectsOffset,
const QString &demosInstallPath); const QString &demosInstallPath);
void parseTutorials(QXmlStreamReader *reader, const QString &projectsOffset); void parseTutorials(QXmlStreamReader *reader, const QString &projectsOffset);
QStringList exampleSources(QString *examplesInstallPath, QString *demosInstallPath);
ExampleSetModel* m_exampleSetModel; ExampleSetModel m_exampleSetModel;
QList<ExtraExampleSet> m_extraExampleSets;
QList<ExampleItem> m_exampleItems; QList<ExampleItem> m_exampleItems;
int m_selectedExampleSetIndex = -1;
}; };
class ExamplesListModelFilter : public QSortFilterProxyModel class ExamplesListModelFilter : public QSortFilterProxyModel
@@ -150,41 +158,19 @@ class ExamplesListModelFilter : public QSortFilterProxyModel
public: public:
ExamplesListModelFilter(ExamplesListModel *sourceModel, bool showTutorialsOnly, QObject *parent); ExamplesListModelFilter(ExamplesListModel *sourceModel, bool showTutorialsOnly, QObject *parent);
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;
int rowCount(const QModelIndex &parent = QModelIndex()) const final;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const final;
void setSearchString(const QString &arg); void setSearchString(const QString &arg);
void setFilterTags(const QStringList &arg);
void updateFilter();
void handleQtVersionsChanged();
signals:
void filterTagsChanged(const QStringList &arg);
void searchStringChanged(const QString &arg);
private: private:
void setFilterStrings(const QStringList &arg); bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const final;
void timerEvent(QTimerEvent *event) final;
void qtVersionManagerLoaded();
void helpManagerInitialized();
void exampleDataRequested() const;
void tryToInitialize();
void timerEvent(QTimerEvent *event);
void delayedUpdateFilter(); void delayedUpdateFilter();
const bool m_showTutorialsOnly; const bool m_showTutorialsOnly;
QString m_searchString; QString m_searchString;
QStringList m_filterTags; QStringList m_filterTags;
QStringList m_filterStrings; QStringList m_filterStrings;
ExamplesListModel *m_sourceModel;
int m_timerId = 0; int m_timerId = 0;
bool m_qtVersionManagerInitialized = false;
bool m_helpManagerInitialized = false;
bool m_initalized = false;
bool m_exampleDataRequested = false;
}; };
} // namespace Internal } // namespace Internal

View File

@@ -247,7 +247,7 @@ static QString resourcePath()
class SearchBox : public QFrame class SearchBox : public QFrame
{ {
public: public:
SearchBox(const QString &placeHolderText, QWidget *parent) SearchBox(QWidget *parent)
: QFrame(parent) : QFrame(parent)
{ {
setFrameShape(QFrame::Box); setFrameShape(QFrame::Box);
@@ -257,7 +257,6 @@ public:
pal.setColor(QPalette::Base, themeColor(Theme::Welcome_BackgroundColor)); pal.setColor(QPalette::Base, themeColor(Theme::Welcome_BackgroundColor));
m_lineEdit = new QLineEdit; m_lineEdit = new QLineEdit;
m_lineEdit->setPlaceholderText(placeHolderText);
m_lineEdit->setFrame(false); m_lineEdit->setFrame(false);
m_lineEdit->setFont(sizedFont(14, this)); m_lineEdit->setFont(sizedFont(14, this));
m_lineEdit->setAttribute(Qt::WA_MacShowFocusRect, false); m_lineEdit->setAttribute(Qt::WA_MacShowFocusRect, false);
@@ -586,44 +585,48 @@ public:
static ExamplesListModel *s_examplesModel = new ExamplesListModel(this); static ExamplesListModel *s_examplesModel = new ExamplesListModel(this);
m_examplesModel = s_examplesModel; m_examplesModel = s_examplesModel;
m_filteredModel = new ExamplesListModelFilter(m_examplesModel, !m_isExamples, this); auto filteredModel = new ExamplesListModelFilter(m_examplesModel, !m_isExamples, this);
auto searchBox = new SearchBox(this);
m_searcher = searchBox->m_lineEdit;
auto vbox = new QVBoxLayout(this); auto vbox = new QVBoxLayout(this);
vbox->setContentsMargins(30, 27, 20, 20); vbox->setContentsMargins(30, 27, 20, 20);
if (m_isExamples) { if (m_isExamples) {
m_qtVersionSelector = new QComboBox(this); m_searcher->setPlaceholderText(tr("Search in Examples..."));
m_qtVersionSelector->setMinimumWidth(itemWidth);
m_qtVersionSelector->setMaximumWidth(itemWidth); auto exampleSetSelector = new QComboBox(this);
m_searchBox = new SearchBox(tr("Search in Examples..."), this); exampleSetSelector->setMinimumWidth(itemWidth);
exampleSetSelector->setMaximumWidth(itemWidth);
ExampleSetModel *exampleSetModel = m_examplesModel->exampleSetModel();
exampleSetSelector->setModel(exampleSetModel);
exampleSetSelector->setCurrentIndex(exampleSetModel->selectedExampleSet());
connect(exampleSetSelector, static_cast<void(QComboBox::*)(int)>(&QComboBox::activated),
exampleSetModel, &ExampleSetModel::selectExampleSet);
connect(exampleSetModel, &ExampleSetModel::selectedExampleSetChanged,
exampleSetSelector, &QComboBox::setCurrentIndex);
auto hbox = new QHBoxLayout; auto hbox = new QHBoxLayout;
hbox->setSpacing(17); hbox->setSpacing(17);
hbox->addWidget(m_qtVersionSelector); hbox->addWidget(exampleSetSelector);
hbox->addWidget(m_searchBox); hbox->addWidget(searchBox);
vbox->addItem(hbox); vbox->addItem(hbox);
connect(m_qtVersionSelector, static_cast<void(QComboBox::*)(int)>(&QComboBox::activated),
m_examplesModel, &ExamplesListModel::selectExampleSet);
connect(m_examplesModel, &ExamplesListModel::selectedExampleSetChanged,
this, &ExamplesPageWidget::updateStuff);
} else { } else {
m_searchBox = new SearchBox(tr("Search in Tutorials..."), this); m_searcher->setPlaceholderText(tr("Search in Tutorials..."));
vbox->addWidget(m_searchBox); vbox->addWidget(searchBox);
} }
m_gridModel.setSourceModel(m_filteredModel); m_gridModel.setSourceModel(filteredModel);
m_gridView = new GridView(this); auto gridView = new GridView(this);
m_gridView->setModel(&m_gridModel); gridView->setModel(&m_gridModel);
m_gridView->setItemDelegate(&m_exampleDelegate); gridView->setItemDelegate(&m_exampleDelegate);
vbox->addWidget(m_gridView); vbox->addWidget(gridView);
connect(&m_exampleDelegate, &ExampleDelegate::tagClicked, connect(&m_exampleDelegate, &ExampleDelegate::tagClicked,
this, &ExamplesPageWidget::onTagClicked); this, &ExamplesPageWidget::onTagClicked);
connect(m_filteredModel, &ExamplesListModelFilter::filterTagsChanged, connect(m_searcher, &QLineEdit::textChanged,
this, &ExamplesPageWidget::updateStuff); filteredModel, &ExamplesListModelFilter::setSearchString);
connect(m_filteredModel, &ExamplesListModelFilter::searchStringChanged,
this, &ExamplesPageWidget::updateStuff);
connect(m_searchBox->m_lineEdit, &QLineEdit::textChanged,
m_filteredModel, &ExamplesListModelFilter::setSearchString);
} }
int bestColumnCount() const int bestColumnCount() const
@@ -639,26 +642,14 @@ public:
void onTagClicked(const QString &tag) void onTagClicked(const QString &tag)
{ {
QString text = m_searchBox->m_lineEdit->text(); QString text = m_searcher->text();
m_searchBox->m_lineEdit->setText(text + QString("tag:\"%1\" ").arg(tag)); m_searcher->setText(text + QString("tag:\"%1\" ").arg(tag));
}
void updateStuff()
{
if (m_isExamples) {
m_qtVersionSelector->clear();
m_qtVersionSelector->addItems(m_examplesModel->exampleSets());
m_qtVersionSelector->setCurrentIndex(m_examplesModel->selectedExampleSet());
}
} }
const bool m_isExamples; const bool m_isExamples;
ExampleDelegate m_exampleDelegate; ExampleDelegate m_exampleDelegate;
QPointer<ExamplesListModel> m_examplesModel; QPointer<ExamplesListModel> m_examplesModel;
ExamplesListModelFilter *m_filteredModel; QLineEdit *m_searcher;
SearchBox *m_searchBox;
QComboBox *m_qtVersionSelector = nullptr;
GridView *m_gridView;
GridProxyModel m_gridModel; GridProxyModel m_gridModel;
}; };