Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
helpdocsettings.cpp
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
5
6#include <QtHelp/QCompressedHelpInfo>
7#include <QtHelp/QHelpEngineCore>
8#include <QtHelp/QHelpFilterEngine>
9
10#include <QtCore/QVersionNumber>
11
12#include <QtDebug>
13
15
32
33
34HelpDocSettings::HelpDocSettings()
36{
37}
38
39HelpDocSettings::HelpDocSettings(const HelpDocSettings &) = default;
40
41HelpDocSettings::HelpDocSettings(HelpDocSettings &&) = default;
42
43HelpDocSettings::~HelpDocSettings() = default;
44
45HelpDocSettings &HelpDocSettings::operator=(const HelpDocSettings &) = default;
46
47HelpDocSettings &HelpDocSettings::operator=(HelpDocSettings &&) = default;
48
49bool HelpDocSettings::addDocumentation(const QString &fileName)
50{
51 const QCompressedHelpInfo info = QCompressedHelpInfo::fromCompressedHelpFile(fileName);
52
53 if (info.isNull())
54 return false;
55
56 const QString namespaceName = info.namespaceName();
57
58 if (d->m_namespaceToFileName.contains(namespaceName))
59 return false;
60
61 if (d->m_fileNameToNamespace.contains(fileName))
62 return false;
63
64 const QString component = info.component();
65 const QVersionNumber version = info.version();
66
67 d->m_namespaceToFileName.insert(namespaceName, fileName);
68 d->m_fileNameToNamespace.insert(fileName, namespaceName);
69
70 d->m_namespaceToComponent.insert(namespaceName, component);
71 d->m_componentToNamespace[component].append(namespaceName);
72
73 d->m_namespaceToVersion.insert(namespaceName, version);
74 d->m_versionToNamespace[version].append(namespaceName);
75
76 return true;
77}
78
79bool HelpDocSettings::removeDocumentation(const QString &namespaceName)
80{
81 if (namespaceName.isEmpty())
82 return false;
83
84 const QString fileName = d->m_namespaceToFileName.value(namespaceName);
85 if (fileName.isEmpty())
86 return false;
87
88 const QString component = d->m_namespaceToComponent.value(namespaceName);
89 const QVersionNumber version = d->m_namespaceToVersion.value(namespaceName);
90
91 d->m_namespaceToComponent.remove(namespaceName);
92 d->m_namespaceToVersion.remove(namespaceName);
93 d->m_namespaceToFileName.remove(namespaceName);
94 d->m_fileNameToNamespace.remove(fileName);
95 d->m_componentToNamespace[component].removeOne(namespaceName);
96 if (d->m_componentToNamespace[component].isEmpty())
97 d->m_componentToNamespace.remove(component);
98 d->m_versionToNamespace[version].removeOne(namespaceName);
99 if (d->m_versionToNamespace[version].isEmpty())
100 d->m_versionToNamespace.remove(version);
101
102 return true;
103}
104
105QString HelpDocSettings::namespaceName(const QString &fileName) const
106{
107 return d->m_fileNameToNamespace.value(fileName);
108}
109
110QStringList HelpDocSettings::components() const
111{
112 return d->m_componentToNamespace.keys();
113}
114
115QList<QVersionNumber> HelpDocSettings::versions() const
116{
117 return d->m_versionToNamespace.keys();
118}
119
120QStringList HelpDocSettings::namespaces() const
121{
122 return d->m_namespaceToFileName.keys();
123}
124
125QMap<QString, QString> HelpDocSettings::namespaceToFileName() const
126{
127 return d->m_namespaceToFileName;
128}
129
130HelpDocSettings HelpDocSettings::readSettings(QHelpEngineCore *helpEngine)
131{
132 QHelpFilterEngine *filterEngine = helpEngine->filterEngine();
133
134 HelpDocSettings docSettings;
135 docSettings.d->m_namespaceToComponent = filterEngine->namespaceToComponent();
136 docSettings.d->m_namespaceToVersion = filterEngine->namespaceToVersion();
137 for (auto it = docSettings.d->m_namespaceToComponent.constBegin();
138 it != docSettings.d->m_namespaceToComponent.constEnd(); ++it) {
139 const QString namespaceName = it.key();
140 const QString namespaceFileName = helpEngine->documentationFileName(namespaceName);
141 docSettings.d->m_namespaceToFileName.insert(namespaceName, namespaceFileName);
142 docSettings.d->m_fileNameToNamespace.insert(namespaceFileName, namespaceName);
143 docSettings.d->m_componentToNamespace[it.value()].append(namespaceName);
144 }
145 for (auto it = docSettings.d->m_namespaceToVersion.constBegin();
146 it != docSettings.d->m_namespaceToVersion.constEnd(); ++it) {
147 docSettings.d->m_versionToNamespace[it.value()].append(it.key());
148 }
149
150 return docSettings;
151}
152
153static QMap<QString, QString> subtract(const QMap<QString, QString> &minuend,
154 const QMap<QString, QString> &subtrahend)
155{
156 auto result = minuend;
157
158 for (auto itSubtrahend = subtrahend.cbegin(); itSubtrahend != subtrahend.cend(); ++itSubtrahend) {
159 auto itResult = result.find(itSubtrahend.key());
160 if (itResult != result.end() && itSubtrahend.value() == itResult.value())
161 result.erase(itResult);
162 }
163
164 return result;
165}
166
167bool HelpDocSettings::applySettings(QHelpEngineCore *helpEngine,
168 const HelpDocSettings &settings)
169{
170 const HelpDocSettings oldSettings = readSettings(helpEngine);
171
172 const QMap<QString, QString> docsToRemove = subtract(
173 oldSettings.namespaceToFileName(),
174 settings.namespaceToFileName());
175 const QMap<QString, QString> docsToAdd = subtract(
176 settings.namespaceToFileName(),
177 oldSettings.namespaceToFileName());
178
179 for (auto it = docsToRemove.cbegin(); it != docsToRemove.cend(); ++it) {
180 if (!helpEngine->unregisterDocumentation(it.key()))
181 qWarning() << "Cannot unregister documentation:" << it.key();
182 }
183
184 for (const QString &fileName : docsToAdd) {
185 if (!helpEngine->registerDocumentation(fileName))
186 qWarning() << "Cannot register documentation file:" << fileName;
187 }
188
189 return !docsToRemove.isEmpty() || !docsToAdd.isEmpty();
190}
191
192QT_END_NAMESPACE
QMap< QString, QVersionNumber > m_namespaceToVersion
QMap< QString, QStringList > m_componentToNamespace
QMap< QString, QString > m_namespaceToFileName
HelpDocSettingsPrivate(const HelpDocSettingsPrivate &other)=default
~HelpDocSettingsPrivate()=default
QMap< QVersionNumber, QStringList > m_versionToNamespace
QMap< QString, QString > m_namespaceToComponent
QMap< QString, QString > m_fileNameToNamespace
HelpDocSettingsPrivate()=default
HelpDocSettings(const HelpDocSettings &other)
bool removeDocumentation(const QString &namespaceName)
QString namespaceName(const QString &fileName) const
static bool applySettings(QHelpEngineCore *helpEngine, const HelpDocSettings &settings)
QMap< QString, QString > namespaceToFileName() const
static HelpDocSettings readSettings(QHelpEngineCore *helpEngine)
QList< QVersionNumber > versions() const
QStringList namespaces() const
HelpDocSettings(HelpDocSettings &&other)
HelpDocSettings & operator=(const HelpDocSettings &other)
QStringList components() const
HelpDocSettings & operator=(HelpDocSettings &&other)
bool addDocumentation(const QString &fileName)
\inmodule QtCore
static QMap< QString, QString > subtract(const QMap< QString, QString > &minuend, const QMap< QString, QString > &subtrahend)
Combined button and popup list for selecting options.