Skip to content
Permalink
Browse files

expression widget: do not provide new expression at each keypress

  • Loading branch information
3nids committed May 6, 2014
1 parent 866a24d commit 41ccfb913264859f0490ee1c60b664bb4fadde9e
Showing with 49 additions and 32 deletions.
  1. +28 −3 src/gui/qgsfieldexpressionwidget.cpp
  2. +12 −3 src/gui/qgsfieldexpressionwidget.h
  3. +9 −22 src/gui/qgsfieldmodel.cpp
  4. +0 −4 src/gui/qgsfieldmodel.h
@@ -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() ) );
}
@@ -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();
@@ -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() )
@@ -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 );
@@ -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();
}
@@ -28,7 +28,6 @@ class QgsMapLayer;
class QgsVectorLayer;
class QgsFieldModel;


class GUI_EXPORT QgsFieldExpressionWidget : public QWidget
{
Q_OBJECT
@@ -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;
@@ -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 );
}

@@ -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;
}
@@ -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();
@@ -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.
@@ -85,8 +83,6 @@ class GUI_EXPORT QgsFieldModel : public QAbstractItemModel
bool mAllowExpression;

private:
QgsFeature mFeature;

void fetchFeature();

// QAbstractItemModel interface

0 comments on commit 41ccfb9

Please sign in to comment.
You can’t perform that action at this time.