Gui: Switch back to a single Reset button

The reset button opens a menu that offers all options to reset the user settings.
This commit is contained in:
wmayer
2023-10-14 19:12:58 +02:00
committed by wwmayer
parent 74c236f947
commit b99feff9fb
3 changed files with 200 additions and 268 deletions

View File

@@ -6,163 +6,95 @@
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
<width>570</width>
<height>454</height>
</rect>
</property>
<property name="windowTitle">
<string>Preferences</string>
</property>
<property name="locale">
<locale language="English" country="UnitedStates"/>
</property>
<property name="sizeGripEnabled">
<bool>true</bool>
</property>
<property name="modal">
<bool>true</bool>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="0" alignment="Qt::AlignHCenter">
<widget class="QToolButton" name="buttonResetAll">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>120</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>128</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string>Reset All</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QDialogButtonBox" name="buttonBox_2">
<property name="standardButtons">
<set>QDialogButtonBox::Help</set>
</property>
</widget>
</item>
<item row="1" column="8" alignment="Qt::AlignRight">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="standardButtons">
<set>QDialogButtonBox::Apply|QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
<item row="1" column="7">
<widget class="QToolButton" name="buttonResetTab">
<property name="text">
<string>Reset Tab</string>
</property>
</widget>
</item>
<item row="0" column="0" colspan="9">
<layout class="QGridLayout">
<property name="margin">
<number>9</number>
</property>
<property name="spacing">
<number>6</number>
</property>
<item row="0" column="0">
<layout class="QHBoxLayout">
<property name="spacing">
<number>6</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<property name="margin">
<number>0</number>
</property>
<item>
<layout class="QVBoxLayout" name="listBoxVL">
<item alignment="Qt::AlignHCenter">
<widget class="QListWidget" name="listBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>120</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>128</width>
<height>16777215</height>
</size>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Sunken</enum>
</property>
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="iconSize">
<size>
<width>96</width>
<height>96</height>
</size>
</property>
<property name="spacing">
<number>12</number>
</property>
<property name="viewMode">
<enum>QListView::IconMode</enum>
</property>
<property name="itemAlignment">
<set>Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item alignment="Qt::AlignHCenter">
<widget class="QToolButton" name="buttonResetGroup">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>120</width>
<height>40</height>
</size>
</property>
<property name="text">
<string notr="true"/>
</property>
</widget>
</item>
</layout>
<widget class="QListWidget" name="listBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>120</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>128</width>
<height>16777215</height>
</size>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Sunken</enum>
</property>
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="iconSize">
<size>
<width>96</width>
<height>96</height>
</size>
</property>
<property name="spacing">
<number>12</number>
</property>
<property name="viewMode">
<enum>QListView::IconMode</enum>
</property>
</widget>
</item>
<item>
<widget class="QStackedWidget" name="tabWidgetStack"/>
</item>
</layout>
</item>
<item row="1" column="0">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="standardButtons">
<set>QDialogButtonBox::Apply|QDialogButtonBox::Cancel|QDialogButtonBox::Help|QDialogButtonBox::Ok|QDialogButtonBox::Reset</set>
</property>
</widget>
</item>
</layout>
</widget>
<layoutdefault spacing="6" margin="11"/>
<tabstops>
<tabstop>listBox</tabstop>
</tabstops>
<resources/>
<connections>
<connection>

View File

