exampleslistmodel.cpp 30.7 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3 4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8 9 10 11
** 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
12 13 14
** 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.
15
**
16 17 18 19 20 21 22
** 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.
23
**
hjk's avatar
hjk committed
24
****************************************************************************/
25

26 27
#include "exampleslistmodel.h"

28
#include <QDebug>
29
#include <QDir>
30
#include <QFile>
31
#include <QUrl>
32
#include <QXmlStreamReader>
33

34
#include <coreplugin/helpmanager.h>
35
#include <coreplugin/icore.h>
36

37
#include <qtsupport/qtkitinformation.h>
38
#include <qtsupport/qtversionmanager.h>
39 40

#include <utils/algorithm.h>
41
#include <utils/environment.h>
42
#include <utils/qtcassert.h>
43 44 45 46 47 48

#include <algorithm>

namespace QtSupport {
namespace Internal {

49 50
static bool debugExamples()
{
51
    static bool isDebugging = qEnvironmentVariableIsSet("QTC_DEBUG_EXAMPLESMODEL");
52 53
    return isDebugging;
}
54

55 56 57
static const char kSelectedExampleSetKey[] = "WelcomePage/SelectedExampleSet";

void ExampleSetModel::writeCurrentIdToSettings(int currentIndex) const
58 59
{
    QSettings *settings = Core::ICore::settings();
60
    settings->setValue(QLatin1String(kSelectedExampleSetKey), getId(currentIndex));
61 62
}

63
int ExampleSetModel::readCurrentIndexFromSettings() const
64
{
65 66 67 68 69 70
    QVariant id = Core::ICore::settings()->value(QLatin1String(kSelectedExampleSetKey));
    for (int i=0; i < rowCount(); i++) {
        if (id == getId(i))
            return i;
    }
    return -1;
71 72
}

73
ExampleSetModel::ExampleSetModel()
74
{
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    // 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);
107 108 109 110 111

    if (auto helpManager = Core::HelpManager::instance()) {
        connect(helpManager, &Core::HelpManager::setupFinished,
                this, &ExampleSetModel::helpManagerInitialized);
    }
112
}
113

114
void ExampleSetModel::recreateModel(const QList<BaseQtVersion *> &qtVersions)
115 116 117
{
    beginResetModel();
    clear();
118

119
    QSet<QString> extraManifestDirs;
120 121
    for (int i = 0; i < m_extraExampleSets.size(); ++i)  {
        const ExtraExampleSet &set = m_extraExampleSets.at(i);
122
        QStandardItem *newItem = new QStandardItem();
123
        newItem->setData(set.displayName, Qt::DisplayRole);
124 125 126 127 128 129 130 131
        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);
    }

132
    foreach (BaseQtVersion *version, qtVersions) {
133 134 135 136 137 138 139 140
        // 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;
        }
141
        QStandardItem *newItem = new QStandardItem();
142
        newItem->setData(version->displayName(), Qt::DisplayRole);
143 144
        newItem->setData(version->displayName(), Qt::UserRole + 1);
        newItem->setData(version->uniqueId(), Qt::UserRole + 2);
145
        newItem->setData(QVariant(), Qt::UserRole + 3);
146
        appendRow(newItem);
147
    }
148 149
    endResetModel();
}
150

151 152 153 154
int ExampleSetModel::indexForQtVersion(BaseQtVersion *qtVersion) const
{
    // return either the entry with the same QtId, or an extra example set with same path

155 156 157
    if (!qtVersion)
        return -1;

158 159 160
    // check for Qt version
    for (int i = 0; i < rowCount(); ++i) {
        if (getType(i) == QtExampleSet && getQtId(i) == qtVersion->uniqueId())
161
            return i;
162
    }
163

164 165 166
    // check for extra set
    const QString &documentationPath = qtVersion->documentationPath();
    for (int i = 0; i < rowCount(); ++i) {
167 168
        if (getType(i) == ExtraExampleSetType
                && m_extraExampleSets.at(getExtraExampleSetIndex(i)).manifestPath == documentationPath)
169 170 171
            return i;
    }
    return -1;
172
}
173

174
QVariant ExampleSetModel::getDisplayName(int i) const
175
{
176 177 178
    if (i < 0 || i >= rowCount())
        return QVariant();
    return data(index(i, 0), Qt::UserRole + 1);
179 180
}

