Skip to content

Commit

Permalink
Fix layer saving in transaction groups
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Jan 18, 2016
1 parent 75926f5 commit 433e3b8
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 5 deletions.
12 changes: 12 additions & 0 deletions python/core/qgstransactiongroup.sip
Expand Up @@ -31,6 +31,18 @@ class QgsTransactionGroup : QObject
*/
bool addLayer( QgsVectorLayer* layer );

/**
* Get the set of layers currently managed by this transaction group.
*
* @return Layer set
*/
QSet<QgsVectorLayer*> layers() const;

/**
* Returns true if any of the layers in this group reports a modification.
*/
bool modified() const;

/**
* Return the connection string used by this transaction group.
* Layers need be compatible when added.
Expand Down
13 changes: 12 additions & 1 deletion src/app/qgisapp.cpp
Expand Up @@ -7066,6 +7066,17 @@ bool QgisApp::toggleEditing( QgsMapLayer *layer, bool allowCancel )

bool res = true;

QString connString = QgsDataSourceURI( vlayer->source() ).connectionInfo();
QString key = vlayer->providerType();

QgsTransactionGroup* tg = mTransactionGroups.find( qMakePair( key, connString ) ).value();

bool isModified = false;

// Assume changes if: a) the layer reports modifications or b) its transaction group was modified
if ( vlayer->isModified() || ( tg && tg->layers().contains( vlayer ) && tg->modified() ) )
isModified = true;

if ( !vlayer->isEditable() && !vlayer->isReadOnly() )
{
if ( !( vlayer->dataProvider()->capabilities() & QgsVectorDataProvider::EditingCapabilities ) )
Expand All @@ -7091,7 +7102,7 @@ bool QgisApp::toggleEditing( QgsMapLayer *layer, bool allowCancel )
vlayer->triggerRepaint();
}
}
else if ( vlayer->isModified() )
else if ( isModified )
{
QMessageBox::StandardButtons buttons = QMessageBox::Save | QMessageBox::Discard;
if ( allowCancel )
Expand Down
15 changes: 15 additions & 0 deletions src/core/qgstransactiongroup.cpp
Expand Up @@ -59,6 +59,21 @@ bool QgsTransactionGroup::addLayer( QgsVectorLayer* layer )
return true;
}

QSet<QgsVectorLayer*> QgsTransactionGroup::layers() const
{
return mLayers;
}

bool QgsTransactionGroup::modified() const
{
Q_FOREACH ( QgsVectorLayer* layer, mLayers )
{
if ( layer->isModified() )
return true;
}
return false;
}

void QgsTransactionGroup::onEditingStarted()
{
if ( !mTransaction.isNull() )
Expand Down
12 changes: 12 additions & 0 deletions src/core/qgstransactiongroup.h
Expand Up @@ -37,6 +37,18 @@ class CORE_EXPORT QgsTransactionGroup : public QObject
*/
bool addLayer( QgsVectorLayer* layer );

/**
* Get the set of layers currently managed by this transaction group.
*
* @return Layer set
*/
QSet<QgsVectorLayer*> layers() const;

/**
* Returns true if any of the layers in this group reports a modification.
*/
bool modified() const;

/**
* Return the connection string used by this transaction group.
* Layers need be compatible when added.
Expand Down
1 change: 1 addition & 0 deletions src/core/qgsvectorlayer.h
Expand Up @@ -378,6 +378,7 @@ struct CORE_EXPORT QgsVectorJoinInfo
*
* Provider to display vector data in a GRASS GIS layer.
*
* TODO QGIS3: Remove virtual from non-inherited methods (like isModified, isReadOnly)
*/


Expand Down
22 changes: 22 additions & 0 deletions src/core/qgsvectorlayereditpassthrough.cpp
Expand Up @@ -17,6 +17,17 @@
#include "qgsvectorlayer.h"
#include "qgsvectordataprovider.h"

QgsVectorLayerEditPassthrough::QgsVectorLayerEditPassthrough( QgsVectorLayer* layer )
: mModified( false )
{
L = layer;
}

bool QgsVectorLayerEditPassthrough::isModified() const
{
return mModified;
}

