27#include "moc_qgstransactiongroup.cpp"
29using namespace Qt::StringLiterals;
41 if ( mConnString.isEmpty() )
51 mLayers.insert( layer );
54 connect( layer, &QgsVectorLayer::destroyed,
this, &QgsTransactionGroup::onLayerDeleted );
66 const auto constMLayers = mLayers;
69 if ( layer->isModified() )
75void QgsTransactionGroup::onEditingStarted()
85 mTransaction->begin( errorMsg );
87 const auto triggeringLayer = qobject_cast<QgsVectorLayer *>( sender() );
89 const auto constMLayers = mLayers;
92 mTransaction->addLayer( layer,
true );
95 if ( layer != triggeringLayer )
97 layer->startEditing();
105void QgsTransactionGroup::onLayerDeleted()
107 mLayers.remove(
static_cast<QgsVectorLayer *
>( sender() ) );
110void QgsTransactionGroup::onBeforeCommitChanges(
bool stopEditing )
112 if ( mEditingStopping )
116 for ( QgsVectorLayer *layer : std::as_const( mLayers ) )
118 emit layer->beforeCommitChecks( stopEditing );
121 for ( QgsVectorLayer *layer : std::as_const( mLayers ) )
123 if ( !layer->allowCommit() )
130 mEditingStopping =
true;
132 const QgsVectorLayer *triggeringLayer = qobject_cast<QgsVectorLayer *>( sender() );
135 if ( mTransaction->commit( errMsg ) )
137 const auto constMLayers = mLayers;
138 for ( QgsVectorLayer *layer : constMLayers )
140 if ( layer != triggeringLayer )
142 layer->commitChanges( stopEditing );
148 disableTransaction();
152 if ( !mTransaction->begin( errMsg ) )
154 QgsDebugError( u
"Could not restart a transaction for %1: %2"_s.arg( triggeringLayer->
name() ).arg( errMsg ) );
161 restartTransaction( triggeringLayer );
163 mEditingStopping =
false;
166void QgsTransactionGroup::onRollback()
168 if ( mEditingStopping )
171 mEditingStopping =
true;
173 QgsVectorLayer *triggeringLayer = qobject_cast<QgsVectorLayer *>( sender() );
176 if ( mTransaction->rollback( errMsg ) )
178 const auto constMLayers = mLayers;
179 for ( QgsVectorLayer *layer : constMLayers )
181 if ( layer != triggeringLayer )
184 disableTransaction();
188 restartTransaction( triggeringLayer );
190 mEditingStopping =
false;
193void QgsTransactionGroup::disableTransaction()
195 mTransaction.reset();
197 const auto constMLayers = mLayers;
198 for ( QgsVectorLayer *layer : constMLayers )
205void QgsTransactionGroup::restartTransaction(
const QgsVectorLayer *layer )
218 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)