summaryrefslogtreecommitdiffstats
path: root/examples/help/contextsensitivehelp/helpbrowser.cpp
blob: 7dfb110cd3b7e8787f22f3d56cf530c8ba7b73eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

#include <QtCore/QFileInfo>
#include <QtCore/QLibraryInfo>
#include <QtCore/QStandardPaths>
#include <QtWidgets/QApplication>
#include <QtHelp/QHelpEngineCore>

#include "helpbrowser.h"
#include "qhelplink.h"

using namespace Qt::StringLiterals;

static QString documentationDirectory()
{
    QStringList paths;
#ifdef SRCDIR
    paths.append(QLatin1StringView(SRCDIR));
#endif
    paths.append(QLibraryInfo::path(QLibraryInfo::ExamplesPath));
    paths.append(QCoreApplication::applicationDirPath());
    paths.append(QStandardPaths::standardLocations(QStandardPaths::AppDataLocation));
    for (const auto &dir : std::as_const(paths)) {
        const QString path = dir + "/docs"_L1;
        if (QFileInfo::exists(path))
            return path;
    }
    return {};
}

HelpBrowser::HelpBrowser(QWidget *parent)
    : QTextBrowser(parent)
{
    const QString collectionFile = documentationDirectory() + "/wateringmachine.qhc"_L1;

    m_helpEngine = new QHelpEngineCore(collectionFile, this);
    if (!m_helpEngine->setupData()) {
        delete m_helpEngine;
        m_helpEngine = nullptr;
    }
}

void HelpBrowser::showHelpForKeyword(const QString &id)
{
    if (m_helpEngine) {
        QList<QHelpLink> documents = m_helpEngine->documentsForIdentifier(id);
        if (documents.count())
            setSource(documents.first().url);
    }
}

QVariant HelpBrowser::loadResource(int type, const QUrl &name)
{
    QByteArray ba;
    if (type < 4 && m_helpEngine) {
        QUrl url(name);
        if (name.isRelative())
            url = source().resolved(url);
        ba = m_helpEngine->fileData(url);
    }
    return ba;
}