QGIS API Documentation 4.1.0-Master (60fea48833c)
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 mEditingStopping = true;
116
117 const QgsVectorLayer *triggeringLayer = qobject_cast<QgsVectorLayer *>( sender() );
118
119 QString errMsg;
120 if ( mTransaction->commit( errMsg ) )
121 {
122 const auto constMLayers = mLayers;
123 for ( QgsVectorLayer *layer : constMLayers )
124 {
125 if ( layer != triggeringLayer )
126 {
127 layer->commitChanges( stopEditing );
128 }
129 }
130
131 if ( stopEditing )
132 {
133 disableTransaction();
134 }
135 else
136 {
137 if ( !mTransaction->begin( errMsg ) )
138 {
139 QgsDebugError( u"Could not restart a transaction for %1: %2"_s.arg( triggeringLayer->name() ).arg( errMsg ) );
140 }
141 }
142 }
143 else
144 {
145 emit commitError( errMsg );
146 restartTransaction( triggeringLayer );
147 }
148 mEditingStopping = false;
149}
150
151void QgsTransactionGroup::onRollback()
152{
153 if ( mEditingStopping )
154 return;
155
156 mEditingStopping = true;
157
158 QgsVectorLayer *triggeringLayer = qobject_cast<QgsVectorLayer *>( sender() );
159
160 QString errMsg;
161 if ( mTransaction->rollback( errMsg ) )
162 {
163 const auto constMLayers = mLayers;
164 for ( QgsVectorLayer *layer : constMLayers )
165 {
166 if ( layer != triggeringLayer )
167 layer->rollBack();
168 }
169 disableTransaction();
170 }
171 else
172 {
173 restartTransaction( triggeringLayer );
174 }
175 mEditingStopping = false;
176}
177
178void QgsTransactionGroup::disableTransaction()
179{
180 mTransaction.reset();
181
182 const auto constMLayers = mLayers;
183 for ( QgsVectorLayer *layer : constMLayers )
184 {
185 disconnect( layer, &QgsVectorLayer::beforeCommitChanges, this, &QgsTransactionGroup::onBeforeCommitChanges );
186 disconnect( layer, &QgsVectorLayer::beforeRollBack, this, &QgsTransactionGroup::onRollback );
187 }
188}
189
190void QgsTransactionGroup::restartTransaction( const QgsVectorLayer *layer )
191{
192 // Restart editing the calling layer in the next event loop cycle
193 QTimer::singleShot( 0, layer, &QgsVectorLayer::startEditing );
194}
195
197{
198 return mProviderKey;
199}
200
202{
203 return mLayers.isEmpty();
204}
205
207{
208 return mConnString;
209}
QString name
Definition qgsmaplayer.h:87
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:59