Don't reset or apply option pages which haven't been shown

This makes the annoying hasBeenShown() check in each apply()
and reset() implementation unnecessary. It should not break
anything because if the page hasn't been shown there is nothing
to reset/apply anyways.
This commit is contained in:
Martchus 2017-09-27 00:10:30 +02:00
parent afe3373896
commit da485f5702
5 changed files with 87 additions and 66 deletions

View File

@ -27,11 +27,16 @@ OptionCategory::~OptionCategory()
/*!
* \brief Applies all pages.
* \remarks Pages which have not been shown yet must have not been initialized anyways
* and hence are skipped.
* \sa OptionPage::apply()
*/
bool OptionCategory::applyAllPages()
{
for (OptionPage *page : m_pages) {
if (!page->hasBeenShown()) {
continue;
}
if (!page->apply()) {
return false;
}
@ -41,12 +46,16 @@ bool OptionCategory::applyAllPages()
/*!
* \brief Resets all pages.
* \remarks Pages which have not been shown yet must have not been initialized anyways
* and hence are skipped.
* \sa OptionPage::reset()
*/
void OptionCategory::resetAllPages()
{
for (OptionPage *page : m_pages) {
page->reset();
if (page->hasBeenShown()) {
page->reset();
}
}
}

View File

@ -90,16 +90,26 @@ bool OptionPage::matches(const QString &searchKeyWord)
/*!
* \fn OptionPage::apply()
* \brief Applies altered settings.
* \remarks
* The SettingsDialog and any other classes/functions of this library will not call
* this method if the option page has not been shown yet. Hence it is (no longer) necessary
* to use OptionPage::hasBeenShown() to check whether the page has been initialized
* yet.
*/
/*!
* \fn OptionPage::reset()
* \brief Discards altered settings and resets relevant widgets.
* \remarks
* The SettingsDialog and any other classes/functions of this library will not call
* this method if the option page has not been shown yet. Hence it is (no longer) necessary
* to use OptionPage::hasBeenShown() to check whether the page has been initialized
* yet.
*/
/*!
* \fn OptionPage::setupWidget()
* \brief Creates the widget for the page. Called in the first invocation of
* \brief Creates the widget for the page. Called on the first invocation of
* widget().
*/
} // namespace Dialogs

View File

@ -50,6 +50,8 @@ inline QWidget *OptionPage::parentWindow() const
/*!
* \brief Returns an indication whether the option page has been shown yet.
* \remarks If this is true, the method OptionPage::setupWidget() has already
* been called.
*/
inline bool OptionPage::hasBeenShown() const
{

View File

@ -231,42 +231,38 @@ QtAppearanceOptionPage::~QtAppearanceOptionPage()
bool QtAppearanceOptionPage::apply()
{
if (hasBeenShown()) {
m_settings.font = ui()->fontComboBox->font();
m_settings.customFont = !ui()->fontCheckBox->isChecked();
m_settings.widgetStyle = ui()->widgetStyleComboBox->currentText();
m_settings.customWidgetStyle = !ui()->widgetStyleCheckBox->isChecked();
m_settings.styleSheetPath = ui()->styleSheetPathSelection->lineEdit()->text();
m_settings.customStyleSheet = !ui()->styleSheetCheckBox->isChecked();
m_settings.palette = ui()->paletteToolButton->palette();
m_settings.customPalette = !ui()->paletteCheckBox->isChecked();
m_settings.iconTheme = ui()->iconThemeComboBox->currentIndex() != -1 ? ui()->iconThemeComboBox->currentData().toString()
: ui()->iconThemeComboBox->currentText();
m_settings.customIconTheme = !ui()->iconThemeCheckBox->isChecked();
}
m_settings.font = ui()->fontComboBox->font();
m_settings.customFont = !ui()->fontCheckBox->isChecked();
m_settings.widgetStyle = ui()->widgetStyleComboBox->currentText();
m_settings.customWidgetStyle = !ui()->widgetStyleCheckBox->isChecked();
m_settings.styleSheetPath = ui()->styleSheetPathSelection->lineEdit()->text();
m_settings.customStyleSheet = !ui()->styleSheetCheckBox->isChecked();
m_settings.palette = ui()->paletteToolButton->palette();
m_settings.customPalette = !ui()->paletteCheckBox->isChecked();
m_settings.iconTheme
= ui()->iconThemeComboBox->currentIndex() != -1 ? ui()->iconThemeComboBox->currentData().toString() : ui()->iconThemeComboBox->currentText();
m_settings.customIconTheme = !ui()->iconThemeCheckBox->isChecked();
return true;
}
void QtAppearanceOptionPage::reset()
{
if (hasBeenShown()) {
ui()->fontComboBox->setCurrentFont(m_settings.font);
ui()->fontCheckBox->setChecked(!m_settings.customFont);
ui()->widgetStyleComboBox->setCurrentText(
m_settings.widgetStyle.isEmpty() ? (QApplication::style() ? QApplication::style()->objectName() : QString()) : m_settings.widgetStyle);
ui()->widgetStyleCheckBox->setChecked(!m_settings.customWidgetStyle);
ui()->styleSheetPathSelection->lineEdit()->setText(m_settings.styleSheetPath);
ui()->styleSheetCheckBox->setChecked(!m_settings.customStyleSheet);
ui()->paletteToolButton->setPalette(m_settings.palette);
ui()->paletteCheckBox->setChecked(!m_settings.customPalette);
int iconThemeIndex = ui()->iconThemeComboBox->findData(m_settings.iconTheme);
if (iconThemeIndex != -1) {
ui()->iconThemeComboBox->setCurrentIndex(iconThemeIndex);
} else {
ui()->iconThemeComboBox->setCurrentText(m_settings.iconTheme);
}
ui()->iconThemeCheckBox->setChecked(!m_settings.customIconTheme);
ui()->fontComboBox->setCurrentFont(m_settings.font);
ui()->fontCheckBox->setChecked(!m_settings.customFont);
ui()->widgetStyleComboBox->setCurrentText(
m_settings.widgetStyle.isEmpty() ? (QApplication::style() ? QApplication::style()->objectName() : QString()) : m_settings.widgetStyle);
ui()->widgetStyleCheckBox->setChecked(!m_settings.customWidgetStyle);
ui()->styleSheetPathSelection->lineEdit()->setText(m_settings.styleSheetPath);
ui()->styleSheetCheckBox->setChecked(!m_settings.customStyleSheet);
ui()->paletteToolButton->setPalette(m_settings.palette);
ui()->paletteCheckBox->setChecked(!m_settings.customPalette);
int iconThemeIndex = ui()->iconThemeComboBox->findData(m_settings.iconTheme);
if (iconThemeIndex != -1) {
ui()->iconThemeComboBox->setCurrentIndex(iconThemeIndex);
} else {
ui()->iconThemeComboBox->setCurrentText(m_settings.iconTheme);
}
ui()->iconThemeCheckBox->setChecked(!m_settings.customIconTheme);
}
QWidget *QtAppearanceOptionPage::setupWidget()
@ -342,19 +338,15 @@ QtLanguageOptionPage::~QtLanguageOptionPage()
bool QtLanguageOptionPage::apply()
{
if (hasBeenShown()) {
m_settings.localeName = ui()->localeComboBox->currentText();
m_settings.customLocale = !ui()->localeCheckBox->isChecked();
}
m_settings.localeName = ui()->localeComboBox->currentText();
m_settings.customLocale = !ui()->localeCheckBox->isChecked();
return true;
}
void QtLanguageOptionPage::reset()
{
if (hasBeenShown()) {
ui()->localeComboBox->setCurrentText(m_settings.localeName);
ui()->localeCheckBox->setChecked(!m_settings.customLocale);
}
ui()->localeComboBox->setCurrentText(m_settings.localeName);
ui()->localeCheckBox->setChecked(!m_settings.customLocale);
}
QWidget *QtLanguageOptionPage::setupWidget()
@ -391,21 +383,17 @@ QtEnvOptionPage::~QtEnvOptionPage()
bool QtEnvOptionPage::apply()
{
if (hasBeenShown()) {
m_settings.additionalPluginDirectory = ui()->pluginPathSelection->lineEdit()->text();
m_settings.additionalIconThemeSearchPath = ui()->iconThemeSearchPathSelection->lineEdit()->text();
TranslationFiles::additionalTranslationFilePath() = ui()->translationPathSelection->lineEdit()->text();
}
m_settings.additionalPluginDirectory = ui()->pluginPathSelection->lineEdit()->text();
m_settings.additionalIconThemeSearchPath = ui()->iconThemeSearchPathSelection->lineEdit()->text();
TranslationFiles::additionalTranslationFilePath() = ui()->translationPathSelection->lineEdit()->text();
return true;
}
void QtEnvOptionPage::reset()
{
if (hasBeenShown()) {
ui()->pluginPathSelection->lineEdit()->setText(m_settings.additionalPluginDirectory);
ui()->iconThemeSearchPathSelection->lineEdit()->setText(m_settings.additionalIconThemeSearchPath);
ui()->translationPathSelection->lineEdit()->setText(TranslationFiles::additionalTranslationFilePath());
}
ui()->pluginPathSelection->lineEdit()->setText(m_settings.additionalPluginDirectory);
ui()->iconThemeSearchPathSelection->lineEdit()->setText(m_settings.additionalIconThemeSearchPath);
ui()->translationPathSelection->lineEdit()->setText(TranslationFiles::additionalTranslationFilePath());
}
} // namespace Dialogs

View File

@ -111,7 +111,9 @@ void SettingsDialog::showEvent(QShowEvent *event)
if (!event->spontaneous()) {
for (OptionCategory *category : m_categoryModel->categories()) {
for (OptionPage *page : category->pages()) {
page->reset();
if (page->hasBeenShown()) {
page->reset();
}
}
}
}
@ -223,36 +225,46 @@ void SettingsDialog::updateTabWidget()
}
/*!
* \brief Applies all changes. Calls OptionCategory::applyAllPages() for each
* category.
* \brief Applies all changes. Calls OptionCategory::applyAllPages() for each category.
* \remarks Pages which have not been shown yet must have not been initialized anyways
* and hence are skipped.
*/
bool SettingsDialog::apply()
{
// apply each page in each category and gather error messages
QString errorMessage;
for (OptionCategory *category : m_categoryModel->categories()) {
for (OptionPage *page : category->pages()) {
if (!page->apply()) {
if (errorMessage.isEmpty()) {
errorMessage = tr("<p><b>Errors occured when applying changes:</b></p><ul>");
}
QStringList &errors = const_cast<OptionPage *>(page)->errors();
if (errors.isEmpty()) {
if (!page->hasBeenShown() || page->apply()) {
// nothing to apply or no error
continue;
}
// add error message
if (errorMessage.isEmpty()) {
errorMessage = tr("<p><b>Errors occured when applying changes:</b></p><ul>");
}
QStringList &errors = const_cast<OptionPage *>(page)->errors();
if (errors.isEmpty()) {
errorMessage.append(QStringLiteral("<li><i>") % category->displayName() % QLatin1Char('/') % page->widget()->windowTitle()
% QStringLiteral("</i>: ") % tr("unknonw error") % QStringLiteral("</li>"));
} else {
for (const QString &error : errors) {
errorMessage.append(QStringLiteral("<li><i>") % category->displayName() % QLatin1Char('/') % page->widget()->windowTitle()
% QStringLiteral("</i>: ") % tr("unknonw error") % QStringLiteral("</li>"));
} else {
for (const QString &error : errors) {
errorMessage.append(QStringLiteral("<li><i>") % category->displayName() % QLatin1Char('/') % page->widget()->windowTitle()
% QStringLiteral("</i>: ") % error % QStringLiteral("</li>"));
}
errors.clear();
% QStringLiteral("</i>: ") % error % QStringLiteral("</li>"));
}
errors.clear();
}
}
}
// show error messages (if errors occured)
if (!errorMessage.isEmpty()) {
errorMessage.append(QStringLiteral("</ul>"));
QMessageBox::warning(this, windowTitle(), errorMessage);
}
// return status
emit applied();
return errorMessage.isEmpty();
}