[Feature Request] [Core] [Preferences] [UI/UX] Buttons to reset default values of Pages/Groups (#10688)

* Gui: added buttons to restore the default values of the selected Tab or Group
 * reformatted texts for better translations
 * Morphed buttons from QPushButton to QToolButton
 * added code to resize items list and buttons on language change

Signed-off-by: CalligaroV <vincenzo.calligaro@gmail.com>
This commit is contained in:
Vincenzo Calligaro
2023-10-02 23:14:29 +02:00
committed by GitHub
parent be85c50c62
commit 283961539d
3 changed files with 279 additions and 66 deletions

View File

@@ -29,6 +29,7 @@
# include <QAbstractButton>
# include <QApplication>
# include <QDebug>
# include <QLabel>
# include <QMessageBox>
# include <QScreen>
# include <QScrollArea>
@@ -73,16 +74,29 @@ DlgPreferencesImp::DlgPreferencesImp(QWidget* parent, Qt::WindowFlags fl)
invalidParameter(false), canEmbedScrollArea(true), restartRequired(false)
{
ui->setupUi(this);
setupConnections();
QFontMetrics fm(font());
int length = QtTools::horizontalAdvance(fm, longestGroupName());
ui->listBox->setFixedWidth(Base::clamp<int>(length + 20, 108, 120));
ui->listBox->setGridSize(QSize(108, 75));
ui->listBox->setGridSize(QSize(Base::clamp<int>(length + 20, 108, 120), 75));
ui->buttonResetGroup->setFixedWidth(Base::clamp<int>(length + 20, 108, 120));
ui->buttonResetGroup->setLayout(new QVBoxLayout(ui->buttonResetGroup));
auto ResetGroup = new QLabel(ui->buttonResetGroup);
ResetGroup->setAlignment(Qt::AlignCenter);
ResetGroup->setWordWrap(true);
ui->buttonResetGroup->layout()->setMargin(0);
ui->buttonResetGroup->layout()->addWidget(ResetGroup);
ui->buttonResetAll->setFixedWidth(Base::clamp<int>(length + 20, 108, 120));
// remove unused help button
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
connect(ui->buttonBox, &QDialogButtonBox::clicked,
this, &DlgPreferencesImp::onButtonBoxClicked);
connect(ui->buttonBox, &QDialogButtonBox::helpRequested,
connect(ui->buttonBox_2, &QDialogButtonBox::helpRequested,
getMainWindow(), &MainWindow::whatsThis);
connect(ui->listBox, &QListWidget::currentItemChanged,
this, &DlgPreferencesImp::changeGroup);
@@ -105,6 +119,17 @@ DlgPreferencesImp::~DlgPreferencesImp()
}
}
void DlgPreferencesImp::setupConnections()
{
connect(ui->buttonResetTab, &QToolButton::clicked,
this, &DlgPreferencesImp::onButtonResetTabClicked);
connect(ui->buttonResetGroup, &QToolButton::clicked,
this, &DlgPreferencesImp::onButtonResetGroupClicked);
connect(ui->buttonResetAll, &QToolButton::clicked,
this, &DlgPreferencesImp::restoreDefaults);
}
void DlgPreferencesImp::setupPages()
{
// make sure that pages are ready to create
@@ -204,11 +229,37 @@ void DlgPreferencesImp::createPageInGroup(QTabWidget *tabWidget, const std::stri
}
}
void DlgPreferencesImp::relabelResetButtons()
{
int groupIndex = ui->listBox->currentRow();
QFontMetrics fm(font());
QString group = fm.elidedText(ui->listBox->item(groupIndex)->text(), Qt::ElideRight, ui->buttonResetGroup->width()-4);
QTabWidget* tabWidget = static_cast<QTabWidget*>(ui->tabWidgetStack->currentWidget());
int tabIndex = tabWidget->currentIndex();
static_cast<QLabel*>(ui->buttonResetGroup->layout()->itemAt(0)->widget())->setText(tr("Reset Group %1").arg(group));
ui->buttonResetTab->setText(tr("Reset Tab %1").arg(tabWidget->tabText(tabIndex)));
}
void DlgPreferencesImp::changeTab(int current)
{
Q_UNUSED(current);
relabelResetButtons();
}
void DlgPreferencesImp::changeGroup(QListWidgetItem *current, QListWidgetItem *previous)
{
if (!current)
current = previous;
ui->tabWidgetStack->setCurrentIndex(ui->listBox->row(current));
auto tabWidget = static_cast<QTabWidget*>(ui->tabWidgetStack->currentWidget());
connect(tabWidget, &QTabWidget::currentChanged,
this, &DlgPreferencesImp::changeTab);
relabelResetButtons();
}
/**
@@ -352,8 +403,6 @@ void DlgPreferencesImp::onButtonBoxClicked(QAbstractButton* btn)
{
if (ui->buttonBox->standardButton(btn) == QDialogButtonBox::Apply)
applyChanges();
else if (ui->buttonBox->standardButton(btn) == QDialogButtonBox::Reset)
restoreDefaults();
}
void DlgPreferencesImp::restoreDefaults()
@@ -578,6 +627,14 @@ void DlgPreferencesImp::changeEvent(QEvent *e)
QByteArray group = item->data(GroupNameRole).toByteArray();
item->setText(QObject::tr(group.constData()));
}
//resizes items list and buttons
QFontMetrics fm(font());
int length = QtTools::horizontalAdvance(fm, longestGroupName());
ui->listBox->setFixedWidth(Base::clamp<int>(length + 20, 108, 120));
ui->listBox->setGridSize(QSize(Base::clamp<int>(length + 20, 108, 120), 75));
ui->buttonResetGroup->setFixedWidth(Base::clamp<int>(length + 20, 108, 120));
ui->buttonResetAll->setFixedWidth(Base::clamp<int>(length + 20, 108, 120));
} else {
QWidget::changeEvent(e);
}
@@ -596,4 +653,86 @@ void DlgPreferencesImp::reload()
applyChanges();
}
void DlgPreferencesImp::restorePageDefaults(PreferencePage** page)
{
QList<QObject*> prefs = (*page)->findChildren<QObject*>();
for (const auto & pref : prefs) {
if (!pref->property("prefPath").isNull() && !pref->property("prefEntry").isNull()) {
std::string path = pref->property("prefPath").toString().toStdString();
std::string entry = pref->property("prefEntry").toString().toStdString();
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath(std::string("User parameter:BaseApp/Preferences/" + path).c_str());
for (const auto & pn : hGrp->GetParameterNames(entry.c_str())){
hGrp->RemoveAttribute(pn.first, pn.second.c_str());
}
}
}
std::string pageName = (*page)->property("PageName").toString().toStdString();
(*page) = WidgetFactory().createPreferencePage(pageName.c_str());
(*page)->loadSettings();
(*page)->setProperty("PageName", QVariant(QString::fromStdString(pageName)));
}
void DlgPreferencesImp::onButtonResetTabClicked()
{
auto tabWidget = static_cast<QTabWidget*>(ui->tabWidgetStack->widget(ui->listBox->currentRow()));
QMessageBox box(this);
box.setIcon(QMessageBox::Question);
box.setWindowTitle(tr("Reset Tab Parameters"));
box.setText(tr("All the parameters for the Tab %1 will be deleted.").arg(tabWidget->tabText(tabWidget->currentIndex())));
box.setInformativeText(tr("Do you want to continue?"));
box.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
box.setDefaultButton(QMessageBox::No);
if (box.exec() == QMessageBox::Yes) {
int pageIndex = tabWidget->currentIndex();
QString pageText = tabWidget->tabText(pageIndex);
PreferencePage* page = qobject_cast<PreferencePage*>(tabWidget->widget(pageIndex));
restorePageDefaults(&page);
page->setProperty("GroupName", tabWidget->property("GroupName"));
tabWidget->removeTab(pageIndex);
tabWidget->insertTab(pageIndex, page, pageText);
tabWidget->setCurrentIndex(pageIndex);
applyChanges();
}
}
void DlgPreferencesImp::onButtonResetGroupClicked()
{
QMessageBox box(this);
box.setIcon(QMessageBox::Question);
box.setWindowTitle(tr("Reset Group Parameters"));
box.setText(tr("All the parameters for the Group %1 will be deleted.").arg(ui->listBox->currentItem()->text()));
box.setInformativeText(tr("Do you want to continue?"));
box.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
box.setDefaultButton(QMessageBox::No);
if (box.exec() == QMessageBox::Yes) {
auto tabWidget = static_cast<QTabWidget*>(ui->tabWidgetStack->widget(ui->listBox->currentRow()));
int pageIndex = tabWidget->currentIndex();
for (int i = 0; i < tabWidget->count(); i++) {
QString pageText = tabWidget->tabText(i);
PreferencePage* page = qobject_cast<PreferencePage*>(tabWidget->widget(i));
restorePageDefaults(&page);
page->setProperty("GroupName", tabWidget->property("GroupName"));
tabWidget->removeTab(i);
tabWidget->insertTab(i, page, pageText);
}
tabWidget->setCurrentIndex(pageIndex);
applyChanges();
}
}
#include "moc_DlgPreferencesImp.cpp"