Skip to content

Commit d667bf5

Browse files
committedJun 23, 2017
Move iterator exception handling to c++
1 parent a8b364e commit d667bf5

File tree

4 files changed

+70
-32
lines changed

4 files changed

+70
-32
lines changed
 

‎python/core/processing/qgsprocessingcontext.sip

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ Destination project
142142
.. seealso:: invalidGeometryCheck()
143143
%End
144144

145-
146145
void setInvalidGeometryCallback( SIP_PYCALLABLE / AllowNone / );
147146
%Docstring
148147
Sets a callback function to use when encountering an invalid geometry and
@@ -198,16 +197,29 @@ Destination project
198197
%Docstring
199198
Sets the default ``encoding`` to use for newly created files.
200199
.. seealso:: defaultEncoding()
200+
%End
201+
202+
QgsProcessingFeedback *feedback();
203+
%Docstring
204+
Returns the associated feedback object.
205+
.. seealso:: setFeedback()
206+
:rtype: QgsProcessingFeedback
207+
%End
208+
209+
void setFeedback( QgsProcessingFeedback *feedback );
210+
%Docstring
211+
Sets an associated ``feedback`` object. This allows context related functions
212+
to report feedback and errors to users and processing logs. While ideally this feedback
213+
object should outlive the context, only a weak pointer to ``feedback`` is stored
214+
and no errors will occur if feedback is deleted before the context.
215+
Ownership of ``feedback`` is not transferred.
216+
.. seealso:: setFeedback()
201217
%End
202218

203219
private:
204220
QgsProcessingContext( const QgsProcessingContext &other );
205221
};
206222

207-
208-
209-
210-
211223
QFlags<QgsProcessingContext::Flag> operator|(QgsProcessingContext::Flag f1, QFlags<QgsProcessingContext::Flag> f2);
212224

213225

‎python/plugins/processing/gui/AlgorithmDialogBase.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def resetGUI(self):
189189

190190
def setInfo(self, msg, error=False, escape_html=True):
191191
if error:
192-
self.txtLog.append('<span style="color:red"><br>{}<br></span>'.format(msg, quote=False))
192+
self.txtLog.append('<span style="color:red">{}</span><br />'.format(msg, quote=False))
193193
elif escape_html:
194194
self.txtLog.append(html.escape(msg))
195195
else:

