QGIS API Documentation 4.3.0-Master (0cef2469b72)
Loading...
Searching...
No Matches
qgstransactiongroup.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgstransactiongroup.cpp - QgsTransactionGroup
3 ---------------------------------------------
4
5 begin : 15.1.2016
6 Copyright : (C) 2016 Matthias Kuhn
7 Email : matthias at opengis dot ch
8 ***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16#include "qgstransactiongroup.h"
17
18#include "qgsdatasourceuri.h"
19#include "qgslogger.h"
20#include "qgstransaction.h"
22#include "qgsvectorlayer.h"
23
24#include <QString>
25#include <QTimer>
26
27#include "moc_qgstransactiongroup.cpp"
28
29using namespace Qt::StringLiterals;
30
32 : QObject( parent )
33{}
34
36{
38 return false;
39
40 const QString connString = QgsTransaction::connectionString( layer->source() );
41 if ( mConnString.isEmpty() )
42 {
43 mConnString = connString;
44 mProviderKey = layer->providerType();
45 }
46 else if ( mConnString != connString || mProviderKey != layer->providerType() )
47 {
48 return false;
49 }
50
51 mLayers.insert( layer );
52
53 connect( layer, &QgsVectorLayer::beforeEditingStarted, this, &QgsTransactionGroup::onEditingStarted );
54 connect( layer, &QgsVectorLayer::destroyed, this, &QgsTransactionGroup::onLayerDeleted );
55
56 return true;
57}
58
59QSet<QgsVectorLayer *> QgsTransactionGroup::layers() const
60{
61 return mLayers;
62}
63
65{
66 const auto constMLayers = mLayers;
67 for ( QgsVectorLayer *layer : constMLayers )
68 {
69 if ( layer->isModified() )
70 return true;
71 }
72 return false;
73}
74
75void QgsTransactionGroup::onEditingStarted()
76{
77 if ( mTransaction )
78 return;
79
80 mTransaction.reset( QgsTransaction::create( mConnString, mProviderKey ) );
81 if ( !mTransaction )
82 return;
83
84 QString errorMsg;
85 mTransaction->begin( errorMsg );
86
87 const auto triggeringLayer = qobject_cast<QgsVectorLayer *>( sender() );
88
89 const auto constMLayers = mLayers;
90 for ( QgsVectorLayer *layer : constMLayers )
91 {
92 mTransaction->addLayer( layer, true );
93
94 // Do not start editing the triggering layer, it will be started by the caller
95 if ( layer != triggeringLayer )
96 {
97 layer->startEditing();
98 }
99
100 connect( layer, &QgsVectorLayer::beforeCommitChanges, this, &QgsTransactionGroup::onBeforeCommitChanges );
101 connect( layer, &QgsVectorLayer::beforeRollBack, this, &QgsTransactionGroup::onRollback );
102 }
103}
104
105void QgsTransactionGroup::onLayerDeleted()
106{
107 mLayers.remove( static_cast<QgsVectorLayer *>( sender() ) );
108}
109
110void QgsTransactionGroup::onBeforeCommitChanges( bool stopEditing )
111{
112 if ( mEditingStopping )
113 return;
114
115 // checks have to be done for all the layers
116 for ( QgsVectorLayer *layer : std::as_const( mLayers ) )
117 {
118 emit layer->beforeCommitChecks( stopEditing );
119 }
120
121 for ( QgsVectorLayer *layer : std::as_const( mLayers ) )
122 {
123 if ( !layer->allowCommit() )
124 {
125 //if one layer does not allow, we cannot commit
126 return;
127 }
128 }
129
130 mEditingStopping = true;
131
132 const QgsVectorLayer *triggeringLayer = qobject_cast<QgsVectorLayer *>( sender() );
133
134 QString errMsg;
135 if ( mTransaction->commit( errMsg ) )
136 {
137 const auto constMLayers = mLayers;
138 for ( QgsVectorLayer *layer : constMLayers )
139 {
140 if ( layer != triggeringLayer )
141 {
142 layer->commitChanges( stopEditing );
143 }
144 }
145
146 if ( stopEditing )
147 {
148 disableTransaction();
149 }
150 else
151 {
152 if ( !mTransaction->begin( errMsg ) )
153 {
154 QgsDebugError( u"Could not restart a transaction for %1: %2"_s.arg( triggeringLayer->name() ).arg( errMsg ) );
155 }
156 }
157 }
158 else
159 {
160 emit commitError( errMsg );
161 restartTransaction( triggeringLayer );
162 }
163 mEditingStopping = false;
164}
165
166void QgsTransactionGroup::onRollback()
167{
168 if ( mEditingStopping )
169 return;
170
171 mEditingStopping = true;
172
173 QgsVectorLayer *triggeringLayer = qobject_cast<QgsVectorLayer *>( sender() );
174
175 QString errMsg;
176 if ( mTransaction->rollback( errMsg ) )
177 {
178 const auto constMLayers = mLayers;
179 for ( QgsVectorLayer *layer : constMLayers )
180 {
181 if ( layer != triggeringLayer )
182 layer->rollBack();
183 }
184 disableTransaction();
185 }
186 else
187 {
188 restartTransaction( triggeringLayer );
189 }
190 mEditingStopping = false;
191}
192
193void QgsTransactionGroup::disableTransaction()
194{
195 mTransaction.reset();
196
197 const auto constMLayers = mLayers;
198 for ( QgsVectorLayer *layer : constMLayers )
199 {
200 disconnect( layer, &QgsVectorLayer::beforeCommitChanges, this, &QgsTransactionGroup::onBeforeCommitChanges );
201 disconnect( layer, &QgsVectorLayer::beforeRollBack, this, &QgsTransactionGroup::onRollback );
202 }
203}
204
205void QgsTransactionGroup::restartTransaction( const QgsVectorLayer *layer )
206{
207 // Restart editing the calling layer in the next event loop cycle
208 QTimer::singleShot( 0, layer, &QgsVectorLayer::startEditing );
209}
210
212{
213 return mProviderKey;
214}
215
217{
218 return mLayers.isEmpty();
219}
220
222{
223 return mConnString;
224}
QString name
Definition qgsmaplayer.h:88
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)
Definition qgslogger.h:71