Add QgsExpressionBuilderWidget::loadFieldsAndValues

This commit is contained in:
Sandro Mani 2015-06-08 19:11:00 +02:00 committed by Nyall Dawson
parent e4d0d27cd2
commit f4e356bcef
3 changed files with 48 additions and 13 deletions

View File

@ -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 );

View File

@ -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 );
@ -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.
@ -281,6 +301,8 @@ 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;
@ -288,16 +310,21 @@ void QgsExpressionBuilderWidget::fillFieldValues( int fieldIndex, int countLimit
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 );
@ -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()
@ -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 )

View File

@ -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 );
@ -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
@ -222,6 +227,7 @@ class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExp
bool mExpressionValid;
QgsDistanceArea mDa;
QString mRecentKey;
QMap<QString, QStringList> mFieldValues;
};