Skip to content

Commit c4645ea

Browse files
committedApr 2, 2013
Merge pull request #495 from matthias-kuhn/fix-7485
[FIX #7485]: Don't crash when expression fails to parse in dual view/preview column.
2 parents 00ae45b + 002f73e commit c4645ea

File tree

5 files changed

+45
-9
lines changed

5 files changed

+45
-9
lines changed
 

‎src/gui/attributetable/qgsdualview.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <QDialog>
2828
#include <QMenu>
2929
#include <QProgressDialog>
30+
#include <QMessageBox>
3031

3132
QgsDualView::QgsDualView( QWidget* parent )
3233
: QStackedWidget( parent )
@@ -262,7 +263,16 @@ void QgsDualView::previewColumnChanged( QObject* action )
262263

263264
if ( previewAction )
264265
{
265-
mFeatureList->setDisplayExpression( previewAction->text() );
266+
if ( !mFeatureList->setDisplayExpression( previewAction->text() ) )
267+
{
268+
QMessageBox::warning( this
269+
, tr( "Could not set preview column" )
270+
, tr( "Could not set column '%1' as preview column.\nParser error:\n%2" )
271+
.arg( previewAction->text() )
272+
.arg( mFeatureList->parserErrorString() )
273+
);
274+
}
275+
266276
mFeatureListPreviewButton->setDefaultAction( previewAction );
267277
mFeatureListPreviewButton->setPopupMode( QToolButton::InstantPopup );
268278
}

‎src/gui/attributetable/qgsfeaturelistmodel.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ QItemSelectionModel* QgsFeatureListModel::masterSelection()
107107
return mFilterModel->masterSelection();
108108
}
109109

110-
void QgsFeatureListModel::setDisplayExpression( const QString expression )
110+
bool QgsFeatureListModel::setDisplayExpression( const QString expression )
111111
{
112112
const QgsFields fields = mFilterModel->layer()->dataProvider()->fields();
113113

@@ -117,15 +117,21 @@ void QgsFeatureListModel::setDisplayExpression( const QString expression )
117117

118118
if ( exp->hasParserError() )
119119
{
120-
QString msg = exp->parserErrorString();
120+
mParserErrorString = exp->parserErrorString();
121121
delete exp;
122-
throw QgsException( msg );
122+
return false;
123123
}
124124

125125
delete mExpression;
126126
mExpression = exp;
127127

128128
emit( dataChanged( index( 0, 0 ), index( rowCount() - 1, 0 ) ) );
129+
return true;
130+
}
131+
132+
QString QgsFeatureListModel::parserErrorString()
133+
{
134+
return mParserErrorString;
129135
}
130136

131137
const QString& QgsFeatureListModel::displayExpression() const

‎src/gui/attributetable/qgsfeaturelistmodel.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,18 @@ class QgsFeatureListModel : public QAbstractProxyModel
5353
QItemSelectionModel* masterSelection();
5454

5555
/**
56-
* @throws QgsException in case the expression cannot be parsed
56+
* @param expression A {@link QgsExpression} compatible string.
57+
* @return true if the expression could be set, false if there was a parse error.
58+
* If it fails, the old expression will still be applied. Call {@link parserErrorString()}
59+
* for a meaningful error message.
5760
*/
58-
void setDisplayExpression( const QString expression );
61+
bool setDisplayExpression( const QString expression );
5962

63+
/**
64+
* @brief Returns a detailed message about errors while parsing a QgsExpression.
65+
* @return A message containg information about the parser error.
66+
*/
67+
QString parserErrorString();
6068

6169
const QString& displayExpression() const;
6270
bool featureByIndex( const QModelIndex& index, QgsFeature& feat );
@@ -87,6 +95,7 @@ class QgsFeatureListModel : public QAbstractProxyModel
8795
private:
8896
QgsExpression* mExpression;
8997
QgsAttributeTableFilterModel* mFilterModel;
98+
QString mParserErrorString;
9099
};
91100

92101
Q_DECLARE_METATYPE( QgsFeatureListModel::FeatureInfo )

‎src/gui/attributetable/qgsfeaturelistview.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,21 @@ void QgsFeatureListView::setModel( QgsFeatureListModel* featureListModel )
7171
connect( mCurrentEditSelectionModel, SIGNAL( selectionChanged( QItemSelection, QItemSelection ) ), SLOT( editSelectionChanged( QItemSelection, QItemSelection ) ) );
7272
}
7373

74-
void QgsFeatureListView::setDisplayExpression( const QString expression )
74+
bool QgsFeatureListView::setDisplayExpression( const QString expression )
7575
{
76-
mModel->setDisplayExpression( expression );
76+
return mModel->setDisplayExpression( expression );
7777
}
7878

7979
const QString& QgsFeatureListView::displayExpression() const
8080
{
8181
return mModel->displayExpression();
8282
}
8383

84+
QString QgsFeatureListView::parserErrorString()
85+
{
86+
return mModel->parserErrorString();
87+
}
88+
8489
void QgsFeatureListView::mouseMoveEvent( QMouseEvent *event )
8590
{
8691
QPoint pos = event->pos();

‎src/gui/attributetable/qgsfeaturelistview.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class GUI_EXPORT QgsFeatureListView : public QListView
8080
*
8181
* @see QgsExpression
8282
*/
83-
void setDisplayExpression( const QString displayExpression );
83+
bool setDisplayExpression( const QString displayExpression );
8484

8585
/**
8686
* Returns the expression which is currently used to render the features.
@@ -91,6 +91,12 @@ class GUI_EXPORT QgsFeatureListView : public QListView
9191
*/
9292
const QString& displayExpression() const;
9393

94+
/**
95+
* Returns a detailed message about errors while parsing a QgsExpression.
96+
*
97+
* @return A message containg information about the parser error.
98+
*/
99+
QString parserErrorString();
94100

95101
protected:
96102
virtual void mouseMoveEvent( QMouseEvent *event );

0 commit comments

Comments
 (0)
Please sign in to comment.