Skip to content

Commit

Permalink
[processing] enable multiple types selection for vector, feature source
Browse files Browse the repository at this point in the history
and map layer parameters
  • Loading branch information
alexbruy authored and nyalldawson committed Apr 4, 2020
1 parent 51cc520 commit 997ceba
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 15 deletions.
8 changes: 8 additions & 0 deletions python/gui/auto_generated/qgscheckablecombobox.sip.in
Expand Up @@ -69,6 +69,14 @@ no items selected.
Returns currently checked items.

.. seealso:: :py:func:`setCheckedItems`
%End

QVariantList checkedItemsData() const;
%Docstring
Returns userData (stored in the Qt.UserRole) associated with
currently checked items.

.. seealso:: :py:func:`checkedItems`
%End

Qt::CheckState itemCheckState( int index ) const;
Expand Down
46 changes: 37 additions & 9 deletions src/gui/processing/qgsprocessingwidgetwrapperimpl.cpp
Expand Up @@ -57,6 +57,7 @@
#include "qgsprocessingmaplayercombobox.h"
#include "qgsrasterbandcombobox.h"
#include "qgsprocessingoutputdestinationwidget.h"
#include "qgscheckablecombobox.h"
#include <QToolButton>
#include <QLabel>
#include <QHBoxLayout>
Expand Down Expand Up @@ -5306,7 +5307,7 @@ QgsProcessingMapLayerParameterDefinitionWidget::QgsProcessingMapLayerParameterDe
vlayout->setContentsMargins( 0, 0, 0, 0 );

vlayout->addWidget( new QLabel( tr( "Layer type" ) ) );
mLayerTypeComboBox = new QComboBox();
mLayerTypeComboBox = new QgsCheckableComboBox();
mLayerTypeComboBox->addItem( tr( "Any Map Layer" ), QgsProcessing::TypeMapLayer );
mLayerTypeComboBox->addItem( tr( "Vector (Point)" ), QgsProcessing::TypeVectorPoint );
mLayerTypeComboBox->addItem( tr( "Vector (Line)" ), QgsProcessing::TypeVectorLine );
Expand All @@ -5316,7 +5317,12 @@ QgsProcessingMapLayerParameterDefinitionWidget::QgsProcessingMapLayerParameterDe
mLayerTypeComboBox->addItem( tr( "Mesh" ), QgsProcessing::TypeMesh );

if ( const QgsProcessingParameterMapLayer *layerParam = dynamic_cast<const QgsProcessingParameterMapLayer *>( definition ) )
mLayerTypeComboBox->setCurrentIndex( mLayerTypeComboBox->findData( layerParam->dataTypes().at( 0 ) ) );
{
for ( int i : layerParam->dataTypes() )
{
mLayerTypeComboBox->setItemCheckState( mLayerTypeComboBox->findData( i ), Qt::Checked );
}
}

vlayout->addWidget( mLayerTypeComboBox );

Expand All @@ -5325,8 +5331,12 @@ QgsProcessingMapLayerParameterDefinitionWidget::QgsProcessingMapLayerParameterDe

QgsProcessingParameterDefinition *QgsProcessingMapLayerParameterDefinitionWidget::createParameter( const QString &name, const QString &description, QgsProcessingParameterDefinition::Flags flags ) const
{
QList< int > dataTypes;
for ( const QVariant &v : mLayerTypeComboBox->checkedItemsData() )
dataTypes << v.toInt();

auto param = qgis::make_unique< QgsProcessingParameterMapLayer >( name, description );
param->setDataTypes( QList< int >() << mLayerTypeComboBox->currentData().toInt() );
param->setDataTypes( dataTypes );
param->setFlags( flags );
return param.release();
}
Expand Down Expand Up @@ -5499,15 +5509,20 @@ QgsProcessingVectorLayerParameterDefinitionWidget::QgsProcessingVectorLayerParam
vlayout->setContentsMargins( 0, 0, 0, 0 );

vlayout->addWidget( new QLabel( tr( "Geometry type" ) ) );
mGeometryTypeComboBox = new QComboBox();
mGeometryTypeComboBox = new QgsCheckableComboBox();
mGeometryTypeComboBox->addItem( tr( "Geometry Not Required" ), QgsProcessing::TypeVector );
mGeometryTypeComboBox->addItem( tr( "Point" ), QgsProcessing::TypeVectorPoint );
mGeometryTypeComboBox->addItem( tr( "Line" ), QgsProcessing::TypeVectorLine );
mGeometryTypeComboBox->addItem( tr( "Polygon" ), QgsProcessing::TypeVectorPolygon );
mGeometryTypeComboBox->addItem( tr( "Any Geometry Type" ), QgsProcessing::TypeVectorAnyGeometry );

