27#include "moc_qgstransactiongroup.cpp"
29using namespace Qt::StringLiterals;
43 if ( mConnString.isEmpty() )
53 mLayers.insert( layer );
56 connect( layer, &QgsVectorLayer::destroyed,
this, &QgsTransactionGroup::onLayerDeleted );
68 const auto constMLayers = mLayers;
71 if ( layer->isModified() )
77void QgsTransactionGroup::onEditingStarted()
87 mTransaction->begin( errorMsg );
89 const auto triggeringLayer = qobject_cast<QgsVectorLayer *>( sender() );
91 const auto constMLayers = mLayers;
94 mTransaction->addLayer( layer,
true );
97 if ( layer != triggeringLayer )
99 layer->startEditing();
108void QgsTransactionGroup::onLayerDeleted()
110 mLayers.remove(
static_cast<QgsVectorLayer *
>( sender() ) );
113void QgsTransactionGroup::onBeforeCommitChanges(
bool stopEditing )
115 if ( mEditingStopping )
118 mEditingStopping =
true;
120 const QgsVectorLayer *triggeringLayer = qobject_cast<QgsVectorLayer *>( sender() );
123 if ( mTransaction->commit( errMsg ) )
125 const auto constMLayers = mLayers;
126 for ( QgsVectorLayer *layer : constMLayers )
128 if ( layer != triggeringLayer )
130 layer->commitChanges( stopEditing );
136 disableTransaction();
140 if ( ! mTransaction->begin( errMsg ) )
142 QgsDebugError( u
"Could not restart a transaction for %1: %2"_s.arg( triggeringLayer->
name() ).arg( errMsg ) );
150 restartTransaction( triggeringLayer );
152 mEditingStopping =
false;
155void QgsTransactionGroup::onRollback()
157 if ( mEditingStopping )
160 mEditingStopping =
true;
162 QgsVectorLayer *triggeringLayer = qobject_cast<QgsVectorLayer *>( sender() );
165 if ( mTransaction->rollback( errMsg ) )
167 const auto constMLayers = mLayers;
168 for ( QgsVectorLayer *layer : constMLayers )
170 if ( layer != triggeringLayer )
173 disableTransaction();
177 restartTransaction( triggeringLayer );
179 mEditingStopping =
false;
182void QgsTransactionGroup::disableTransaction()
184 mTransaction.reset();
186 const auto constMLayers = mLayers;
187 for ( QgsVectorLayer *layer : constMLayers )
194void QgsTransactionGroup::restartTransaction(
const QgsVectorLayer *layer )
207 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)