Skip to content
Permalink
Browse files

Merge pull request #34871 from elpaso/pgraster-set-subset-string-app-…

…expose

[feature] PG raster expose set filter to app
  • Loading branch information
elpaso committed Mar 5, 2020
2 parents 3246a92 + 404fbb5 commit 542c183f005bc406d251c2ea029452b5e1cc5ad9
@@ -332,6 +332,28 @@ Set contrast enhancement algorithm



virtual QString subsetString() const;
%Docstring
Returns the string (typically sql) used to define a subset of the layer.

:return: The subset string or null QString if not implemented by the provider

.. versionadded:: 3.12
%End

virtual bool setSubsetString( const QString &subset );
%Docstring
Sets the string (typically sql) used to define a subset of the layer

:param subset: The subset string. This may be the where clause of a sql statement
or other definition string specific to the underlying dataprovider
and data store.

:return: ``True``, when setting the subset string was successful, ``False`` otherwise

.. versionadded:: 3.12
%End


void setDefaultContrastEnhancement();
%Docstring
@@ -398,6 +420,16 @@ Sets the coordinate transform context to ``transformContext``
.. versionadded:: 3.8
%End

signals:

void subsetStringChanged();
%Docstring
Emitted when the layer's subset string has changed.

.. versionadded:: 3.12
%End


protected:
virtual bool readSymbology( const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories );

@@ -415,6 +447,7 @@ Sets the coordinate transform context to ``transformContext``

virtual QString decodedSource( const QString &source, const QString &provider, const QgsReadWriteContext &context ) const;


};


@@ -2623,7 +2623,7 @@ void QgisApp::createActions()
connect( mActionSetLayerCRS, &QAction::triggered, this, &QgisApp::setLayerCrs );
connect( mActionSetProjectCRSFromLayer, &QAction::triggered, this, &QgisApp::setProjectCrsFromLayer );
connect( mActionLayerProperties, &QAction::triggered, this, &QgisApp::layerProperties );
connect( mActionLayerSubsetString, &QAction::triggered, this, &QgisApp::layerSubsetString );
connect( mActionLayerSubsetString, &QAction::triggered, this, qgis::overload<>::of( &QgisApp::layerSubsetString ) );
connect( mActionAddToOverview, &QAction::triggered, this, &QgisApp::isInOverview );
connect( mActionAddAllToOverview, &QAction::triggered, this, &QgisApp::addAllToOverview );
connect( mActionRemoveAllFromOverview, &QAction::triggered, this, &QgisApp::removeAllFromOverview );
@@ -10482,11 +10482,61 @@ void QgisApp::duplicateVectorStyle( QgsVectorLayer *srcLayer, QgsVectorLayer *de
}
}


void QgisApp::layerSubsetString()
{
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( activeLayer() );
layerSubsetString( activeLayer() );
}

void QgisApp::layerSubsetString( QgsMapLayer *mapLayer )
{

QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mapLayer );
if ( !vlayer )
{
// Try PG raster
QgsRasterLayer *rlayer = qobject_cast<QgsRasterLayer *>( mapLayer );
if ( rlayer )
{
QgsRasterDataProvider *provider = rlayer->dataProvider();
if ( provider &&
provider->supportsSubsetString() )
{
// PG raster is the only one for now
if ( provider->name() == QStringLiteral( "postgresraster" ) )
{
// We need a vector for the sql editor
QgsDataSourceUri vectorUri { provider->dataSourceUri() };
vectorUri.setGeometryColumn( QString() );
vectorUri.setSrid( QString() );
QgsVectorLayer vlayer { vectorUri.uri( ), QStringLiteral( "pgrasterlayer" ), QStringLiteral( "postgres" ) };
if ( vlayer.isValid( ) )
{
// launch the query builder
QgsQueryBuilder qb { &vlayer };
QString subsetBefore = vlayer.subsetString();

// Set the sql in the query builder to the same in the prop dialog
// (in case the user has already changed it)
qb.setSql( rlayer->subsetString() );
// Open the query builder and refresh symbology if sql has changed
// Note: repaintRequested is emitted directly from QgsQueryBuilder
// when the sql is set in the layer.
if ( qb.exec() && ( subsetBefore != qb.sql() ) && mLayerTreeView )
{
if ( rlayer->setSubsetString( qb.sql() ) )
{
mLayerTreeView->refreshLayerSymbology( rlayer->id() );
activateDeactivateLayerRelatedActions( rlayer );
}
}
}
}
}
}
return;
}


bool joins = !vlayer->vectorJoins().isEmpty();
if ( vlayer->vectorJoins().size() == 1 )
@@ -957,8 +957,14 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
//! Duplicate map layer(s) in legend
void duplicateLayers( const QList<QgsMapLayer *> &lyrList = QList<QgsMapLayer *>() );

//! change layer subset of current vector layer
void layerSubsetString();
/**
* Changes layer subset of \a mapLayer
* \since QGIS 3.12
*/
void layerSubsetString( QgsMapLayer *mapLayer );

//! change layer subset of the active layer
void layerSubsetString( );

//! Sets scale visibility of selected layers
void setLayerScaleVisibility();
@@ -248,11 +248,18 @@ QMenu *QgsAppLayerTreeViewMenuProvider::createContextMenu()