@@ -28,14 +28,17 @@
# include <cstring>
# include <QAbstractButton>
# include <QApplication>
# include <QCursor>
# include <QDebug>
# include <QLabel>
# include <QMenu>
# include <QMessageBox>
# include <QScreen>
# include <QScrollArea>
# include <QScrollBar>
# include <QTimer>
# include <QToolTip>
# include <QProcess>
# include <QPushButton>
#endif
#include <App/Application.h>
@@ -74,32 +77,16 @@ 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(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()->setContentsMargins(0, 0, 0, 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_2, &QDialogButtonBox::helpRequested,
getMainWindow(), &MainWindow::whatsThis);
connect(ui->listBox, &QListWidget::currentItemChanged,
this, &DlgPreferencesImp::changeGroup);
setupConnections();
setupPages();
@@ -121,15 +108,19 @@ 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);
connect(ui->buttonBox, &QDialogButtonBox::clicked,
this, &DlgPreferencesImp::onButtonBoxClicked);
connect(ui->buttonBox, &QDialogButtonBox::helpRequested,
getMainWindow(), &MainWindow::whatsThis);
connect(ui->listBox, &QListWidget::currentItemChanged,
this, &DlgPreferencesImp::changeGroup);
if (auto reset = ui->buttonBox->button(QDialogButtonBox::Reset)) {
QString text = reset->text();
text.append(QLatin1String("..."));
reset->setText(text);
}
}
void DlgPreferencesImp::setupPages()
{
// make sure that pages are ready to create
@@ -229,37 +220,11 @@ 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();
}
/**
@@ -401,8 +366,46 @@ void DlgPreferencesImp::reject()
void DlgPreferencesImp::onButtonBoxClicked(QAbstractButton* btn)
{
if (ui->buttonBox->standardButton(btn) == QDialogButtonBox::Apply)
if (ui->buttonBox->standardButton(btn) == QDialogButtonBox::Apply) {
applyChanges();
}
else if (ui->buttonBox->standardButton(btn) == QDialogButtonBox::Reset) {
showResetOptions();
}
}
void DlgPreferencesImp::showResetOptions()
{
// clang-format off
QMenu menu(this);
// Reset per tab
auto tabWidget = static_cast<QTabWidget*>(ui->tabWidgetStack->currentWidget());
int tabIndex = tabWidget->currentIndex();
QString tabText = tabWidget->tabText(tabIndex);
QAction* tabAction = menu.addAction(tr("Reset tab '%1'...").arg(tabText), this,
&DlgPreferencesImp::onButtonResetTabClicked);
tabAction->setToolTip(tr("Resets the user settings for the tab '%1'").arg(tabText));
// Reset per group
int groupIndex = ui->listBox->currentRow();
QString group = ui->listBox->item(groupIndex)->text();
QAction* grpAction = menu.addAction(tr("Reset group '%1'...").arg(group), this,
&DlgPreferencesImp::onButtonResetGroupClicked);
grpAction->setToolTip(tr("Resets the user settings for the group '%1'").arg(group));
// Reset all
QAction* allAction = menu.addAction(tr("Reset all..."), this,
&DlgPreferencesImp::restoreDefaults);
allAction->setToolTip(tr("Resets the user settings entirely"));
connect(&menu, &QMenu::hovered, [&menu](QAction* hover){
QPoint pos = menu.pos();
pos.rx() += menu.width() + 10;
QToolTip::showText(pos, hover->toolTip());
});
menu.exec(QCursor::pos());
// clang-format on
}
void DlgPreferencesImp::restoreDefaults()
@@ -430,6 +433,88 @@ void DlgPreferencesImp::restoreDefaults()
}
}
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 Settings"));
box.setText(tr("All the settings 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 Settings"));
box.setText(tr("All the settings 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();
}
}
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)));
}
/**
* If the dialog is currently showing and the static variable _pages changed, this function
* will rescan that list of pages and add any that are new to the current dialog. It will not
@@ -633,8 +718,6 @@ void DlgPreferencesImp::changeEvent(QEvent *e)
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);
}
@@ -653,86 +736,4 @@ 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"

View File

@@ -136,11 +136,9 @@ protected:
void resizeEvent(QResizeEvent*) override;
void onButtonResetTabClicked();
void onButtonResetGroupClicked();
void relabelResetButtons();
protected Q_SLOTS:
void changeTab(int current);
void changeGroup(QListWidgetItem *current, QListWidgetItem *previous);
void onButtonBoxClicked(QAbstractButton*);
void resizeWindow(int w, int h);
@@ -153,6 +151,7 @@ private:
QTabWidget* createTabForGroup(const std::string& groupName);
void createPageInGroup(QTabWidget* tabWidget, const std::string& pageName);
void applyChanges();
void showResetOptions();
void restoreDefaults();
void restorePageDefaults(PreferencePage**);
QString longestGroupName() const;