Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix misleading error message when an attribute form fails to
save changes

Regardless of the reason why the saving failed, a message about
invalid JSON was always shown... which is totally confusing for
users when a table has NO json fields or values (ノ`Д´)ノ
  • Loading branch information
nyalldawson committed Feb 15, 2021
1 parent 92a4899 commit fd657be
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
12 changes: 12 additions & 0 deletions python/gui/auto_generated/qgsattributeform.sip.in
Expand Up @@ -277,6 +277,18 @@ Update all editors to correspond to a different feature.
Save all the values from the editors to the layer.

:return: ``True`` if successful
%End

bool saveWithDetails( QString *error /Out/ = 0 );
%Docstring
Save all the values from the editors to the layer.


:return: - ``True`` if save was successful
- error: will be set to an explanatory error message if an error occurs while saving the form.


.. versionadded:: 3.18
%End

void resetValues();
Expand Down
8 changes: 6 additions & 2 deletions src/gui/qgsattributedialog.cpp
Expand Up @@ -66,15 +66,19 @@ void QgsAttributeDialog::setHighlight( QgsHighlight *h )

void QgsAttributeDialog::accept()
{
bool didSave = mAttributeForm->save();
QString error;
const bool didSave = mAttributeForm->saveWithDetails( &error );
if ( didSave )
{
QDialog::accept();
}
else
{
if ( error.isEmpty() )
error = tr( "An unknown error was encountered saving attributes" );

mMessageBar->pushMessage( QString(),
tr( "Your JSON value is invalid and has not been saved" ),
error,
Qgis::MessageLevel::Critical );
}
}
Expand Down
19 changes: 15 additions & 4 deletions src/gui/qgsattributeform.cpp
Expand Up @@ -325,7 +325,7 @@ void QgsAttributeForm::setFeature( const QgsFeature &feature )
mIsSettingFeature = false;
}

bool QgsAttributeForm::saveEdits()
bool QgsAttributeForm::saveEdits( QString *error )
{
bool success = true;
bool changedLayer = false;
Expand All @@ -349,9 +349,11 @@ bool QgsAttributeForm::saveEdits()
if ( eww )
{
// check for invalid JSON values
QgsTextEditWrapper *text_edit = qobject_cast<QgsTextEditWrapper *>( eww );
if ( text_edit && text_edit->isInvalidJSON() )
QgsTextEditWrapper *textEdit = qobject_cast<QgsTextEditWrapper *>( eww );
if ( textEdit && textEdit->isInvalidJSON() )
{
if ( error )
*error = tr( "JSON value for %1 is invalid and has not been saved" ).arg( eww->field().name() );
return false;
}
QVariantList dstVars = QVariantList() << dst.at( eww->fieldIdx() );
Expand Down Expand Up @@ -739,6 +741,14 @@ bool QgsAttributeForm::saveMultiEdits()

bool QgsAttributeForm::save()
{
return saveWithDetails( nullptr );
}

bool QgsAttributeForm::saveWithDetails( QString *error )
{
if ( error )
error->clear();

if ( mIsSaving )
return true;

Expand Down Expand Up @@ -784,7 +794,7 @@ bool QgsAttributeForm::save()
case QgsAttributeEditorContext::FixAttributeMode:
case QgsAttributeEditorContext::SearchMode:
case QgsAttributeEditorContext::AggregateSearchMode:
success = saveEdits();
success = saveEdits( error );
break;

case QgsAttributeEditorContext::MultiEditMode:
Expand All @@ -799,6 +809,7 @@ bool QgsAttributeForm::save()
return success;
}


void QgsAttributeForm::resetValues()
{
mValuesInitialized = false;
Expand Down
13 changes: 12 additions & 1 deletion src/gui/qgsattributeform.h
Expand Up @@ -293,6 +293,17 @@ class GUI_EXPORT QgsAttributeForm : public QWidget
*/
bool save();

/**
* Save all the values from the editors to the layer.
*
* \param error if specified, will be set to an explanatory error message if an error occurs while saving the form.
*
* \returns TRUE if save was successful
*
* \since QGIS 3.18
*/
bool saveWithDetails( QString *error SIP_OUT = nullptr );

/**
* Sets all values to the values of the current feature
*/
Expand Down Expand Up @@ -388,7 +399,7 @@ class GUI_EXPORT QgsAttributeForm : public QWidget
void scanForEqualAttributes( QgsFeatureIterator &fit, QSet< int > &mixedValueFields, QHash< int, QVariant > &fieldSharedValues ) const;

//! Save single feature or add feature edits
bool saveEdits();
bool saveEdits( QString *error );

//! fill up dependency map for default values
void createDefaultValueDependencies();
Expand Down

0 comments on commit fd657be

Please sign in to comment.