1/****************************************************************************
2**
3** Copyright (C) 2016 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:GPL-EXCEPT$
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 General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28#include "preferencesdialog.h"
29
30#include "centralwidget.h"
31#include "fontpanel.h"
32#include "helpenginewrapper.h"
33#include "openpagesmanager.h"
34#include "helpdocsettingswidget.h"
35
36#include <QtCore/QVersionNumber>
37
38#include <QtGui/QFontDatabase>
39
40#include <QtHelp/QHelpEngineCore>
41#include <QtHelp/QHelpFilterData>
42#include <QtHelp/QHelpFilterEngine>
43#include <QtHelp/QHelpFilterSettingsWidget>
44
45#include <QtDebug>
46
47QT_BEGIN_NAMESPACE
48
49PreferencesDialog::PreferencesDialog(QWidget *parent)
50 : QDialog(parent)
51 , m_appFontChanged(false)
52 , m_browserFontChanged(false)
53 , helpEngine(HelpEngineWrapper::instance())
54 , m_hideFiltersTab(!helpEngine.filterFunctionalityEnabled())
55 , m_hideDocsTab(!helpEngine.documentationManagerEnabled())
56{
57 m_ui.setupUi(this);
58
59 connect(sender: m_ui.buttonBox->button(which: QDialogButtonBox::Ok), signal: &QAbstractButton::clicked,
60 receiver: this, slot: &PreferencesDialog::okClicked);
61 connect(sender: m_ui.buttonBox->button(which: QDialogButtonBox::Apply), signal: &QAbstractButton::clicked,
62 receiver: this, slot: &PreferencesDialog::applyClicked);
63 connect(sender: m_ui.buttonBox->button(which: QDialogButtonBox::Cancel), signal: &QAbstractButton::clicked,
64 receiver: this, slot: &QDialog::reject);
65
66 m_docSettings = HelpDocSettings::readSettings(helpEngine: helpEngine.helpEngine());
67
68 if (m_hideDocsTab) {
69 m_ui.tabWidget->removeTab(index: m_ui.tabWidget->indexOf(widget: m_ui.docsTab));
70 } else {
71 connect(sender: m_ui.docSettingsWidget, signal: &HelpDocSettingsWidget::docSettingsChanged,
72 slot: [this](const HelpDocSettings &settings) {
73 m_docSettings = settings;
74 if (m_hideFiltersTab)
75 return;
76
77 m_ui.filterSettingsWidget->setAvailableComponents(m_docSettings.components());
78 m_ui.filterSettingsWidget->setAvailableVersions(m_docSettings.versions());
79 });
80
81 m_ui.docSettingsWidget->setDocSettings(m_docSettings);
82 }
83
84 if (m_hideFiltersTab) {
85 m_ui.tabWidget->removeTab(index: m_ui.tabWidget->indexOf(widget: m_ui.filtersTab));
86 } else {
87 m_ui.filterSettingsWidget->setAvailableComponents(m_docSettings.components());
88 m_ui.filterSettingsWidget->setAvailableVersions(m_docSettings.versions());
89 m_ui.filterSettingsWidget->readSettings(filterEngine: helpEngine.filterEngine());
90 }
91
92 updateFontSettingsPage();
93 updateOptionsPage();
94
95 if (helpEngine.usesAppFont())
96 setFont(helpEngine.appFont());
97}
98
99void PreferencesDialog::okClicked()
100{
101 applyChanges();
102 accept();
103}
104
105void PreferencesDialog::applyClicked()
106{
107 applyChanges();
108
109 m_docSettings = HelpDocSettings::readSettings(helpEngine: helpEngine.helpEngine());
110
111 if (!m_hideDocsTab)
112 m_ui.docSettingsWidget->setDocSettings(m_docSettings);
113 if (!m_hideFiltersTab) {
114 m_ui.filterSettingsWidget->setAvailableComponents(m_docSettings.components());
115 m_ui.filterSettingsWidget->setAvailableVersions(m_docSettings.versions());
116 m_ui.filterSettingsWidget->readSettings(filterEngine: helpEngine.filterEngine());
117 }
118}
119
120void PreferencesDialog::applyChanges()
121{
122 bool changed = false;
123 if (!m_hideDocsTab)
124 changed = HelpDocSettings::applySettings(helpEngine: helpEngine.helpEngine(), settings: m_docSettings);
125 if (!m_hideFiltersTab)
126 changed = changed || m_ui.filterSettingsWidget->applySettings(filterEngine: helpEngine.filterEngine());
127
128 if (changed) {
129 // In order to update the filtercombobox and indexwidget
130 // according to the new filter configuration.
131 helpEngine.setupData();
132 }
133
134 helpEngine.setShowTabs(m_ui.showTabs->isChecked());
135 if (m_showTabs != m_ui.showTabs->isChecked())
136 emit updateUserInterface();
137
138 if (m_appFontChanged) {
139 helpEngine.setAppFont(m_appFontPanel->selectedFont());
140 helpEngine.setUseAppFont(m_appFontPanel->isChecked());
141 helpEngine.setAppWritingSystem(m_appFontPanel->writingSystem());
142 emit updateApplicationFont();
143 m_appFontChanged = false;
144 }
145
146 if (m_browserFontChanged) {
147 helpEngine.setBrowserFont(m_browserFontPanel->selectedFont());
148 helpEngine.setUseBrowserFont(m_browserFontPanel->isChecked());
149 helpEngine.setBrowserWritingSystem(m_browserFontPanel->writingSystem());
150 emit updateBrowserFont();
151 m_browserFontChanged = false;
152 }
153
154 QString homePage = m_ui.homePageLineEdit->text();
155 if (homePage.isEmpty())
156 homePage = QLatin1String("help");
157 helpEngine.setHomePage(homePage);
158
159 const int option = m_ui.helpStartComboBox->currentIndex();
160 helpEngine.setStartOption(option);
161}
162
163void PreferencesDialog::updateFontSettingsPage()
164{
165 m_browserFontPanel = new FontPanel(this);
166 m_browserFontPanel->setCheckable(true);
167 m_ui.stackedWidget_2->insertWidget(index: 0, w: m_browserFontPanel);
168
169 m_appFontPanel = new FontPanel(this);
170 m_appFontPanel->setCheckable(true);
171 m_ui.stackedWidget_2->insertWidget(index: 1, w: m_appFontPanel);
172
173 m_ui.stackedWidget_2->setCurrentIndex(0);
174
175 const QString customSettings(tr(s: "Use custom settings"));
176 m_appFontPanel->setTitle(customSettings);
177
178 QFont font = helpEngine.appFont();
179 m_appFontPanel->setSelectedFont(font);
180
181 QFontDatabase::WritingSystem system = helpEngine.appWritingSystem();
182 m_appFontPanel->setWritingSystem(system);
183
184 m_appFontPanel->setChecked(helpEngine.usesAppFont());
185
186 m_browserFontPanel->setTitle(customSettings);
187
188 font = helpEngine.browserFont();
189 m_browserFontPanel->setSelectedFont(font);
190
191 system = helpEngine.browserWritingSystem();
192 m_browserFontPanel->setWritingSystem(system);
193
194 m_browserFontPanel->setChecked(helpEngine.usesBrowserFont());
195
196 connect(sender: m_appFontPanel, signal: &QGroupBox::toggled,
197 receiver: this, slot: &PreferencesDialog::appFontSettingToggled);
198 connect(sender: m_browserFontPanel, signal: &QGroupBox::toggled,
199 receiver: this, slot: &PreferencesDialog::browserFontSettingToggled);
200
201 const QList<QComboBox*> &appCombos = m_appFontPanel->findChildren<QComboBox*>();
202 for (QComboBox* box : appCombos) {
203 connect(sender: box, signal: QOverload<int>::of(ptr: &QComboBox::currentIndexChanged),
204 receiver: this, slot: &PreferencesDialog::appFontSettingChanged);
205 }
206
207 const QList<QComboBox*> &browserCombos = m_browserFontPanel->findChildren<QComboBox*>();
208 for (QComboBox* box : browserCombos) {
209 connect(sender: box, signal: QOverload<int>::of(ptr: &QComboBox::currentIndexChanged),
210 receiver: this, slot: &PreferencesDialog::browserFontSettingChanged);
211 }
212}
213
214void PreferencesDialog::appFontSettingToggled(bool on)
215{
216 Q_UNUSED(on);
217 m_appFontChanged = true;
218}
219
220void PreferencesDialog::appFontSettingChanged(int index)
221{
222 Q_UNUSED(index);
223 m_appFontChanged = true;
224}
225
226void PreferencesDialog::browserFontSettingToggled(bool on)
227{
228 Q_UNUSED(on);
229 m_browserFontChanged = true;
230}
231
232void PreferencesDialog::browserFontSettingChanged(int index)
233{
234 Q_UNUSED(index);
235 m_browserFontChanged = true;
236}
237
238void PreferencesDialog::updateOptionsPage()
239{
240 m_ui.homePageLineEdit->setText(helpEngine.homePage());
241
242 int option = helpEngine.startOption();
243 m_ui.helpStartComboBox->setCurrentIndex(option);
244
245 m_showTabs = helpEngine.showTabs();
246 m_ui.showTabs->setChecked(m_showTabs);
247
248 connect(sender: m_ui.blankPageButton, signal: &QAbstractButton::clicked,
249 receiver: this, slot: &PreferencesDialog::setBlankPage);
250 connect(sender: m_ui.currentPageButton, signal: &QAbstractButton::clicked,
251 receiver: this, slot: &PreferencesDialog::setCurrentPage);
252 connect(sender: m_ui.defaultPageButton, signal: &QAbstractButton::clicked,
253 receiver: this, slot: &PreferencesDialog::setDefaultPage);
254}
255
256void PreferencesDialog::setBlankPage()
257{
258 m_ui.homePageLineEdit->setText(QLatin1String("about:blank"));
259}
260
261void PreferencesDialog::setCurrentPage()
262{
263 QString homepage = CentralWidget::instance()->currentSource().toString();
264 if (homepage.isEmpty())
265 homepage = QLatin1String("help");
266
267 m_ui.homePageLineEdit->setText(homepage);
268}
269
270void PreferencesDialog::setDefaultPage()
271{
272 m_ui.homePageLineEdit->setText(helpEngine.defaultHomePage());
273}
274
275QT_END_NAMESPACE
276

source code of qttools/src/assistant/assistant/preferencesdialog.cpp