181 182
// id is either the Qt version uniqueId, or the display name of the extra example set
QVariant ExampleSetModel::getId(int i) const
183
{
184 185 186
    if (i < 0 || i >= rowCount())
        return QVariant();
    QModelIndex modelIndex = index(i, 0);
187
    QVariant variant = data(modelIndex, Qt::UserRole + 2);
188 189 190 191 192 193 194 195 196 197 198 199 200
    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;
201
    return ExtraExampleSetType;
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
}

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();
222
}
223

224 225
ExamplesListModel::ExamplesListModel(QObject *parent)
    : QAbstractListModel(parent)
226
{
227 228
    connect(&m_exampleSetModel, &ExampleSetModel::selectedExampleSetChanged,
            this, &ExamplesListModel::updateExamples);
229 230
}

231
static QString fixStringForTags(const QString &string)
232 233
{
    QString returnString = string;
234 235 236 237
    returnString.remove(QLatin1String("<i>"));
    returnString.remove(QLatin1String("</i>"));
    returnString.remove(QLatin1String("<tt>"));
    returnString.remove(QLatin1String("</tt>"));
238 239 240
    return returnString;
}

241
static QStringList trimStringList(const QStringList &stringlist)
242
{
243
    return Utils::transform(stringlist, [](const QString &str) { return str.trimmed(); });
244 245
}

246 247 248 249 250 251 252 253
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;
254
    if (QFile::exists(installResolvedPath))
255 256 257 258 259
        return installResolvedPath;
    // doesn't exist, just return relative
    return relativeResolvedPath;
}

260 261 262 263
static bool isValidExampleOrDemo(ExampleItem &item)
{
    static QString invalidPrefix = QLatin1String("qthelp:////"); /* means that the qthelp url
                                                                    doesn't have any namespace */
264
    QString reason;
265
    bool ok = true;
266
    if (!item.hasSourceCode || !QFileInfo::exists(item.projectPath)) {
267
        ok = false;
268
        reason = QString::fromLatin1("projectPath \"%1\" empty or does not exist").arg(item.projectPath);
269
    } else if (item.imageUrl.startsWith(invalidPrefix) || !QUrl(item.imageUrl).isValid()) {
270
        ok = false;
271
        reason = QString::fromLatin1("imageUrl \"%1\" not valid").arg(item.imageUrl);
272 273
    } else if (!item.docUrl.isEmpty()
             && (item.docUrl.startsWith(invalidPrefix) || !QUrl(item.docUrl).isValid())) {
274
        ok = false;
275
        reason = QString::fromLatin1("docUrl \"%1\" non-empty but not valid").arg(item.docUrl);
276 277
    }
    if (!ok) {
278
        item.tags.append(QLatin1String("broken"));
279
        if (debugExamples())
280
            qWarning() << QString::fromLatin1("ERROR: Item \"%1\" broken: %2").arg(item.name, reason);
281 282
    }
    if (debugExamples() && item.description.isEmpty())
283
        qWarning() << QString::fromLatin1("WARNING: Item \"%1\" has no description").arg(item.name);
284
    return ok || debugExamples();
285 286
}

287 288
void ExamplesListModel::parseExamples(QXmlStreamReader *reader,
    const QString &projectsOffset, const QString &examplesInstallPath)
289 290
{
    ExampleItem item;
291
    const QChar slash = QLatin1Char('/');
292 293 294 295 296 297 298 299 300 301
    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();
302
                item.projectPath = relativeOrInstallPath(item.projectPath, projectsOffset, examplesInstallPath);
303
                item.imageUrl = attributes.value(QLatin1String("imageUrl")).toString();
304
                item.docUrl = attributes.value(QLatin1String("docUrl")).toString();
305
                item.isHighlighted = attributes.value(QLatin1String("isHighlighted")).toString() == QLatin1String("true");
306

307
            } else if (reader->name() == QLatin1String("fileToOpen")) {
308 309 310 311 312 313 314 315
                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;
316
            } else if (reader->name() == QLatin1String("description")) {
317
                item.description = fixStringForTags(reader->readElementText(QXmlStreamReader::ErrorOnUnexpectedElement));
318
            } else if (reader->name() == QLatin1String("dependency")) {
319
                item.dependencies.append(projectsOffset + slash + reader->readElementText(QXmlStreamReader::ErrorOnUnexpectedElement));
320
            } else if (reader->name() == QLatin1String("tags")) {
321 322 323 324
                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));
        }
325 326
            break;
        case QXmlStreamReader::EndElement:
