1 /***************************************************************************
2 qgsvectorlayereditpassthrough.cpp
3 ---------------------
4 begin : Jan 12 2015
5 copyright : (C) 2015 by Sandro Mani
6 email : manisandro at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16 #include "qgsvectorlayereditpassthrough.h"
17 #include "qgsvectorlayer.h"
18 #include "qgsvectordataprovider.h"
19 #include "qgsvectorlayerundopassthroughcommand.h"
20 #include "qgstransaction.h"
21
QgsVectorLayerEditPassthrough(QgsVectorLayer * layer)22 QgsVectorLayerEditPassthrough::QgsVectorLayerEditPassthrough( QgsVectorLayer *layer )
23 : mModified( false )
24 {
25 L = layer;
26 }
27
isModified() const28 bool QgsVectorLayerEditPassthrough::isModified() const
29 {
30 return mModified;
31 }
32
modify(QgsVectorLayerUndoPassthroughCommand * cmd)33 bool QgsVectorLayerEditPassthrough::modify( QgsVectorLayerUndoPassthroughCommand *cmd )
34 {
35 L->undoStack()->push( cmd ); // push takes ownership -> no need for cmd to be a smart ptr
36 if ( cmd->hasError() )
37 return false;
38
39 if ( !mModified )
40 {
41 mModified = true;
42 emit layerModified();
43 }
44
45 return true;
46 }
47
addFeature(QgsFeature & f)48 bool QgsVectorLayerEditPassthrough::addFeature( QgsFeature &f )
49 {
50 QgsVectorLayerUndoPassthroughCommandAddFeatures *cmd = new QgsVectorLayerUndoPassthroughCommandAddFeatures( this, QgsFeatureList() << f );
51 if ( !modify( cmd ) ) // modify takes owneship -> no need for cmd to be a smart ptr
52 return false;
53
54 const QgsFeatureList features = cmd->features();
55 f = features.at( features.count() - 1 );
56 return true;
57 }
58
addFeatures(QgsFeatureList & features)59 bool QgsVectorLayerEditPassthrough::addFeatures( QgsFeatureList &features )
60 {
61 QgsVectorLayerUndoPassthroughCommandAddFeatures *cmd = new QgsVectorLayerUndoPassthroughCommandAddFeatures( this, features );
62 if ( !modify( cmd ) ) // modify takes owneship -> no need for cmd to be a smart ptr
63 return false;
64
65 features = cmd->features();
66 return true;
67 }
68
deleteFeature(QgsFeatureId fid)69 bool QgsVectorLayerEditPassthrough::deleteFeature( QgsFeatureId fid )
70 {
71 return modify( new QgsVectorLayerUndoPassthroughCommandDeleteFeatures( this, QgsFeatureIds() << fid ) );
72 }
73
deleteFeatures(const QgsFeatureIds & fids)74 bool QgsVectorLayerEditPassthrough::deleteFeatures( const QgsFeatureIds &fids )
75 {
76 return modify( new QgsVectorLayerUndoPassthroughCommandDeleteFeatures( this, fids ) );
77 }
78
changeGeometry(QgsFeatureId fid,const QgsGeometry & geom)79 bool QgsVectorLayerEditPassthrough::changeGeometry( QgsFeatureId fid, const QgsGeometry &geom )
80 {
81 return modify( new QgsVectorLayerUndoPassthroughCommandChangeGeometry( this, fid, geom ) );
82 }
83
changeAttributeValue(QgsFeatureId fid,int field,const QVariant & newValue,const QVariant &)84 bool QgsVectorLayerEditPassthrough::changeAttributeValue( QgsFeatureId fid, int field, const QVariant &newValue, const QVariant &/*oldValue*/ )
85 {
86 return modify( new QgsVectorLayerUndoPassthroughCommandChangeAttribute( this, fid, field, newValue ) );
87 }
88
changeAttributeValues(QgsFeatureId fid,const QgsAttributeMap & newValues,const QgsAttributeMap & oldValues)89 bool QgsVectorLayerEditPassthrough::changeAttributeValues( QgsFeatureId fid, const QgsAttributeMap &newValues, const QgsAttributeMap &oldValues )
90 {
91 return modify( new QgsVectorLayerUndoPassthroughCommandChangeAttributes( this, fid, newValues, oldValues ) );
92 }
93
addAttribute(const QgsField & field)94 bool QgsVectorLayerEditPassthrough::addAttribute( const QgsField &field )
95 {
96 return modify( new QgsVectorLayerUndoPassthroughCommandAddAttribute( this, field ) );
97 }
98
deleteAttribute(int attr)99 bool QgsVectorLayerEditPassthrough::deleteAttribute( int attr )
100 {
101 return modify( new QgsVectorLayerUndoPassthroughCommandDeleteAttribute( this, attr ) );
102 }
103
renameAttribute(int attr,const QString & newName)104 bool QgsVectorLayerEditPassthrough::renameAttribute( int attr, const QString &newName )
105 {
106 return modify( new QgsVectorLayerUndoPassthroughCommandRenameAttribute( this, attr, newName ) );
107 }
108
commitChanges(QStringList &)109 bool QgsVectorLayerEditPassthrough::commitChanges( QStringList & /*commitErrors*/ )
110 {
111 mModified = false;
112 return true;
113 }
114
rollBack()115 void QgsVectorLayerEditPassthrough::rollBack()
116 {
117 mModified = false;
118 }
119
update(QgsTransaction * tr,const QString & sql,const QString & name)120 bool QgsVectorLayerEditPassthrough::update( QgsTransaction *tr, const QString &sql, const QString &name )
121 {
122 return modify( new QgsVectorLayerUndoPassthroughCommandUpdate( this, tr, sql, name ) );
123 }
124