Skip to content

Commit

Permalink
Decouple transaction handling from QgsProject
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Oct 29, 2017
1 parent 5dbeaf5 commit 0c321fb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 48 deletions.
13 changes: 3 additions & 10 deletions python/core/qgstransaction.sip
Expand Up @@ -11,6 +11,7 @@




class QgsTransaction : QObject /Abstract/
{
%Docstring
Expand Down Expand Up @@ -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
Expand Down
45 changes: 17 additions & 28 deletions src/core/qgstransaction.cpp
Expand Up @@ -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();
}


Expand All @@ -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 )
Expand All @@ -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 )
Expand Down Expand Up @@ -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 )
Expand Down
14 changes: 4 additions & 10 deletions src/core/qgstransaction.h
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -182,7 +176,7 @@ class CORE_EXPORT QgsTransaction : public QObject SIP_ABSTRACT
QString mConnString;

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

private:

Expand Down

0 comments on commit 0c321fb

Please sign in to comment.