Skip to content

Commit

Permalink
menu action to only display the used values if formatter can provide …
Browse files Browse the repository at this point in the history
…available values
  • Loading branch information
signedav committed Dec 16, 2019
1 parent 8e0b938 commit 83be397
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 7 deletions.
22 changes: 20 additions & 2 deletions python/gui/auto_generated/qgsexpressionbuilderwidget.sip.in
Expand Up @@ -299,12 +299,30 @@ with the context.

void loadSampleValues();
%Docstring
Load sample values into the sample value area
Load sample values into the sample value area.
Including available values, in case the formatter can
provide them (eg. RelationReference).
%End

void loadAllValues();
%Docstring
Load all unique values from the set layer into the sample area
Load all unique values from the set layer into the sample area.
Including all available values, in case the formatter can
provide them (eg. RelationReference).
%End

void loadSampleUsedValues();
%Docstring
Load used sample values into the sample value area.
Only the used ones. Without available values, even if the
formatter can provide them (eg. RelationReference).
%End

void loadAllUsedValues();
%Docstring
Load all unique values from the set layer into the sample area.
Only the used ones. Without available values, even if the
formatter can provide them (eg. RelationReference).
%End

void autosave();
Expand Down
43 changes: 41 additions & 2 deletions src/gui/qgsexpressionbuilderwidget.cpp
Expand Up @@ -462,7 +462,7 @@ void QgsExpressionBuilderWidget::loadFieldsAndValues( const QMap<QString, QVaria
mFieldValues = fieldValues;
}

void QgsExpressionBuilderWidget::fillFieldValues( const QString &fieldName, int countLimit )
void QgsExpressionBuilderWidget::fillFieldValues( const QString &fieldName, int countLimit, bool forceUsedValues )
{
// TODO We should really return a error the user of the widget that
// the there is no layer set.
Expand All @@ -481,7 +481,7 @@ void QgsExpressionBuilderWidget::fillFieldValues( const QString &fieldName, int
const QgsFieldFormatter *formatter = QgsApplication::fieldFormatterRegistry()->fieldFormatter( setup.type() );

QList<QVariant> values;
if ( cbxValuesInUse->isVisible() && !cbxValuesInUse->isChecked() )
if ( cbxValuesInUse->isVisible() && !cbxValuesInUse->isChecked() && !forceUsedValues )
{
values = formatter->availableValues( setup.config(), countLimit );
}
Expand Down Expand Up @@ -1108,6 +1108,19 @@ void QgsExpressionBuilderWidget::showContextMenu( QPoint pt )
QMenu *menu = new QMenu( this );
menu->addAction( tr( "Load First 10 Unique Values" ), this, SLOT( loadSampleValues() ) );
menu->addAction( tr( "Load All Unique Values" ), this, SLOT( loadAllValues() ) );
const QgsFields fields = mLayer->fields();
int fieldIndex = fields.lookupField( item->text() );
if ( fieldIndex != -1 )
{
const QgsEditorWidgetSetup setup = fields.at( fieldIndex ).editorWidgetSetup();
const QgsFieldFormatter *formatter = QgsApplication::fieldFormatterRegistry()->fieldFormatter( setup.type() );

if ( formatter->flags() & QgsFieldFormatter::CanProvideAvailableValues )
{
menu->addAction( tr( "Load First 10 Unique Used Values" ), this, SLOT( loadSampleUsedValues() ) );
menu->addAction( tr( "Load All Unique Used Values" ), this, SLOT( loadAllUsedValues() ) );
}
}
menu->popup( expressionTree->mapToGlobal( pt ) );
}
}
Expand Down Expand Up @@ -1138,6 +1151,32 @@ void QgsExpressionBuilderWidget::loadAllValues()
fillFieldValues( item->text(), -1 );
}

void QgsExpressionBuilderWidget::loadSampleUsedValues()
{
QModelIndex idx = mProxyModel->mapToSource( expressionTree->currentIndex() );
QgsExpressionItem *item = dynamic_cast<QgsExpressionItem *>( mModel->itemFromIndex( idx ) );
// TODO We should really return a error the user of the widget that
// the there is no layer set.
if ( !mLayer || !item )
return;

mValueGroupBox->show();
fillFieldValues( item->text(), 10, true );
}

void QgsExpressionBuilderWidget::loadAllUsedValues()
{
QModelIndex idx = mProxyModel->mapToSource( expressionTree->currentIndex() );
QgsExpressionItem *item = dynamic_cast<QgsExpressionItem *>( mModel->itemFromIndex( idx ) );
// TODO We should really return a error the user of the widget that
// the there is no layer set.
if ( !mLayer || !item )
return;

mValueGroupBox->show();
fillFieldValues( item->text(), -1, true );
}

void QgsExpressionBuilderWidget::txtPython_textChanged()
{
lblAutoSave->setText( tr( "Saving…" ) );
Expand Down
24 changes: 21 additions & 3 deletions src/gui/qgsexpressionbuilderwidget.h
Expand Up @@ -302,15 +302,33 @@ class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExp
public slots:

/**
* Load sample values into the sample value area
* Load sample values into the sample value area.
* Including available values, in case the formatter can
* provide them (eg. RelationReference).
*/
void loadSampleValues();

/**
* Load all unique values from the set layer into the sample area
* Load all unique values from the set layer into the sample area.
* Including all available values, in case the formatter can
* provide them (eg. RelationReference).
*/
void loadAllValues();

/**
* Load used sample values into the sample value area.
* Only the used ones. Without available values, even if the
* formatter can provide them (eg. RelationReference).
*/
void loadSampleUsedValues();

/**
* Load all unique values from the set layer into the sample area.
* Only the used ones. Without available values, even if the
* formatter can provide them (eg. RelationReference).
*/
void loadAllUsedValues();

/**
* Auto save the current Python function code.
*/
Expand Down Expand Up @@ -377,7 +395,7 @@ class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExp
void clearErrors();
void runPythonCode( const QString &code );
void updateFunctionTree();
void fillFieldValues( const QString &fieldName, int countLimit );
void fillFieldValues( const QString &fieldName, int countLimit, bool forceUsedValues = false );
QString getFunctionHelp( QgsExpressionFunction *function );
QString loadFunctionHelp( QgsExpressionItem *functionName );
QString helpStylesheet() const;
Expand Down

0 comments on commit 83be397

Please sign in to comment.