‎python/plugins/processing/tools/dataobjects.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,27 +71,13 @@ def createContext(feedback=None):
7171
"""
7272
context = QgsProcessingContext()
7373
context.setProject(QgsProject.instance())
74+
context.setFeedback(feedback)
7475

7576
invalid_features_method = ProcessingConfig.getSetting(ProcessingConfig.FILTER_INVALID_GEOMETRIES)
7677
if invalid_features_method is None:
7778
invalid_features_method = QgsFeatureRequest.GeometryAbortOnInvalid
7879
context.setInvalidGeometryCheck(invalid_features_method)
7980

80-
def raise_invalid_geometry_error(f, feedback=feedback):
81-
if feedback:
82-
feedback.pushInfo(QCoreApplication.translate("FeatureIterator",
83-
'Feature with id {} has invalid geometry, skipping feature.'.format(f.id())))
84-
85-
if context.invalidGeometryCheck() == QgsFeatureRequest.GeometrySkipInvalid:
86-
context.setInvalidGeometryCallback(raise_invalid_geometry_error)
87-
88-
def raise_transform_error(f, feedback=feedback):
89-
if feedback:
90-
feedback.pushInfo(QCoreApplication.translate("FeatureIterator",
91-
'Encountered a transform error when reprojecting feature with id {}.'.format(f.id())))
92-
93-
context.setTransformErrorCallback(raise_transform_error)
94-
9581
settings = QgsSettings()
9682
context.setDefaultEncoding(settings.value("/Processing/encoding", "System"))
9783

‎src/core/processing/qgsprocessingcontext.h

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "qgsfeaturerequest.h"
2626
#include "qgsmaplayerlistutils.h"
2727
#include "qgsexception.h"
28+
#include "qgsprocessingfeedback.h"
2829

2930
/**
3031
* \class QgsProcessingContext
@@ -50,7 +51,15 @@ class CORE_EXPORT QgsProcessingContext
5051
/**
5152
* Constructor for QgsProcessingContext.
5253
*/
53-
QgsProcessingContext() = default;
54+
QgsProcessingContext()
55+
{
56+
auto callback = [ = ]( const QgsFeature & feature )
57+
{
58+
if ( mFeedback )
59+
mFeedback->reportError( QObject::tr( "Encountered a transform error when reprojecting feature with id %1." ).arg( feature.id() ) );
60+
};
61+
mTransformErrorCallback = callback;
62+
}
5463

5564
//! QgsProcessingContext cannot be copied
5665
QgsProcessingContext( const QgsProcessingContext &other ) = delete;
@@ -176,17 +185,34 @@ class CORE_EXPORT QgsProcessingContext
176185
{
177186
mInvalidGeometryCheck = check;
178187

179-
if ( mInvalidGeometryCheck == QgsFeatureRequest::GeometryAbortOnInvalid )
188+
switch ( mInvalidGeometryCheck )
180189
{
181-
auto callback = []( const QgsFeature & feature )
190+
case QgsFeatureRequest::GeometryAbortOnInvalid:
191+
{
192+
auto callback = []( const QgsFeature & feature )
193+
{
194+
throw QgsProcessingException( QObject::tr( "Feature (%1) has invalid geometry. Please fix the geometry or change the Processing setting to the \"Ignore invalid input features\" option." ).arg( feature.id() ) );
195+
};
196+
mInvalidGeometryCallback = callback;
197+
break;
198+
}
199+
200+
case QgsFeatureRequest::GeometrySkipInvalid:
182201
{
183-
throw QgsProcessingException( QObject::tr( "Feature (%1) has invalid geometry. Please fix the geometry or change the Processing setting to the \"Ignore invalid input features\" option." ).arg( feature.id() ) );
184-
};
185-
mInvalidGeometryCallback = callback;
202+
auto callback = [ = ]( const QgsFeature & feature )
203+
{
204+
if ( mFeedback )
205+
mFeedback->reportError( QObject::tr( "Feature (%1) has invalid geometry and has been skipped. Please fix the geometry or change the Processing setting to the \"Ignore invalid input features\" option." ).arg( feature.id() ) );
206+
};
207+
mInvalidGeometryCallback = callback;
208+
break;
209+
}
210+
211+
default:
212+
break;
186213
}
187214
}
188215

189-
190216
/**
191217
* Sets a callback function to use when encountering an invalid geometry and
192218
* invalidGeometryCheck() is set to GeometryAbortOnInvalid. This function will be
@@ -268,6 +294,22 @@ class CORE_EXPORT QgsProcessingContext
268294
*/
269295
void setDefaultEncoding( const QString &encoding ) { mDefaultEncoding = encoding; }
270296

297+
/**
298+
* Returns the associated feedback object.
299+
* \see setFeedback()
300+
*/
301+
QgsProcessingFeedback *feedback() { return mFeedback; }
302+
303+
/**
304+
* Sets an associated \a feedback object. This allows context related functions
305+
* to report feedback and errors to users and processing logs. While ideally this feedback
306+
* object should outlive the context, only a weak pointer to \a feedback is stored
307+
* and no errors will occur if feedback is deleted before the context.
308+
* Ownership of \a feedback is not transferred.
309+
* \see setFeedback()
310+
*/
311+
void setFeedback( QgsProcessingFeedback *feedback ) { mFeedback = feedback; }
312+
271313
private:
272314

273315
QgsProcessingContext::Flags mFlags = 0;
@@ -282,15 +324,13 @@ class CORE_EXPORT QgsProcessingContext
282324
QString mDefaultEncoding;
283325
QMap< QString, LayerDetails > mLayersToLoadOnCompletion;
284326

327+
QPointer< QgsProcessingFeedback > mFeedback;
328+
285329
#ifdef SIP_RUN
286330
QgsProcessingContext( const QgsProcessingContext &other );
287331
#endif
288332
};
289333

290-
291-
292-
293-
294334
Q_DECLARE_OPERATORS_FOR_FLAGS( QgsProcessingContext::Flags )
295335

296336
#endif // QGSPROCESSINGPARAMETERS_H

0 commit comments

Comments
 (0)
Please sign in to comment.