From 065295de436b6fdb1f4f11213a42a810f867613c Mon Sep 17 00:00:00 2001 From: Kacper Donat Date: Sat, 27 Jul 2024 23:32:44 +0200 Subject: [PATCH] Gui: Remove unnecessary scrollbars from Preferences --- src/Gui/DlgPreferencesImp.cpp | 21 ++++++++++++++++++--- src/Gui/PropertyPage.cpp | 23 ++++++++++++++--------- src/Gui/PropertyPage.h | 8 +++++++- 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/src/Gui/DlgPreferencesImp.cpp b/src/Gui/DlgPreferencesImp.cpp index cf6be8493d..d17f9e94c2 100644 --- a/src/Gui/DlgPreferencesImp.cpp +++ b/src/Gui/DlgPreferencesImp.cpp @@ -287,9 +287,19 @@ PreferencePage* DlgPreferencesImp::createPreferencePage(const std::string& pageN return nullptr; } + auto resetMargins = [](QWidget* widget) { + widget->setContentsMargins(0, 0, 0, 0); + widget->layout()->setContentsMargins(0, 0, 0, 0); + }; + // settings layout already takes care for margins, we need to reset everything to 0 - page->setContentsMargins(0, 0, 0, 0); - page->layout()->setContentsMargins(0, 0, 0, 0); + resetMargins(page); + + // special handling for PreferenceUiForm to reset margins for forms too + if (auto uiFormPage = qobject_cast(page)) { + resetMargins(uiFormPage->form()); + } + page->setProperty(GroupNameProperty, QString::fromStdString(groupName)); page->setProperty(PageNameProperty, QString::fromStdString(pageName)); @@ -352,15 +362,20 @@ int DlgPreferencesImp::minimumPageWidth() const int DlgPreferencesImp::minimumDialogWidth(int pageWidth) const { + // this is additional safety spacing to ensure that everything fits with scrollbar etc. + const auto additionalMargin = style()->pixelMetric(QStyle::PM_ScrollBarExtent) + 8; + QSize size = ui->groupWidgetStack->sizeHint(); + int diff = pageWidth - size.width(); int dw = width(); + if (diff > 0) { const int offset = 2; dw += diff + offset; } - return dw; + return dw + additionalMargin; } void DlgPreferencesImp::updatePageDependentWidgets() diff --git a/src/Gui/PropertyPage.cpp b/src/Gui/PropertyPage.cpp index 2526ad8967..3a8711481b 100644 --- a/src/Gui/PropertyPage.cpp +++ b/src/Gui/PropertyPage.cpp @@ -124,19 +124,19 @@ void PreferencePage::requireRestart() PreferenceUiForm::PreferenceUiForm(const QString& fn, QWidget* parent) : PreferencePage(parent) - , form(nullptr) + , _form(nullptr) { auto loader = UiLoader::newInstance(); loader->setWorkingDirectory(QFileInfo(fn).absolutePath()); QFile file(fn); if (file.open(QFile::ReadOnly)) { - form = loader->load(&file, this); + _form = loader->load(&file, this); } file.close(); - if (form) { - this->setWindowTitle(form->windowTitle()); + if (_form) { + this->setWindowTitle(_form->windowTitle()); auto layout = new QVBoxLayout; - layout->addWidget(form); + layout->addWidget(_form); setLayout(layout); } else { @@ -155,7 +155,7 @@ void PreferenceUiForm::changeEvent(QEvent *e) template void PreferenceUiForm::loadPrefWidgets() { - QList pw = form->findChildren(); + QList pw = _form->findChildren(); for (typename QList::iterator it = pw.begin(); it != pw.end(); ++it) (*it)->onRestore(); } @@ -163,14 +163,14 @@ void PreferenceUiForm::loadPrefWidgets() template void PreferenceUiForm::savePrefWidgets() { - QList pw = form->findChildren(); + QList pw = _form->findChildren(); for (typename QList::iterator it = pw.begin(); it != pw.end(); ++it) (*it)->onSave(); } void PreferenceUiForm::loadSettings() { - if (!form) + if (!_form) return; // search for all pref widgets to restore their settings @@ -191,7 +191,7 @@ void PreferenceUiForm::loadSettings() void PreferenceUiForm::saveSettings() { - if (!form) + if (!_form) return; // search for all pref widgets to save their settings @@ -210,6 +210,11 @@ void PreferenceUiForm::saveSettings() savePrefWidgets(); } +QWidget* Gui::Dialog::PreferenceUiForm::form() +{ + return _form; +} + void PreferencePage::resetSettingsToDefaults() { auto prefs = this->findChildren(); diff --git a/src/Gui/PropertyPage.h b/src/Gui/PropertyPage.h index 9fbf03e0b5..8408f98133 100644 --- a/src/Gui/PropertyPage.h +++ b/src/Gui/PropertyPage.h @@ -75,6 +75,10 @@ public: bool isRestartRequired() const; void requireRestart(); + + // this fixes issue with wordWrap on labels affecting size hints: + // https://stackoverflow.com/questions/78276854/layout-ignoring-sizehints-when-qlabel-with-text-wrap-is-present + bool hasHeightForWidth() const override { return false; } public Q_SLOTS: virtual void loadSettings()=0; @@ -102,6 +106,8 @@ public: void loadSettings() override; void saveSettings() override; + QWidget* form(); + protected: void changeEvent(QEvent *e) override; @@ -112,7 +118,7 @@ private: void savePrefWidgets(); private: - QWidget* form; + QWidget* _form; }; /** Base class for custom pages.