bool QgsVectorLayerEditPassthrough::addFeature( QgsFeature& f )
{

Expand All @@ -26,6 +37,7 @@ bool QgsVectorLayerEditPassthrough::addFeature( QgsFeature& f )
{
f = fl.first();
emit featureAdded( f.id() );
mModified = true;
return true;
}
return false;
Expand All @@ -39,6 +51,7 @@ bool QgsVectorLayerEditPassthrough::addFeatures( QgsFeatureList& features )
{
emit featureAdded( f.id() );
}
mModified = true;
return true;
}
return false;
Expand All @@ -49,6 +62,7 @@ bool QgsVectorLayerEditPassthrough::deleteFeature( QgsFeatureId fid )
if ( L->dataProvider()->deleteFeatures( QgsFeatureIds() << fid ) )
{
emit featureDeleted( fid );
mModified = true;
return true;
}
return false;
Expand All @@ -61,6 +75,7 @@ bool QgsVectorLayerEditPassthrough::deleteFeatures( QgsFeatureIds fids )
Q_FOREACH ( QgsFeatureId fid, fids )
emit featureDeleted( fid );

mModified = true;
return true;
}
return false;
Expand All @@ -73,6 +88,7 @@ bool QgsVectorLayerEditPassthrough::changeGeometry( QgsFeatureId fid, QgsGeometr
if ( L->dataProvider()->changeGeometryValues( geomMap ) )
{
emit geometryChanged( fid, *geom );
mModified = true;
return true;
}
return false;
Expand All @@ -87,6 +103,7 @@ bool QgsVectorLayerEditPassthrough::changeAttributeValue( QgsFeatureId fid, int
if ( L->dataProvider()->changeAttributeValues( attribMap ) )
{
emit attributeValueChanged( fid, field, newValue );
mModified = true;
return true;
}
return false;
Expand All @@ -97,6 +114,7 @@ bool QgsVectorLayerEditPassthrough::addAttribute( const QgsField &field )
if ( L->dataProvider()->addAttributes( QList<QgsField>() << field ) )
{
emit attributeAdded( L->dataProvider()->fieldNameIndex( field.name() ) );
mModified = true;
return true;
}
return false;
Expand All @@ -106,17 +124,21 @@ bool QgsVectorLayerEditPassthrough::deleteAttribute( int attr )
{
if ( L->dataProvider()->deleteAttributes( QgsAttributeIds() << attr ) )
{
mModified = true;
emit attributeDeleted( attr );
mModified = true;
return true;
}
return false;
}

bool QgsVectorLayerEditPassthrough::commitChanges( QStringList& /*commitErrors*/ )
{
mModified = false;
return true;
}

void QgsVectorLayerEditPassthrough::rollBack()
{
mModified = false;
}
6 changes: 4 additions & 2 deletions src/core/qgsvectorlayereditpassthrough.h
Expand Up @@ -23,8 +23,8 @@ class CORE_EXPORT QgsVectorLayerEditPassthrough : public QgsVectorLayerEditBuffe
{
Q_OBJECT
public:
QgsVectorLayerEditPassthrough( QgsVectorLayer* layer ) { L = layer; }
bool isModified() const override { return true; }
QgsVectorLayerEditPassthrough( QgsVectorLayer* layer );
bool isModified() const override;
bool addFeature( QgsFeature& f ) override;
bool addFeatures( QgsFeatureList& features ) override;
bool deleteFeature( QgsFeatureId fid ) override;
Expand All @@ -36,6 +36,8 @@ class CORE_EXPORT QgsVectorLayerEditPassthrough : public QgsVectorLayerEditBuffe
bool commitChanges( QStringList& commitErrors ) override;
void rollBack() override;

private:
bool mModified;
};

#endif // QGSVECTORLAYEREDITPASSTHROUGH_H
11 changes: 9 additions & 2 deletions src/gui/qgsmapcanvas.h
Expand Up @@ -285,14 +285,21 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
//! return list of layers within map canvas.
QList<QgsMapLayer*> layers() const;

/** Freeze/thaw the map canvas. This is used to prevent the canvas from
/**
* Freeze/thaw the map canvas. This is used to prevent the canvas from
* responding to events while layers are being added/removed etc.
* @param frz Boolean specifying if the canvas should be frozen (true) or
* thawed (false). Default is true.
*
* TODO remove in QGIS 3
*/
void freeze( bool frz = true );

/** Accessor for frozen status of canvas */
/**
* Accessor for frozen status of canvas
*
* TODO remove in QGIS 3
*/
bool isFrozen();

//! Flag the canvas as dirty and needed a refresh
Expand Down

0 comments on commit 433e3b8

Please sign in to comment.