if ( provider && provider->supportsSubsetString() )
{
QAction *action = menu->addAction( tr( "&Filter…" ), QgisApp::instance(), &QgisApp::layerSubsetString );
QAction *action = menu->addAction( tr( "&Filter…" ), QgisApp::instance(), qgis::overload<>::of( &QgisApp::layerSubsetString ) );
action->setEnabled( !vlayer->isEditable() );
}
}

if ( rlayer &&
rlayer->dataProvider() &&
rlayer->dataProvider()->supportsSubsetString() )
{
menu->addAction( tr( "&Filter…" ), QgisApp::instance(), qgis::overload<>::of( &QgisApp::layerSubsetString ) );
}

// change data source is only supported for vectors and rasters
if ( vlayer || rlayer )
{
@@ -21,6 +21,8 @@
#include "qgslayertreeview.h"
#include "qgsquerybuilder.h"
#include "qgsvectorlayer.h"
#include "qgsrasterlayer.h"
#include "qgisapp.h"


QgsLayerTreeViewFilterIndicatorProvider::QgsLayerTreeViewFilterIndicatorProvider( QgsLayerTreeView *view )
@@ -34,15 +36,8 @@ void QgsLayerTreeViewFilterIndicatorProvider::onIndicatorClicked( const QModelIn
if ( !QgsLayerTree::isLayer( node ) )
return;

QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( QgsLayerTree::toLayer( node )->layer() );
if ( !vlayer || vlayer->isEditable() )
return;
QgisApp::instance()->layerSubsetString( QgsLayerTree::toLayer( node )->layer() );

// launch the query builder
QgsQueryBuilder qb( vlayer );
qb.setSql( vlayer->subsetString() );
if ( qb.exec() )
vlayer->setSubsetString( qb.sql() );
}

QString QgsLayerTreeViewFilterIndicatorProvider::iconName( QgsMapLayer *layer )
@@ -54,34 +49,54 @@ QString QgsLayerTreeViewFilterIndicatorProvider::iconName( QgsMapLayer *layer )
QString QgsLayerTreeViewFilterIndicatorProvider::tooltipText( QgsMapLayer *layer )
{
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
if ( !vlayer )
return QString();
return QStringLiteral( "<b>%1:</b><br>%2" ).arg( tr( "Filter" ), vlayer->subsetString().toHtmlEscaped() );
if ( vlayer )
return QStringLiteral( "<b>%1:</b><br>%2" ).arg( tr( "Filter" ), vlayer->subsetString().toHtmlEscaped() );

// PG raster
QgsRasterLayer *rlayer = qobject_cast<QgsRasterLayer *>( layer );
if ( rlayer && rlayer->dataProvider() && rlayer->dataProvider()->supportsSubsetString() )
return QStringLiteral( "<b>%1:</b><br>%2" ).arg( tr( "Filter" ), rlayer->subsetString().toHtmlEscaped() );

return QString();
}

void QgsLayerTreeViewFilterIndicatorProvider::connectSignals( QgsMapLayer *layer )
{
QgsLayerTreeViewIndicatorProvider::connectSignals( layer );
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
if ( !vlayer )
return;
connect( vlayer, &QgsVectorLayer::subsetStringChanged, this, &QgsLayerTreeViewFilterIndicatorProvider::onLayerChanged );
if ( vlayer )
connect( vlayer, &QgsVectorLayer::subsetStringChanged, this, &QgsLayerTreeViewFilterIndicatorProvider::onLayerChanged );

// PG raster
QgsRasterLayer *rlayer = qobject_cast<QgsRasterLayer *>( layer );
if ( rlayer && rlayer->dataProvider() && rlayer->dataProvider()->supportsSubsetString() )
connect( rlayer, &QgsRasterLayer::subsetStringChanged, this, &QgsLayerTreeViewFilterIndicatorProvider::onLayerChanged );

}

void QgsLayerTreeViewFilterIndicatorProvider::disconnectSignals( QgsMapLayer *layer )
{
QgsLayerTreeViewIndicatorProvider::disconnectSignals( layer );
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
if ( !vlayer )
return;
disconnect( vlayer, &QgsVectorLayer::subsetStringChanged, this, &QgsLayerTreeViewFilterIndicatorProvider::onLayerChanged );
if ( vlayer )
disconnect( vlayer, &QgsVectorLayer::subsetStringChanged, this, &QgsLayerTreeViewFilterIndicatorProvider::onLayerChanged );

QgsRasterLayer *rlayer = qobject_cast<QgsRasterLayer *>( layer );
if ( rlayer && rlayer->dataProvider() && rlayer->dataProvider()->supportsSubsetString() )
disconnect( rlayer, &QgsRasterLayer::subsetStringChanged, this, &QgsLayerTreeViewFilterIndicatorProvider::onLayerChanged );
}

bool QgsLayerTreeViewFilterIndicatorProvider::acceptLayer( QgsMapLayer *layer )
{
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
if ( !vlayer )
return false;
return ! vlayer->subsetString().isEmpty();
if ( vlayer )
return ! vlayer->subsetString().isEmpty();

// PG raster
QgsRasterLayer *rlayer = qobject_cast<QgsRasterLayer *>( layer );
if ( rlayer && rlayer->dataProvider() && rlayer->dataProvider()->supportsSubsetString() )
return ! rlayer->subsetString().isEmpty();

return false;
}

0 comments on commit 542c183

Please sign in to comment.
You can’t perform that action at this time.