Skip to content

Commit

Permalink
Gui: Prevent invalid editors in VarSet dialog
Browse files Browse the repository at this point in the history
In the VarSet dialog, we can create an editor after the name and type
has been determined.  However, if the name is changed after an editor
has been created, the editor is invalid because the underlying property
has been removed.  In that case, the function onNameDetermined() should
clean up the invalid editor and this happens in most cases.
Unfortunately, it cannot handle the case in which a click happens on the
invalid editor itself.  This click should result in onNameDetermined() but
since the editor is already invalid, onNameDetermined() is triggered too
late.

The current commit solves this by listening for every change in the name
of the property and handle the editors accordingly.
  • Loading branch information
pieterhijma authored and chennes committed Jul 14, 2024
1 parent 7805c48 commit a4862b6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
24 changes: 19 additions & 5 deletions src/Gui/DlgAddPropertyVarSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ void DlgAddPropertyVarSet::initializeWidgets(ViewProviderVarSet* viewProvider)

connect(this, &QDialog::finished,
this, [viewProvider](int result) { viewProvider->onFinished(result); });
connect(ui->lineEditName, &QLineEdit::editingFinished,
connect(ui->lineEditName, &QLineEdit::textChanged,
this, &DlgAddPropertyVarSet::onNamePropertyDetermined);

std::string title = "Add a property to " + varSet->getFullName();
Expand All @@ -179,7 +179,9 @@ void DlgAddPropertyVarSet::setOkEnabled(bool enabled)

void DlgAddPropertyVarSet::clearEditors()
{
bool beforeBlocked = ui->lineEditName->blockSignals(true);
ui->lineEditName->clear();
ui->lineEditName->blockSignals(beforeBlocked);
removeEditor();
setOkEnabled(false);
namePropertyToAdd.clear();
Expand Down Expand Up @@ -269,14 +271,26 @@ void DlgAddPropertyVarSet::createProperty(std::string& name, std::string& group)
setOkEnabled(true);
}

void DlgAddPropertyVarSet::onNamePropertyDetermined()
void DlgAddPropertyVarSet::onNamePropertyDetermined(const QString& text)
{
if (!namePropertyToAdd.empty()) {
// we were already adding a name, so remove that property
// We were already adding a property with this name. We have to remove
// the property, the editor because it is associated with the property,
// and we have to abort the transaction.
varSet->removeDynamicProperty(namePropertyToAdd.c_str());
removeEditor();
App::Document* doc = varSet->getDocument();
if (doc->hasPendingTransaction()) {
doc->abortTransaction();
}
namePropertyToAdd.clear();
}
if (text.isEmpty()) {
// We can not define a property, so we should not have an editor for the value.
clearEditors();
return;
}
QString nameProperty = ui->lineEditName->text();
std::string name = nameProperty.toUtf8().constData();
std::string name = text.toUtf8().constData();
std::string group = comboBoxGroup.currentText().toUtf8().constData();
if(name.empty() || group.empty()
|| name != Base::Tools::getIdentifier(name)
Expand Down
2 changes: 1 addition & 1 deletion src/Gui/DlgAddPropertyVarSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public Q_SLOTS:
bool isSupportedType(std::string& type);
void createProperty(std::string& name, std::string& group);

void onNamePropertyDetermined();
void onNamePropertyDetermined(const QString& text);
void onGroupDetermined();
void onTypePropertyDetermined();

Expand Down

0 comments on commit a4862b6

Please sign in to comment.