Gui: add new PrefCheckableGroupBox widget

This commit is contained in:
Furgo
2025-06-11 01:51:40 +02:00
parent 7388758ad9
commit 5362c818c8
4 changed files with 62 additions and 0 deletions

View File

@@ -864,4 +864,37 @@ void PrefFontBox::savePreferences()
getWindowParameter()->SetASCII(entryName(), currName.toUtf8());
}
// --------------------------------------------------------------------
PrefCheckableGroupBox::PrefCheckableGroupBox(QWidget* parent)
: QGroupBox(parent), PrefWidget()
{
}
PrefCheckableGroupBox::~PrefCheckableGroupBox() = default;
void PrefCheckableGroupBox::restorePreferences()
{
if (getWindowParameter().isNull() || entryName().isEmpty()) {
failedToRestore(objectName());
return;
}
// Default value is the current state of the checkbox (usually from .ui on first load)
bool defaultValueInUi = isChecked();
bool actualValue = getWindowParameter()->GetBool(entryName(), defaultValueInUi);
setChecked(actualValue);
}
void PrefCheckableGroupBox::savePreferences()
{
if (getWindowParameter().isNull() || entryName().isEmpty())
{
failedToSave(objectName());
return;
}
getWindowParameter()->SetBool(entryName(), isChecked());
}
#include "moc_PrefWidgets.cpp"

View File

@@ -26,6 +26,7 @@
#include <QCheckBox>
#include <QComboBox>
#include <QFontComboBox>
#include <QGroupBox>
#include <QRadioButton>
#include <QTextEdit>
@@ -426,6 +427,31 @@ protected:
void savePreferences() override;
};
/**
* The PrefCheckableGroupBox class allows a QGroupBox to act as a boolean preference.
* Its 'checked' state is saved to and restored from the FreeCAD parameter system
* using the 'prefEntry' and 'prefPath' dynamic properties set in the .ui file.
* When the GroupBox is checked, its children are enabled; when unchecked, disabled (standard
* QGroupBox behavior).
*/
class GuiExport PrefCheckableGroupBox : public QGroupBox, public PrefWidget
{
Q_OBJECT
Q_PROPERTY(QByteArray prefEntry READ entryName WRITE setEntryName) // clazy:exclude=qproperty-without-notify
Q_PROPERTY(QByteArray prefPath READ paramGrpPath WRITE setParamGrpPath) // clazy:exclude=qproperty-without-notify
public:
explicit PrefCheckableGroupBox(QWidget* parent = nullptr);
~PrefCheckableGroupBox() override;
protected:
// restore from/save to parameters
void restorePreferences() override;
void savePreferences() override;
};
} // namespace Gui
#endif // GUI_PREFWIDGETS_H

View File

@@ -187,6 +187,7 @@ void PreferenceUiForm::loadSettings()
loadPrefWidgets<Gui::PrefColorButton *>();
loadPrefWidgets<Gui::PrefUnitSpinBox *>();
loadPrefWidgets<Gui::PrefQuantitySpinBox*>();
loadPrefWidgets<Gui::PrefCheckableGroupBox*>();
}
void PreferenceUiForm::saveSettings()
@@ -208,6 +209,7 @@ void PreferenceUiForm::saveSettings()
savePrefWidgets<Gui::PrefColorButton *>();
savePrefWidgets<Gui::PrefUnitSpinBox *>();
savePrefWidgets<Gui::PrefQuantitySpinBox*>();
savePrefWidgets<Gui::PrefCheckableGroupBox*>();
}
QWidget* Gui::Dialog::PreferenceUiForm::form()

View File

@@ -115,6 +115,7 @@ WidgetFactorySupplier::WidgetFactorySupplier()
new WidgetProducer<Gui::PrefComboBox>;
new WidgetProducer<Gui::PrefFontBox>;
new WidgetProducer<Gui::PrefCheckBox>;
new WidgetProducer<Gui::PrefCheckableGroupBox>;
new WidgetProducer<Gui::PrefRadioButton>;
new WidgetProducer<Gui::PrefSlider>;
new WidgetProducer<Gui::PrefFileChooser>;