if ( const QgsProcessingParameterVectorLayer *vectorParam = dynamic_cast<const QgsProcessingParameterVectorLayer *>( definition ) )
mGeometryTypeComboBox->setCurrentIndex( mGeometryTypeComboBox->findData( vectorParam->dataTypes().at( 0 ) ) );
{
for ( int i : vectorParam->dataTypes() )
{
mGeometryTypeComboBox->setItemCheckState( mGeometryTypeComboBox->findData( i ), Qt::Checked );
}
}

vlayout->addWidget( mGeometryTypeComboBox );

Expand All @@ -5516,7 +5531,11 @@ QgsProcessingVectorLayerParameterDefinitionWidget::QgsProcessingVectorLayerParam

QgsProcessingParameterDefinition *QgsProcessingVectorLayerParameterDefinitionWidget::createParameter( const QString &name, const QString &description, QgsProcessingParameterDefinition::Flags flags ) const
{
auto param = qgis::make_unique< QgsProcessingParameterVectorLayer >( name, description, QList< int >() << mGeometryTypeComboBox->currentData().toInt() );
QList< int > dataTypes;
for ( const QVariant &v : mGeometryTypeComboBox->checkedItemsData() )
dataTypes << v.toInt();

auto param = qgis::make_unique< QgsProcessingParameterVectorLayer >( name, description, dataTypes );
param->setFlags( flags );
return param.release();
}
Expand Down Expand Up @@ -5588,15 +5607,20 @@ QgsProcessingFeatureSourceParameterDefinitionWidget::QgsProcessingFeatureSourceP
vlayout->setContentsMargins( 0, 0, 0, 0 );

vlayout->addWidget( new QLabel( tr( "Geometry type" ) ) );
mGeometryTypeComboBox = new QComboBox();
mGeometryTypeComboBox = new QgsCheckableComboBox();
mGeometryTypeComboBox->addItem( tr( "Geometry Not Required" ), QgsProcessing::TypeVector );
mGeometryTypeComboBox->addItem( tr( "Point" ), QgsProcessing::TypeVectorPoint );
mGeometryTypeComboBox->addItem( tr( "Line" ), QgsProcessing::TypeVectorLine );
mGeometryTypeComboBox->addItem( tr( "Polygon" ), QgsProcessing::TypeVectorPolygon );
mGeometryTypeComboBox->addItem( tr( "Any Geometry Type" ), QgsProcessing::TypeVectorAnyGeometry );

if ( const QgsProcessingParameterFeatureSource *sourceParam = dynamic_cast<const QgsProcessingParameterFeatureSource *>( definition ) )
mGeometryTypeComboBox->setCurrentIndex( mGeometryTypeComboBox->findData( sourceParam->dataTypes().at( 0 ) ) );
{
for ( int i : sourceParam->dataTypes() )
{
mGeometryTypeComboBox->setItemCheckState( mGeometryTypeComboBox->findData( i ), Qt::Checked );
}
}

vlayout->addWidget( mGeometryTypeComboBox );

Expand All @@ -5605,7 +5629,11 @@ QgsProcessingFeatureSourceParameterDefinitionWidget::QgsProcessingFeatureSourceP

QgsProcessingParameterDefinition *QgsProcessingFeatureSourceParameterDefinitionWidget::createParameter( const QString &name, const QString &description, QgsProcessingParameterDefinition::Flags flags ) const
{
auto param = qgis::make_unique< QgsProcessingParameterFeatureSource >( name, description, QList< int >() << mGeometryTypeComboBox->currentData().toInt() );
QList< int > dataTypes;
for ( const QVariant &v : mGeometryTypeComboBox->checkedItemsData() )
dataTypes << v.toInt();

auto param = qgis::make_unique< QgsProcessingParameterFeatureSource >( name, description, dataTypes );
param->setFlags( flags );
return param.release();
}
Expand Down
7 changes: 4 additions & 3 deletions src/gui/processing/qgsprocessingwidgetwrapperimpl.h
Expand Up @@ -64,6 +64,7 @@ class QgsProcessingMatrixModelerWidget;
class QgsProcessingMapLayerComboBox;
class QgsRasterBandComboBox;
class QgsProcessingLayerOutputDestinationWidget;
class QgsCheckableComboBox;

///@cond PRIVATE

Expand Down Expand Up @@ -1634,7 +1635,7 @@ class GUI_EXPORT QgsProcessingMapLayerParameterDefinitionWidget : public QgsProc

private:

QComboBox *mLayerTypeComboBox = nullptr;
QgsCheckableComboBox *mLayerTypeComboBox = nullptr;
};

class GUI_EXPORT QgsProcessingMapLayerWidgetWrapper : public QgsAbstractProcessingParameterWidgetWrapper, public QgsProcessingParameterWidgetFactoryInterface
Expand Down Expand Up @@ -1718,7 +1719,7 @@ class GUI_EXPORT QgsProcessingVectorLayerParameterDefinitionWidget : public QgsP

private:

