1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2020 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt Assistant of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "qhelpfiltersettings_p.h" |
41 | #include "qhelpfilterdata.h" |
42 | |
43 | #include <QtCore/QMap> |
44 | #include <QtHelp/QHelpFilterEngine> |
45 | |
46 | QT_BEGIN_NAMESPACE |
47 | |
48 | class QHelpFilterSettingsPrivate : public QSharedData |
49 | { |
50 | public: |
51 | QHelpFilterSettingsPrivate() = default; |
52 | QHelpFilterSettingsPrivate(const QHelpFilterSettingsPrivate &other) = default; |
53 | ~QHelpFilterSettingsPrivate() = default; |
54 | |
55 | QMap<QString, QHelpFilterData> m_filterToData; |
56 | QString m_currentFilter; |
57 | }; |
58 | |
59 | QHelpFilterSettings::QHelpFilterSettings() |
60 | : d(new QHelpFilterSettingsPrivate) |
61 | { |
62 | } |
63 | |
64 | QHelpFilterSettings::QHelpFilterSettings(const QHelpFilterSettings &) = default; |
65 | |
66 | QHelpFilterSettings::QHelpFilterSettings(QHelpFilterSettings &&) = default; |
67 | |
68 | QHelpFilterSettings::~QHelpFilterSettings() = default; |
69 | |
70 | QHelpFilterSettings &QHelpFilterSettings::operator=(const QHelpFilterSettings &) = default; |
71 | |
72 | QHelpFilterSettings &QHelpFilterSettings::operator=(QHelpFilterSettings &&) = default; |
73 | |
74 | void QHelpFilterSettings::setFilter(const QString &filterName, |
75 | const QHelpFilterData &filterData) |
76 | { |
77 | d->m_filterToData.insert(akey: filterName, avalue: filterData); |
78 | } |
79 | |
80 | void QHelpFilterSettings::removeFilter(const QString &filterName) |
81 | { |
82 | d->m_filterToData.remove(akey: filterName); |
83 | } |
84 | |
85 | QStringList QHelpFilterSettings::filterNames() const |
86 | { |
87 | return d->m_filterToData.keys(); |
88 | } |
89 | |
90 | QHelpFilterData QHelpFilterSettings::filterData(const QString &filterName) const |
91 | { |
92 | return d->m_filterToData.value(akey: filterName); |
93 | } |
94 | |
95 | QMap<QString, QHelpFilterData> QHelpFilterSettings::filters() const |
96 | { |
97 | return d->m_filterToData; |
98 | } |
99 | |
100 | void QHelpFilterSettings::setCurrentFilter(const QString &filterName) |
101 | { |
102 | d->m_currentFilter = filterName; |
103 | } |
104 | |
105 | QString QHelpFilterSettings::currentFilter() const |
106 | { |
107 | return d->m_currentFilter; |
108 | } |
109 | |
110 | QHelpFilterSettings QHelpFilterSettings::readSettings(const QHelpFilterEngine *filterEngine) |
111 | { |
112 | QHelpFilterSettings filterSettings; |
113 | |
114 | const QStringList allFilters = filterEngine->filters(); |
115 | for (const QString &filter : allFilters) |
116 | filterSettings.setFilter(filterName: filter, filterData: filterEngine->filterData(filterName: filter)); |
117 | |
118 | filterSettings.setCurrentFilter(filterEngine->activeFilter()); |
119 | |
120 | return filterSettings; |
121 | } |
122 | |
123 | static QMap<QString, QHelpFilterData> subtract(const QMap<QString, QHelpFilterData> &minuend, |
124 | const QMap<QString, QHelpFilterData> &subtrahend) |
125 | { |
126 | QMap<QString, QHelpFilterData> result = minuend; |
127 | |
128 | for (auto itSubtrahend = subtrahend.cbegin(); itSubtrahend != subtrahend.cend(); ++itSubtrahend) { |
129 | auto itResult = result.find(akey: itSubtrahend.key()); |
130 | if (itResult != result.end() && itSubtrahend.value() == itResult.value()) |
131 | result.erase(it: itResult); |
132 | } |
133 | |
134 | return result; |
135 | } |
136 | |
137 | bool QHelpFilterSettings::applySettings(QHelpFilterEngine *filterEngine, |
138 | const QHelpFilterSettings &settings) |
139 | { |
140 | bool changed = false; |
141 | const QHelpFilterSettings oldSettings = readSettings(filterEngine); |
142 | |
143 | const QMap<QString, QHelpFilterData> filtersToRemove = subtract( |
144 | minuend: oldSettings.filters(), |
145 | subtrahend: settings.filters()); |
146 | const QMap<QString, QHelpFilterData> filtersToAdd = subtract( |
147 | minuend: settings.filters(), |
148 | subtrahend: oldSettings.filters()); |
149 | |
150 | const QString ¤tFilter = filterEngine->activeFilter(); |
151 | |
152 | for (const QString &filter : filtersToRemove.keys()) { |
153 | filterEngine->removeFilter(filterName: filter); |
154 | if (currentFilter == filter && !filtersToAdd.contains(akey: filter)) |
155 | filterEngine->setActiveFilter(QString()); |
156 | changed = true; |
157 | } |
158 | |
159 | for (auto it = filtersToAdd.cbegin(); it != filtersToAdd.cend(); ++it) { |
160 | filterEngine->setFilterData(filterName: it.key(), filterData: it.value()); |
161 | changed = true; |
162 | } |
163 | |
164 | if (changed) |
165 | filterEngine->setActiveFilter(settings.currentFilter()); |
166 | |
167 | return changed; |
168 | } |
169 | |
170 | QT_END_NAMESPACE |
171 | |