Skip to content

Commit

Permalink
Show expression previews
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Mar 22, 2024
1 parent 0dd3e1e commit 0b5890b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/gui/raster/qgsrasterlayertemporalpropertieswidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "qgsmaplayerconfigwidget.h"
#include "qgsdatetimeedit.h"
#include "qgsexpressionbuilderdialog.h"
#include "qgsexpressioncontextutils.h"
#include <QMenu>
#include <QAction>

Expand Down Expand Up @@ -190,6 +191,17 @@ void QgsRasterLayerTemporalPropertiesWidget::calculateRangeByExpression( bool is

QgsExpressionBuilderDialog dlg = QgsExpressionBuilderDialog( nullptr, isUpper ? mFixedRangeUpperExpression : mFixedRangeLowerExpression, this, QStringLiteral( "generic" ), expressionContext );
dlg.setExpectedOutputFormat( !isUpper ? tr( "Temporal range start date / time" ) : tr( "Temporal range end date / time" ) );

QList<QPair<QString, QVariant> > bandChoices;
for ( int band = 1; band <= mLayer->bandCount(); ++band )
{
bandChoices << qMakePair( mLayer->dataProvider()->displayBandName( band ), band );
}
dlg.expressionBuilder()->setCustomPreviewGenerator( tr( "Band" ), bandChoices, [this]( const QVariant & value )-> QgsExpressionContext
{
return createExpressionContextForBand( value.toInt() );
} );

if ( dlg.exec() )
{
if ( isUpper )
Expand All @@ -211,6 +223,19 @@ void QgsRasterLayerTemporalPropertiesWidget::calculateRangeByExpression( bool is
}
}

QgsExpressionContext QgsRasterLayerTemporalPropertiesWidget::createExpressionContextForBand( int band ) const
{
QgsExpressionContext context;
context.appendScopes( QgsExpressionContextUtils::globalProjectLayerScopes( mLayer ) );
QgsExpressionContextScope *bandScope = new QgsExpressionContextScope();
bandScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "band" ), band, true, false, tr( "Band number" ) ) );
bandScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "band_name" ), ( mLayer && mLayer->dataProvider() ) ? mLayer->dataProvider()->displayBandName( band ) : QString(), true, false, tr( "Band name" ) ) );
bandScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "band_description" ), ( mLayer && mLayer->dataProvider() ) ? mLayer->dataProvider()->bandDescription( band ) : QString(), true, false, tr( "Band description" ) ) );
context.appendScope( bandScope );
context.setHighlightedVariables( { QStringLiteral( "band" ), QStringLiteral( "band_name" ), QStringLiteral( "band_description" )} );
return context;
}

///@cond PRIVATE

//
Expand Down
2 changes: 2 additions & 0 deletions src/gui/raster/qgsrasterlayertemporalpropertieswidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

class QgsRasterLayer;
class QgsMapLayerConfigWidget;
class QgsExpressionContext;

#ifndef SIP_RUN
///@cond PRIVATE
Expand Down Expand Up @@ -111,6 +112,7 @@ class GUI_EXPORT QgsRasterLayerTemporalPropertiesWidget : public QWidget, privat
void calculateRangeByExpression( bool isUpper );

private:
QgsExpressionContext createExpressionContextForBand( int band ) const;

/**
* The corresponding map layer with temporal attributes
Expand Down

0 comments on commit 0b5890b

Please sign in to comment.