1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qhelpfiltersettings_p.h"
5#include "qhelpfilterdata.h"
6
7#include <QtCore/QMap>
8#include <QtHelp/QHelpFilterEngine>
9
10QT_BEGIN_NAMESPACE
11
12class QHelpFilterSettingsPrivate : public QSharedData
13{
14public:
15 QHelpFilterSettingsPrivate() = default;
16 QHelpFilterSettingsPrivate(const QHelpFilterSettingsPrivate &other) = default;
17 ~QHelpFilterSettingsPrivate() = default;
18
19 QMap<QString, QHelpFilterData> m_filterToData;
20 QString m_currentFilter;
21};
22
23QHelpFilterSettings::QHelpFilterSettings()
24 : d(new QHelpFilterSettingsPrivate)
25{
26}
27
28QHelpFilterSettings::QHelpFilterSettings(const QHelpFilterSettings &) = default;
29
30QHelpFilterSettings::QHelpFilterSettings(QHelpFilterSettings &&) = default;
31
32QHelpFilterSettings::~QHelpFilterSettings() = default;
33
34QHelpFilterSettings &QHelpFilterSettings::operator=(const QHelpFilterSettings &) = default;
35
36QHelpFilterSettings &QHelpFilterSettings::operator=(QHelpFilterSettings &&) = default;
37
38void QHelpFilterSettings::setFilter(const QString &filterName,
39 const QHelpFilterData &filterData)
40{
41 d->m_filterToData.insert(key: filterName, value: filterData);
42}
43
44void QHelpFilterSettings::removeFilter(const QString &filterName)
45{
46 d->m_filterToData.remove(key: filterName);
47}
48
49QStringList QHelpFilterSettings::filterNames() const
50{
51 return d->m_filterToData.keys();
52}
53
54QHelpFilterData QHelpFilterSettings::filterData(const QString &filterName) const
55{
56 return d->m_filterToData.value(key: filterName);
57}
58
59QMap<QString, QHelpFilterData> QHelpFilterSettings::filters() const
60{
61 return d->m_filterToData;
62}
63
64void QHelpFilterSettings::setCurrentFilter(const QString &filterName)
65{
66 d->m_currentFilter = filterName;
67}
68
69QString QHelpFilterSettings::currentFilter() const
70{
71 return d->m_currentFilter;
72}
73
74QHelpFilterSettings QHelpFilterSettings::readSettings(const QHelpFilterEngine *filterEngine)
75{
76 QHelpFilterSettings filterSettings;
77
78 const QStringList allFilters = filterEngine->filters();
79 for (const QString &filter : allFilters)
80 filterSettings.setFilter(filterName: filter, filterData: filterEngine->filterData(filterName: filter));
81
82 filterSettings.setCurrentFilter(filterEngine->activeFilter());
83
84 return filterSettings;
85}
86
87static QMap<QString, QHelpFilterData> subtract(const QMap<QString, QHelpFilterData> &minuend,
88 const QMap<QString, QHelpFilterData> &subtrahend)
89{
90 QMap<QString, QHelpFilterData> result = minuend;
91
92 for (auto itSubtrahend = subtrahend.cbegin(); itSubtrahend != subtrahend.cend(); ++itSubtrahend) {
93 auto itResult = result.find(key: itSubtrahend.key());
94 if (itResult != result.end() && itSubtrahend.value() == itResult.value())
95 result.erase(it: itResult);
96 }
97
98 return result;
99}
100
101bool QHelpFilterSettings::applySettings(QHelpFilterEngine *filterEngine,
102 const QHelpFilterSettings &settings)
103{
104 bool changed = false;
105 const QHelpFilterSettings oldSettings = readSettings(filterEngine);
106
107 const QMap<QString, QHelpFilterData> filtersToRemove = subtract(
108 minuend: oldSettings.filters(),
109 subtrahend: settings.filters());
110 const QMap<QString, QHelpFilterData> filtersToAdd = subtract(
111 minuend: settings.filters(),
112 subtrahend: oldSettings.filters());
113
114 const QString &currentFilter = filterEngine->activeFilter();
115
116 for (const QString &filter : filtersToRemove.keys()) {
117 filterEngine->removeFilter(filterName: filter);
118 if (currentFilter == filter && !filtersToAdd.contains(key: filter))
119 filterEngine->setActiveFilter(QString());
120 changed = true;
121 }
122
123 for (auto it = filtersToAdd.cbegin(); it != filtersToAdd.cend(); ++it) {
124 filterEngine->setFilterData(filterName: it.key(), filterData: it.value());
125 changed = true;
126 }
127
128 if (changed)
129 filterEngine->setActiveFilter(settings.currentFilter());
130
131 return changed;
132}
133
134QT_END_NAMESPACE
135

source code of qttools/src/assistant/help/qhelpfiltersettings.cpp