QComboBox *mGeometryTypeComboBox = nullptr;
QgsCheckableComboBox *mGeometryTypeComboBox = nullptr;
};

class GUI_EXPORT QgsProcessingVectorLayerWidgetWrapper : public QgsProcessingMapLayerWidgetWrapper
Expand Down Expand Up @@ -1762,7 +1763,7 @@ class GUI_EXPORT QgsProcessingFeatureSourceParameterDefinitionWidget : public Qg

private:

QComboBox *mGeometryTypeComboBox = nullptr;
QgsCheckableComboBox *mGeometryTypeComboBox = nullptr;
};

class GUI_EXPORT QgsProcessingFeatureSourceWidgetWrapper : public QgsProcessingMapLayerWidgetWrapper
Expand Down
18 changes: 18 additions & 0 deletions src/gui/qgscheckablecombobox.cpp
Expand Up @@ -148,6 +148,24 @@ QStringList QgsCheckableComboBox::checkedItems() const
return items;
}

QVariantList QgsCheckableComboBox::checkedItemsData() const
{
QVariantList data;

if ( model() )
{
QModelIndex index = model()->index( 0, modelColumn(), rootModelIndex() );
QModelIndexList indexes = model()->match( index, Qt::CheckStateRole, Qt::Checked, -1, Qt::MatchExactly );
const auto constIndexes = indexes;
for ( const QModelIndex &index : constIndexes )
{
data += index.data( Qt::UserRole ).toString();
}
}

return data;
}

Qt::CheckState QgsCheckableComboBox::itemCheckState( int index ) const
{
return static_cast<Qt::CheckState>( itemData( index, Qt::CheckStateRole ).toInt() );
Expand Down
7 changes: 7 additions & 0 deletions src/gui/qgscheckablecombobox.h
Expand Up @@ -169,6 +169,13 @@ class GUI_EXPORT QgsCheckableComboBox : public QComboBox
*/
QStringList checkedItems() const;

/**
* Returns userData (stored in the Qt::UserRole) associated with
* currently checked items.
* \see checkedItems()
*/
QVariantList checkedItemsData() const;

/**
* Returns the checked state of the item identified by index
* \param index item index
Expand Down
6 changes: 3 additions & 3 deletions tests/src/gui/testprocessinggui.cpp
Expand Up @@ -5635,7 +5635,7 @@ void TestProcessingGui::testMapLayerWrapper()
QCOMPARE( def->description(), QStringLiteral( "test desc" ) );
QVERIFY( def->flags() & QgsProcessingParameterDefinition::FlagOptional );
QVERIFY( def->flags() & QgsProcessingParameterDefinition::FlagAdvanced );
QCOMPARE( static_cast< QgsProcessingParameterMapLayer * >( def.get() )->dataTypes(), QList< int >() << QgsProcessing::TypeRaster );
QCOMPARE( static_cast< QgsProcessingParameterMapLayer * >( def.get() )->dataTypes(), QList< int >() << QgsProcessing::TypeVectorPoint << QgsProcessing::TypeRaster );
}

void TestProcessingGui::testRasterLayerWrapper()
Expand Down Expand Up @@ -5951,7 +5951,7 @@ void TestProcessingGui::testVectorLayerWrapper()
QCOMPARE( def->description(), QStringLiteral( "test desc" ) );
QVERIFY( def->flags() & QgsProcessingParameterDefinition::FlagOptional );
QVERIFY( def->flags() & QgsProcessingParameterDefinition::FlagAdvanced );
QCOMPARE( static_cast< QgsProcessingParameterVectorLayer * >( def.get() )->dataTypes(), QList< int >() << QgsProcessing::TypeVectorLine );
QCOMPARE( static_cast< QgsProcessingParameterVectorLayer * >( def.get() )->dataTypes(), QList< int >() << QgsProcessing::TypeVectorPoint << QgsProcessing::TypeVectorLine );
}

void TestProcessingGui::testFeatureSourceWrapper()
Expand Down Expand Up @@ -6125,7 +6125,7 @@ void TestProcessingGui::testFeatureSourceWrapper()
QCOMPARE( def->description(), QStringLiteral( "test desc" ) );
QVERIFY( def->flags() & QgsProcessingParameterDefinition::FlagOptional );
QVERIFY( def->flags() & QgsProcessingParameterDefinition::FlagAdvanced );
QCOMPARE( static_cast< QgsProcessingParameterFeatureSource * >( def.get() )->dataTypes(), QList< int >() << QgsProcessing::TypeVectorPoint );
QCOMPARE( static_cast< QgsProcessingParameterFeatureSource * >( def.get() )->dataTypes(), QList< int >() << QgsProcessing::TypeVectorPoint << QgsProcessing::TypeVectorLine );
}

void TestProcessingGui::testMeshLayerWrapper()
Expand Down

0 comments on commit 997ceba

Please sign in to comment.