Skip to content

Commit

Permalink
add init methods for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed Mar 26, 2020
1 parent 2857896 commit ed7c73f
Show file tree
Hide file tree
Showing 12 changed files with 139 additions and 35 deletions.
3 changes: 3 additions & 0 deletions python/gui/auto_additions/qgsexpressionbuilderwidget.py
@@ -0,0 +1,3 @@
# The following has been generated automatically from src/gui/qgsexpressionbuilderwidget.h
QgsExpressionBuilderWidget.Flag.baseClass = QgsExpressionBuilderWidget
Flag = QgsExpressionBuilderWidget # dirty hack since SIP seems to introduce the flags in module
45 changes: 40 additions & 5 deletions python/gui/auto_generated/qgsexpressionbuilderwidget.sip.in
Expand Up @@ -25,12 +25,44 @@ See QgsExpressionBuilderDialog for example of usage.
%End
public:

enum Flag
{
LoadNothing,
LoadRecent,
LoadUserExpressions,
LoadAll,
};
typedef QFlags<QgsExpressionBuilderWidget::Flag> Flags;



QgsExpressionBuilderWidget( QWidget *parent /TransferThis/ = 0 );
%Docstring
Create a new expression builder widget with an optional parent.
%End
~QgsExpressionBuilderWidget();

void init( const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = QStringLiteral( "generic" ), const Flags &flags = LoadAll );
%Docstring
Initialize without any layer

.. versionadded:: 3.14
%End

void initWithLayer( QgsVectorLayer *layer, const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = QStringLiteral( "generic" ), const Flags &flags = LoadAll );
%Docstring
Initialize with a layer

.. versionadded:: 3.14
%End

void initWithFields( const QgsFields &fields, const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = QStringLiteral( "generic" ), const Flags &flags = LoadAll );
%Docstring
Initialize with given fields without any layer

.. versionadded:: 3.14
%End

void setLayer( QgsVectorLayer *layer );
%Docstring
Sets layer in order to get the fields and values
Expand All @@ -45,20 +77,23 @@ Sets layer in order to get the fields and values
Returns the current layer or a None.
%End

void loadFieldNames( const QgsFields &fields = QgsFields() );
void loadFieldNames();
%Docstring

.. deprecated:: QGIS 3.14
this is now done automatically
%End

void loadFieldsAndValues( const QMap<QString, QStringList> &fieldValues );
void loadFieldNames( const QgsFields &fields );
%Docstring
Loads field names and values from the specified map.

.. note::
.. deprecated:: QGIS 3.14
use epxressionTree()->loadFieldNames() instead
%End

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

.. versionadded:: 2.12

Expand Down
5 changes: 5 additions & 0 deletions python/gui/auto_generated/qgsexpressiontreeview.sip.in
Expand Up @@ -135,6 +135,11 @@ Returns a newly created menu instance
void setLayer( QgsVectorLayer *layer );
%Docstring
Sets layer in order to get the fields and values
%End

void loadFieldNames( const QgsFields &fields );
%Docstring
This allows to load fields without specifying a layer
%End

void setExpressionContext( const QgsExpressionContext &context );
Expand Down
7 changes: 2 additions & 5 deletions src/gui/attributetable/qgsdualview.cpp
Expand Up @@ -919,11 +919,8 @@ void QgsDualView::modifySort()

QgsExpressionBuilderWidget *expressionBuilder = new QgsExpressionBuilderWidget();
QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( mLayer ) );
expressionBuilder->setExpressionContext( context );
expressionBuilder->setLayer( mLayer );
expressionBuilder->loadFieldNames();
expressionBuilder->loadRecent( QStringLiteral( "generic" ) );
expressionBuilder->loadUserExpressions( );

expressionBuilder->initWithLayer( mLayer, context, QStringLiteral( "generic" ) );
expressionBuilder->setExpressionText( sortExpression().isEmpty() ? mLayer->displayExpression() : sortExpression() );

sortingGroupBox->layout()->addWidget( expressionBuilder );
Expand Down
5 changes: 2 additions & 3 deletions src/gui/qgsexpressionbuilderdialog.cpp
Expand Up @@ -31,9 +31,8 @@ QgsExpressionBuilderDialog::QgsExpressionBuilderDialog( QgsVectorLayer *layer, c
builder->setExpressionContext( context );
builder->setLayer( layer );
builder->setExpressionText( startText );
builder->loadFieldNames();
builder->loadRecent( mRecentKey );
builder->loadUserExpressions( );
builder->expressionTree()->loadRecent( mRecentKey );
builder->expressionTree()->loadUserExpressions( );

connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsExpressionBuilderDialog::showHelp );
}
Expand Down
24 changes: 24 additions & 0 deletions src/gui/qgsexpressionbuilderwidget.cpp
Expand Up @@ -229,6 +229,30 @@ QgsExpressionBuilderWidget::~QgsExpressionBuilderWidget()
delete mExpressionTreeMenuProvider;
}

