2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2011-06-30 17:31:41 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2011-06-30 17:31:41 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2011-06-30 17:31:41 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-15 14:57:40 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2011-06-30 17:31:41 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2011-06-30 17:31:41 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2011-06-30 17:31:41 +02:00
|
|
|
|
2010-11-11 16:49:17 +01:00
|
|
|
#include "exampleslistmodel.h"
|
|
|
|
|
|
2012-12-19 10:19:35 +01:00
|
|
|
#include <QDebug>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QDir>
|
2012-12-19 10:19:35 +01:00
|
|
|
#include <QFile>
|
2012-12-05 14:04:55 +01:00
|
|
|
#include <QUrl>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QXmlStreamReader>
|
2010-11-11 16:49:17 +01:00
|
|
|
|
2011-07-01 18:28:56 +02:00
|
|
|
#include <coreplugin/helpmanager.h>
|
2012-12-19 10:19:35 +01:00
|
|
|
#include <coreplugin/icore.h>
|
2017-01-20 12:24:56 +01:00
|
|
|
|
2012-12-19 10:19:35 +01:00
|
|
|
#include <qtsupport/qtkitinformation.h>
|
2010-11-11 16:49:17 +01:00
|
|
|
#include <qtsupport/qtversionmanager.h>
|
2017-01-20 12:24:56 +01:00
|
|
|
|
|
|
|
|
#include <utils/algorithm.h>
|
2012-12-19 10:19:35 +01:00
|
|
|
#include <utils/environment.h>
|
2012-01-31 16:36:52 +01:00
|
|
|
#include <utils/qtcassert.h>
|
2010-11-11 16:49:17 +01:00
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
|
|
namespace QtSupport {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
2014-03-27 17:50:58 +01:00
|
|
|
static bool debugExamples()
|
|
|
|
|
{
|
|
|
|
|
static bool isDebugging = !qgetenv("QTC_DEBUG_EXAMPLESMODEL").isEmpty();
|
|
|
|
|
return isDebugging;
|
|
|
|
|
}
|
2013-02-04 10:28:16 +01:00
|
|
|
|
2014-03-27 17:50:58 +01:00
|
|
|
static const char kSelectedExampleSetKey[] = "WelcomePage/SelectedExampleSet";
|
|
|
|
|
|
|
|
|
|
void ExampleSetModel::writeCurrentIdToSettings(int currentIndex) const
|
2013-02-04 10:28:16 +01:00
|
|
|
{
|
|
|
|
|
QSettings *settings = Core::ICore::settings();
|
2014-03-27 17:50:58 +01:00
|
|
|
settings->setValue(QLatin1String(kSelectedExampleSetKey), getId(currentIndex));
|
2013-02-04 10:28:16 +01:00
|
|
|
}
|
|
|
|
|
|
2014-03-27 17:50:58 +01:00
|
|
|
int ExampleSetModel::readCurrentIndexFromSettings() const
|
2013-02-04 10:28:16 +01:00
|
|
|
{
|
2014-03-27 17:50:58 +01:00
|
|
|
QVariant id = Core::ICore::settings()->value(QLatin1String(kSelectedExampleSetKey));
|
|
|
|
|
for (int i=0; i < rowCount(); i++) {
|
|
|
|
|
if (id == getId(i))
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
2013-02-04 10:28:16 +01:00
|
|
|
}
|
|
|
|
|
|
2017-01-20 12:24:56 +01:00
|
|
|
ExampleSetModel::ExampleSetModel()
|
2013-02-04 10:28:16 +01:00
|
|
|
{
|
2017-01-20 12:24:56 +01:00
|
|
|
// 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);
|
2017-02-08 10:57:10 +01:00
|
|
|
|
|
|
|
|
if (auto helpManager = Core::HelpManager::instance()) {
|
|
|
|
|
connect(helpManager, &Core::HelpManager::setupFinished,
|
|
|
|
|
this, &ExampleSetModel::helpManagerInitialized);
|
|
|
|
|
}
|
2014-02-03 15:56:57 +01:00
|
|
|
}
|
2013-02-04 10:28:16 +01:00
|
|
|
|
2017-01-20 12:24:56 +01:00
|
|
|
void ExampleSetModel::recreateModel()
|
2014-02-03 15:56:57 +01:00
|
|
|
{
|
|
|
|
|
beginResetModel();
|
|
|
|
|
clear();
|
2013-06-17 14:22:55 +02:00
|
|
|
|
2014-03-27 17:50:58 +01:00
|
|
|
QSet<QString> extraManifestDirs;
|
2017-01-20 12:24:56 +01:00
|
|
|
for (int i = 0; i < m_extraExampleSets.size(); ++i) {
|
|
|
|
|
const ExtraExampleSet &set = m_extraExampleSets.at(i);
|
2014-03-27 17:50:58 +01:00
|
|
|
QStandardItem *newItem = new QStandardItem();
|
2017-01-20 12:24:56 +01:00
|
|
|
newItem->setData(set.displayName, Qt::DisplayRole);
|
2014-03-27 17:50:58 +01:00
|
|
|
newItem->setData(set.displayName, Qt::UserRole + 1);
|
|
|
|
|
newItem->setData(QVariant(), Qt::UserRole + 2);
|
|
|
|
|
newItem->setData(i, Qt::UserRole + 3);
|
|
|
|
|
appendRow(newItem);
|
|
|
|
|
|
|
|
|
|
extraManifestDirs.insert(set.manifestPath);
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-01 15:11:51 +01:00
|
|
|
foreach (BaseQtVersion *version, QtVersionManager::versions()) {
|
2014-03-27 17:50:58 +01:00
|
|
|
// sanitize away qt versions that have already been added through extra sets
|
|
|
|
|
if (extraManifestDirs.contains(version->documentationPath())) {
|
|
|
|
|
if (debugExamples()) {
|
|
|
|
|
qWarning() << "Not showing Qt version because manifest path is already added through InstalledExamples settings:"
|
|
|
|
|
<< version->displayName();
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2014-02-03 17:17:08 +01:00
|
|
|
QStandardItem *newItem = new QStandardItem();
|
2017-01-20 12:24:56 +01:00
|
|
|
newItem->setData(version->displayName(), Qt::DisplayRole);
|
2014-02-03 17:17:08 +01:00
|
|
|
newItem->setData(version->displayName(), Qt::UserRole + 1);
|
|
|
|
|
newItem->setData(version->uniqueId(), Qt::UserRole + 2);
|
2014-03-27 17:50:58 +01:00
|
|
|
newItem->setData(QVariant(), Qt::UserRole + 3);
|
2014-02-03 17:17:08 +01:00
|
|
|
appendRow(newItem);
|
2013-02-04 10:28:16 +01:00
|
|
|
}
|
2014-02-03 15:56:57 +01:00
|
|
|
endResetModel();
|
|
|
|
|
}
|
2013-02-04 10:28:16 +01:00
|
|
|
|
2014-03-27 17:50:58 +01:00
|
|
|
int ExampleSetModel::indexForQtVersion(BaseQtVersion *qtVersion) const
|
|
|
|
|
{
|
|
|
|
|
// return either the entry with the same QtId, or an extra example set with same path
|
|
|
|
|
|
2014-04-01 16:37:49 +02:00
|
|
|
if (!qtVersion)
|
|
|
|
|
return -1;
|
|
|
|
|
|
2014-03-27 17:50:58 +01:00
|
|
|
// check for Qt version
|
|
|
|
|
for (int i = 0; i < rowCount(); ++i) {
|
|
|
|
|
if (getType(i) == QtExampleSet && getQtId(i) == qtVersion->uniqueId())
|
2014-02-03 15:56:57 +01:00
|
|
|
return i;
|
2013-02-04 10:28:16 +01:00
|
|
|
}
|
2017-01-20 12:24:56 +01:00
|
|
|
|
2014-03-27 17:50:58 +01:00
|
|
|
// check for extra set
|
|
|
|
|
const QString &documentationPath = qtVersion->documentationPath();
|
|
|
|
|
for (int i = 0; i < rowCount(); ++i) {
|
2017-01-20 12:24:56 +01:00
|
|
|
if (getType(i) == ExtraExampleSetType
|
|
|
|
|
&& m_extraExampleSets.at(getExtraExampleSetIndex(i)).manifestPath == documentationPath)
|
2014-03-27 17:50:58 +01:00
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
2014-02-03 15:56:57 +01:00
|
|
|
}
|
2013-02-04 10:28:16 +01:00
|
|
|
|
2014-03-27 17:50:58 +01:00
|
|
|
QVariant ExampleSetModel::getDisplayName(int i) const
|
2014-02-03 15:56:57 +01:00
|
|
|
{
|
2014-03-27 17:50:58 +01:00
|
|
|
if (i < 0 || i >= rowCount())
|
|
|
|
|
return QVariant();
|
|
|
|
|
return data(index(i, 0), Qt::UserRole + 1);
|
2014-02-03 15:56:57 +01:00
|
|
|
}
|
|
|
|
|
|
2014-03-27 17:50:58 +01:00
|
|
|
// id is either the Qt version uniqueId, or the display name of the extra example set
|
|
|
|
|
QVariant ExampleSetModel::getId(int i) const
|
2014-02-03 15:56:57 +01:00
|
|
|
{
|
2014-03-27 17:50:58 +01:00
|
|
|
if (i < 0 || i >= rowCount())
|
|
|
|
|
return QVariant();
|
|
|
|
|
QModelIndex modelIndex = index(i, 0);
|
2014-02-03 15:56:57 +01:00
|
|
|
QVariant variant = data(modelIndex, Qt::UserRole + 2);
|
2014-03-27 17:50:58 +01:00
|
|
|
if (variant.isValid()) // set from qt version
|
|
|
|
|
return variant;
|
|
|
|
|
return getDisplayName(i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ExampleSetModel::ExampleSetType ExampleSetModel::getType(int i) const
|
|
|
|
|
{
|
|
|
|
|
if (i < 0 || i >= rowCount())
|
|
|
|
|
return InvalidExampleSet;
|
|
|
|
|
QModelIndex modelIndex = index(i, 0);
|
|
|
|
|
QVariant variant = data(modelIndex, Qt::UserRole + 2); /*Qt version uniqueId*/
|
|
|
|
|
if (variant.isValid())
|
|
|
|
|
return QtExampleSet;
|
2017-01-20 12:24:56 +01:00
|
|
|
return ExtraExampleSetType;
|
2014-03-27 17:50:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ExampleSetModel::getQtId(int i) const
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(i >= 0, return -1);
|
|
|
|
|
QModelIndex modelIndex = index(i, 0);
|
|
|
|
|
QVariant variant = data(modelIndex, Qt::UserRole + 2);
|
|
|
|
|
QTC_ASSERT(variant.isValid(), return -1);
|
|
|
|
|
QTC_ASSERT(variant.canConvert<int>(), return -1);
|
|
|
|
|
return variant.toInt();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ExampleSetModel::getExtraExampleSetIndex(int i) const
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(i >= 0, return -1);
|
|
|
|
|
QModelIndex modelIndex = index(i, 0);
|
|
|
|
|
QVariant variant = data(modelIndex, Qt::UserRole + 3);
|
|
|
|
|
QTC_ASSERT(variant.isValid(), return -1);
|
|
|
|
|
QTC_ASSERT(variant.canConvert<int>(), return -1);
|
|
|
|
|
return variant.toInt();
|
2014-02-03 15:56:57 +01:00
|
|
|
}
|
2013-02-04 10:28:16 +01:00
|
|
|
|
2017-01-20 12:24:56 +01:00
|
|
|
ExamplesListModel::ExamplesListModel(QObject *parent)
|
|
|
|
|
: QAbstractListModel(parent)
|
2010-11-11 16:49:17 +01:00
|
|
|
{
|
2017-01-20 12:24:56 +01:00
|
|
|
connect(&m_exampleSetModel, &ExampleSetModel::selectedExampleSetChanged,
|
|
|
|
|
this, &ExamplesListModel::updateExamples);
|
2010-11-11 16:49:17 +01:00
|
|
|
}
|
|
|
|
|
|
2012-12-19 10:19:35 +01:00
|
|
|
static QString fixStringForTags(const QString &string)
|
2011-09-28 11:30:30 +02:00
|
|
|
{
|
|
|
|
|
QString returnString = string;
|
2011-09-28 15:25:07 +02:00
|
|
|
returnString.remove(QLatin1String("<i>"));
|
|
|
|
|
returnString.remove(QLatin1String("</i>"));
|
|
|
|
|
returnString.remove(QLatin1String("<tt>"));
|
|
|
|
|
returnString.remove(QLatin1String("</tt>"));
|
2011-09-28 11:30:30 +02:00
|
|
|
return returnString;
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-19 10:19:35 +01:00
|
|
|
static QStringList trimStringList(const QStringList &stringlist)
|
2012-03-06 13:14:42 +01:00
|
|
|
{
|
2014-07-29 22:02:56 -07:00
|
|
|
return Utils::transform(stringlist, [](const QString &str) { return str.trimmed(); });
|
2012-03-06 13:14:42 +01:00
|
|
|
}
|
|
|
|
|
|
2012-11-27 17:13:02 +01:00
|
|
|
static QString relativeOrInstallPath(const QString &path, const QString &manifestPath,
|
|
|
|
|
const QString &installPath)
|
|
|
|
|
{
|
|
|
|
|
const QChar slash = QLatin1Char('/');
|
|
|
|
|
const QString relativeResolvedPath = manifestPath + slash + path;
|
|
|
|
|
const QString installResolvedPath = installPath + slash + path;
|
|
|
|
|
if (QFile::exists(relativeResolvedPath))
|
|
|
|
|
return relativeResolvedPath;
|
2012-12-19 10:19:35 +01:00
|
|
|
if (QFile::exists(installResolvedPath))
|
2012-11-27 17:13:02 +01:00
|
|
|
return installResolvedPath;
|
|
|
|
|
// doesn't exist, just return relative
|
|
|
|
|
return relativeResolvedPath;
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-05 14:04:55 +01:00
|
|
|
static bool isValidExampleOrDemo(ExampleItem &item)
|
|
|
|
|
{
|
|
|
|
|
static QString invalidPrefix = QLatin1String("qthelp:////"); /* means that the qthelp url
|
|
|
|
|
doesn't have any namespace */
|
2012-12-12 14:01:36 +01:00
|
|
|
QString reason;
|
2012-12-05 14:04:55 +01:00
|
|
|
bool ok = true;
|
2014-10-24 10:28:28 +02:00
|
|
|
if (!item.hasSourceCode || !QFileInfo::exists(item.projectPath)) {
|
2012-12-05 14:04:55 +01:00
|
|
|
ok = false;
|
2014-04-17 14:09:47 +02:00
|
|
|
reason = QString::fromLatin1("projectPath \"%1\" empty or does not exist").arg(item.projectPath);
|
2012-12-12 14:01:36 +01:00
|
|
|
} else if (item.imageUrl.startsWith(invalidPrefix) || !QUrl(item.imageUrl).isValid()) {
|
2012-12-05 14:04:55 +01:00
|
|
|
ok = false;
|
2014-04-17 14:09:47 +02:00
|
|
|
reason = QString::fromLatin1("imageUrl \"%1\" not valid").arg(item.imageUrl);
|
2012-12-12 14:01:36 +01:00
|
|
|
} else if (!item.docUrl.isEmpty()
|
|
|
|
|
&& (item.docUrl.startsWith(invalidPrefix) || !QUrl(item.docUrl).isValid())) {
|
2012-12-05 14:04:55 +01:00
|
|
|
ok = false;
|
2014-04-17 14:09:47 +02:00
|
|
|
reason = QString::fromLatin1("docUrl \"%1\" non-empty but not valid").arg(item.docUrl);
|
2012-12-12 14:01:36 +01:00
|
|
|
}
|
|
|
|
|
if (!ok) {
|
2012-12-05 14:04:55 +01:00
|
|
|
item.tags.append(QLatin1String("broken"));
|
2012-12-12 14:01:36 +01:00
|
|
|
if (debugExamples())
|
2014-04-17 14:09:47 +02:00
|
|
|
qWarning() << QString::fromLatin1("ERROR: Item \"%1\" broken: %2").arg(item.name, reason);
|
2012-12-12 14:01:36 +01:00
|
|
|
}
|
|
|
|
|
if (debugExamples() && item.description.isEmpty())
|
2014-04-17 14:09:47 +02:00
|
|
|
qWarning() << QString::fromLatin1("WARNING: Item \"%1\" has no description").arg(item.name);
|
2012-12-12 14:01:36 +01:00
|
|
|
return ok || debugExamples();
|
2012-12-05 14:04:55 +01:00
|
|
|
}
|
|
|
|
|
|
2012-12-19 10:19:35 +01:00
|
|
|
void ExamplesListModel::parseExamples(QXmlStreamReader *reader,
|
|
|
|
|
const QString &projectsOffset, const QString &examplesInstallPath)
|
2010-11-11 16:49:17 +01:00
|
|
|
{
|
|
|
|
|
ExampleItem item;
|
2012-01-06 17:43:27 +01:00
|
|
|
const QChar slash = QLatin1Char('/');
|
2010-11-11 16:49:17 +01:00
|
|
|
while (!reader->atEnd()) {
|
|
|
|
|
switch (reader->readNext()) {
|
|
|
|
|
case QXmlStreamReader::StartElement:
|
|
|
|
|
if (reader->name() == QLatin1String("example")) {
|
|
|
|
|
item = ExampleItem();
|
|
|
|
|
item.type = Example;
|
|
|
|
|
QXmlStreamAttributes attributes = reader->attributes();
|
|
|
|
|
item.name = attributes.value(QLatin1String("name")).toString();
|
|
|
|
|
item.projectPath = attributes.value(QLatin1String("projectPath")).toString();
|
|
|
|
|
item.hasSourceCode = !item.projectPath.isEmpty();
|
2012-11-27 17:13:02 +01:00
|
|
|
item.projectPath = relativeOrInstallPath(item.projectPath, projectsOffset, examplesInstallPath);
|
2011-08-16 14:56:18 +02:00
|
|
|
item.imageUrl = attributes.value(QLatin1String("imageUrl")).toString();
|
2010-11-11 16:49:17 +01:00
|
|
|
item.docUrl = attributes.value(QLatin1String("docUrl")).toString();
|
2013-02-04 10:16:02 +01:00
|
|
|
|
|
|
|
|
if (attributes.hasAttribute(QLatin1String("isHighlighted")))
|
|
|
|
|
item.isHighlighted = attributes.value(QLatin1String("isHighlighted")).toString() == QLatin1String("true");
|
|
|
|
|
|
2010-11-11 16:49:17 +01:00
|
|
|
} else if (reader->name() == QLatin1String("fileToOpen")) {
|
2014-06-16 12:50:32 +02:00
|
|
|
const QString mainFileAttribute = reader->attributes().value(
|
|
|
|
|
QLatin1String("mainFile")).toString();
|
|
|
|
|
const QString filePath = relativeOrInstallPath(
|
|
|
|
|
reader->readElementText(QXmlStreamReader::ErrorOnUnexpectedElement),
|
|
|
|
|
projectsOffset, examplesInstallPath);
|
|
|
|
|
item.filesToOpen.append(filePath);
|
|
|
|
|
if (mainFileAttribute.compare(QLatin1String("true"), Qt::CaseInsensitive) == 0)
|
|
|
|
|
item.mainFile = filePath;
|
2010-11-11 16:49:17 +01:00
|
|
|
} else if (reader->name() == QLatin1String("description")) {
|
2012-12-19 10:19:35 +01:00
|
|
|
item.description = fixStringForTags(reader->readElementText(QXmlStreamReader::ErrorOnUnexpectedElement));
|
2011-11-25 13:40:10 +01:00
|
|
|
} else if (reader->name() == QLatin1String("dependency")) {
|
2012-01-06 17:43:27 +01:00
|
|
|
item.dependencies.append(projectsOffset + slash + reader->readElementText(QXmlStreamReader::ErrorOnUnexpectedElement));
|
2010-11-11 16:49:17 +01:00
|
|
|
} else if (reader->name() == QLatin1String("tags")) {
|
2012-03-06 13:14:42 +01:00
|
|
|
item.tags = trimStringList(reader->readElementText(QXmlStreamReader::ErrorOnUnexpectedElement).split(QLatin1Char(','), QString::SkipEmptyParts));
|
|
|
|
|
} else if (reader->name() == QLatin1String("platforms")) {
|
|
|
|
|
item.platforms = trimStringList(reader->readElementText(QXmlStreamReader::ErrorOnUnexpectedElement).split(QLatin1Char(','), QString::SkipEmptyParts));
|
|
|
|
|
}
|
2010-11-11 16:49:17 +01:00
|
|
|
break;
|
|
|
|
|
case QXmlStreamReader::EndElement:
|
2012-03-27 18:39:31 +02:00
|
|
|
if (reader->name() == QLatin1String("example")) {
|
2012-12-05 14:04:55 +01:00
|
|
|
if (isValidExampleOrDemo(item))
|
2012-12-19 10:19:35 +01:00
|
|
|
m_exampleItems.append(item);
|
2012-03-27 18:39:31 +02:00
|
|
|
} else if (reader->name() == QLatin1String("examples")) {
|
2012-12-19 10:19:35 +01:00
|
|
|
return;
|
2012-03-27 18:39:31 +02:00
|
|
|
}
|
2010-11-11 16:49:17 +01:00
|
|
|
break;
|
|
|
|
|
default: // nothing
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-19 10:19:35 +01:00
|
|
|
void ExamplesListModel::parseDemos(QXmlStreamReader *reader,
|
|
|
|
|
const QString &projectsOffset, const QString &demosInstallPath)
|
2010-11-11 16:49:17 +01:00
|
|
|
{
|
|
|
|
|
ExampleItem item;
|
2012-01-06 17:43:27 +01:00
|
|
|
const QChar slash = QLatin1Char('/');
|
2010-11-11 16:49:17 +01:00
|
|
|
while (!reader->atEnd()) {
|
|
|
|
|
switch (reader->readNext()) {
|
|
|
|
|
case QXmlStreamReader::StartElement:
|
|
|
|
|
if (reader->name() == QLatin1String("demo")) {
|
|
|
|
|
item = ExampleItem();
|
|
|
|
|
item.type = Demo;
|
|
|
|
|
QXmlStreamAttributes attributes = reader->attributes();
|
|
|
|
|
item.name = attributes.value(QLatin1String("name")).toString();
|
|
|
|
|
item.projectPath = attributes.value(QLatin1String("projectPath")).toString();
|
|
|
|
|
item.hasSourceCode = !item.projectPath.isEmpty();
|
2012-11-27 17:13:02 +01:00
|
|
|
item.projectPath = relativeOrInstallPath(item.projectPath, projectsOffset, demosInstallPath);
|
2010-11-11 16:49:17 +01:00
|
|
|
item.imageUrl = attributes.value(QLatin1String("imageUrl")).toString();
|
|
|
|
|
item.docUrl = attributes.value(QLatin1String("docUrl")).toString();
|
2013-02-04 10:16:02 +01:00
|
|
|
|
|
|
|
|
if (attributes.hasAttribute(QLatin1String("isHighlighted")))
|
|
|
|
|
item.isHighlighted = attributes.value(QLatin1String("isHighlighted")).toString() == QLatin1String("true");
|
|
|
|
|
|
2010-11-11 16:49:17 +01:00
|
|
|
} else if (reader->name() == QLatin1String("fileToOpen")) {
|
2012-11-27 17:13:02 +01:00
|
|
|
item.filesToOpen.append(relativeOrInstallPath(reader->readElementText(QXmlStreamReader::ErrorOnUnexpectedElement),
|
|
|
|
|
projectsOffset, demosInstallPath));
|
2010-11-11 16:49:17 +01:00
|
|
|
} else if (reader->name() == QLatin1String("description")) {
|
2011-11-25 13:40:10 +01:00
|
|
|
item.description = fixStringForTags(reader->readElementText(QXmlStreamReader::ErrorOnUnexpectedElement));
|
|
|
|
|
} else if (reader->name() == QLatin1String("dependency")) {
|
2012-01-06 17:43:27 +01:00
|
|
|
item.dependencies.append(projectsOffset + slash + reader->readElementText(QXmlStreamReader::ErrorOnUnexpectedElement));
|
2010-11-11 16:49:17 +01:00
|
|
|
} else if (reader->name() == QLatin1String("tags")) {
|
2012-01-06 17:43:27 +01:00
|
|
|
item.tags = reader->readElementText(QXmlStreamReader::ErrorOnUnexpectedElement).split(QLatin1Char(','));
|
2010-11-11 16:49:17 +01:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case QXmlStreamReader::EndElement:
|
2012-11-27 17:43:52 +01:00
|
|
|
if (reader->name() == QLatin1String("demo")) {
|
2012-12-05 14:04:55 +01:00
|
|
|
if (isValidExampleOrDemo(item))
|
2012-12-19 10:19:35 +01:00
|
|
|
m_exampleItems.append(item);
|
2012-11-27 17:43:52 +01:00
|
|
|
} else if (reader->name() == QLatin1String("demos")) {
|
2012-12-19 10:19:35 +01:00
|
|
|
return;
|
2012-11-27 17:43:52 +01:00
|
|
|
}
|
2010-11-11 16:49:17 +01:00
|
|
|
break;
|
|
|
|
|
default: // nothing
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-19 10:19:35 +01:00
|
|
|
void ExamplesListModel::parseTutorials(QXmlStreamReader *reader, const QString &projectsOffset)
|
2010-11-11 16:49:17 +01:00
|
|
|
{
|
|
|
|
|
ExampleItem item;
|
2012-01-06 17:43:27 +01:00
|
|
|
const QChar slash = QLatin1Char('/');
|
2010-11-11 16:49:17 +01:00
|
|
|
while (!reader->atEnd()) {
|
|
|
|
|
switch (reader->readNext()) {
|
|
|
|
|
case QXmlStreamReader::StartElement:
|
|
|
|
|
if (reader->name() == QLatin1String("tutorial")) {
|
|
|
|
|
item = ExampleItem();
|
|
|
|
|
item.type = Tutorial;
|
|
|
|
|
QXmlStreamAttributes attributes = reader->attributes();
|
|
|
|
|
item.name = attributes.value(QLatin1String("name")).toString();
|
|
|
|
|
item.projectPath = attributes.value(QLatin1String("projectPath")).toString();
|
|
|
|
|
item.hasSourceCode = !item.projectPath.isEmpty();
|
2012-01-06 17:43:27 +01:00
|
|
|
item.projectPath.prepend(slash);
|
2010-11-11 16:49:17 +01:00
|
|
|
item.projectPath.prepend(projectsOffset);
|
2011-11-23 12:25:32 +01:00
|
|
|
if (attributes.hasAttribute(QLatin1String("imageUrl")))
|
|
|
|
|
item.imageUrl = attributes.value(QLatin1String("imageUrl")).toString();
|
|
|
|
|
if (attributes.hasAttribute(QLatin1String("docUrl")))
|
|
|
|
|
item.docUrl = attributes.value(QLatin1String("docUrl")).toString();
|
|
|
|
|
if (attributes.hasAttribute(QLatin1String("isVideo")))
|
|
|
|
|
item.isVideo = attributes.value(QLatin1String("isVideo")).toString() == QLatin1String("true");
|
|
|
|
|
if (attributes.hasAttribute(QLatin1String("videoUrl")))
|
|
|
|
|
item.videoUrl = attributes.value(QLatin1String("videoUrl")).toString();
|
|
|
|
|
if (attributes.hasAttribute(QLatin1String("videoLength")))
|
|
|
|
|
item.videoLength = attributes.value(QLatin1String("videoLength")).toString();
|
2010-11-11 16:49:17 +01:00
|
|
|
} else if (reader->name() == QLatin1String("fileToOpen")) {
|
2012-01-06 17:43:27 +01:00
|
|
|
item.filesToOpen.append(projectsOffset + slash + reader->readElementText(QXmlStreamReader::ErrorOnUnexpectedElement));
|
2010-11-11 16:49:17 +01:00
|
|
|
} else if (reader->name() == QLatin1String("description")) {
|
2011-11-25 13:40:10 +01:00
|
|
|
item.description = fixStringForTags(reader->readElementText(QXmlStreamReader::ErrorOnUnexpectedElement));
|
|
|
|
|
} else if (reader->name() == QLatin1String("dependency")) {
|
2012-01-06 17:43:27 +01:00
|
|
|
item.dependencies.append(projectsOffset + slash + reader->readElementText(QXmlStreamReader::ErrorOnUnexpectedElement));
|
2010-11-11 16:49:17 +01:00
|
|
|
} else if (reader->name() == QLatin1String("tags")) {
|
2012-01-06 17:43:27 +01:00
|
|
|
item.tags = reader->readElementText(QXmlStreamReader::ErrorOnUnexpectedElement).split(QLatin1Char(','));
|
2010-11-11 16:49:17 +01:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case QXmlStreamReader::EndElement:
|
|
|
|
|
if (reader->name() == QLatin1String("tutorial"))
|
2012-12-19 10:19:35 +01:00
|
|
|
m_exampleItems.append(item);
|
2010-11-11 16:49:17 +01:00
|
|
|
else if (reader->name() == QLatin1String("tutorials"))
|
2012-12-19 10:19:35 +01:00
|
|
|
return;
|
2010-11-11 16:49:17 +01:00
|
|
|
break;
|
|
|
|
|
default: // nothing
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-31 16:36:52 +01:00
|
|
|
void ExamplesListModel::updateExamples()
|
2010-11-11 16:49:17 +01:00
|
|
|
{
|
2012-11-27 17:13:02 +01:00
|
|
|
QString examplesInstallPath;
|
|
|
|
|
QString demosInstallPath;
|
2012-12-19 10:19:35 +01:00
|
|
|
|
2017-01-20 12:24:56 +01:00
|
|
|
QStringList sources = m_exampleSetModel.exampleSources(&examplesInstallPath, &demosInstallPath);
|
2012-12-19 10:19:35 +01:00
|
|
|
|
|
|
|
|
beginResetModel();
|
|
|
|
|
m_exampleItems.clear();
|
|
|
|
|
|
|
|
|
|
foreach (const QString &exampleSource, sources) {
|
2010-11-11 16:49:17 +01:00
|
|
|
QFile exampleFile(exampleSource);
|
|
|
|
|
if (!exampleFile.open(QIODevice::ReadOnly)) {
|
2012-12-12 14:01:36 +01:00
|
|
|
if (debugExamples())
|
|
|
|
|
qWarning() << "ERROR: Could not open file" << exampleSource;
|
2012-01-31 16:36:52 +01:00
|
|
|
continue;
|
2010-11-11 16:49:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QFileInfo fi(exampleSource);
|
|
|
|
|
QString offsetPath = fi.path();
|
|
|
|
|
QDir examplesDir(offsetPath);
|
|
|
|
|
QDir demosDir(offsetPath);
|
|
|
|
|
|
2012-12-12 14:01:36 +01:00
|
|
|
if (debugExamples())
|
2014-04-17 14:09:47 +02:00
|
|
|
qWarning() << QString::fromLatin1("Reading file \"%1\"...").arg(fi.absoluteFilePath());
|
2010-11-11 16:49:17 +01:00
|
|
|
QXmlStreamReader reader(&exampleFile);
|
|
|
|
|
while (!reader.atEnd())
|
|
|
|
|
switch (reader.readNext()) {
|
|
|
|
|
case QXmlStreamReader::StartElement:
|
|
|
|
|
if (reader.name() == QLatin1String("examples"))
|
2012-12-19 10:19:35 +01:00
|
|
|
parseExamples(&reader, examplesDir.path(), examplesInstallPath);
|
2010-11-11 16:49:17 +01:00
|
|
|
else if (reader.name() == QLatin1String("demos"))
|
2012-12-19 10:19:35 +01:00
|
|
|
parseDemos(&reader, demosDir.path(), demosInstallPath);
|
2010-11-11 16:49:17 +01:00
|
|
|
else if (reader.name() == QLatin1String("tutorials"))
|
2012-12-19 10:19:35 +01:00
|
|
|
parseTutorials(&reader, examplesDir.path());
|
2010-11-11 16:49:17 +01:00
|
|
|
break;
|
|
|
|
|
default: // nothing
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-12 14:01:36 +01:00
|
|
|
if (reader.hasError() && debugExamples())
|
2014-04-17 14:09:47 +02:00
|
|
|
qWarning() << QString::fromLatin1("ERROR: Could not parse file as XML document (%1)").arg(exampleSource);
|
2010-11-11 16:49:17 +01:00
|
|
|
}
|
2012-12-19 10:19:35 +01:00
|
|
|
endResetModel();
|
2010-11-11 16:49:17 +01:00
|
|
|
}
|
|
|
|
|
|
2017-01-20 12:24:56 +01:00
|
|
|
void ExampleSetModel::updateQtVersionList()
|
2014-02-03 17:17:08 +01:00
|
|
|
{
|
2017-01-12 10:31:44 +01:00
|
|
|
QList<BaseQtVersion*> versions
|
2017-01-12 10:44:57 +01:00
|
|
|
= QtVersionManager::sortVersions(
|
|
|
|
|
QtVersionManager::versions(BaseQtVersion::isValidPredicate([](const BaseQtVersion *v) {
|
2017-01-11 14:43:48 +01:00
|
|
|
return v->hasExamples() || v->hasDemos();
|
2017-01-12 10:44:57 +01:00
|
|
|
})));
|
2014-02-03 17:17:08 +01:00
|
|
|
|
|
|
|
|
// prioritize default qt version
|
|
|
|
|
ProjectExplorer::Kit *defaultKit = ProjectExplorer::KitManager::defaultKit();
|
|
|
|
|
BaseQtVersion *defaultVersion = QtKitInformation::qtVersion(defaultKit);
|
|
|
|
|
if (defaultVersion && versions.contains(defaultVersion))
|
|
|
|
|
versions.move(versions.indexOf(defaultVersion), 0);
|
|
|
|
|
|
2017-01-20 12:24:56 +01:00
|
|
|
recreateModel();
|
2014-02-04 09:29:38 +01:00
|
|
|
|
2014-03-27 17:50:58 +01:00
|
|
|
int currentIndex = m_selectedExampleSetIndex;
|
|
|
|
|
if (currentIndex < 0) // reset from settings
|
2017-01-20 12:24:56 +01:00
|
|
|
currentIndex = readCurrentIndexFromSettings();
|
2014-02-04 09:29:38 +01:00
|
|
|
|
2017-01-20 12:24:56 +01:00
|
|
|
ExampleSetModel::ExampleSetType currentType = getType(currentIndex);
|
2014-02-04 09:29:38 +01:00
|
|
|
|
2014-03-27 17:50:58 +01:00
|
|
|
if (currentType == ExampleSetModel::InvalidExampleSet) {
|
|
|
|
|
// select examples corresponding to 'highest' Qt version
|
|
|
|
|
BaseQtVersion *highestQt = findHighestQtVersion();
|
2017-01-20 12:24:56 +01:00
|
|
|
currentIndex = indexForQtVersion(highestQt);
|
2014-03-27 17:50:58 +01:00
|
|
|
} else if (currentType == ExampleSetModel::QtExampleSet) {
|
|
|
|
|
// try to select the previously selected Qt version, or
|
|
|
|
|
// select examples corresponding to 'highest' Qt version
|
2017-01-20 12:24:56 +01:00
|
|
|
int currentQtId = getQtId(currentIndex);
|
2017-02-01 15:11:51 +01:00
|
|
|
BaseQtVersion *newQtVersion = QtVersionManager::version(currentQtId);
|
2014-03-27 17:50:58 +01:00
|
|
|
if (!newQtVersion)
|
|
|
|
|
newQtVersion = findHighestQtVersion();
|
2017-01-20 12:24:56 +01:00
|
|
|
currentIndex = indexForQtVersion(newQtVersion);
|
2014-03-27 17:50:58 +01:00
|
|
|
} // nothing to do for extra example sets
|
|
|
|
|
selectExampleSet(currentIndex);
|
2017-01-20 12:24:56 +01:00
|
|
|
emit selectedExampleSetChanged(currentIndex);
|
2014-02-04 09:29:38 +01:00
|
|
|
}
|
|
|
|
|
|
2017-01-20 12:24:56 +01:00
|
|
|
BaseQtVersion *ExampleSetModel::findHighestQtVersion() const
|
2014-02-04 09:29:38 +01:00
|
|
|
{
|
2017-02-01 15:11:51 +01:00
|
|
|
BaseQtVersion *newVersion = nullptr;
|
|
|
|
|
const QList<BaseQtVersion *> versions = QtVersionManager::versions();
|
|
|
|
|
for (BaseQtVersion *version : versions) {
|
2014-02-04 09:29:38 +01:00
|
|
|
if (!newVersion) {
|
|
|
|
|
newVersion = version;
|
|
|
|
|
} else {
|
|
|
|
|
if (version->qtVersion() > newVersion->qtVersion()) {
|
|
|
|
|
newVersion = version;
|
|
|
|
|
} else if (version->qtVersion() == newVersion->qtVersion()
|
|
|
|
|
&& version->uniqueId() < newVersion->uniqueId()) {
|
|
|
|
|
newVersion = version;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!newVersion && !versions.isEmpty())
|
|
|
|
|
newVersion = versions.first();
|
|
|
|
|
|
2014-03-27 17:50:58 +01:00
|
|
|
return newVersion;
|
2014-02-03 17:17:08 +01:00
|
|
|
}
|
|
|
|
|
|
2017-01-20 12:24:56 +01:00
|
|
|
QStringList ExampleSetModel::exampleSources(QString *examplesInstallPath, QString *demosInstallPath)
|
2010-11-11 16:49:17 +01:00
|
|
|
{
|
2012-01-31 16:36:52 +01:00
|
|
|
QStringList sources;
|
|
|
|
|
|
|
|
|
|
// Qt Creator shipped tutorials
|
2017-01-26 16:23:04 +01:00
|
|
|
sources << ":/qtsupport/qtcreator_tutorials.xml";
|
2010-11-11 16:49:17 +01:00
|
|
|
|
2014-03-27 17:50:58 +01:00
|
|
|
QString examplesPath;
|
|
|
|
|
QString demosPath;
|
|
|
|
|
QString manifestScanPath;
|
|
|
|
|
|
2017-01-20 12:24:56 +01:00
|
|
|
ExampleSetModel::ExampleSetType currentType = getType(m_selectedExampleSetIndex);
|
|
|
|
|
if (currentType == ExampleSetModel::ExtraExampleSetType) {
|
|
|
|
|
int index = getExtraExampleSetIndex(m_selectedExampleSetIndex);
|
2014-03-27 17:50:58 +01:00
|
|
|
ExtraExampleSet exampleSet = m_extraExampleSets.at(index);
|
|
|
|
|
manifestScanPath = exampleSet.manifestPath;
|
|
|
|
|
examplesPath = exampleSet.examplesPath;
|
|
|
|
|
demosPath = exampleSet.examplesPath;
|
|
|
|
|
} else if (currentType == ExampleSetModel::QtExampleSet) {
|
2017-01-20 12:24:56 +01:00
|
|
|
int qtId = getQtId(m_selectedExampleSetIndex);
|
2017-02-01 15:11:51 +01:00
|
|
|
foreach (BaseQtVersion *version, QtVersionManager::versions()) {
|
2014-03-27 17:50:58 +01:00
|
|
|
if (version->uniqueId() == qtId) {
|
|
|
|
|
manifestScanPath = version->documentationPath();
|
|
|
|
|
examplesPath = version->examplesPath();
|
|
|
|
|
demosPath = version->demosPath();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-08-03 14:19:10 +02:00
|
|
|
}
|
2014-03-27 17:50:58 +01:00
|
|
|
if (!manifestScanPath.isEmpty()) {
|
|
|
|
|
// search for examples-manifest.xml, demos-manifest.xml in <path>/*/
|
|
|
|
|
QDir dir = QDir(manifestScanPath);
|
2014-02-04 09:57:17 +01:00
|
|
|
const QStringList examplesPattern(QLatin1String("examples-manifest.xml"));
|
|
|
|
|
const QStringList demosPattern(QLatin1String("demos-manifest.xml"));
|
|
|
|
|
QFileInfoList fis;
|
2014-03-27 17:50:58 +01:00
|
|
|
foreach (QFileInfo subDir, dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot)) {
|
2014-02-04 09:57:17 +01:00
|
|
|
fis << QDir(subDir.absoluteFilePath()).entryInfoList(examplesPattern);
|
|
|
|
|
fis << QDir(subDir.absoluteFilePath()).entryInfoList(demosPattern);
|
|
|
|
|
}
|
2014-03-27 17:50:58 +01:00
|
|
|
foreach (const QFileInfo &fi, fis)
|
|
|
|
|
sources.append(fi.filePath());
|
2010-11-11 16:49:17 +01:00
|
|
|
}
|
2014-03-27 17:50:58 +01:00
|
|
|
if (examplesInstallPath)
|
|
|
|
|
*examplesInstallPath = examplesPath;
|
|
|
|
|
if (demosInstallPath)
|
|
|
|
|
*demosInstallPath = demosPath;
|
2010-11-11 16:49:17 +01:00
|
|
|
|
2012-01-31 16:36:52 +01:00
|
|
|
return sources;
|
2010-11-11 16:49:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ExamplesListModel::rowCount(const QModelIndex &) const
|
|
|
|
|
{
|
2012-12-19 10:19:35 +01:00
|
|
|
return m_exampleItems.size();
|
2010-11-11 16:49:17 +01:00
|
|
|
}
|
|
|
|
|
|
2013-02-04 10:16:02 +01:00
|
|
|
QString prefixForItem(const ExampleItem &item)
|
|
|
|
|
{
|
|
|
|
|
if (item.isHighlighted)
|
|
|
|
|
return QLatin1String("0000 ");
|
|
|
|
|
return QString();
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-11 16:49:17 +01:00
|
|
|
QVariant ExamplesListModel::data(const QModelIndex &index, int role) const
|
|
|
|
|
{
|
2017-01-16 18:06:28 +01:00
|
|
|
if (!index.isValid() || index.row() >= m_exampleItems.count())
|
2010-11-11 16:49:17 +01:00
|
|
|
return QVariant();
|
|
|
|
|
|
2017-01-16 18:06:28 +01:00
|
|
|
const ExampleItem &item = m_exampleItems.at(index.row());
|
2010-11-11 16:49:17 +01:00
|
|
|
switch (role)
|
|
|
|
|
{
|
|
|
|
|
case Qt::DisplayRole: // for search only
|
2017-01-16 18:06:28 +01:00
|
|
|
return QString(prefixForItem(item) + item.name + ' ' + item.tags.join(' '));
|
|
|
|
|
case Qt::UserRole:
|
|
|
|
|
return QVariant::fromValue<ExampleItem>(item);
|
2010-11-11 16:49:17 +01:00
|
|
|
default:
|
|
|
|
|
return QVariant();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-20 12:24:56 +01:00
|
|
|
void ExampleSetModel::selectExampleSet(int index)
|
2012-02-01 13:15:08 +01:00
|
|
|
{
|
2017-01-20 12:24:56 +01:00
|
|
|
if (index != m_selectedExampleSetIndex) {
|
|
|
|
|
m_selectedExampleSetIndex = index;
|
|
|
|
|
writeCurrentIdToSettings(m_selectedExampleSetIndex);
|
|
|
|
|
emit selectedExampleSetChanged(m_selectedExampleSetIndex);
|
|
|
|
|
}
|
2012-02-01 13:15:08 +01:00
|
|
|
}
|
2011-07-01 18:28:56 +02:00
|
|
|
|
2017-01-20 12:24:56 +01:00
|
|
|
void ExampleSetModel::qtVersionManagerLoaded()
|
2014-02-04 09:29:38 +01:00
|
|
|
{
|
2017-01-20 12:24:56 +01:00
|
|
|
m_qtVersionManagerInitialized = true;
|
|
|
|
|
tryToInitialize();
|
2014-02-04 09:29:38 +01:00
|
|
|
}
|
|
|
|
|
|
2017-01-20 12:24:56 +01:00
|
|
|
void ExampleSetModel::helpManagerInitialized()
|
2014-02-04 09:29:38 +01:00
|
|
|
{
|
2017-01-20 12:24:56 +01:00
|
|
|
m_helpManagerInitialized = true;
|
|
|
|
|
tryToInitialize();
|
2014-02-04 09:29:38 +01:00
|
|
|
}
|
|
|
|
|
|
2017-01-20 12:24:56 +01:00
|
|
|
|
|
|
|
|
void ExampleSetModel::tryToInitialize()
|
2017-01-16 18:06:28 +01:00
|
|
|
{
|
2017-01-20 12:24:56 +01:00
|
|
|
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();
|
2017-01-16 18:06:28 +01:00
|
|
|
}
|
|
|
|
|
|
2017-01-20 12:24:56 +01:00
|
|
|
|
2017-01-16 18:06:28 +01:00
|
|
|
ExamplesListModelFilter::ExamplesListModelFilter(ExamplesListModel *sourceModel, bool showTutorialsOnly, QObject *parent) :
|
2013-02-04 10:28:16 +01:00
|
|
|
QSortFilterProxyModel(parent),
|
2017-01-20 12:24:56 +01:00
|
|
|
m_showTutorialsOnly(showTutorialsOnly)
|
2013-10-15 13:21:17 +02:00
|
|
|
{
|
2017-01-20 12:24:56 +01:00
|
|
|
setSourceModel(sourceModel);
|
2017-01-16 18:06:28 +01:00
|
|
|
setDynamicSortFilter(true);
|
|
|
|
|
setFilterCaseSensitivity(Qt::CaseInsensitive);
|
|
|
|
|
sort(0);
|
2010-11-11 16:49:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ExamplesListModelFilter::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
|
|
|
|
|
{
|
2017-01-16 18:06:28 +01:00
|
|
|
const ExampleItem item = sourceModel()->index(sourceRow, 0, sourceParent).data(Qt::UserRole).value<ExampleItem>();
|
2011-07-22 14:29:14 +02:00
|
|
|
|
2017-01-16 18:06:28 +01:00
|
|
|
if (m_showTutorialsOnly && item.type != Tutorial)
|
|
|
|
|
return false;
|
2011-11-23 12:25:32 +01:00
|
|
|
|
2017-01-16 18:06:28 +01:00
|
|
|
if (!m_showTutorialsOnly && item.type != Example && item.type != Demo)
|
|
|
|
|
return false;
|
2011-07-22 14:29:14 +02:00
|
|
|
|
|
|
|
|
if (!m_filterTags.isEmpty()) {
|
2017-01-16 18:06:28 +01:00
|
|
|
return Utils::allOf(m_filterTags, [&item](const QString &filterTag) {
|
|
|
|
|
return item.tags.contains(filterTag);
|
2014-07-07 19:02:26 +02:00
|
|
|
});
|
2011-07-22 14:29:14 +02:00
|
|
|
}
|
|
|
|
|
|
2016-10-31 14:32:47 +01:00
|
|
|
if (!m_filterStrings.isEmpty()) {
|
2017-01-16 18:06:28 +01:00
|
|
|
for (const QString &subString : m_filterStrings) {
|
2011-07-22 14:29:14 +02:00
|
|
|
bool wordMatch = false;
|
2017-01-16 18:06:28 +01:00
|
|
|
wordMatch |= bool(item.name.contains(subString, Qt::CaseInsensitive));
|
2011-07-22 14:29:14 +02:00
|
|
|
if (wordMatch)
|
|
|
|
|
continue;
|
2017-01-16 18:06:28 +01:00
|
|
|
const auto subMatch = [&subString](const QString &elem) { return elem.contains(subString); };
|
|
|
|
|
wordMatch |= Utils::contains(item.tags, subMatch);
|
2011-07-22 14:29:14 +02:00
|
|
|
if (wordMatch)
|
|
|
|
|
continue;
|
2017-01-16 18:06:28 +01:00
|
|
|
wordMatch |= bool(item.description.contains(subString, Qt::CaseInsensitive));
|
2011-07-22 14:29:14 +02:00
|
|
|
if (!wordMatch)
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2010-11-11 16:49:17 +01:00
|
|
|
}
|
|
|
|
|
|
2012-12-19 10:19:35 +01:00
|
|
|
return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
|
2010-11-11 16:49:17 +01:00
|
|
|
}
|
|
|
|
|
|
2012-05-22 16:52:43 +02:00
|
|
|
void ExamplesListModelFilter::delayedUpdateFilter()
|
|
|
|
|
{
|
|
|
|
|
if (m_timerId != 0)
|
|
|
|
|
killTimer(m_timerId);
|
|
|
|
|
|
|
|
|
|
m_timerId = startTimer(320);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ExamplesListModelFilter::timerEvent(QTimerEvent *timerEvent)
|
|
|
|
|
{
|
|
|
|
|
if (m_timerId == timerEvent->timerId()) {
|
2017-01-20 12:24:56 +01:00
|
|
|
invalidateFilter();
|
2012-05-22 16:52:43 +02:00
|
|
|
killTimer(m_timerId);
|
|
|
|
|
m_timerId = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-26 08:35:57 +01:00
|
|
|
struct SearchStringLexer
|
|
|
|
|
{
|
2011-07-22 14:29:14 +02:00
|
|
|
QString code;
|
|
|
|
|
const QChar *codePtr;
|
|
|
|
|
QChar yychar;
|
|
|
|
|
QString yytext;
|
|
|
|
|
|
|
|
|
|
enum TokenKind {
|
|
|
|
|
END_OF_STRING = 0,
|
|
|
|
|
TAG,
|
|
|
|
|
STRING_LITERAL,
|
|
|
|
|
UNKNOWN
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
inline void yyinp() { yychar = *codePtr++; }
|
|
|
|
|
|
|
|
|
|
SearchStringLexer(const QString &code)
|
|
|
|
|
: code(code)
|
|
|
|
|
, codePtr(code.unicode())
|
2012-01-06 17:43:27 +01:00
|
|
|
, yychar(QLatin1Char(' ')) { }
|
2011-07-22 14:29:14 +02:00
|
|
|
|
|
|
|
|
int operator()() { return yylex(); }
|
|
|
|
|
|
|
|
|
|
int yylex() {
|
|
|
|
|
while (yychar.isSpace())
|
|
|
|
|
yyinp(); // skip all the spaces
|
|
|
|
|
|
|
|
|
|
yytext.clear();
|
|
|
|
|
|
|
|
|
|
if (yychar.isNull())
|
|
|
|
|
return END_OF_STRING;
|
|
|
|
|
|
|
|
|
|
QChar ch = yychar;
|
|
|
|
|
yyinp();
|
|
|
|
|
|
|
|
|
|
switch (ch.unicode()) {
|
|
|
|
|
case '"':
|
|
|
|
|
case '\'':
|
|
|
|
|
{
|
|
|
|
|
const QChar quote = ch;
|
|
|
|
|
yytext.clear();
|
|
|
|
|
while (!yychar.isNull()) {
|
|
|
|
|
if (yychar == quote) {
|
|
|
|
|
yyinp();
|
|
|
|
|
break;
|
2014-05-05 17:16:54 +03:00
|
|
|
}
|
|
|
|
|
if (yychar == QLatin1Char('\\')) {
|
2011-07-22 14:29:14 +02:00
|
|
|
yyinp();
|
|
|
|
|
switch (yychar.unicode()) {
|
2012-01-06 17:43:27 +01:00
|
|
|
case '"': yytext += QLatin1Char('"'); yyinp(); break;
|
|
|
|
|
case '\'': yytext += QLatin1Char('\''); yyinp(); break;
|
|
|
|
|
case '\\': yytext += QLatin1Char('\\'); yyinp(); break;
|
2011-07-22 14:29:14 +02:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
yytext += yychar;
|
|
|
|
|
yyinp();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return STRING_LITERAL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
2012-01-06 17:43:27 +01:00
|
|
|
if (ch.isLetterOrNumber() || ch == QLatin1Char('_')) {
|
2011-07-22 14:29:14 +02:00
|
|
|
yytext.clear();
|
|
|
|
|
yytext += ch;
|
2012-01-06 17:43:27 +01:00
|
|
|
while (yychar.isLetterOrNumber() || yychar == QLatin1Char('_')) {
|
2011-07-22 14:29:14 +02:00
|
|
|
yytext += yychar;
|
|
|
|
|
yyinp();
|
|
|
|
|
}
|
2012-01-06 17:43:27 +01:00
|
|
|
if (yychar == QLatin1Char(':') && yytext == QLatin1String("tag")) {
|
2011-07-22 14:29:14 +02:00
|
|
|
yyinp();
|
|
|
|
|
return TAG;
|
|
|
|
|
}
|
|
|
|
|
return STRING_LITERAL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
yytext += ch;
|
|
|
|
|
return UNKNOWN;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-31 14:32:47 +01:00
|
|
|
void ExamplesListModelFilter::setSearchString(const QString &arg)
|
2011-07-22 14:29:14 +02:00
|
|
|
{
|
2016-10-31 14:32:47 +01:00
|
|
|
if (m_searchString == arg)
|
|
|
|
|
return;
|
2017-01-20 12:24:56 +01:00
|
|
|
|
2016-10-31 14:32:47 +01:00
|
|
|
m_searchString = arg;
|
2017-01-20 12:24:56 +01:00
|
|
|
m_filterTags.clear();
|
|
|
|
|
m_filterStrings.clear();
|
|
|
|
|
|
2016-10-31 14:32:47 +01:00
|
|
|
// parse and update
|
2011-07-22 14:29:14 +02:00
|
|
|
SearchStringLexer lex(arg);
|
|
|
|
|
bool isTag = false;
|
|
|
|
|
while (int tk = lex()) {
|
|
|
|
|
if (tk == SearchStringLexer::TAG) {
|
|
|
|
|
isTag = true;
|
2017-01-20 12:24:56 +01:00
|
|
|
m_filterStrings.append(lex.yytext);
|
2011-07-22 14:29:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (tk == SearchStringLexer::STRING_LITERAL) {
|
|
|
|
|
if (isTag) {
|
2017-01-20 12:24:56 +01:00
|
|
|
m_filterStrings.pop_back();
|
|
|
|
|
m_filterTags.append(lex.yytext);
|
2011-07-22 14:29:14 +02:00
|
|
|
isTag = false;
|
|
|
|
|
} else {
|
2017-01-20 12:24:56 +01:00
|
|
|
m_filterStrings.append(lex.yytext);
|
2011-07-22 14:29:14 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-22 16:52:43 +02:00
|
|
|
delayedUpdateFilter();
|
2011-07-22 14:29:14 +02:00
|
|
|
}
|
|
|
|
|
|
2010-11-11 16:49:17 +01:00
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace QtSupport
|