8 #include "../misc/dialogutils.h"
10 #include "ui_settingsdialog.h"
12 #include <QItemSelectionModel>
13 #include <QMessageBox>
14 #include <QScrollArea>
16 #include <QStringBuilder>
35 , m_currentCategory(nullptr)
36 , m_tabBarAlwaysVisible(true)
39 makeHeading(m_ui->headingLabel);
40 setStyleSheet(dialogStyle());
43 m_categoryFilterModel->setSourceModel(m_categoryModel);
44 m_ui->categoriesListView->setModel(m_categoryFilterModel);
48 connect(m_ui->categoriesListView->selectionModel(), &QItemSelectionModel::currentChanged,
this, &SettingsDialog::currentCategoryChanged);
50 connect(m_ui->abortPushButton, &QPushButton::clicked,
this, &SettingsDialog::reject);
51 connect(m_ui->applyPushButton, &QPushButton::clicked,
this, &SettingsDialog::apply);
52 connect(m_ui->okPushButton, &QPushButton::clicked,
this, &SettingsDialog::accept);
54 connect(
this, &SettingsDialog::accepted,
this, &SettingsDialog::apply);
55 connect(
this, &SettingsDialog::rejected,
this, &SettingsDialog::reset);
57 connect(m_ui->filterLineEdit, &QLineEdit::textChanged, m_categoryFilterModel, &OptionCategoryFilterModel::setFilterFixedString);
58 connect(m_ui->filterLineEdit, &QLineEdit::textChanged,
this, &SettingsDialog::updateTabWidget);
74 m_tabBarAlwaysVisible = value;
75 if (m_currentCategory) {
76 m_ui->pagesTabWidget->tabBar()->setHidden(!value && m_currentCategory->
pages().size() == 1);
88 return m_categoryModel->
category(categoryIndex);
101 if (pageIndex < category->pages().length()) {
113 if (event->spontaneous()) {
132 void SettingsDialog::currentCategoryChanged(
const QModelIndex &index)
143 if (m_currentCategory) {
144 m_currentCategory->
setCurrentIndex(m_ui->pagesTabWidget->currentIndex());
147 if (m_currentCategory !=
category) {
152 m_currentCategory =
nullptr;
153 m_ui->headingLabel->setText(tr(
"No category selected"));
169 const bool hasSingleCategory = singleCategory !=
nullptr;
170 m_ui->filterLineEdit->setHidden(hasSingleCategory);
171 m_ui->categoriesListView->setHidden(hasSingleCategory);
172 m_ui->headingLabel->setHidden(hasSingleCategory);
173 if (hasSingleCategory) {
174 m_ui->filterLineEdit->clear();
183 void SettingsDialog::updateTabWidget()
185 if (!m_currentCategory) {
186 m_ui->pagesTabWidget->clear();
189 m_ui->pagesTabWidget->setUpdatesEnabled(
false);
190 const QString searchKeyWord = m_ui->filterLineEdit->text();
191 int index = 0, pageIndex = 0;
192 for (OptionPage *
const page : m_currentCategory->
pages()) {
194 QScrollArea *scrollArea;
195 if (index < m_ui->pagesTabWidget->count()) {
196 scrollArea = qobject_cast<QScrollArea *>(m_ui->pagesTabWidget->widget(index));
197 scrollArea->takeWidget();
198 m_ui->pagesTabWidget->setTabText(index,
page->
widget()->windowTitle());
199 m_ui->pagesTabWidget->setTabIcon(index,
page->
widget()->windowIcon());
201 scrollArea =
new QScrollArea(m_ui->pagesTabWidget);
202 scrollArea->setFrameStyle(QFrame::NoFrame);
203 scrollArea->setBackgroundRole(QPalette::Base);
204 scrollArea->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
205 scrollArea->setWidgetResizable(
true);
206 m_ui->pagesTabWidget->addTab(scrollArea,
page->
widget()->windowTitle());
207 m_ui->pagesTabWidget->setTabIcon(index,
page->
widget()->windowIcon());
210 page->
widget()->layout()->setAlignment(Qt::AlignTop | Qt::AlignLeft);
216 m_ui->pagesTabWidget->setCurrentIndex(pageIndex);
220 while (index < m_ui->pagesTabWidget->count()) {
221 QScrollArea *
const scrollArea = qobject_cast<QScrollArea *>(m_ui->pagesTabWidget->widget(index));
222 scrollArea->takeWidget();
223 m_ui->pagesTabWidget->removeTab(index);
226 m_ui->pagesTabWidget->tabBar()->setHidden(!m_tabBarAlwaysVisible && m_ui->pagesTabWidget->count() == 1);
227 m_ui->pagesTabWidget->setUpdatesEnabled(
true);
235 bool SettingsDialog::apply()
238 QString errorMessage;
247 if (errorMessage.isEmpty()) {
248 errorMessage = tr(
"<p><b>Errors occurred when applying changes:</b></p><ul>");
250 QStringList &errors =
const_cast<OptionPage *
>(
page)->errors();
251 if (errors.isEmpty()) {
253 % QStringLiteral(
"</i>: ") % tr(
"unknonw error") % QStringLiteral(
"</li>"));
255 for (
const QString &error : errors) {
257 % QStringLiteral(
"</i>: ") % error % QStringLiteral(
"</li>"));
265 if (!errorMessage.isEmpty()) {
266 errorMessage.append(QStringLiteral(
"</ul>"));
267 QMessageBox::warning(
this, windowTitle(), errorMessage);
272 return errorMessage.isEmpty();
279 void SettingsDialog::reset()
The OptionCategoryFilterModel class is used by SettingsDialog to filter option categories.
The OptionCategoryModel class is used by SettingsDialog to store and display option categories.
OptionCategory * category(const QModelIndex &index) const
Returns the category for the specified model index.
QList< OptionCategory * > categories
void setCategories(const QList< OptionCategory * > &categories)
Sets the categories for the model.
The OptionCategory class wraps associated option pages.
void setCurrentIndex(int currentIndex)
Sets the current index.
int currentIndex() const
Returns the index of the currently shown page.
void resetAllPages()
Resets all pages.
QList< OptionPage * > pages
The OptionPage class is the base class for SettingsDialog pages.
virtual bool apply()=0
Applies altered settings.
virtual void reset()=0
Discards altered settings and resets relevant widgets.
bool matches(const QString &searchKeyWord)
Returns whether the pages matches the specified searchKeyWord.
bool hasBeenShown() const
Returns an indication whether the option page has been shown yet.
QWidget * widget()
Returns the widget for the option page.
The SettingsDialog class provides a framework for creating settings dialogs with different categories...
OptionCategoryModel * categoryModel()
Returns the category model used by the settings dialog to manage the categories.
void setTabBarAlwaysVisible(bool value)
Sets whether the tab bar is always visible.
OptionCategory * category(int categoryIndex) const
Returns the category for the specified categoryIndex.
SettingsDialog(QWidget *parent=nullptr)
Constructs a settings dialog.
void showCategory(OptionCategory *category)
Sets the current category to the specified category and updates the relevant widgets to show it.
OptionPage * page(int categoryIndex, int pageIndex) const
Returns the page for the specified categoryIndex and the specified pageIndex.
~SettingsDialog() override
Destroys the settings dialog.
void showEvent(QShowEvent *event) override
Resets all pages before the dialog is shown by the application.
void setSingleCategory(OptionCategory *singleCategory)
Enables single-category mode to show only the specified singleCategory.