Skip to content

Commit c38d3e1

Browse files
committedMay 29, 2014
[composer] Remove use of runtime_error for providing feedback from atlas
1 parent 88b8369 commit c38d3e1

File tree

6 files changed

+130
-88
lines changed

6 files changed

+130
-88
lines changed
 

‎python/core/composer/qgsatlascomposition.sip

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,27 @@ public:
7474
* atlas page.
7575
* @returns filename pattern
7676
* @see setFilenamePattern
77+
* @see filenamePatternErrorString
7778
* @note This property has no effect when exporting to PDF if singleFile() is true
7879
*/
7980
QString filenamePattern() const;
8081

8182
/**Sets the filename expression used for generating output filenames for each
8283
* atlas page.
84+
* @returns true if filename expression could be successful set, false if expression is invalid
8385
* @param pattern expression to use for output filenames
8486
* @see filenamePattern
87+
* @see filenamePatternErrorString
8588
* @note This method has no effect when exporting to PDF if singleFile() is true
8689
*/
87-
void setFilenamePattern( const QString& pattern );
90+
bool setFilenamePattern( const QString& pattern );
91+
92+
/**Returns an error string from parsing the filename expression.
93+
* @returns filename pattern parser error
94+
* @see setFilenamePattern
95+
* @see filenamePattern
96+
*/
97+
QString filenamePatternErrorString() const;
8898

8999
/**Returns the coverage layer used for the atlas features
90100
* @returns atlas coverage layer
@@ -125,6 +135,13 @@ public:
125135

126136
QString featureFilter() const;
127137
void setFeatureFilter( const QString& expression );
138+
139+
/**Returns an error string from parsing the feature filter expression.
140+
* @returns filename pattern parser error
141+
* @see setFilenamePattern
142+
* @see filenamePattern
143+
*/
144+
QString featureFilterErrorString() const;
128145

129146
QString sortKeyAttributeName() const;
130147
void setSortKeyAttributeName( QString fieldName );
@@ -157,11 +174,15 @@ public:
157174
/** Returns the number of features in the coverage layer */
158175
int numFeatures() const;
159176

160-
/** Prepare the atlas map for the given feature. Sets the extent and context variables */
161-
void prepareForFeature( int i );
177+
/**Prepare the atlas map for the given feature. Sets the extent and context variables
178+
* @returns true if feature was successfully prepared
179+
*/
180+
bool prepareForFeature( int i );
162181

163-
/** Prepare the atlas map for the given feature. Sets the extent and context variables */
164-
void prepareForFeature( QgsFeature * feat );
182+
/**Prepare the atlas map for the given feature. Sets the extent and context variables
183+
* @returns true if feature was successfully prepared
184+
*/
185+
bool prepareForFeature( QgsFeature * feat );
165186

166187
/** Returns the current filename. Must be called after prepareForFeature( i ) */
167188
const QString& currentFilename() const;

‎src/app/composer/qgsatlascompositionwidget.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,24 @@ void QgsAtlasCompositionWidget::changeCoverageLayer( QgsMapLayer *layer )
8888
}
8989
}
9090

91-
void QgsAtlasCompositionWidget::on_mAtlasFilenamePatternEdit_textChanged( const QString& text )
91+
void QgsAtlasCompositionWidget::on_mAtlasFilenamePatternEdit_editingFinished()
9292
{
9393
QgsAtlasComposition* atlasMap = &mComposition->atlasComposition();
9494
if ( !atlasMap )
9595
{
9696
return;
9797
}
9898

99-
atlasMap->setFilenamePattern( text );
99+
if ( ! atlasMap->setFilenamePattern( mAtlasFilenamePatternEdit->text() ) )
100+
{
101+
//expression could not be set
102+
QMessageBox::warning( this
103+
, tr( "Could not evaluate filename pattern" )
104+
, tr( "Could not set filename pattern as '%1'.\nParser error:\n%2" )
105+
.arg( mAtlasFilenamePatternEdit->text() )
106+
.arg( atlasMap->filenamePatternErrorString() )
107+
);
108+
}
100109
}
101110

102111
void QgsAtlasCompositionWidget::on_mAtlasFilenameExpressionButton_clicked()

‎src/app/composer/qgsatlascompositionwidget.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class QgsAtlasCompositionWidget:
3737
public slots:
3838
void on_mUseAtlasCheckBox_stateChanged( int state );
3939
void changeCoverageLayer( QgsMapLayer* layer );
40-
void on_mAtlasFilenamePatternEdit_textChanged( const QString& text );
40+
void on_mAtlasFilenamePatternEdit_editingFinished();
4141
void on_mAtlasFilenameExpressionButton_clicked();
4242
void on_mAtlasHideCoverageCheckBox_stateChanged( int state );
4343
void on_mAtlasSingleFileCheckBox_stateChanged( int state );