327
            if (reader->name() == QLatin1String("example")) {
328
                if (isValidExampleOrDemo(item))
329
                    m_exampleItems.append(item);
330
            } else if (reader->name() == QLatin1String("examples")) {
331
                return;
332
            }
333 334 335 336 337 338 339
            break;
        default: // nothing
            break;
        }
    }
}

340 341
void ExamplesListModel::parseDemos(QXmlStreamReader *reader,
    const QString &projectsOffset, const QString &demosInstallPath)
342 343
{
    ExampleItem item;
344
    const QChar slash = QLatin1Char('/');
345 346 347 348 349 350 351 352 353 354
    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();
355
                item.projectPath = relativeOrInstallPath(item.projectPath, projectsOffset, demosInstallPath);
356 357
                item.imageUrl = attributes.value(QLatin1String("imageUrl")).toString();
                item.docUrl = attributes.value(QLatin1String("docUrl")).toString();
358
                item.isHighlighted = attributes.value(QLatin1String("isHighlighted")).toString() == QLatin1String("true");
359
            } else if (reader->name() == QLatin1String("fileToOpen")) {
360 361
                item.filesToOpen.append(relativeOrInstallPath(reader->readElementText(QXmlStreamReader::ErrorOnUnexpectedElement),
                                                              projectsOffset, demosInstallPath));
362
            } else if (reader->name() == QLatin1String("description")) {
363 364
                item.description =  fixStringForTags(reader->readElementText(QXmlStreamReader::ErrorOnUnexpectedElement));
            } else if (reader->name() == QLatin1String("dependency")) {
365
                item.dependencies.append(projectsOffset + slash + reader->readElementText(QXmlStreamReader::ErrorOnUnexpectedElement));
366
            } else if (reader->name() == QLatin1String("tags")) {
367
                item.tags = reader->readElementText(QXmlStreamReader::ErrorOnUnexpectedElement).split(QLatin1Char(','));
368 369 370
            }
            break;
        case QXmlStreamReader::EndElement:
Eike Ziller's avatar
Eike Ziller committed
371
            if (reader->name() == QLatin1String("demo")) {
372
                if (isValidExampleOrDemo(item))
373
                    m_exampleItems.append(item);
Eike Ziller's avatar
Eike Ziller committed
374
            } else if (reader->name() == QLatin1String("demos")) {
375
                return;
Eike Ziller's avatar
Eike Ziller committed
376
            }
377 378 379 380 381 382 383
            break;
        default: // nothing
            break;
        }
    }
}

384
void ExamplesListModel::parseTutorials(QXmlStreamReader *reader, const QString &projectsOffset)
385 386
{
    ExampleItem item;
387
    const QChar slash = QLatin1Char('/');
388 389 390 391 392 393 394 395 396 397
    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();
398
                item.projectPath.prepend(slash);
399
                item.projectPath.prepend(projectsOffset);
400 401 402 403 404
                item.imageUrl = attributes.value(QLatin1String("imageUrl")).toString();
                item.docUrl = attributes.value(QLatin1String("docUrl")).toString();
                item.isVideo = attributes.value(QLatin1String("isVideo")).toString() == QLatin1String("true");
                item.videoUrl = attributes.value(QLatin1String("videoUrl")).toString();
                item.videoLength = attributes.value(QLatin1String("videoLength")).toString();
405
            } else if (reader->name() == QLatin1String("fileToOpen")) {
406
                item.filesToOpen.append(projectsOffset + slash + reader->readElementText(QXmlStreamReader::ErrorOnUnexpectedElement));
407
            } else if (reader->name() == QLatin1String("description")) {
408 409
                item.description =  fixStringForTags(reader->readElementText(QXmlStreamReader::ErrorOnUnexpectedElement));
            } else if (reader->name() == QLatin1String("dependency")) {
410
                item.dependencies.append(projectsOffset + slash + reader->readElementText(QXmlStreamReader::ErrorOnUnexpectedElement));
411
            } else if (reader->name() == QLatin1String("tags")) {
412
                item.tags = reader->readElementText(QXmlStreamReader::ErrorOnUnexpectedElement).split(QLatin1Char(','));
413 414 415 416
            }
            break;
        case QXmlStreamReader::EndElement:
            if (reader->name() == QLatin1String("tutorial"))
417
                m_exampleItems.append(item);
418
            else if (reader->name() == QLatin1String("tutorials"))
419
                return;
420 421 422 423 424 425 426
            break;
        default: // nothing
            break;
        }
    }
}

