Skip to content
Permalink
Browse files

Decouple transaction handling from QgsProject

  • Loading branch information
m-kuhn committed Oct 29, 2017
1 parent 5dbeaf5 commit 0c321fb5b041290d82de61ab126da826afad553d
Showing with 24 additions and 48 deletions.
  1. +3 −10 python/core/qgstransaction.sip
  2. +17 −28 src/core/qgstransaction.cpp
  3. +4 −10 src/core/qgstransaction.h
@@ -11,6 +11,7 @@




class QgsTransaction : QObject /Abstract/
{
%Docstring
@@ -47,24 +48,16 @@ class QgsTransaction : QObject /Abstract/
:rtype: QgsTransaction
%End

static QgsTransaction *create( const QStringList &layerIds ) /Factory/;
static QgsTransaction *create( const QSet<QgsVectorLayer *> &layers ) /Factory/;
%Docstring
Create a transaction which includes the layers specified with
``layerIds``.
Create a transaction which includes the ``layers``.
All layers are expected to have the same connection string and data
provider.
:rtype: QgsTransaction
%End

virtual ~QgsTransaction();

bool addLayer( const QString &layerId );
%Docstring
Add the layer with ``layerId`` to the transaction. The layer must not be
in edit mode and the connection string must match.
:rtype: bool
%End

bool addLayer( QgsVectorLayer *layer );
%Docstring
Add the ``layer`` to the transaction. The layer must not be
@@ -44,30 +44,28 @@ QgsTransaction *QgsTransaction::create( const QString &connString, const QString
return ts;
}

QgsTransaction *QgsTransaction::create( const QStringList &layerIds )
QgsTransaction *QgsTransaction::create( const QSet<QgsVectorLayer *> &layers )
{
if ( layerIds.isEmpty() )
if ( layers.isEmpty() )
return nullptr;

QgsVectorLayer *layer = qobject_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( layerIds.first() ) );
if ( !layer )
return nullptr;

QString connStr = QgsDataSourceUri( layer->source() ).connectionInfo( false );
QString providerKey = layer->dataProvider()->name();
QgsTransaction *ts = QgsTransaction::create( connStr, providerKey );
if ( !ts )
return nullptr;
QgsVectorLayer *firstLayer = *layers.constBegin();

Q_FOREACH ( const QString &layerId, layerIds )
QString connStr = QgsDataSourceUri( firstLayer->source() ).connectionInfo( false );
QString providerKey = firstLayer->dataProvider()->name();
std::unique_ptr<QgsTransaction> transaction( QgsTransaction::create( connStr, providerKey ) );
if ( transaction )
{
if ( !ts->addLayer( layerId ) )
for ( QgsVectorLayer *layer : layers )
{
delete ts;
return nullptr;
if ( !transaction->addLayer( layer ) )
{
transaction.reset();
break;
}
}
}
return ts;
return transaction.release();
}


@@ -83,12 +81,6 @@ QgsTransaction::~QgsTransaction()
setLayerTransactionIds( nullptr );
}

bool QgsTransaction::addLayer( const QString &layerId )
{
QgsVectorLayer *layer = qobject_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( layerId ) );
return addLayer( layer );
}

bool QgsTransaction::addLayer( QgsVectorLayer *layer )
{
if ( !layer )
@@ -113,7 +105,7 @@ bool QgsTransaction::addLayer( QgsVectorLayer *layer )
}

connect( this, &QgsTransaction::afterRollback, layer->dataProvider(), &QgsVectorDataProvider::dataChanged );
connect( QgsProject::instance(), static_cast < void ( QgsProject::* )( const QStringList & ) >( &QgsProject::layersWillBeRemoved ), this, &QgsTransaction::onLayersDeleted );
connect( layer, &QgsVectorLayer::destroyed, this, &QgsTransaction::onLayerDeleted );
mLayers.insert( layer );

if ( mTransactionActive )
@@ -177,12 +169,9 @@ bool QgsTransaction::supportsTransaction( const QgsVectorLayer *layer )
return lib->resolve( "createTransaction" );
}

void QgsTransaction::onLayersDeleted( const QStringList &layerids )
void QgsTransaction::onLayerDeleted()
{
Q_FOREACH ( const QString &layerid, layerids )
Q_FOREACH ( QgsVectorLayer *l, mLayers )
if ( l->id() == layerid )
mLayers.remove( l );
mLayers.remove( qobject_cast<QgsVectorLayer *>( sender() ) );
}

void QgsTransaction::setLayerTransactionIds( QgsTransaction *transaction )
@@ -52,6 +52,7 @@ class QgsVectorLayer;
*
* Edits on features can get rejected if another conflicting transaction is active.
*/

class CORE_EXPORT QgsTransaction : public QObject SIP_ABSTRACT
{
Q_OBJECT
@@ -65,21 +66,14 @@ class CORE_EXPORT QgsTransaction : public QObject SIP_ABSTRACT
static QgsTransaction *create( const QString &connString, const QString &providerKey ) SIP_FACTORY;

/**
* Create a transaction which includes the layers specified with
* \a layerIds.
* Create a transaction which includes the \a layers.
* All layers are expected to have the same connection string and data
* provider.
*/
static QgsTransaction *create( const QStringList &layerIds ) SIP_FACTORY;
static QgsTransaction *create( const QSet<QgsVectorLayer *> &layers ) SIP_FACTORY;

virtual ~QgsTransaction();

/**
* Add the layer with \a layerId to the transaction. The layer must not be
* in edit mode and the connection string must match.
*/
bool addLayer( const QString &layerId );

/**
* Add the \a layer to the transaction. The layer must not be
* in edit mode and the connection string must match.
@@ -182,7 +176,7 @@ class CORE_EXPORT QgsTransaction : public QObject SIP_ABSTRACT
QString mConnString;

private slots:
void onLayersDeleted( const QStringList &layerids );
void onLayerDeleted();

private:

0 comments on commit 0c321fb

Please sign in to comment.
You can’t perform that action at this time.