Skip to content

Commit

Permalink
Merge pull request #495 from matthias-kuhn/fix-7485
Browse files Browse the repository at this point in the history
[FIX #7485]: Don't crash when expression fails to parse in dual view/preview column.
  • Loading branch information
jef-n committed Apr 2, 2013
2 parents 00ae45b + 002f73e commit c4645ea
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 9 deletions.
12 changes: 11 additions & 1 deletion src/gui/attributetable/qgsdualview.cpp
Expand Up @@ -27,6 +27,7 @@
#include <QDialog>
#include <QMenu>
#include <QProgressDialog>
#include <QMessageBox>

QgsDualView::QgsDualView( QWidget* parent )
: QStackedWidget( parent )
Expand Down Expand Up @@ -262,7 +263,16 @@ void QgsDualView::previewColumnChanged( QObject* action )

if ( previewAction )
{
mFeatureList->setDisplayExpression( previewAction->text() );
if ( !mFeatureList->setDisplayExpression( previewAction->text() ) )
{
QMessageBox::warning( this
, tr( "Could not set preview column" )
, tr( "Could not set column '%1' as preview column.\nParser error:\n%2" )
.arg( previewAction->text() )
.arg( mFeatureList->parserErrorString() )
);
}

mFeatureListPreviewButton->setDefaultAction( previewAction );
mFeatureListPreviewButton->setPopupMode( QToolButton::InstantPopup );
}
Expand Down
12 changes: 9 additions & 3 deletions src/gui/attributetable/qgsfeaturelistmodel.cpp
Expand Up @@ -107,7 +107,7 @@ QItemSelectionModel* QgsFeatureListModel::masterSelection()
return mFilterModel->masterSelection();
}

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

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

if ( exp->hasParserError() )
{
QString msg = exp->parserErrorString();
mParserErrorString = exp->parserErrorString();
delete exp;
throw QgsException( msg );
return false;
}

delete mExpression;
mExpression = exp;

emit( dataChanged( index( 0, 0 ), index( rowCount() - 1, 0 ) ) );
return true;
}

QString QgsFeatureListModel::parserErrorString()
{
return mParserErrorString;
}

const QString& QgsFeatureListModel::displayExpression() const
Expand Down
13 changes: 11 additions & 2 deletions src/gui/attributetable/qgsfeaturelistmodel.h
Expand Up @@ -53,10 +53,18 @@ class QgsFeatureListModel : public QAbstractProxyModel
QItemSelectionModel* masterSelection();

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

/**
* @brief Returns a detailed message about errors while parsing a QgsExpression.
* @return A message containg information about the parser error.
*/
QString parserErrorString();

const QString& displayExpression() const;
bool featureByIndex( const QModelIndex& index, QgsFeature& feat );
Expand Down Expand Up @@ -87,6 +95,7 @@ class QgsFeatureListModel : public QAbstractProxyModel
private:
QgsExpression* mExpression;
QgsAttributeTableFilterModel* mFilterModel;
QString mParserErrorString;
};

Q_DECLARE_METATYPE( QgsFeatureListModel::FeatureInfo )
Expand Down
9 changes: 7 additions & 2 deletions src/gui/attributetable/qgsfeaturelistview.cpp
Expand Up @@ -71,16 +71,21 @@ void QgsFeatureListView::setModel( QgsFeatureListModel* featureListModel )
connect( mCurrentEditSelectionModel, SIGNAL( selectionChanged( QItemSelection, QItemSelection ) ), SLOT( editSelectionChanged( QItemSelection, QItemSelection ) ) );
}

void QgsFeatureListView::setDisplayExpression( const QString expression )
bool QgsFeatureListView::setDisplayExpression( const QString expression )
{
mModel->setDisplayExpression( expression );
return mModel->setDisplayExpression( expression );
}

const QString& QgsFeatureListView::displayExpression() const
{
return mModel->displayExpression();
}

QString QgsFeatureListView::parserErrorString()
{
return mModel->parserErrorString();
}

void QgsFeatureListView::mouseMoveEvent( QMouseEvent *event )
{
QPoint pos = event->pos();
Expand Down
8 changes: 7 additions & 1 deletion src/gui/attributetable/qgsfeaturelistview.h
Expand Up @@ -80,7 +80,7 @@ class GUI_EXPORT QgsFeatureListView : public QListView
*
* @see QgsExpression
*/
void setDisplayExpression( const QString displayExpression );
bool setDisplayExpression( const QString displayExpression );

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

/**
* Returns a detailed message about errors while parsing a QgsExpression.
*
* @return A message containg information about the parser error.
*/
QString parserErrorString();

protected:
virtual void mouseMoveEvent( QMouseEvent *event );
Expand Down

0 comments on commit c4645ea

Please sign in to comment.