Skip to content

Commit

Permalink
expression widget: do not provide new expression at each keypress
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed May 6, 2014
1 parent 866a24d commit 41ccfb9
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 32 deletions.
31 changes: 28 additions & 3 deletions src/gui/qgsfieldexpressionwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ QgsFieldExpressionWidget::QgsFieldExpressionWidget( QWidget *parent )
layout->addWidget( mButton );

connect( mCombo->lineEdit(), SIGNAL( textEdited( QString ) ), this, SLOT( expressionEdited( QString ) ) );
connect( mCombo->lineEdit(), SIGNAL( editingFinished() ), this, SLOT( expressionEditingFinished() ) );
connect( mCombo, SIGNAL( activated( int ) ), this, SLOT( currentFieldChanged( int ) ) );
connect( mButton, SIGNAL( clicked() ), this, SLOT( editExpression() ) );
}
Expand Down Expand Up @@ -147,6 +148,12 @@ void QgsFieldExpressionWidget::editExpression()

void QgsFieldExpressionWidget::expressionEdited( const QString expression )
{
updateLineEditStyle( expression );
}

void QgsFieldExpressionWidget::expressionEditingFinished()
{
const QString expression = mCombo->lineEdit()->text();
QModelIndex idx = mFieldModel->setExpression( expression );
mCombo->setCurrentIndex( idx.row() );
currentFieldChanged();
Expand All @@ -172,7 +179,7 @@ void QgsFieldExpressionWidget::currentFieldChanged( int i /* =0 */ )
emit fieldChanged( fieldName, isValid );
}

void QgsFieldExpressionWidget::updateLineEditStyle()
void QgsFieldExpressionWidget::updateLineEditStyle( const QString expression )
{
QPalette palette;
if ( !isEnabled() )
Expand All @@ -182,8 +189,15 @@ void QgsFieldExpressionWidget::updateLineEditStyle()
else
{
bool isExpression, isValid;
currentField( &isExpression, &isValid );

if ( !expression.isEmpty() )
{
isExpression = true;
isValid = isExpressionValid( expression );
}
else
{
currentField( &isExpression, &isValid );
}
QFont font = mCombo->lineEdit()->font();
font.setItalic( isExpression );
mCombo->lineEdit()->setFont( font );
Expand All @@ -199,3 +213,14 @@ void QgsFieldExpressionWidget::updateLineEditStyle()
}
mCombo->lineEdit()->setPalette( palette );
}

bool QgsFieldExpressionWidget::isExpressionValid( const QString expressionStr )
{
QgsVectorLayer* vl = layer();
if ( !vl )
return false;

QgsExpression expression( expressionStr );
expression.prepare( vl->pendingFields() );
return !expression.hasParserError();
}
15 changes: 12 additions & 3 deletions src/gui/qgsfieldexpressionwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class QgsMapLayer;
class QgsVectorLayer;
class QgsFieldModel;


class GUI_EXPORT QgsFieldExpressionWidget : public QWidget
{
Q_OBJECT
Expand Down Expand Up @@ -78,12 +77,22 @@ class GUI_EXPORT QgsFieldExpressionWidget : public QWidget
//! open the expression dialog to edit the current or add a new expression
void editExpression();

//! when expression is edited by the user in the line edit
//! when expression is edited by the user in the line edit, it will be checked for validity
void expressionEdited( const QString expression );

//! when expression has been edited (finished) it will be added to the model
void expressionEditingFinished();

void currentFieldChanged( int i = 0 );

void updateLineEditStyle();
/**
* @brief updateLineEditStyle will re-style (color/font) the line edit depending on content and status
* @param expression if expression is given it will be evaluated for the given string, otherwise it takes
* current expression from the model
*/
void updateLineEditStyle( const QString expression = QString() );

bool isExpressionValid( const QString expressionStr );

private:
QComboBox* mCombo;
Expand Down
31 changes: 9 additions & 22 deletions src/gui/qgsfieldmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,6 @@ QModelIndex QgsFieldModel::setExpression( const QString expression )
mExpression = QList<QString>() << expression;
endResetModel();

// fetch feature to be evaluate the expression
if ( !mFeature.isValid() )
{
mLayer->getFeatures().nextFeature( mFeature );
}

return index( mFields.count() , 0 );
}

Expand Down Expand Up @@ -209,16 +203,11 @@ QVariant QgsFieldModel::data( const QModelIndex &index, int role ) const
{
if ( exprIdx >= 0 )
{
if ( !mLayer )
return false;
QgsExpression exp( mExpression[exprIdx] );
if ( mFeature.isValid() )
{
exp.evaluate( &mFeature, mLayer->pendingFields() );
return !exp.hasEvalError();
}
else
{
return QVariant();
}
exp.prepare( mLayer->pendingFields() );
return !exp.hasParserError();
}
return true;
}
Expand All @@ -241,15 +230,13 @@ QVariant QgsFieldModel::data( const QModelIndex &index, int role ) const
if ( exprIdx >= 0 )
{
// if expression, test validity
if ( !mLayer )
return false;
QgsExpression exp( mExpression[exprIdx] );

if ( mFeature.isValid() )
exp.prepare( mLayer->pendingFields() );
if ( exp.hasParserError() )
{
exp.evaluate( &mFeature, mLayer->pendingFields() );
if ( exp.hasEvalError() )
{
return QBrush( QColor( Qt::red ) );
}
return QBrush( QColor( Qt::red ) );
}
}
return QVariant();
Expand Down
4 changes: 0 additions & 4 deletions src/gui/qgsfieldmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@

#include "qgsvectorlayer.h"

class QgsFeature;

/**
* @brief The QgsFieldModel class is a model to display the list of fields of a layer in widgets.
* If allowed, expressions might be added to the end of the model.
Expand Down Expand Up @@ -85,8 +83,6 @@ class GUI_EXPORT QgsFieldModel : public QAbstractItemModel
bool mAllowExpression;

private:
QgsFeature mFeature;

void fetchFeature();

// QAbstractItemModel interface
Expand Down

0 comments on commit 41ccfb9

Please sign in to comment.