427
void ExamplesListModel::updateExamples()
428
{
429 430
    QString examplesInstallPath;
    QString demosInstallPath;
431

432
    QStringList sources = m_exampleSetModel.exampleSources(&examplesInstallPath, &demosInstallPath);
433 434 435 436 437

    beginResetModel();
    m_exampleItems.clear();

    foreach (const QString &exampleSource, sources) {
438 439
        QFile exampleFile(exampleSource);
        if (!exampleFile.open(QIODevice::ReadOnly)) {
440 441
            if (debugExamples())
                qWarning() << "ERROR: Could not open file" << exampleSource;
442
            continue;
443 444 445 446 447 448 449
        }

        QFileInfo fi(exampleSource);
        QString offsetPath = fi.path();
        QDir examplesDir(offsetPath);
        QDir demosDir(offsetPath);

450
        if (debugExamples())
451
            qWarning() << QString::fromLatin1("Reading file \"%1\"...").arg(fi.absoluteFilePath());
452 453 454 455 456
        QXmlStreamReader reader(&exampleFile);
        while (!reader.atEnd())
            switch (reader.readNext()) {
            case QXmlStreamReader::StartElement:
                if (reader.name() == QLatin1String("examples"))
457
                    parseExamples(&reader, examplesDir.path(), examplesInstallPath);
458
                else if (reader.name() == QLatin1String("demos"))
459
                    parseDemos(&reader, demosDir.path(), demosInstallPath);
460
                else if (reader.name() == QLatin1String("tutorials"))
461
                    parseTutorials(&reader, examplesDir.path());
462 463 464 465 466
                break;
            default: // nothing
                break;
            }

467
        if (reader.hasError() && debugExamples())
468
            qWarning() << QString::fromLatin1("ERROR: Could not parse file as XML document (%1)").arg(exampleSource);
469
    }
470
    endResetModel();
471 472
}

473
void ExampleSetModel::updateQtVersionList()
474
{
475
    QList<BaseQtVersion*> versions
476 477
            = QtVersionManager::sortVersions(
                QtVersionManager::versions(BaseQtVersion::isValidPredicate([](const BaseQtVersion *v) {
478
        return v->hasExamples() || v->hasDemos();
479
    })));
480 481 482 483 484 485 486

    // 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);

487
    recreateModel(versions);
488

489 490
    int currentIndex = m_selectedExampleSetIndex;
    if (currentIndex < 0) // reset from settings
491
        currentIndex = readCurrentIndexFromSettings();
492

493
    ExampleSetModel::ExampleSetType currentType = getType(currentIndex);
494

495 496
    if (currentType == ExampleSetModel::InvalidExampleSet) {
        // select examples corresponding to 'highest' Qt version
497
        BaseQtVersion *highestQt = findHighestQtVersion(versions);
498
        currentIndex = indexForQtVersion(highestQt);
499 500 501
    } else if (currentType == ExampleSetModel::QtExampleSet) {
        // try to select the previously selected Qt version, or
        // select examples corresponding to 'highest' Qt version
502
        int currentQtId = getQtId(currentIndex);
503
        BaseQtVersion *newQtVersion = QtVersionManager::version(currentQtId);
504
        if (!newQtVersion)
505
            newQtVersion = findHighestQtVersion(versions);
506
        currentIndex = indexForQtVersion(newQtVersion);
507 508
    } // nothing to do for extra example sets
    selectExampleSet(currentIndex);
509
    emit selectedExampleSetChanged(currentIndex);
510 511
}

