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 )
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( u
"Could not restart a transaction for %1: %2"_s.arg( triggeringLayer->
name() ).arg( errMsg ) );
146 restartTransaction( triggeringLayer );
148 mEditingStopping =
false;
151void QgsTransactionGroup::onRollback()
153 if ( mEditingStopping )
156 mEditingStopping =
true;
158 QgsVectorLayer *triggeringLayer = qobject_cast<QgsVectorLayer *>( sender() );
161 if ( mTransaction->rollback( errMsg ) )
163 const auto constMLayers = mLayers;
164 for ( QgsVectorLayer *layer : constMLayers )
166 if ( layer != triggeringLayer )
169 disableTransaction();
173 restartTransaction( triggeringLayer );
175 mEditingStopping =
false;
178void QgsTransactionGroup::disableTransaction()
180 mTransaction.reset();
182 const auto constMLayers = mLayers;
183 for ( QgsVectorLayer *layer : constMLayers )
190void QgsTransactionGroup::restartTransaction(
const QgsVectorLayer *layer )
203 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)