‎src/app/composer/qgscomposer.cpp

Lines changed: 21 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,15 +1336,11 @@ void QgsComposer::exportCompositionAsPDF( QgsComposer::OutputMode mode )
13361336

13371337
QPainter painter;
13381338

1339-
try
1340-
{
1341-
loadAtlasPredefinedScalesFromProject();
1342-
atlasMap->beginRender();
1343-
}
1344-
catch ( std::exception& e )
1339+
loadAtlasPredefinedScalesFromProject();
1340+
if ( ! atlasMap->beginRender() && !atlasMap->featureFilterErrorString().isEmpty() )
13451341
{
13461342
QMessageBox::warning( this, tr( "Atlas processing error" ),
1347-
e.what(),
1343+
tr( "Feature filter parser error: %1" ).arg( atlasMap->featureFilterErrorString() ),
13481344
QMessageBox::Ok,
13491345
QMessageBox::Ok );
13501346
mView->setPaintingEnabled( true );
@@ -1371,14 +1367,10 @@ void QgsComposer::exportCompositionAsPDF( QgsComposer::OutputMode mode )
13711367
atlasMap->endRender();
13721368
break;
13731369
}
1374-
try
1375-
{
1376-
atlasMap->prepareForFeature( featureI );
1377-
}
1378-
catch ( std::runtime_error& e )
1370+
if ( !atlasMap->prepareForFeature( featureI ) )
13791371
{
13801372
QMessageBox::warning( this, tr( "Atlas processing error" ),
1381-
e.what(),
1373+
tr( "Atlas processing error" ),
13821374
QMessageBox::Ok,
13831375
QMessageBox::Ok );
13841376
mView->setPaintingEnabled( true );
@@ -1491,15 +1483,12 @@ void QgsComposer::printComposition( QgsComposer::OutputMode mode )
14911483

14921484
mComposition->beginPrint( mPrinter );
14931485
QPainter painter( &mPrinter );
1494-
try
1495-
{
1496-
loadAtlasPredefinedScalesFromProject();
1497-
atlasMap->beginRender();
1498-
}
1499-
catch ( std::exception& e )
1486+
1487+
loadAtlasPredefinedScalesFromProject();
1488+
if ( ! atlasMap->beginRender() && !atlasMap->featureFilterErrorString().isEmpty() )
15001489
{
15011490
QMessageBox::warning( this, tr( "Atlas processing error" ),
1502-
e.what(),
1491+
tr( "Feature filter parser error: %1" ).arg( atlasMap->featureFilterErrorString() ),
15031492
QMessageBox::Ok,
15041493
QMessageBox::Ok );
15051494
mView->setPaintingEnabled( true );
@@ -1518,21 +1507,16 @@ void QgsComposer::printComposition( QgsComposer::OutputMode mode )
15181507
atlasMap->endRender();
15191508
break;
15201509
}
1521-
try
1522-
{
1523-
atlasMap->prepareForFeature( i );
1524-
}
1525-
catch ( std::runtime_error& e )
1510+
if ( !atlasMap->prepareForFeature( i ) )
15261511
{
15271512
QMessageBox::warning( this, tr( "Atlas processing error" ),
1528-
e.what(),
1513+
tr( "Atlas processing error" ),
15291514
QMessageBox::Ok,
15301515
QMessageBox::Ok );
15311516
mView->setPaintingEnabled( true );
15321517
return;
15331518
}
15341519

1535-
15361520
if ( i > 0 )
15371521
{
15381522
mPrinter.newPage();
@@ -1753,15 +1737,11 @@ void QgsComposer::exportCompositionAsImage( QgsComposer::OutputMode mode )
17531737
mView->setPaintingEnabled( false );
17541738
QApplication::setOverrideCursor( Qt::BusyCursor );
17551739

1756-
try
1757-
{
1758-
loadAtlasPredefinedScalesFromProject();
1759-
atlasMap->beginRender();
1760-
}
1761-
catch ( std::exception& e )
1740+
loadAtlasPredefinedScalesFromProject();
1741+
if ( ! atlasMap->beginRender() && !atlasMap->featureFilterErrorString().isEmpty() )
17621742
{
17631743
QMessageBox::warning( this, tr( "Atlas processing error" ),
1764-
e.what(),
1744+
tr( "Feature filter parser error: %1" ).arg( atlasMap->featureFilterErrorString() ),
17651745
QMessageBox::Ok,
17661746
QMessageBox::Ok );
17671747
mView->setPaintingEnabled( true );
@@ -1781,14 +1761,10 @@ void QgsComposer::exportCompositionAsImage( QgsComposer::OutputMode mode )
17811761
atlasMap->endRender();
17821762
break;
17831763
}
1784-
try
1785-
{
1786-
atlasMap->prepareForFeature( feature );
1787-
}
1788-
catch ( std::runtime_error& e )
1764+
if ( ! atlasMap->prepareForFeature( feature ) )
17891765
{
17901766
QMessageBox::warning( this, tr( "Atlas processing error" ),
1791-
e.what(),
1767+
tr( "Atlas processing error" ),
17921768
QMessageBox::Ok,
17931769
QMessageBox::Ok );
17941770
mView->setPaintingEnabled( true );
@@ -2021,15 +1997,11 @@ void QgsComposer::exportCompositionAsSVG( QgsComposer::OutputMode mode )
20211997
int featureI = 0;
20221998
if ( mode == QgsComposer::Atlas )
20231999
{
2024-
try
2025-
{
2026-
loadAtlasPredefinedScalesFromProject();
2027-
atlasMap->beginRender();
2028-
}
2029-
catch ( std::exception& e )
2000+
loadAtlasPredefinedScalesFromProject();
2001+
if ( ! atlasMap->beginRender() && !atlasMap->featureFilterErrorString().isEmpty() )
20302002
{
20312003
QMessageBox::warning( this, tr( "Atlas processing error" ),
2032-
e.what(),
2004+
tr( "Feature filter parser error: %1" ).arg( atlasMap->featureFilterErrorString() ),
20332005
QMessageBox::Ok,
20342006
QMessageBox::Ok );
20352007
mView->setPaintingEnabled( true );
@@ -2053,14 +2025,10 @@ void QgsComposer::exportCompositionAsSVG( QgsComposer::OutputMode mode )
20532025
atlasMap->endRender();
20542026
break;
20552027
}
2056-
try
2057-
{
2058-
atlasMap->prepareForFeature( featureI );
2059-
}
2060-
catch ( std::runtime_error& e )
2028+
if ( !atlasMap->prepareForFeature( featureI ) )
20612029
{
20622030
QMessageBox::warning( this, tr( "Atlas processing error" ),
2063-
e.what(),
2031+
tr( "Atlas processing error" ),
20642032
QMessageBox::Ok,
20652033
QMessageBox::Ok );
20662034
mView->setPaintingEnabled( true );

‎src/core/composer/qgsatlascomposition.cpp

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,17 @@
2626
#include "qgsgeometry.h"
2727
#include "qgsmaplayerregistry.h"
2828
#include "qgsproject.h"
29+
#include "qgsmessagelog.h"
2930

3031
QgsAtlasComposition::QgsAtlasComposition( QgsComposition* composition ) :
3132
mComposition( composition ),
3233
mEnabled( false ),
3334
mHideCoverage( false ), mFilenamePattern( "'output_'||$feature" ),
3435
mCoverageLayer( 0 ), mSingleFile( false ),
3536
mSortFeatures( false ), mSortAscending( true ), mCurrentFeatureNo( 0 ),
36-
mFilterFeatures( false ), mFeatureFilter( "" )
37+
mFilterFeatures( false ), mFeatureFilter( "" ),
38+
mFilenameParserError( QString() ),
39+
mFilterParserError( QString() )
3740
{
3841

3942
// declare special columns with a default value
@@ -181,9 +184,11 @@ int QgsAtlasComposition::updateFeatures()
181184
filterExpression = std::auto_ptr<QgsExpression>( new QgsExpression( mFeatureFilter ) );
182185
if ( filterExpression->hasParserError() )
183186
{
184-
throw std::runtime_error( tr( "Feature filter parser error: %1" ).arg( filterExpression->parserErrorString() ).toLocal8Bit().data() );
187+
mFilterParserError = filterExpression->parserErrorString();
188+
return 0;
185189
}
186190
}
191+
mFilterParserError = QString();
187192

188193
// We cannot use nextFeature() directly since the feature pointer is rewinded by the rendering process
189194
// We thus store the feature ids for future extraction
@@ -198,7 +203,7 @@ int QgsAtlasComposition::updateFeatures()
198203
QVariant result = filterExpression->evaluate( &feat, mCoverageLayer->pendingFields() );
199204
if ( filterExpression->hasEvalError() )
200205
{
201-
throw std::runtime_error( tr( "Feature filter eval error: %1" ).arg( filterExpression->evalErrorString() ).toLocal8Bit().data() );
206+
QgsMessageLog::logMessage( tr( "Atlas filter eval error: %1" ).arg( filterExpression->evalErrorString() ), tr( "Composer" ) );
202207
}
203208

204209
// skip this feature if the filter evaluation if false
@@ -326,23 +331,23 @@ void QgsAtlasComposition::lastFeature()
326331
prepareForFeature( mFeatureIds.size() - 1 );
327332
}
328333

329-
void QgsAtlasComposition::prepareForFeature( QgsFeature * feat )
334+
bool QgsAtlasComposition::prepareForFeature( QgsFeature * feat )
330335
{
331336
int featureI = mFeatureIds.indexOf( feat->id() );
332-
prepareForFeature( featureI );
337+
return prepareForFeature( featureI );
333338
}
334339

335-
void QgsAtlasComposition::prepareForFeature( int featureI )
340+
bool QgsAtlasComposition::prepareForFeature( int featureI )
336341
{
337342
if ( !mCoverageLayer )
338343
{
339-
return;
344+
return false;
340345
}
341346

342347
if ( mFeatureIds.size() == 0 )
343348
{
344349
emit statusMsgChanged( tr( "No matching atlas features" ) );
345-
return;
350+
return false;
346351
}
347352

348353
mCurrentFeatureNo = featureI;
@@ -355,7 +360,11 @@ void QgsAtlasComposition::prepareForFeature( int featureI )
355360
QgsExpression::setSpecialColumn( "$feature", QVariant(( int )featureI + 1 ) );
356361

357362
// generate filename for current feature
358-
evalFeatureFilename();
363+
if ( !evalFeatureFilename() )
364+
{
365+
//error evaluating filename
366+
return false;
367+
}
359368

360369
emit featureChanged( &mCurrentFeature );
361370
emit statusMsgChanged( QString( tr( "Atlas feature %1 of %2" ) ).arg( featureI + 1 ).arg( mFeatureIds.size() ) );
@@ -382,7 +391,7 @@ void QgsAtlasComposition::prepareForFeature( int featureI )
382391
if ( atlasMaps.isEmpty() )
383392
{
384393
//no atlas enabled maps
385-
return;
394+
return true;
386395
}
387396

388397
// compute extent of current feature in the map CRS. This should be set on a per-atlas map basis,
@@ -395,6 +404,8 @@ void QgsAtlasComposition::prepareForFeature( int featureI )
395404
{
396405
prepareMap( *mit );
397406
}
407+
408+
return true;
398409
}
399410

400411
void QgsAtlasComposition::computeExtent( QgsComposerMap* map )
@@ -670,16 +681,18 @@ void QgsAtlasComposition::setHideCoverage( bool hide )
670681

671682
}
672683

673-
void QgsAtlasComposition::setFilenamePattern( const QString& pattern )
684+
bool QgsAtlasComposition::setFilenamePattern( const QString& pattern )
674685
{
675686
mFilenamePattern = pattern;
676-
updateFilenameExpression();
687+
return updateFilenameExpression();
677688
}
678689

679-
void QgsAtlasComposition::updateFilenameExpression()
690+
bool QgsAtlasComposition::updateFilenameExpression()
680691
{
681692
if ( !mCoverageLayer )
682-
return;
693+
{
694+
return false;
695+
}
683696

684697
const QgsFields& fields = mCoverageLayer->pendingFields();
685698

@@ -690,7 +703,8 @@ void QgsAtlasComposition::updateFilenameExpression()
690703
// test for evaluation errors
691704
if ( mFilenameExpr->hasParserError() )
692705
{
693-
throw std::runtime_error( tr( "Filename parsing error: %1" ).arg( mFilenameExpr->parserErrorString() ).toLocal8Bit().data() );
706+
mFilenameParserError = mFilenameExpr->parserErrorString();
707+
return false;
694708
}
695709

696710
// prepare the filename expression
@@ -702,22 +716,24 @@ void QgsAtlasComposition::updateFilenameExpression()
702716
{
703717
evalFeatureFilename();
704718
}
705-
719+
return true;
706720
}
707721

708-
void QgsAtlasComposition::evalFeatureFilename()
722+
bool QgsAtlasComposition::evalFeatureFilename()
709723
{
710724
//generate filename for current atlas feature
711725
if ( mFilenamePattern.size() > 0 )
712726
{
713727
QVariant filenameRes = mFilenameExpr->evaluate( &mCurrentFeature, mCoverageLayer->pendingFields() );
714728
if ( mFilenameExpr->hasEvalError() )
715729
{
716-
throw std::runtime_error( tr( "Filename eval error: %1" ).arg( mFilenameExpr->evalErrorString() ).toLocal8Bit().data() );
730+
QgsMessageLog::logMessage( tr( "Atlas filename evaluation error: %1" ).arg( mFilenameExpr->evalErrorString() ), tr( "Composer" ) );
731+
return false;
717732
}
718733

719734
mCurrentFilename = filenameRes.toString();
720735
}
736+
return true;
721737
}
722738

723739
void QgsAtlasComposition::setPredefinedScales( const QVector<double>& scales )

‎src/core/composer/qgsatlascomposition.h

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,27 @@ class CORE_EXPORT QgsAtlasComposition : public QObject
102102
* atlas page.
103103
* @returns filename pattern
104104
* @see setFilenamePattern
105+
* @see filenamePatternErrorString
105106
* @note This property has no effect when exporting to PDF if singleFile() is true
106107
*/
107108
QString filenamePattern() const { return mFilenamePattern; }
108109

109110
/**Sets the filename expression used for generating output filenames for each
110111
* atlas page.
112+
* @returns true if filename expression could be successful set, false if expression is invalid
111113
* @param pattern expression to use for output filenames
112114
* @see filenamePattern
115+
* @see filenamePatternErrorString
113116
* @note This method has no effect when exporting to PDF if singleFile() is true
114117
*/
115-
void setFilenamePattern( const QString& pattern );
118+
bool setFilenamePattern( const QString& pattern );
119+
120+
/**Returns an error string from parsing the filename expression.
121+
* @returns filename pattern parser error
122+
* @see setFilenamePattern
123+
* @see filenamePattern
124+
*/
125+
QString filenamePatternErrorString() const { return mFilenameParserError; }
116126

117127
/**Returns the coverage layer used for the atlas features
118128
* @returns atlas coverage layer
@@ -154,6 +164,13 @@ class CORE_EXPORT QgsAtlasComposition : public QObject
154164
QString featureFilter() const { return mFeatureFilter; }
155165
void setFeatureFilter( const QString& expression ) { mFeatureFilter = expression; }
156166

167+
/**Returns an error string from parsing the feature filter expression.
168+
* @returns filename pattern parser error
169+
* @see setFilenamePattern
170+
* @see filenamePattern
171+
*/
172+
QString featureFilterErrorString() const { return mFilterParserError; }
173+
157174
QString sortKeyAttributeName() const { return mSortKeyAttributeName; }
158175
void setSortKeyAttributeName( QString fieldName ) { mSortKeyAttributeName = fieldName; }
159176

@@ -185,11 +202,15 @@ class CORE_EXPORT QgsAtlasComposition : public QObject
185202
/** Returns the number of features in the coverage layer */
186203
int numFeatures() const;
187204

188-
/** Prepare the atlas map for the given feature. Sets the extent and context variables */
189-
void prepareForFeature( int i );
205+
/**Prepare the atlas map for the given feature. Sets the extent and context variables
206+
* @returns true if feature was successfully prepared
207+
*/
208+
bool prepareForFeature( int i );
190209

191-
/** Prepare the atlas map for the given feature. Sets the extent and context variables */
192-
void prepareForFeature( QgsFeature * feat );
210+
/**Prepare the atlas map for the given feature. Sets the extent and context variables
211+
* @returns true if feature was successfully prepared
212+
*/
213+
bool prepareForFeature( QgsFeature * feat );
193214

194215
/** Returns the current filename. Must be called after prepareForFeature( i ) */
195216
const QString& currentFilename() const;
@@ -237,11 +258,15 @@ class CORE_EXPORT QgsAtlasComposition : public QObject
237258
void featureChanged( QgsFeature* feature );
238259

239260
private:
240-
/**Updates the filename expression*/
241-
void updateFilenameExpression();
261+
/**Updates the filename expression.
262+
* @returns true if expression was successfully parsed, false if expression is invalid
263+
*/
264+
bool updateFilenameExpression();
242265

243-
/**Evaluates filename for current feature*/
244-
void evalFeatureFilename();
266+
/**Evaluates filename for current feature
267+
* @returns true if feature filename was successfully evaluated
268+
*/
269+
bool evalFeatureFilename();
245270

246271
QgsComposition* mComposition;
247272

@@ -261,6 +286,9 @@ class CORE_EXPORT QgsAtlasComposition : public QObject
261286
// current atlas feature number
262287
int mCurrentFeatureNo;
263288

289+
QString mFilenameParserError;
290+
QString mFilterParserError;
291+
264292
public:
265293
typedef QMap< QgsFeatureId, QVariant > SorterKeys;
266294

0 commit comments

Comments
 (0)
Please sign in to comment.