|
| 1 | +/*************************************************************************** |
| 2 | + qgstransactiongroup.cpp - QgsTransactionGroup |
| 3 | +
|
| 4 | + --------------------- |
| 5 | + begin : 15.1.2016 |
| 6 | + copyright : (C) 2016 by mku |
| 7 | + email : [your-email-here] |
| 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 "qgstransaction.h" |
| 19 | +#include "qgsvectorlayer.h" |
| 20 | +#include "qgsdatasourceuri.h" |
| 21 | +#include "qgsvectordataprovider.h" |
| 22 | + |
| 23 | +#include <QTimer> |
| 24 | + |
| 25 | +QgsTransactionGroup::QgsTransactionGroup( QObject *parent ) |
| 26 | + : QObject( parent ) |
| 27 | + , mEditingStarting( false ) |
| 28 | + , mEditingStopping( false ) |
| 29 | + , mTransaction( nullptr ) |
| 30 | +{ |
| 31 | + |
| 32 | +} |
| 33 | + |
| 34 | +QgsTransactionGroup::~QgsTransactionGroup() |
| 35 | +{ |
| 36 | + delete mTransaction; |
| 37 | +} |
| 38 | + |
| 39 | +bool QgsTransactionGroup::addLayer( QgsVectorLayer* layer ) |
| 40 | +{ |
| 41 | + if ( !QgsTransaction::supportsTransaction( layer ) ) |
| 42 | + return false; |
| 43 | + |
| 44 | + QString connString = QgsDataSourceURI( layer->source() ).connectionInfo(); |
| 45 | + |
| 46 | + if ( mConnString.isEmpty() ) |
| 47 | + { |
| 48 | + mConnString = connString; |
| 49 | + mProviderKey = layer->providerType(); |
| 50 | + } |
| 51 | + else if ( mConnString != connString || mProviderKey != layer->providerType() ) |
| 52 | + { |
| 53 | + return false; |
| 54 | + } |
| 55 | + |
| 56 | + mLayers.insert( layer ); |
| 57 | + |
| 58 | + connect( layer, SIGNAL( beforeEditingStarted() ), this, SLOT( onEditingStarted() ) ); |
| 59 | + connect( layer, SIGNAL( layerDeleted() ), this, SLOT( onLayerDeleted() ) ); |
| 60 | + |
| 61 | + return true; |
| 62 | +} |
| 63 | + |
| 64 | +void QgsTransactionGroup::onEditingStarted() |
| 65 | +{ |
| 66 | + if ( mTransaction ) |
| 67 | + return; |
| 68 | + |
| 69 | + mTransaction = QgsTransaction::create( mConnString, mProviderKey ); |
| 70 | + |
| 71 | + QString errorMsg; |
| 72 | + mTransaction->begin( errorMsg ); |
| 73 | + |
| 74 | + Q_FOREACH ( QgsVectorLayer* layer, mLayers ) |
| 75 | + { |
| 76 | + mTransaction->addLayer( layer ); |
| 77 | + layer->startEditing(); |
| 78 | + connect( layer, SIGNAL( beforeCommitChanges() ), this, SLOT( onCommitChanges() ) ); |
| 79 | + connect( layer, SIGNAL( beforeRollBack() ), this, SLOT( onRollback() ) ); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +void QgsTransactionGroup::onLayerDeleted() |
| 84 | +{ |
| 85 | + mLayers.remove( qobject_cast<QgsVectorLayer*>( sender() ) ); |
| 86 | +} |
| 87 | + |
| 88 | +void QgsTransactionGroup::onCommitChanges() |
| 89 | +{ |
| 90 | + if ( mEditingStopping ) |
| 91 | + return; |
| 92 | + |
| 93 | + mEditingStopping = true; |
| 94 | + |
| 95 | + QgsVectorLayer* triggeringLayer = qobject_cast<QgsVectorLayer*>( sender() ); |
| 96 | + |
| 97 | + QString errMsg; |
| 98 | + if ( mTransaction->commit( errMsg ) ) |
| 99 | + { |
| 100 | + Q_FOREACH ( QgsVectorLayer* layer, mLayers ) |
| 101 | + { |
| 102 | + if ( layer != sender() ) |
| 103 | + layer->commitChanges(); |
| 104 | + } |
| 105 | + } |
| 106 | + else |
| 107 | + { |
| 108 | + emit commitError( errMsg ); |
| 109 | + // Restart editing the calling layer in the next event loop cycle |
| 110 | + QTimer::singleShot( 0, triggeringLayer, SLOT( startEditing() ) ); |
| 111 | + } |
| 112 | + mEditingStopping = false; |
| 113 | +} |
| 114 | + |
| 115 | +void QgsTransactionGroup::onRollback() |
| 116 | +{ |
| 117 | + if ( mEditingStopping ) |
| 118 | + return; |
| 119 | + |
| 120 | + mEditingStopping = true; |
| 121 | + |
| 122 | + QgsVectorLayer* triggeringLayer = qobject_cast<QgsVectorLayer*>( sender() ); |
| 123 | + |
| 124 | + QString errMsg; |
| 125 | + if ( mTransaction->rollback( errMsg ) ) |
| 126 | + { |
| 127 | + Q_FOREACH ( QgsVectorLayer* layer, mLayers ) |
| 128 | + { |
| 129 | + if ( layer != triggeringLayer ) |
| 130 | + layer->rollBack(); |
| 131 | + } |
| 132 | + } |
| 133 | + else |
| 134 | + { |
| 135 | + // Restart editing the calling layer in the next event loop cycle |
| 136 | + QTimer::singleShot( 0, triggeringLayer, SLOT( startEditing() ) ); |
| 137 | + } |
| 138 | + mEditingStopping = false; |
| 139 | +} |
| 140 | + |
| 141 | +QString QgsTransactionGroup::providerKey() const |
| 142 | +{ |
| 143 | + return mProviderKey; |
| 144 | +} |
| 145 | + |
| 146 | +bool QgsTransactionGroup::isEmpty() const |
| 147 | +{ |
| 148 | + return mLayers.isEmpty(); |
| 149 | +} |
| 150 | + |
| 151 | +QString QgsTransactionGroup::connString() const |
| 152 | +{ |
| 153 | + return mConnString; |
| 154 | +} |
0 commit comments