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