Skip to content

Commit 31dbea9

Browse files
committedJun 14, 2014
Add isValid shortcut method for QgsExpression. Changes to field model and
expression widget. - Extract valid and isExpression checks in expression widget
1 parent a41ac43 commit 31dbea9

File tree

6 files changed

+64
-19
lines changed

6 files changed

+64
-19
lines changed
 

‎src/core/qgsexpression.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,6 +1729,14 @@ bool QgsExpression::hasSpecialColumn( const QString& name )
17291729
return gmSpecialColumns.contains( name );
17301730
}
17311731

1732+
bool QgsExpression::isValid( const QString &text, const QgsFields &fields, QString errorMessage )
1733+
{
1734+
QgsExpression exp( text );
1735+
exp.prepare( fields );
1736+
errorMessage = exp.parserErrorString();
1737+
return !exp.hasParserError();
1738+
}
1739+
17321740
QList<QgsExpression::Function*> QgsExpression::specialColumns()
17331741
{
17341742
QList<Function*> defs;

‎src/core/qgsexpression.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ class CORE_EXPORT QgsExpression
152152
//! @note added in 2.2
153153
static bool hasSpecialColumn( const QString& name );
154154

155+
static bool isValid( const QString& text, const QgsFields& fields, QString errorMessage );
156+
155157
void setScale( double scale ) { mScale = scale; }
156158

157159
double scale() { return mScale; }

‎src/gui/qgsfieldexpressionwidget.cpp

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,37 +64,50 @@ void QgsFieldExpressionWidget::setFilters( QgsFieldProxyModel::Filters filters )
6464
mFieldProxyModel->setFilters( filters );
6565
}
6666

67+
void QgsFieldExpressionWidget::setLeftHandButtonStyle( bool isLeft )
68+
{
69+
QHBoxLayout* layout = dynamic_cast<QHBoxLayout*>( this->layout() );
70+
if ( isLeft )
71+
{
72+
QLayoutItem* item = layout->takeAt( 1 );
73+
layout->insertWidget( 0, item->widget() );
74+
}
75+
else
76+
layout->addWidget( mCombo );
77+
}
78+
6779
void QgsFieldExpressionWidget::setGeomCalculator( const QgsDistanceArea &da )
6880
{
6981
mDa = QSharedPointer<const QgsDistanceArea>( new QgsDistanceArea( da ) );
7082
}
7183

84+
QString QgsFieldExpressionWidget::currentText()
85+
{
86+
return mCombo->currentText();
87+
}
88+
89+
bool QgsFieldExpressionWidget::isValidExpression( QString& expressionError )
90+
{
91+
return QgsExpression::isValid( currentText(), layer()->pendingFields(), expressionError );
92+
}
93+
94+
bool QgsFieldExpressionWidget::isExpression()
95+
{
96+
return !mFieldProxyModel->sourceFieldModel()->isField( currentText() );
97+
}
98+
7299
QString QgsFieldExpressionWidget::currentField( bool *isExpression , bool *isValid )
73100
{
74-
if ( isExpression )
75-
{
76-
*isExpression = false;
77-
}
101+
QString text = currentText();
78102
if ( isValid )
79103
{
80-
*isValid = true;
104+
*isValid = isValidExpression();
81105
}
82-
83-
int i = mCombo->currentIndex();
84-
const QModelIndex proxyIndex = mFieldProxyModel->index( i, 0 );
85-
if ( !proxyIndex.isValid() )
86-
return mCombo->currentText();
87-
88106
if ( isExpression )
89107
{
90-
*isExpression = mFieldProxyModel->data( proxyIndex, QgsFieldModel::IsExpressionRole ).toBool();
91-
}
92-
if ( isValid )
93-
{
94-
*isValid = mFieldProxyModel->data( proxyIndex, QgsFieldModel::ExpressionValidityRole ).toBool();
108+
*isExpression = this->isExpression();
95109
}
96-
QString expression = mFieldProxyModel->data( proxyIndex, QgsFieldModel::ExpressionRole ).toString();
97-
return expression;
110+
return text;
98111
}
99112

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

146159
void QgsFieldExpressionWidget::editExpression()
147160
{
148-
QString currentExpression = currentField();
161+
QString currentExpression = currentText();
149162
QgsVectorLayer* vl = layer();
150163

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

173186
void QgsFieldExpressionWidget::expressionEditingFinished()
174187
{
188+
QgsDebugMsg( "Editing finsihed" );
175189
const QString expression = mCombo->lineEdit()->text();
176190
QModelIndex idx = mFieldProxyModel->sourceFieldModel()->setExpression( expression );
177191
QModelIndex proxyIndex = mFieldProxyModel->mapFromSource( idx );

‎src/gui/qgsfieldexpressionwidget.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class GUI_EXPORT QgsFieldExpressionWidget : public QWidget
5959
//! setFilters allows fitering according to the type of field
6060
void setFilters( QgsFieldProxyModel::Filters filters );
6161

62+
void setLeftHandButtonStyle( bool isLeft );
63+
6264
//! currently used filter on list of fields
6365
QgsFieldProxyModel::Filters filters() { return mFieldProxyModel->filters(); }
6466

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

77+
/**
78+
* Return true if the current expression is valid
79+
*/
80+
bool isValidExpression( QString& expressionError = QString() );
81+
82+
bool isExpression();
83+
/**
84+
* Retun the current text that is set in the expression area
85+
*/
86+
QString currentText();
87+
7588
//! Returns the currently used layer
7689
QgsVectorLayer* layer();
7790

‎src/gui/qgsfieldmodel.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ QModelIndex QgsFieldModel::indexFromName( const QString &fieldName )
4949
return QModelIndex();
5050
}
5151

52+
bool QgsFieldModel::isField( const QString& expression )
53+
{
54+
int index = mFields.indexFromName( expression );
55+
return index >= 0;
56+
}
57+
5258
void QgsFieldModel::setLayer( QgsVectorLayer *layer )
5359
{
5460
if ( mLayer )

‎src/gui/qgsfieldmodel.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ class GUI_EXPORT QgsFieldModel : public QAbstractItemModel
5454
void setAllowExpression( bool allowExpression );
5555
bool allowExpression() { return mAllowExpression; }
5656

57+
bool isField( const QString& expression );
58+
5759
/**
5860
* @brief setExpression sets a single expression to be added after the fields at the end of the model
5961
* @return the model index of the newly added expression

0 commit comments

Comments
 (0)
Please sign in to comment.