512
BaseQtVersion *ExampleSetModel::findHighestQtVersion(const QList<BaseQtVersion *> &versions) const
513
{
514 515
    BaseQtVersion *newVersion = nullptr;
    for (BaseQtVersion *version : versions) {
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
        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();

531
    return newVersion;
532 533
}

534
QStringList ExampleSetModel::exampleSources(QString *examplesInstallPath, QString *demosInstallPath)
535
{
536 537 538
    QStringList sources;

    // Qt Creator shipped tutorials
539
    sources << ":/qtsupport/qtcreator_tutorials.xml";
540

541 542 543 544
    QString examplesPath;
    QString demosPath;
    QString manifestScanPath;

545 546 547
    ExampleSetModel::ExampleSetType currentType = getType(m_selectedExampleSetIndex);
    if (currentType == ExampleSetModel::ExtraExampleSetType) {
        int index = getExtraExampleSetIndex(m_selectedExampleSetIndex);
548 549 550 551 552
        ExtraExampleSet exampleSet = m_extraExampleSets.at(index);
        manifestScanPath = exampleSet.manifestPath;
        examplesPath = exampleSet.examplesPath;
        demosPath = exampleSet.examplesPath;
    } else if (currentType == ExampleSetModel::QtExampleSet) {
553
        int qtId = getQtId(m_selectedExampleSetIndex);
554
        foreach (BaseQtVersion *version, QtVersionManager::versions()) {
555 556 557 558 559 560 561
            if (version->uniqueId() == qtId) {
                manifestScanPath = version->documentationPath();
                examplesPath = version->examplesPath();
                demosPath = version->demosPath();
                break;
            }
        }
562
    }
563 564 565
    if (!manifestScanPath.isEmpty()) {
        // search for examples-manifest.xml, demos-manifest.xml in <path>/*/
        QDir dir = QDir(manifestScanPath);
566 567 568
        const QStringList examplesPattern(QLatin1String("examples-manifest.xml"));
        const QStringList demosPattern(QLatin1String("demos-manifest.xml"));
        QFileInfoList fis;
569
        foreach (QFileInfo subDir, dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot)) {
570 571 572
            fis << QDir(subDir.absoluteFilePath()).entryInfoList(examplesPattern);
            fis << QDir(subDir.absoluteFilePath()).entryInfoList(demosPattern);
        }
573 574
        foreach (const QFileInfo &fi, fis)
            sources.append(fi.filePath());
575
    }
576 577 578 579
    if (examplesInstallPath)
        *examplesInstallPath = examplesPath;
    if (demosInstallPath)
        *demosInstallPath = demosPath;
580

581
    return sources;
582 583 584 585
}

int ExamplesListModel::rowCount(const QModelIndex &) const
{
586
    return m_exampleItems.size();
587 588
}

589 590 591 592 593 594 595
QString prefixForItem(const ExampleItem &item)
{
    if (item.isHighlighted)
        return QLatin1String("0000 ");
    return QString();
}

596 597
QVariant ExamplesListModel::data(const QModelIndex &index, int role) const
{
598
    if (!index.isValid() || index.row() >= m_exampleItems.count())
599 600
        return QVariant();

601
    const ExampleItem &item = m_exampleItems.at(index.row());
602 603 604
    switch (role)
    {
    case Qt::DisplayRole: // for search only
605 606 607
        return QString(prefixForItem(item) + item.name + ' ' + item.tags.join(' '));
    case Qt::UserRole:
        return QVariant::fromValue<ExampleItem>(item);
608 609 610 611 612
    default:
        return QVariant();
    }
}

613
void ExampleSetModel::selectExampleSet(int index)
614
{
615 616 617 618 619
    if (index != m_selectedExampleSetIndex) {
        m_selectedExampleSetIndex = index;
        writeCurrentIdToSettings(m_selectedExampleSetIndex);
        emit selectedExampleSetChanged(m_selectedExampleSetIndex);
    }
620
}
621

622
void ExampleSetModel::qtVersionManagerLoaded()
623
{
624 625
    m_qtVersionManagerInitialized = true;
    tryToInitialize();
626 627
}

628
void ExampleSetModel::helpManagerInitialized()
629
{
630 631
    m_helpManagerInitialized = true;
    tryToInitialize();
632 633
}

634 635

void ExampleSetModel::tryToInitialize()
636
{
637 638 639 640 641
    if (m_initalized)
        return;
    if (!m_qtVersionManagerInitialized)
        return;
    if (Core::HelpManager::instance() && !m_helpManagerInitialized)
642 643 644 645 646 647 648 649 650 651
        return;

    m_initalized = true;

    connect(QtVersionManager::instance(), &QtVersionManager::qtVersionsChanged,
            this, &ExampleSetModel::updateQtVersionList);
    connect(ProjectExplorer::KitManager::instance(), &ProjectExplorer::KitManager::defaultkitChanged,
            this, &ExampleSetModel::updateQtVersionList);

    updateQtVersionList();
652 653
}

654

655
ExamplesListModelFilter::ExamplesListModelFilter(ExamplesListModel *sourceModel, bool showTutorialsOnly, QObject *parent) :
656
    QSortFilterProxyModel(parent),
657
    m_showTutorialsOnly(showTutorialsOnly)
658
{
659
    setSourceModel(sourceModel);
660 661 662
    setDynamicSortFilter(true);
    setFilterCaseSensitivity(Qt::CaseInsensitive);
    sort(0);
663 664 665 666
}