void QgsExpressionBuilderWidget::init( const QgsExpressionContext &context, const QString &recentCollection, const Flags &flags )
{
setExpressionContext( context );

if ( flags.testFlag( LoadRecent ) )
mExpressionTreeView->loadRecent( recentCollection );

if ( flags.testFlag( LoadUserExpressions ) )
mExpressionTreeView->loadUserExpressions();
}

void QgsExpressionBuilderWidget::initWithLayer( QgsVectorLayer *layer, const QgsExpressionContext &context, const QString &recentCollection, const Flags &flags )
{
init( context, recentCollection, flags );
setLayer( layer );
}

void QgsExpressionBuilderWidget::initWithFields( const QgsFields &fields, const QgsExpressionContext &context, const QString &recentCollection, const Flags &flags )
{
init( context, recentCollection, flags );
mExpressionTreeView->loadFieldNames( fields );
}


void QgsExpressionBuilderWidget::setLayer( QgsVectorLayer *layer )
{
mLayer = layer;
Expand Down
40 changes: 38 additions & 2 deletions src/gui/qgsexpressionbuilderwidget.h
Expand Up @@ -45,12 +45,45 @@ class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExp
Q_OBJECT
public:

/**
* Flag to determine what should be loaded
* \since QGIS 3.14
*/
enum Flag
{
LoadNothing = 0, //!< Do not load anything
LoadRecent = 1 << 1, //!< Load recent expressions given the collection key
LoadUserExpressions = 1 << 2, //!< Load user expressions
LoadAll = LoadRecent | LoadUserExpressions, //!< Load everything
};
Q_DECLARE_FLAGS( Flags, Flag )
Q_FLAG( Flag )


/**
* Create a new expression builder widget with an optional parent.
*/
QgsExpressionBuilderWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
~QgsExpressionBuilderWidget() override;

/**
* Initialize without any layer
* \since QGIS 3.14
*/
void init( const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = QStringLiteral( "generic" ), const Flags &flags = LoadAll );

/**
* Initialize with a layer
* \since QGIS 3.14
*/
void initWithLayer( QgsVectorLayer *layer, const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = QStringLiteral( "generic" ), const Flags &flags = LoadAll );

/**
* Initialize with given fields without any layer
* \since QGIS 3.14
*/
void initWithFields( const QgsFields &fields, const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = QStringLiteral( "generic" ), const Flags &flags = LoadAll );

/**
* Sets layer in order to get the fields and values
* \note this needs to be called before calling loadFieldNames().
Expand All @@ -63,11 +96,13 @@ class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExp
QgsVectorLayer *layer() const;

//! \deprecated since QGIS 3.14 this is now done automatically
Q_DECL_DEPRECATED void loadFieldNames( const QgsFields &fields = QgsFields() ) {Q_UNUSED( fields )}
Q_DECL_DEPRECATED void loadFieldNames() {}

//! \deprecated since QGIS 3.14 use epxressionTree()->loadFieldNames() instead
Q_DECL_DEPRECATED void loadFieldNames( const QgsFields &fields ) {mExpressionTreeView->loadFieldNames( fields );}

/**
* Loads field names and values from the specified map.
* \note The field values must be quoted appropriately if they are strings.
* \since QGIS 2.12
* \deprecated since QGIS 3.14 this will not do anything, use setLayer() instead
*/
Expand Down Expand Up @@ -366,6 +401,7 @@ class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExp
};

int FUNCTION_MARKER_ID = 25;

void createErrorMarkers( QList<QgsExpression::ParserError> errors );
void createMarkers( const QgsExpressionNode *node );
void clearFunctionMarkers();
Expand Down
7 changes: 2 additions & 5 deletions src/gui/qgsexpressionselectiondialog.cpp
Expand Up @@ -55,12 +55,9 @@ QgsExpressionSelectionDialog::QgsExpressionSelectionDialog( QgsVectorLayer *laye
mButtonSelect->addAction( mActionSelectIntersect );
mButtonSelect->setDefaultAction( mActionSelect );

mExpressionBuilder->setLayer( layer );
mExpressionBuilder->setExpressionText( startText );

QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( mLayer ) );
mExpressionBuilder->loadRecent( QStringLiteral( "selection" ) );
mExpressionBuilder->setExpressionContext( context );
mExpressionBuilder->initWithLayer( layer, context, QStringLiteral( "selection" ) );
mExpressionBuilder->setExpressionText( startText );

