Skip to content

Commit

Permalink
Add isValid shortcut method for QgsExpression. Changes to field model…
Browse files Browse the repository at this point in the history
… and

expression widget.

- Extract valid and isExpression checks in expression widget
  • Loading branch information
NathanW2 committed Jun 14, 2014
1 parent a41ac43 commit 31dbea9
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 19 deletions.
8 changes: 8 additions & 0 deletions src/core/qgsexpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1729,6 +1729,14 @@ bool QgsExpression::hasSpecialColumn( const QString& name )
return gmSpecialColumns.contains( name );
}

bool QgsExpression::isValid( const QString &text, const QgsFields &fields, QString errorMessage )
{
QgsExpression exp( text );
exp.prepare( fields );
errorMessage = exp.parserErrorString();
return !exp.hasParserError();
}

QList<QgsExpression::Function*> QgsExpression::specialColumns()
{
QList<Function*> defs;
Expand Down
2 changes: 2 additions & 0 deletions src/core/qgsexpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ class CORE_EXPORT QgsExpression
//! @note added in 2.2
static bool hasSpecialColumn( const QString& name );

static bool isValid( const QString& text, const QgsFields& fields, QString errorMessage );

void setScale( double scale ) { mScale = scale; }

double scale() { return mScale; }
Expand Down
52 changes: 33 additions & 19 deletions src/gui/qgsfieldexpressionwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,37 +64,50 @@ void QgsFieldExpressionWidget::setFilters( QgsFieldProxyModel::Filters filters )
mFieldProxyModel->setFilters( filters );
}

void QgsFieldExpressionWidget::setLeftHandButtonStyle( bool isLeft )
{
QHBoxLayout* layout = dynamic_cast<QHBoxLayout*>( this->layout() );
if ( isLeft )
{
QLayoutItem* item = layout->takeAt( 1 );
layout->insertWidget( 0, item->widget() );
}
else
layout->addWidget( mCombo );
}

void QgsFieldExpressionWidget::setGeomCalculator( const QgsDistanceArea &da )
{
mDa = QSharedPointer<const QgsDistanceArea>( new QgsDistanceArea( da ) );
}

QString QgsFieldExpressionWidget::currentText()
{
return mCombo->currentText();
}

bool QgsFieldExpressionWidget::isValidExpression( QString& expressionError )
{
return QgsExpression::isValid( currentText(), layer()->pendingFields(), expressionError );
}

bool QgsFieldExpressionWidget::isExpression()
{
return !mFieldProxyModel->sourceFieldModel()->isField( currentText() );
}

QString QgsFieldExpressionWidget::currentField( bool *isExpression , bool *isValid )
{
if ( isExpression )
{
*isExpression = false;
}
QString text = currentText();
if ( isValid )
{
*isValid = true;
*isValid = isValidExpression();
}

int i = mCombo->currentIndex();
const QModelIndex proxyIndex = mFieldProxyModel->index( i, 0 );
if ( !proxyIndex.isValid() )
return mCombo->currentText();

if ( isExpression )
{
*isExpression = mFieldProxyModel->data( proxyIndex, QgsFieldModel::IsExpressionRole ).toBool();
}
if ( isValid )
{
*isValid = mFieldProxyModel->data( proxyIndex, QgsFieldModel::ExpressionValidityRole ).toBool();
*isExpression = this->isExpression();
}
QString expression = mFieldProxyModel->data( proxyIndex, QgsFieldModel::ExpressionRole ).toString();
return expression;
return text;
}

QgsVectorLayer *QgsFieldExpressionWidget::layer()
Expand Down Expand Up @@ -145,7 +158,7 @@ void QgsFieldExpressionWidget::setField( const QString &fieldName )

void QgsFieldExpressionWidget::editExpression()
{
QString currentExpression = currentField();
QString currentExpression = currentText();
QgsVectorLayer* vl = layer();

if ( !vl )
Expand All @@ -172,6 +185,7 @@ void QgsFieldExpressionWidget::expressionEdited( const QString expression )

void QgsFieldExpressionWidget::expressionEditingFinished()
{
QgsDebugMsg( "Editing finsihed" );
const QString expression = mCombo->lineEdit()->text();
QModelIndex idx = mFieldProxyModel->sourceFieldModel()->setExpression( expression );
QModelIndex proxyIndex = mFieldProxyModel->mapFromSource( idx );
Expand Down
13 changes: 13 additions & 0 deletions src/gui/qgsfieldexpressionwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class GUI_EXPORT QgsFieldExpressionWidget : public QWidget
//! setFilters allows fitering according to the type of field
void setFilters( QgsFieldProxyModel::Filters filters );

void setLeftHandButtonStyle( bool isLeft );

//! currently used filter on list of fields
QgsFieldProxyModel::Filters filters() { return mFieldProxyModel->filters(); }

Expand All @@ -72,6 +74,17 @@ class GUI_EXPORT QgsFieldExpressionWidget : public QWidget
*/
QString currentField( bool *isExpression = 0, bool *isValid = 0 );

/**
* Return true if the current expression is valid
*/
bool isValidExpression( QString& expressionError = QString() );

bool isExpression();
/**
* Retun the current text that is set in the expression area
*/
QString currentText();

//! Returns the currently used layer
QgsVectorLayer* layer();

Expand Down
6 changes: 6 additions & 0 deletions src/gui/qgsfieldmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ QModelIndex QgsFieldModel::indexFromName( const QString &fieldName )
return QModelIndex();
}

bool QgsFieldModel::isField( const QString& expression )
{
int index = mFields.indexFromName( expression );
return index >= 0;
}

void QgsFieldModel::setLayer( QgsVectorLayer *layer )
{
if ( mLayer )
Expand Down
2 changes: 2 additions & 0 deletions src/gui/qgsfieldmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class GUI_EXPORT QgsFieldModel : public QAbstractItemModel
void setAllowExpression( bool allowExpression );
bool allowExpression() { return mAllowExpression; }

bool isField( const QString& expression );

/**
* @brief setExpression sets a single expression to be added after the fields at the end of the model
* @return the model index of the newly added expression
Expand Down

0 comments on commit 31dbea9

Please sign in to comment.