64 bool editingStarted =
true;
67 if ( !layer->isValid() )
69 editingStarted =
false;
70 QgsLogger::debug( tr(
"Can't start editing invalid layer '%1'." ).arg( layer->name() ) );
74 if ( !layer->dataProvider() )
76 editingStarted =
false;
77 QgsLogger::debug( tr(
"Can't start editing layer '%1' with invalid data provider." ).arg( layer->name() ) );
82 if ( !layer->supportsEditing() )
84 editingStarted =
false;
85 QgsLogger::debug( tr(
"Can't start editing. Layer '%1' doesn't support editing." ).arg( layer->name() ) );
89 if ( layer->editBuffer() )
92 layer->editBuffer()->setEditBufferGroup(
this );
97 emit layer->beforeEditingStarted();
98 layer->dataProvider()->enterUpdateMode();
99 layer->createEditBuffer();
100 layer->editBuffer()->setEditBufferGroup(
this );
101 layer->updateFields();
102 emit layer->editingStarted();
105 if ( ! editingStarted )
107 QStringList rollbackErrors;
108 if ( !
rollBack( rollbackErrors,
true ) )
109 QgsLogger::debug( tr(
"Can't rollback after start editing failure. Roll back detailed errors: %1" ).arg( rollbackErrors.join(
" / " ) ) );
112 mIsEditing = editingStarted;
120 const QSet<QgsVectorLayer *> constModifiedLayers =
modifiedLayers();
121 if ( constModifiedLayers.isEmpty() )
123 editingFinished( stopEditing );
124 mIsEditing = !stopEditing;
128 QMap<QString, QSet<QgsVectorLayer *> > connectionStringsLayers;
133 QList<QgsVectorLayer *> transactionLayers;
134 QList<std::shared_ptr<QgsTransaction> > openTransactions;
135 const QStringList connectionStrings = connectionStringsLayers.keys();
136 for (
const QString &connectionString : connectionStrings )
138 const QString providerKey = ( *connectionStringsLayers.value( connectionString ).begin() )->providerType();
140 std::shared_ptr<QgsTransaction> transaction;
144 commitErrors << tr(
"ERROR: data source '%1', is not available for transactions." ).arg( connectionString );
150 if ( ! transaction->begin( errorMsg ) )
152 commitErrors << tr(
"ERROR: could not start a transaction on data provider '%1', detailed error: '%2'." ).arg( providerKey, errorMsg );
157 const auto constLayers = connectionStringsLayers.value( connectionString );
160 if ( ! transaction->addLayer( layer,
true ) )
162 commitErrors << tr(
"ERROR: could not add layer '%1' to transaction on data provider '%2'." ).arg( layer->name(), providerKey );
167 transactionLayers.append( layer );
170 openTransactions.append( transaction );
177 const QList<QgsVectorLayer *> orderedLayers = orderLayersParentsToChildren( constModifiedLayers );
178 QList<QgsVectorLayer *>::const_iterator orderedLayersIterator;
183 for ( orderedLayersIterator = orderedLayers.constBegin(); orderedLayersIterator != orderedLayers.constEnd(); ++orderedLayersIterator )
185 if ( !( *orderedLayersIterator )->editBuffer() )
187 commitErrors << tr(
"ERROR: edit buffer of layer '%1' is not valid." ).arg( ( *orderedLayersIterator )->name() );
192 success = ( *orderedLayersIterator )->editBuffer()->commitChangesCheckGeometryTypeCompatibility( commitErrors );
198 QSet<QgsVectorLayer *> modifiedLayersOnProviderSide;
203 for ( orderedLayersIterator = orderedLayers.constBegin(); orderedLayersIterator != orderedLayers.constEnd(); ++orderedLayersIterator )
205 QgsFields oldFields = ( *orderedLayersIterator )->fields();
207 bool attributesDeleted =
false;
208 success = ( *orderedLayersIterator )->editBuffer()->commitChangesDeleteAttributes( attributesDeleted, commitErrors );
212 bool attributesRenamed =
false;
213 success = ( *orderedLayersIterator )->editBuffer()->commitChangesRenameAttributes( attributesRenamed, commitErrors );
217 bool attributesAdded =
false;
218 success = ( *orderedLayersIterator )->editBuffer()->commitChangesAddAttributes( attributesAdded, commitErrors );
222 if ( attributesDeleted || attributesRenamed || attributesAdded )
224 if ( ! transactionLayers.contains( ( *orderedLayersIterator ) ) )
225 modifiedLayersOnProviderSide.insert( ( *orderedLayersIterator ) );
227 success = ( *orderedLayersIterator )->editBuffer()->commitChangesCheckAttributesModifications( oldFields, commitErrors );
237 orderedLayersIterator = orderedLayers.
constEnd();
238 while ( orderedLayersIterator != orderedLayers.constBegin() )
240 --orderedLayersIterator;
241 bool featuresDeleted;
242 success = ( *orderedLayersIterator )->editBuffer()->commitChangesDeleteFeatures( featuresDeleted, commitErrors );
246 if ( featuresDeleted && transactionLayers.contains( ( *orderedLayersIterator ) ) )
247 modifiedLayersOnProviderSide.insert( ( *orderedLayersIterator ) );
254 for ( orderedLayersIterator = orderedLayers.constBegin(); orderedLayersIterator != orderedLayers.constEnd(); ++orderedLayersIterator )
257 ( *orderedLayersIterator )->editBuffer()->commitChangesAddFeatures( featuresAdded, commitErrors );
261 if ( featuresAdded && transactionLayers.contains( ( *orderedLayersIterator ) ) )
262 modifiedLayersOnProviderSide.insert( ( *orderedLayersIterator ) );
269 orderedLayersIterator = orderedLayers.constEnd();
270 while ( orderedLayersIterator != orderedLayers.constBegin() )
272 --orderedLayersIterator;
274 bool attributesChanged;
275 success = ( *orderedLayersIterator )->editBuffer()->commitChangesChangeAttributes( attributesChanged, commitErrors );
279 if ( attributesChanged && transactionLayers.contains( ( *orderedLayersIterator ) ) )
280 modifiedLayersOnProviderSide.insert( ( *orderedLayersIterator ) );
287 QList<std::shared_ptr<QgsTransaction> >::iterator openTransactionsIterator = openTransactions.begin();
288 while ( openTransactionsIterator != openTransactions.end() )
291 if ( !( *openTransactionsIterator )->commit( errorMsg ) )
294 commitErrors << tr(
"ERROR: could not commit a transaction, detailed error: '%1'." ).arg( errorMsg );
298 modifiedLayersOnProviderSide += connectionStringsLayers.value( ( *openTransactionsIterator )->connectionString() );
299 openTransactionsIterator = openTransactions.erase( openTransactionsIterator );
307 if ( ! modifiedLayersOnProviderSide.isEmpty() )
309 if ( modifiedLayersOnProviderSide.size() == 1 )
310 commitErrors << tr(
"WARNING: changes to layer '%1' were already sent to data provider and cannot be rolled back." ).arg( ( *modifiedLayersOnProviderSide.begin() )->name() );
313 commitErrors << tr(
"WARNING: changes to following layers were already sent to data provider and cannot be rolled back:" );
314 for (
QgsVectorLayer *layer : std::as_const( modifiedLayersOnProviderSide ) )
315 commitErrors << tr(
"- '%1'" ).arg( layer->name() );
319 QString rollbackError;
320 for (
const std::shared_ptr<QgsTransaction> &transaction : openTransactions )
321 transaction->rollback( rollbackError );
326 editingFinished( stopEditing );
328 if ( success && stopEditing )
338 if ( ! layer->editBuffer() )
341 if ( !layer->dataProvider() )
343 rollbackErrors << tr(
"Layer '%1' doesn't have a valid data provider" ).arg( layer->name() );
347 bool rollbackExtent = !layer->editBuffer()->deletedFeatureIds().isEmpty() ||
348 !layer->editBuffer()->addedFeatures().isEmpty() ||
349 !layer->editBuffer()->changedGeometries().isEmpty();
351 emit layer->beforeRollBack();
353 layer->editBuffer()->rollBack();
355 emit layer->afterRollBack();
357 if ( layer->isModified() )
361 layer->undoStack()->setIndex( 0 );
364 layer->updateFields();
368 layer->clearEditBuffer();
369 layer->undoStack()->clear();
370 emit layer->editingStopped();
373 if ( rollbackExtent )
374 layer->updateExtents();
377 layer->dataProvider()->leaveUpdateMode();
379 layer->triggerRepaint();
382 mIsEditing = ! stopEditing;