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 "helpdocsettings.h" |
41 | #include "helpdocsettingswidget.h" |
42 | #include "ui_helpdocsettingswidget.h" |
43 | |
44 | #include <QtWidgets/QFileDialog> |
45 | #include <QtWidgets/QPushButton> |
46 | |
47 | QT_BEGIN_NAMESPACE |
48 | |
49 | class HelpDocSettingsWidgetPrivate |
50 | { |
51 | HelpDocSettingsWidget *q_ptr; |
52 | Q_DECLARE_PUBLIC(HelpDocSettingsWidget) |
53 | public: |
54 | HelpDocSettingsWidgetPrivate() = default; |
55 | |
56 | void addDocumentation(); |
57 | void removeDocumentation(); |
58 | void applyDocListFilter(QListWidgetItem *item); |
59 | |
60 | QMap<QString, QListWidgetItem *> m_namespaceToItem; |
61 | QHash<QListWidgetItem *, QString> m_itemToNamespace; |
62 | |
63 | Ui::HelpDocSettingsWidget m_ui; |
64 | HelpDocSettings m_settings; |
65 | }; |
66 | |
67 | void HelpDocSettingsWidgetPrivate::addDocumentation() |
68 | { |
69 | Q_Q(HelpDocSettingsWidget); |
70 | |
71 | const QStringList &fileNames = QFileDialog::getOpenFileNames(parent: q, |
72 | caption: q->tr(s: "Add Documentation" ), dir: QString(), filter: q->tr(s: "Qt Compressed Help Files (*.qch)" )); |
73 | if (fileNames.isEmpty()) |
74 | return; |
75 | |
76 | bool added = false; |
77 | |
78 | for (const QString &fileName : fileNames) { |
79 | if (!m_settings.addDocumentation(fileName)) |
80 | continue; |
81 | |
82 | if (!added) { |
83 | added = true; |
84 | m_ui.registeredDocsListWidget->clearSelection(); |
85 | } |
86 | |
87 | const QString namespaceName = m_settings.namespaceName(fileName); |
88 | QListWidgetItem *item = new QListWidgetItem(namespaceName); |
89 | m_namespaceToItem.insert(akey: namespaceName, avalue: item); |
90 | m_itemToNamespace.insert(akey: item, avalue: namespaceName); |
91 | m_ui.registeredDocsListWidget->insertItem(row: m_namespaceToItem.keys().indexOf(t: namespaceName), item); |
92 | |
93 | item->setSelected(true); |
94 | applyDocListFilter(item); |
95 | } |
96 | |
97 | if (added) |
98 | emit q->docSettingsChanged(settings: m_settings); |
99 | } |
100 | |
101 | void HelpDocSettingsWidgetPrivate::removeDocumentation() |
102 | { |
103 | Q_Q(HelpDocSettingsWidget); |
104 | |
105 | const QList<QListWidgetItem *> selectedItems = m_ui.registeredDocsListWidget->selectedItems(); |
106 | if (selectedItems.isEmpty()) |
107 | return; |
108 | |
109 | for (QListWidgetItem *item : selectedItems) { |
110 | const QString namespaceName = m_itemToNamespace.value(akey: item); |
111 | m_itemToNamespace.remove(akey: item); |
112 | m_namespaceToItem.remove(akey: namespaceName); |
113 | delete item; |
114 | |
115 | m_settings.removeDocumentation(namespaceName); |
116 | } |
117 | |
118 | emit q->docSettingsChanged(settings: m_settings); |
119 | } |
120 | |
121 | void HelpDocSettingsWidgetPrivate::applyDocListFilter(QListWidgetItem *item) |
122 | { |
123 | const QString namespaceName = m_itemToNamespace.value(akey: item); |
124 | const QString nameFilter = m_ui.registeredDocsFilterLineEdit->text(); |
125 | |
126 | const bool matches = nameFilter.isEmpty() || namespaceName.contains(s: nameFilter); |
127 | |
128 | if (!matches) |
129 | item->setSelected(false); |
130 | item->setHidden(!matches); |
131 | } |
132 | |
133 | HelpDocSettingsWidget::HelpDocSettingsWidget(QWidget *parent) |
134 | : QWidget(parent) |
135 | , d_ptr(new HelpDocSettingsWidgetPrivate()) |
136 | { |
137 | Q_D(HelpDocSettingsWidget); |
138 | d->q_ptr = this; |
139 | d->m_ui.setupUi(this); |
140 | |
141 | connect(sender: d->m_ui.docAddButton, signal: &QAbstractButton::clicked, |
142 | slot: [this]() { |
143 | Q_D(HelpDocSettingsWidget); |
144 | d->addDocumentation(); |
145 | }); |
146 | connect(sender: d->m_ui.docRemoveButton, signal: &QAbstractButton::clicked, |
147 | slot: [this]() { |
148 | Q_D(HelpDocSettingsWidget); |
149 | d->removeDocumentation(); |
150 | }); |
151 | connect(sender: d->m_ui.registeredDocsFilterLineEdit, signal: &QLineEdit::textChanged, |
152 | slot: [this](const QString &) { |
153 | Q_D(HelpDocSettingsWidget); |
154 | for (const auto item : d->m_namespaceToItem) |
155 | d->applyDocListFilter(item); |
156 | }); |
157 | connect(sender: d->m_ui.registeredDocsListWidget, signal: &QListWidget::itemSelectionChanged, |
158 | slot: [this]() { |
159 | Q_D(HelpDocSettingsWidget); |
160 | d->m_ui.docRemoveButton->setEnabled( |
161 | !d->m_ui.registeredDocsListWidget->selectedItems().isEmpty()); |
162 | }); |
163 | } |
164 | |
165 | HelpDocSettingsWidget::~HelpDocSettingsWidget() = default; |
166 | |
167 | void HelpDocSettingsWidget::setDocSettings(const HelpDocSettings &settings) |
168 | { |
169 | Q_D(HelpDocSettingsWidget); |
170 | d->m_settings = settings; |
171 | |
172 | d->m_ui.registeredDocsListWidget->clear(); |
173 | d->m_namespaceToItem.clear(); |
174 | d->m_itemToNamespace.clear(); |
175 | |
176 | for (const QString &namespaceName : d->m_settings.namespaces()) { |
177 | QListWidgetItem *item = new QListWidgetItem(namespaceName); |
178 | d->m_namespaceToItem.insert(akey: namespaceName, avalue: item); |
179 | d->m_itemToNamespace.insert(akey: item, avalue: namespaceName); |
180 | d->m_ui.registeredDocsListWidget->addItem(aitem: item); |
181 | d->applyDocListFilter(item); |
182 | } |
183 | |
184 | d->m_ui.docRemoveButton->setEnabled( |
185 | !d->m_ui.registeredDocsListWidget->selectedItems().isEmpty()); |
186 | } |
187 | |
188 | HelpDocSettings HelpDocSettingsWidget::docSettings() const |
189 | { |
190 | Q_D(const HelpDocSettingsWidget); |
191 | return d->m_settings; |
192 | } |
193 | |
194 | QT_END_NAMESPACE |
195 | |