Skip to content

Commit cda4950

Browse files
committedSep 26, 2018
Change canCommitChanges to allowCommit flag
1 parent d2bb3d6 commit cda4950

File tree

3 files changed

+106
-6
lines changed

3 files changed

+106
-6
lines changed
 

‎python/core/auto_generated/qgsvectorlayer.sip.in

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2236,6 +2236,44 @@ Test if an edit command is active
22362236
%Docstring
22372237
Configuration and logic to apply automatically on any edit happening on this layer.
22382238

2239+
.. versionadded:: 3.4
2240+
%End
2241+
2242+
bool allowCommit()() const;
2243+
%Docstring
2244+
Controls, if the layer is allowed to commit changes. If this is set to false
2245+
it will not be possible to commit changes on this layer. This can be used to
2246+
define checks on a layer that need to be pass before the layer can be saved.
2247+
If you use this API, make sure that:
2248+
2249+
- the user is visibly informed that his changes were not saved and what he needs
2250+
to do in order to be able to save the changes.
2251+
2252+
- to set the property back to true, once the user has fixed his data.
2253+
2254+
When calling :py:func:`commitChanges`, this flag is checked just after the
2255+
2256+
.. seealso:: :py:func:`beforeCommitChanges`
2257+
2258+
.. versionadded:: 3.4
2259+
%End
2260+
2261+
void setAllowCommit()( bool allowCommit() );
2262+
%Docstring
2263+
Controls, if the layer is allowed to commit changes. If this is set to false
2264+
it will not be possible to commit changes on this layer. This can be used to
2265+
define checks on a layer that need to be pass before the layer can be saved.
2266+
If you use this API, make sure that:
2267+
2268+
- the user is visibly informed that his changes were not saved and what he needs
2269+
to do in order to be able to save the changes.
2270+
2271+
- to set the property back to true, once the user has fixed his data.
2272+
2273+
When calling :py:func:`commitChanges`, this flag is checked just after the
2274+
2275+
.. seealso:: :py:func:`beforeCommitChanges`
2276+
22392277
.. versionadded:: 3.4
22402278
%End
22412279

@@ -2332,6 +2370,13 @@ This signal is emitted when selection was changed
23322370
void layerModified();
23332371
%Docstring
23342372
This signal is emitted when modifications has been done on layer
2373+
%End
2374+
2375+
void allowCommitChanged();
2376+
%Docstring
2377+
Emitted whenever the allowCommitChanged() property of this layer changes.
2378+
2379+
.. versionadded:: 3.4
23352380
%End
23362381

23372382
void beforeModifiedCheck() const;

‎src/core/qgsvectorlayer.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2924,15 +2924,11 @@ bool QgsVectorLayer::commitChanges()
29242924
return false;
29252925
}
29262926

2927-
bool canCommit = true;
2928-
2929-
emit canCommitChanges( &canCommit );
2927+
emit beforeCommitChanges();
29302928

2931-
if ( !canCommit )
2929+
if ( !mAllowCommit )
29322930
return false;
29332931

2934-
emit beforeCommitChanges();
2935-
29362932
bool success = mEditBuffer->commitChanges( mCommitErrors );
29372933

29382934
if ( success )
@@ -4853,6 +4849,20 @@ QgsAbstractVectorLayerLabeling *QgsVectorLayer::readLabelingFromCustomProperties
48534849
return labeling;
48544850
}
48554851

4852+
bool QgsVectorLayer::allowCommit() const
4853+
{
4854+
return mAllowCommit;
4855+
}
4856+
4857+
void QgsVectorLayer::setAllowCommit( bool allowCommit )
4858+
{
4859+
if ( mAllowCommit == allowCommit )
4860+
return;
4861+
4862+
mAllowCommit = allowCommit;
4863+
emit allowCommitChanged();
4864+
}
4865+
48564866
QgsGeometryOptions *QgsVectorLayer::geometryOptions() const
48574867
{
48584868
return mGeometryOptions.get();

‎src/core/qgsvectorlayer.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2010,6 +2010,42 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
20102010
*/
20112011
QgsGeometryOptions *geometryOptions() const;
20122012

2013+
/**
2014+
* Controls, if the layer is allowed to commit changes. If this is set to false
2015+
* it will not be possible to commit changes on this layer. This can be used to
2016+
* define checks on a layer that need to be pass before the layer can be saved.
2017+
* If you use this API, make sure that:
2018+
*
2019+
* - the user is visibly informed that his changes were not saved and what he needs
2020+
* to do in order to be able to save the changes.
2021+
*
2022+
* - to set the property back to true, once the user has fixed his data.
2023+
*
2024+
* When calling \see commitChanges(), this flag is checked just after the
2025+
* \see beforeCommitChanges() signal is emitted, so it's possible to adjust it from there.
2026+
*
2027+
* \since QGIS 3.4
2028+
*/
2029+
bool allowCommit()() const;
2030+
2031+
/**
2032+
* Controls, if the layer is allowed to commit changes. If this is set to false
2033+
* it will not be possible to commit changes on this layer. This can be used to
2034+
* define checks on a layer that need to be pass before the layer can be saved.
2035+
* If you use this API, make sure that:
2036+
*
2037+
* - the user is visibly informed that his changes were not saved and what he needs
2038+
* to do in order to be able to save the changes.
2039+
*
2040+
* - to set the property back to true, once the user has fixed his data.
2041+
*
2042+
* When calling \see commitChanges(), this flag is checked just after the
2043+
* \see beforeCommitChanges() signal is emitted, so it's possible to adjust it from there.
2044+
*
2045+
* \since QGIS 3.4
2046+
*/
2047+
void setAllowCommit()( bool allowCommit() );
2048+
20132049
public slots:
20142050

20152051
/**
@@ -2102,6 +2138,13 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
21022138
//! This signal is emitted when modifications has been done on layer
21032139
void layerModified();
21042140

2141+
/**
2142+
* Emitted whenever the allowCommitChanged() property of this layer changes.
2143+
*
2144+
* \since QGIS 3.4
2145+
*/
2146+
void allowCommitChanged();
2147+
21052148
//! Is emitted, when layer is checked for modifications. Use for last-minute additions
21062149
void beforeModifiedCheck() const;
21072150

@@ -2512,6 +2555,8 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
25122555

25132556
std::unique_ptr<QgsGeometryOptions> mGeometryOptions;
25142557

2558+
bool mAllowCommit = true;
2559+
25152560
friend class QgsVectorLayerFeatureSource;
25162561
};
25172562

0 commit comments

Comments
 (0)
Please sign in to comment.