26#include "moc_qgstransactiongroup.cpp"
40 if ( mConnString.isEmpty() )
50 mLayers.insert( layer );
53 connect( layer, &QgsVectorLayer::destroyed,
this, &QgsTransactionGroup::onLayerDeleted );
65 const auto constMLayers = mLayers;
68 if ( layer->isModified() )
74void QgsTransactionGroup::onEditingStarted()
84 mTransaction->begin( errorMsg );
86 const auto triggeringLayer = qobject_cast<QgsVectorLayer *>( sender() );
88 const auto constMLayers = mLayers;
91 mTransaction->addLayer( layer,
true );
94 if ( layer != triggeringLayer )
96 layer->startEditing();
105void QgsTransactionGroup::onLayerDeleted()
107 mLayers.remove(
static_cast<QgsVectorLayer *
>( sender() ) );
110void QgsTransactionGroup::onBeforeCommitChanges(
bool stopEditing )
112 if ( mEditingStopping )
115 mEditingStopping =
true;
117 const QgsVectorLayer *triggeringLayer = qobject_cast<QgsVectorLayer *>( sender() );
120 if ( mTransaction->commit( errMsg ) )
122 const auto constMLayers = mLayers;
123 for ( QgsVectorLayer *layer : constMLayers )
125 if ( layer != triggeringLayer )
127 layer->commitChanges( stopEditing );
133 disableTransaction();
137 if ( ! mTransaction->begin( errMsg ) )
139 QgsDebugError( QStringLiteral(
"Could not restart a transaction for %1: %2" ).arg( triggeringLayer->
name() ).arg( errMsg ) );
147 restartTransaction( triggeringLayer );
149 mEditingStopping =
false;
152void QgsTransactionGroup::onRollback()
154 if ( mEditingStopping )
157 mEditingStopping =
true;
159 QgsVectorLayer *triggeringLayer = qobject_cast<QgsVectorLayer *>( sender() );
162 if ( mTransaction->rollback( errMsg ) )
164 const auto constMLayers = mLayers;
165 for ( QgsVectorLayer *layer : constMLayers )
167 if ( layer != triggeringLayer )
170 disableTransaction();
174 restartTransaction( triggeringLayer );
176 mEditingStopping =
false;
179void QgsTransactionGroup::disableTransaction()
181 mTransaction.reset();
183 const auto constMLayers = mLayers;
184 for ( QgsVectorLayer *layer : constMLayers )
191void QgsTransactionGroup::restartTransaction(
const QgsVectorLayer *layer )
204 return mLayers.isEmpty();
QString source() const
Returns the source for the layer.
QString providerType() const
Returns the provider type (provider key) for this layer.
QString connString() const
Returns the connection string used by this transaction group.
bool modified() const
Returns true if any of the layers in this group reports a modification.
QgsTransactionGroup(QObject *parent=nullptr)
Constructor for QgsTransactionGroup.
bool isEmpty() const
Returns true if there are no layers in this transaction group.
void commitError(const QString &msg)
Will be emitted whenever there is a commit error.
QString providerKey() const
Returns the provider key used by this transaction group.
bool addLayer(QgsVectorLayer *layer)
Add a layer to this transaction group.
QSet< QgsVectorLayer * > layers() const
Gets the set of layers currently managed by this transaction group.
static bool supportsTransaction(const QgsVectorLayer *layer)
Checks if the provider of a given layer supports transactions.
QString connectionString() const
Returns the connection string of the transaction.
static QgsTransaction * create(const QString &connString, const QString &providerKey)
Create a transaction for the specified connection string connString and provider with providerKey.
Represents a vector layer which manages a vector based dataset.
void beforeCommitChanges(bool stopEditing)
Emitted before changes are committed to the data provider.
Q_INVOKABLE bool startEditing()
Makes the layer editable.
void beforeEditingStarted()
Emitted before editing on this layer is started.
void beforeRollBack()
Emitted before changes are rolled back.
#define QgsDebugError(str)