bool ExamplesListModelFilter::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
667
    const ExampleItem item = sourceModel()->index(sourceRow, 0, sourceParent).data(Qt::UserRole).value<ExampleItem>();
668

669 670
    if (m_showTutorialsOnly && item.type != Tutorial)
        return false;
671

672 673
    if (!m_showTutorialsOnly && item.type != Example && item.type != Demo)
        return false;
674 675

    if (!m_filterTags.isEmpty()) {
676 677
        return Utils::allOf(m_filterTags, [&item](const QString &filterTag) {
            return item.tags.contains(filterTag);
678
        });
679 680
    }

681
    if (!m_filterStrings.isEmpty()) {
682
        for (const QString &subString : m_filterStrings) {
683
            bool wordMatch = false;
684
            wordMatch |= bool(item.name.contains(subString, Qt::CaseInsensitive));
685 686
            if (wordMatch)
                continue;
687 688
            const auto subMatch = [&subString](const QString &elem) { return elem.contains(subString); };
            wordMatch |= Utils::contains(item.tags, subMatch);
689 690
            if (wordMatch)
                continue;
691
            wordMatch |= bool(item.description.contains(subString, Qt::CaseInsensitive));
692 693 694
            if (!wordMatch)
                return false;
        }
695 696
    }

697
    return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
698 699
}

thohartm's avatar
thohartm committed
700 701 702 703 704 705 706 707 708 709 710
void ExamplesListModelFilter::delayedUpdateFilter()
{
    if (m_timerId != 0)
        killTimer(m_timerId);

    m_timerId = startTimer(320);
}

void ExamplesListModelFilter::timerEvent(QTimerEvent *timerEvent)
{
    if (m_timerId == timerEvent->timerId()) {
711
        invalidateFilter();
hjk's avatar
hjk committed
712
        emit layoutChanged();
thohartm's avatar
thohartm committed
713 714 715 716 717
        killTimer(m_timerId);
        m_timerId = 0;
    }
}

hjk's avatar
hjk committed
718 719
struct SearchStringLexer
{
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
    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())
737
        , yychar(QLatin1Char(' ')) { }
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762

    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;
763 764
                }
                if (yychar == QLatin1Char('\\')) {
765 766
                    yyinp();
                    switch (yychar.unicode()) {
767 768 769
                    case '"': yytext += QLatin1Char('"'); yyinp(); break;
                    case '\'': yytext += QLatin1Char('\''); yyinp(); break;
                    case '\\': yytext += QLatin1Char('\\'); yyinp(); break;
770 771 772 773 774 775 776 777 778 779
                    }
                } else {
                    yytext += yychar;
                    yyinp();
                }
            }
            return STRING_LITERAL;
        }

        default:
780
            if (ch.isLetterOrNumber() || ch == QLatin1Char('_')) {
781 782
                yytext.clear();
                yytext += ch;
783
                while (yychar.isLetterOrNumber() || yychar == QLatin1Char('_')) {
784 785 786
                    yytext += yychar;
                    yyinp();
                }
787
                if (yychar == QLatin1Char(':') && yytext == QLatin1String("tag")) {
788 789 790 791 792 793 794 795 796 797 798 799
                    yyinp();
                    return TAG;
                }
                return STRING_LITERAL;
            }
        }

        yytext += ch;
        return UNKNOWN;
    }
};

800
void ExamplesListModelFilter::setSearchString(const QString &arg)
801
{
802 803
    if (m_searchString == arg)
        return;
804

805
    m_searchString = arg;
806 807 808
    m_filterTags.clear();
    m_filterStrings.clear();

809
    // parse and update
810 811 812 813 814
    SearchStringLexer lex(arg);
    bool isTag = false;
    while (int tk = lex()) {
        if (tk == SearchStringLexer::TAG) {
            isTag = true;
815
            m_filterStrings.append(lex.yytext);
816 817 818 819
        }

        if (tk == SearchStringLexer::STRING_LITERAL) {
            if (isTag) {
820 821
                m_filterStrings.pop_back();
                m_filterTags.append(lex.yytext);
822 823
                isTag = false;
            } else {
824
                m_filterStrings.append(lex.yytext);
825 826 827 828
            }
        }
    }

thohartm's avatar
thohartm committed
829
    delayedUpdateFilter();
830 831
}

832 833
} // namespace Internal
} // namespace QtSupport