Gui: Prevent invalid editors in VarSet dialog

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.
This commit is contained in:
Pieter Hijma
2024-07-03 23:03:51 +02:00
committed by Chris Hennes
parent d7e5073ffa
commit fc6120b8dd
2 changed files with 20 additions and 6 deletions

View File

@@ -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();
@@ -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();
@@ -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();
}
QString nameProperty = ui->lineEditName->text();
std::string name = nameProperty.toUtf8().constData();
if (text.isEmpty()) {
// We can not define a property, so we should not have an editor for the value.
clearEditors();
return;
}
std::string name = text.toUtf8().constData();
std::string group = comboBoxGroup.currentText().toUtf8().constData();
if(name.empty() || group.empty()
|| name != Base::Tools::getIdentifier(name)

View File

@@ -87,7 +87,7 @@ private:
bool isSupportedType(std::string& type);
void createProperty(std::string& name, std::string& group);
void onNamePropertyDetermined();
void onNamePropertyDetermined(const QString& text);
void onGroupDetermined();
void onTypePropertyDetermined();