Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add QgsExpressionBuilderWidget::loadFieldsAndValues
  • Loading branch information
manisandro authored and nyalldawson committed Jun 27, 2015
1 parent e4d0d27 commit f4e356b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 13 deletions.
5 changes: 5 additions & 0 deletions python/gui/qgsexpressionbuilderwidget.sip
Expand Up @@ -84,6 +84,11 @@ class QgsExpressionBuilderWidget : QWidget

void loadFieldNames( const QgsFields& fields );

/** Loads field names and values from the specified map.
* @note The field values must be quoted appropriately if they are strings.
*/
void loadFieldsAndValues(const QMap<QString, QStringList>& fieldValues );

/** Sets geometry calculator used in distance/area calculations. */
void setGeomCalculator( const QgsDistanceArea & da );

Expand Down
48 changes: 36 additions & 12 deletions src/gui/qgsexpressionbuilderwidget.cpp
Expand Up @@ -108,13 +108,22 @@ void QgsExpressionBuilderWidget::currentChanged( const QModelIndex &index, const
if ( !item )
return;

if ( item->getItemType() != QgsExpressionItem::Field )
mValueListWidget->clear();
if ( item->getItemType() == QgsExpressionItem::Field && mFieldValues.contains( item->text() ) )
{
mValueListWidget->clear();
const QStringList& values = mFieldValues[item->text()];
mValueListWidget->setUpdatesEnabled( false );
mValueListWidget->blockSignals( true );
foreach ( const QString& value, values )
mValueListWidget->addItem( value );

mValueListWidget->setUpdatesEnabled( true );
mValueListWidget->blockSignals( false );
}


mLoadGroupBox->setVisible( item->getItemType() == QgsExpressionItem::Field && mLayer );
mValueGroupBox->setVisible( item->getItemType() == QgsExpressionItem::Field && mLayer );
mValueGroupBox->setVisible( item->getItemType() == QgsExpressionItem::Field && mLayer || mValueListWidget->count() > 0 );

// Show the help for the current item.
QString help = loadFunctionHelp( item );
Expand Down Expand Up @@ -271,7 +280,18 @@ void QgsExpressionBuilderWidget::loadFieldNames( const QgsFields& fields )
// highlighter->addFields( fieldNames );
}

void QgsExpressionBuilderWidget::fillFieldValues( int fieldIndex, int countLimit )
void QgsExpressionBuilderWidget::loadFieldsAndValues( const QMap<QString, QStringList> &fieldValues )
{
QgsFields fields;
foreach ( const QString& fieldName, fieldValues.keys() )
{
fields.append( QgsField( fieldName ) );
}
loadFieldNames( fields );
mFieldValues = fieldValues;
}

void QgsExpressionBuilderWidget::fillFieldValues( const QString& fieldName, int countLimit )
{
// TODO We should really return a error the user of the widget that
// the there is no layer set.
Expand All @@ -281,23 +301,30 @@ void QgsExpressionBuilderWidget::fillFieldValues( int fieldIndex, int countLimit
// TODO We should thread this so that we don't hold the user up if the layer is massive.
mValueListWidget->clear();

int fieldIndex = mLayer->fieldNameIndex( fieldName );

if ( fieldIndex < 0 )
return;

mValueListWidget->setUpdatesEnabled( false );
mValueListWidget->blockSignals( true );

QList<QVariant> values;
QStringList strValues;
mLayer->uniqueValues( fieldIndex, values, countLimit );
foreach ( QVariant value, values )
{
QString strValue;
if ( value.isNull() )
mValueListWidget->addItem( "NULL" );
strValue = "NULL";
else if ( value.type() == QVariant::Int || value.type() == QVariant::Double || value.type() == QVariant::LongLong )
mValueListWidget->addItem( value.toString() );
strValue = value.toString();
else
mValueListWidget->addItem( "'" + value.toString().replace( "'", "''" ) + "'" );
strValue = "'" + value.toString().replace( "'", "''" ) + "'";
mValueListWidget->addItem( strValue );
strValues.append( strValue );
}
mFieldValues[fieldName] = strValues;

mValueListWidget->setUpdatesEnabled( true );
mValueListWidget->blockSignals( false );
Expand Down Expand Up @@ -590,9 +617,7 @@ void QgsExpressionBuilderWidget::loadSampleValues()
return;

mValueGroupBox->show();
int fieldIndex = mLayer->fieldNameIndex( item->text() );

fillFieldValues( fieldIndex, 10 );
fillFieldValues( item->text(), 10 );
}

void QgsExpressionBuilderWidget::loadAllValues()
Expand All @@ -605,8 +630,7 @@ void QgsExpressionBuilderWidget::loadAllValues()
return;

mValueGroupBox->show();
int fieldIndex = mLayer->fieldNameIndex( item->text() );
fillFieldValues( fieldIndex, -1 );
fillFieldValues( item->text(), -1 );
}

void QgsExpressionBuilderWidget::setExpressionState( bool state )
Expand Down
8 changes: 7 additions & 1 deletion src/gui/qgsexpressionbuilderwidget.h
Expand Up @@ -126,6 +126,11 @@ class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExp

void loadFieldNames( const QgsFields& fields );

/** Loads field names and values from the specified map.
* @note The field values must be quoted appropriately if they are strings.
*/
void loadFieldsAndValues( const QMap<QString, QStringList>& fieldValues );

/** Sets geometry calculator used in distance/area calculations. */
void setGeomCalculator( const QgsDistanceArea & da );

Expand Down Expand Up @@ -203,7 +208,7 @@ class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExp
private:
void runPythonCode( QString code );
void updateFunctionTree();
void fillFieldValues( int fieldIndex, int countLimit );
void fillFieldValues( const QString &fieldName, int countLimit );
QString loadFunctionHelp( QgsExpressionItem* functionName );

/** Formats an expression preview result for display in the widget
Expand All @@ -222,6 +227,7 @@ class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExp
bool mExpressionValid;
QgsDistanceArea mDa;
QString mRecentKey;
QMap<QString, QStringList> mFieldValues;

};

Expand Down

0 comments on commit f4e356b

Please sign in to comment.