// by default, zoom to features is hidden, shown only if canvas is set
mButtonZoomToFeatures->setVisible( false );
Expand Down
20 changes: 13 additions & 7 deletions src/gui/qgsexpressiontreeview.cpp
Expand Up @@ -413,6 +413,17 @@ void QgsExpressionTreeView::loadLayers()
}
}

void QgsExpressionTreeView::loadFieldNames( const QgsFields &fields )
{
for ( int i = 0; i < fields.count(); ++i )
{
const QgsField field = fields.at( i );
QIcon icon = fields.iconForField( i );
registerItem( QStringLiteral( "Fields and Values" ), field.displayNameWithAlias(),
" \"" + field.name() + "\" ", QString(), QgsExpressionItem::Field, false, i, icon );
}
}

void QgsExpressionTreeView::loadFieldNames()
{
if ( mExpressionGroups.contains( QStringLiteral( "Fields and Values" ) ) )
Expand All @@ -421,18 +432,13 @@ void QgsExpressionTreeView::loadFieldNames()
node->removeRows( 0, node->rowCount() );
}

// this can happend if fields are manually set
if ( !mLayer )
return;

const QgsFields &fields = mLayer->fields();

for ( int i = 0; i < fields.count(); ++i )
{
const QgsField field = fields.at( i );
QIcon icon = fields.iconForField( i );
registerItem( QStringLiteral( "Fields and Values" ), field.displayNameWithAlias(),
" \"" + field.name() + "\" ", QString(), QgsExpressionItem::Field, false, i, icon );
}
loadFieldNames( fields );
}

void QgsExpressionTreeView::loadRelations()
Expand Down
5 changes: 5 additions & 0 deletions src/gui/qgsexpressiontreeview.h
Expand Up @@ -156,6 +156,11 @@ class GUI_EXPORT QgsExpressionTreeView : public QTreeView
*/
void setLayer( QgsVectorLayer *layer );

/**
* This allows to load fields without specifying a layer
*/
void loadFieldNames( const QgsFields &fields );

/**
* Sets the expression context for the tree view. The context is used
* to populate the list of available functions and variables.
Expand Down
11 changes: 4 additions & 7 deletions src/gui/vector/qgsfieldcalculator.cpp
Expand Up @@ -13,6 +13,9 @@
* *
***************************************************************************/

#include <QMessageBox>


#include "qgsfieldcalculator.h"
#include "qgsdistancearea.h"
#include "qgsexpression.h"
Expand All @@ -30,7 +33,6 @@
#include "qgsexpressioncontextutils.h"
#include "qgsvectorlayerjoinbuffer.h"

#include <QMessageBox>

// FTC = FieldTypeCombo
constexpr int FTC_TYPE_ROLE_IDX = 0;
Expand Down Expand Up @@ -66,10 +68,6 @@ QgsFieldCalculator::QgsFieldCalculator( QgsVectorLayer *vl, QWidget *parent )
expContext.lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "row_number" ), 1, true ) );
expContext.setHighlightedVariables( QStringList() << QStringLiteral( "row_number" ) );

builder->setLayer( vl );
builder->loadFieldNames();
builder->setExpressionContext( expContext );

populateFields();
populateOutputFieldTypes();

Expand Down Expand Up @@ -151,8 +149,7 @@ QgsFieldCalculator::QgsFieldCalculator( QgsVectorLayer *vl, QWidget *parent )
mOnlyUpdateSelectedCheckBox->setEnabled( mCanChangeAttributeValue && hasselection );
mOnlyUpdateSelectedCheckBox->setText( tr( "Only update %1 selected features" ).arg( vl->selectedFeatureCount() ) );

builder->loadRecent( QStringLiteral( "fieldcalc" ) );
builder->loadUserExpressions( );
builder->initWithLayer( vl, expContext, QStringLiteral( "fieldcalc" ) );

mInfoIcon->setPixmap( style()->standardPixmap( QStyle::SP_MessageBoxInformation ) );

Expand Down
2 changes: 1 addition & 1 deletion src/providers/arcgisrest/qgsafssourceselect.cpp
Expand Up @@ -179,7 +179,7 @@ void QgsAfsSourceSelect::buildQuery( const QgsOwsConnection &connection, const Q

//add available attributes to expression builder
QgsExpressionBuilderWidget *w = d.expressionBuilder();
w->loadFieldNames( provider.fields() );
w->initWithFields( provider.fields() );

if ( d.exec() == QDialog::Accepted )
{
Expand Down

0 comments on commit ed7c73f

Please sign in to comment.