From e478192aa5393de9b7df190341a043f79ec6ac49 Mon Sep 17 00:00:00 2001 From: Pieter Hijma Date: Sat, 5 Oct 2024 14:15:08 +0200 Subject: [PATCH] Gui: Fix showing multiple dialogs VarSet add prop --- src/Gui/DlgAddPropertyVarSet.cpp | 51 +++++++++++++++++--------------- src/Gui/DlgAddPropertyVarSet.h | 16 +++++++--- 2 files changed, 39 insertions(+), 28 deletions(-) diff --git a/src/Gui/DlgAddPropertyVarSet.cpp b/src/Gui/DlgAddPropertyVarSet.cpp index 5993a89f82..3bf227b880 100644 --- a/src/Gui/DlgAddPropertyVarSet.cpp +++ b/src/Gui/DlgAddPropertyVarSet.cpp @@ -67,12 +67,9 @@ DlgAddPropertyVarSet::~DlgAddPropertyVarSet() = default; void DlgAddPropertyVarSet::initializeGroup() { - connect(&comboBoxGroup, &EditFinishedComboBox::editFinished, - this, &DlgAddPropertyVarSet::onEditFinished); comboBoxGroup.setObjectName(QString::fromUtf8("comboBoxGroup")); comboBoxGroup.setInsertPolicy(QComboBox::InsertAtTop); comboBoxGroup.setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); - comboBoxGroup.setEditable(true); auto formLayout = qobject_cast(layout()); formLayout->setWidget(1, QFormLayout::FieldRole, &comboBoxGroup); @@ -102,6 +99,8 @@ void DlgAddPropertyVarSet::initializeGroup() } comboBoxGroup.setEditText(QString::fromStdString(groupNamesSorted[0])); + connect(&comboBoxGroup, &EditFinishedComboBox::editFinished, + this, &DlgAddPropertyVarSet::onEditFinished); } void DlgAddPropertyVarSet::getSupportedTypes(std::vector& types) @@ -269,11 +268,10 @@ void DlgAddPropertyVarSet::createProperty() } catch (Base::Exception& e) { e.ReportException(); - QMessageBox::critical(this, - QObject::tr("Add property"), - QObject::tr("Failed to add property to '%1': %2").arg( - QString::fromLatin1(varSet->getFullName().c_str()), - QString::fromUtf8(e.what()))); + critical(QObject::tr("Add property"), + QObject::tr("Failed to add property to '%1': %2").arg( + QString::fromLatin1(varSet->getFullName().c_str()), + QString::fromUtf8(e.what()))); clearEditors(); return; } @@ -344,18 +342,16 @@ private: void DlgAddPropertyVarSet::checkName() { std::string name = ui->lineEditName->text().toStdString(); if(name.empty() || name != Base::Tools::getIdentifier(name)) { - QMessageBox::critical(getMainWindow(), - QObject::tr("Invalid name"), - QObject::tr("The property name must only contain alpha numericals,\n" - "underscore, and must not start with a digit.")); + critical(QObject::tr("Invalid name"), + QObject::tr("The property name must only contain alpha numericals,\n" + "underscore, and must not start with a digit.")); clearEditors(!CLEAR_NAME); throw CreatePropertyException("Invalid name"); } if(App::ExpressionParser::isTokenAUnit(name) || App::ExpressionParser::isTokenAConstant(name)) { - QMessageBox::critical(getMainWindow(), - QObject::tr("Invalid name"), - QObject::tr("The property name is a reserved word.")); + critical(QObject::tr("Invalid name"), + QObject::tr("The property name is a reserved word.")); clearEditors(!CLEAR_NAME); throw CreatePropertyException("Invalid name"); } @@ -364,11 +360,10 @@ void DlgAddPropertyVarSet::checkName() { // we are adding a new property, check whether it doesn't already exist auto prop = varSet->getPropertyByName(name.c_str()); if(prop && prop->getContainer() == varSet) { - QMessageBox::critical(this, - QObject::tr("Invalid name"), - QObject::tr("The property '%1' already exists in '%2'").arg( - QString::fromLatin1(name.c_str()), - QString::fromLatin1(varSet->getFullName().c_str()))); + critical(QObject::tr("Invalid name"), + QObject::tr("The property '%1' already exists in '%2'").arg( + QString::fromLatin1(name.c_str()), + QString::fromLatin1(varSet->getFullName().c_str()))); clearEditors(!CLEAR_NAME); throw CreatePropertyException("Invalid name"); } @@ -379,10 +374,9 @@ void DlgAddPropertyVarSet::checkGroup() { std::string group = comboBoxGroup.currentText().toStdString(); if (group.empty() || group != Base::Tools::getIdentifier(group)) { - QMessageBox::critical(this, - QObject::tr("Invalid name"), - QObject::tr("The group name must only contain alpha numericals,\n" - "underscore, and must not start with a digit.")); + critical(QObject::tr("Invalid name"), + QObject::tr("The group name must only contain alpha numericals,\n" + "underscore, and must not start with a digit.")); comboBoxGroup.setEditText(QString::fromUtf8("Base")); throw CreatePropertyException("Invalid name"); } @@ -439,6 +433,15 @@ void DlgAddPropertyVarSet::onNamePropertyChanged(const QString& text) } } +void DlgAddPropertyVarSet::critical(const QString& title, const QString& text) { + static bool criticalDialogShown = false; + if (!criticalDialogShown) { + criticalDialogShown = true; + QMessageBox::critical(this, title, text); + criticalDialogShown = false; + } +} + void DlgAddPropertyVarSet::valueChanged() { QVariant data; diff --git a/src/Gui/DlgAddPropertyVarSet.h b/src/Gui/DlgAddPropertyVarSet.h index 59059a6a37..0b7ad129b3 100644 --- a/src/Gui/DlgAddPropertyVarSet.h +++ b/src/Gui/DlgAddPropertyVarSet.h @@ -42,14 +42,21 @@ namespace Dialog { class EditFinishedComboBox : public QComboBox { Q_OBJECT public: - explicit EditFinishedComboBox(QWidget *parent = nullptr) : QComboBox(parent) {} + explicit EditFinishedComboBox(QWidget *parent = nullptr) : QComboBox(parent) { + setEditable(true); + connect(this, QOverload::of(&QComboBox::currentIndexChanged), this, &EditFinishedComboBox::onIndexChanged); + connect(this->lineEdit(), &QLineEdit::editingFinished, this, &EditFinishedComboBox::onEditingFinished); + } Q_SIGNALS: void editFinished(); -protected: - void focusOutEvent(QFocusEvent *event) override { - QComboBox::focusOutEvent(event); +private: + void onEditingFinished() { + Q_EMIT editFinished(); + } + + void onIndexChanged() { Q_EMIT editFinished(); } }; @@ -94,6 +101,7 @@ private: void checkType(); void onEditFinished(); void onNamePropertyChanged(const QString& text); + void critical(const QString& title, const QString& text); void getSupportedTypes(std::vector& types);