69 bool editingStarted =
true;
72 if ( !layer->isValid() )
74 editingStarted =
false;
75 QgsLogger::debug( tr(
"Can't start editing invalid layer '%1'." ).arg( layer->name() ) );
79 if ( !layer->dataProvider() )
81 editingStarted =
false;
82 QgsLogger::debug( tr(
"Can't start editing layer '%1' with invalid data provider." ).arg( layer->name() ) );
87 if ( !layer->supportsEditing() )
89 editingStarted =
false;
90 QgsLogger::debug( tr(
"Can't start editing. Layer '%1' doesn't support editing." ).arg( layer->name() ) );
94 if ( layer->editBuffer() )
97 layer->editBuffer()->setEditBufferGroup(
this );
102 emit layer->beforeEditingStarted();
103 layer->dataProvider()->enterUpdateMode();
104 layer->createEditBuffer();
105 layer->editBuffer()->setEditBufferGroup(
this );
106 layer->updateFields();
107 emit layer->editingStarted();
110 if ( ! editingStarted )
112 QStringList rollbackErrors;
113 if ( !
rollBack( rollbackErrors,
true ) )
114 QgsLogger::debug( tr(
"Can't rollback after start editing failure. Roll back detailed errors: %1" ).arg( rollbackErrors.join(
" / " ) ) );
117 mIsEditing = editingStarted;
125 const QSet<QgsVectorLayer *> constModifiedLayers =
modifiedLayers();
126 if ( constModifiedLayers.isEmpty() )
128 editingFinished( stopEditing );
129 mIsEditing = !stopEditing;
133 QMap<QString, QSet<QgsVectorLayer *> > connectionStringsLayers;
138 QList<QgsVectorLayer *> transactionLayers;
139 QList<std::shared_ptr<QgsTransaction> > openTransactions;
140 const QStringList connectionStrings = connectionStringsLayers.keys();
141 for (
const QString &connectionString : connectionStrings )
143 const QString providerKey = ( *connectionStringsLayers.value( connectionString ).begin() )->providerType();
145 std::shared_ptr<QgsTransaction> transaction;
149 commitErrors << tr(
"ERROR: data source '%1', is not available for transactions." ).arg( connectionString );
155 if ( ! transaction->begin( errorMsg ) )
157 commitErrors << tr(
"ERROR: could not start a transaction on data provider '%1', detailed error: '%2'." ).arg( providerKey, errorMsg );
162 const auto constLayers = connectionStringsLayers.value( connectionString );
165 if ( ! transaction->addLayer( layer,
true ) )
167 commitErrors << tr(
"ERROR: could not add layer '%1' to transaction on data provider '%2'." ).arg( layer->name(), providerKey );
172 transactionLayers.append( layer );
175 openTransactions.append( transaction );
182 const QList<QgsVectorLayer *> orderedLayers = orderLayersParentsToChildren( constModifiedLayers );
183 QList<QgsVectorLayer *>::const_iterator orderedLayersIterator;
188 for ( orderedLayersIterator = orderedLayers.constBegin(); orderedLayersIterator != orderedLayers.constEnd(); ++orderedLayersIterator )
190 if ( !( *orderedLayersIterator )->editBuffer() )
192 commitErrors << tr(
"ERROR: edit buffer of layer '%1' is not valid." ).arg( ( *orderedLayersIterator )->name() );
197 success = ( *orderedLayersIterator )->editBuffer()->commitChangesCheckGeometryTypeCompatibility( commitErrors );
203 QSet<QgsVectorLayer *> modifiedLayersOnProviderSide;
208 for ( orderedLayersIterator = orderedLayers.constBegin(); orderedLayersIterator != orderedLayers.constEnd(); ++orderedLayersIterator )
210 QgsFields oldFields = ( *orderedLayersIterator )->fields();
212 bool attributesDeleted =
false;
213 success = ( *orderedLayersIterator )->editBuffer()->commitChangesDeleteAttributes( attributesDeleted, commitErrors );
217 bool attributesRenamed =
false;
218 success = ( *orderedLayersIterator )->editBuffer()->commitChangesRenameAttributes( attributesRenamed, commitErrors );
222 bool attributesAdded =
false;
223 success = ( *orderedLayersIterator )->editBuffer()->commitChangesAddAttributes( attributesAdded, commitErrors );
227 if ( attributesDeleted || attributesRenamed || attributesAdded )
229 if ( ! transactionLayers.contains( ( *orderedLayersIterator ) ) )
230 modifiedLayersOnProviderSide.insert( ( *orderedLayersIterator ) );
232 success = ( *orderedLayersIterator )->editBuffer()->commitChangesCheckAttributesModifications( oldFields, commitErrors );
242 orderedLayersIterator = orderedLayers.
constEnd();
243 while ( orderedLayersIterator != orderedLayers.constBegin() )
245 --orderedLayersIterator;
246 bool featuresDeleted;
247 success = ( *orderedLayersIterator )->editBuffer()->commitChangesDeleteFeatures( featuresDeleted, commitErrors );
251 if ( featuresDeleted && transactionLayers.contains( ( *orderedLayersIterator ) ) )
252 modifiedLayersOnProviderSide.insert( ( *orderedLayersIterator ) );
259 for ( orderedLayersIterator = orderedLayers.constBegin(); orderedLayersIterator != orderedLayers.constEnd(); ++orderedLayersIterator )
262 ( *orderedLayersIterator )->editBuffer()->commitChangesAddFeatures( featuresAdded, commitErrors );
266 if ( featuresAdded && transactionLayers.contains( ( *orderedLayersIterator ) ) )
267 modifiedLayersOnProviderSide.insert( ( *orderedLayersIterator ) );
274 orderedLayersIterator = orderedLayers.constEnd();
275 while ( orderedLayersIterator != orderedLayers.constBegin() )
277 --orderedLayersIterator;
279 bool attributesChanged;
280 success = ( *orderedLayersIterator )->editBuffer()->commitChangesChangeAttributes( attributesChanged, commitErrors );
284 if ( attributesChanged && transactionLayers.contains( ( *orderedLayersIterator ) ) )
285 modifiedLayersOnProviderSide.insert( ( *orderedLayersIterator ) );
292 QList<std::shared_ptr<QgsTransaction> >::iterator openTransactionsIterator = openTransactions.begin();
293 while ( openTransactionsIterator != openTransactions.end() )
296 if ( !( *openTransactionsIterator )->commit( errorMsg ) )
299 commitErrors << tr(
"ERROR: could not commit a transaction, detailed error: '%1'." ).arg( errorMsg );
303 modifiedLayersOnProviderSide += connectionStringsLayers.value( ( *openTransactionsIterator )->connectionString() );
304 openTransactionsIterator = openTransactions.erase( openTransactionsIterator );
312 if ( ! modifiedLayersOnProviderSide.isEmpty() )
314 if ( modifiedLayersOnProviderSide.size() == 1 )
315 commitErrors << tr(
"WARNING: changes to layer '%1' were already sent to data provider and cannot be rolled back." ).arg( ( *modifiedLayersOnProviderSide.begin() )->name() );
318 commitErrors << tr(
"WARNING: changes to following layers were already sent to data provider and cannot be rolled back:" );
319 for (
QgsVectorLayer *layer : std::as_const( modifiedLayersOnProviderSide ) )
320 commitErrors << tr(
"- '%1'" ).arg( layer->name() );
324 QString rollbackError;
325 for (
const std::shared_ptr<QgsTransaction> &transaction : openTransactions )
326 transaction->rollback( rollbackError );
331 editingFinished( stopEditing );
333 if ( success && stopEditing )
343 if ( ! layer->editBuffer() )
346 if ( !layer->dataProvider() )
348 rollbackErrors << tr(
"Layer '%1' doesn't have a valid data provider" ).arg( layer->name() );
352 bool rollbackExtent = !layer->editBuffer()->deletedFeatureIds().isEmpty() ||
353 !layer->editBuffer()->addedFeatures().isEmpty() ||
354 !layer->editBuffer()->changedGeometries().isEmpty();
356 emit layer->beforeRollBack();
358 layer->editBuffer()->rollBack();
360 emit layer->afterRollBack();
362 if ( layer->isModified() )
366 layer->undoStack()->setIndex( 0 );
369 layer->updateFields();
373 layer->clearEditBuffer();
374 layer->undoStack()->clear();
375 emit layer->editingStopped();
378 if ( rollbackExtent )
379 layer->updateExtents();
382 layer->dataProvider()->leaveUpdateMode();
384 layer->triggerRepaint();
387 mIsEditing = ! stopEditing;