Skip to content
Permalink
Browse files

Add filtering by attribute type to QgsPointCloudAttributeComboBox

  • Loading branch information
nyalldawson committed Nov 12, 2020
1 parent 38e6b6b commit 3798f146733820a5e5e5004f33b2bae60c9b55e7
@@ -29,6 +29,20 @@ It might be combined with a QgsMapLayerComboBox to automatically update attribut
%Docstring
QgsPointCloudAttributeComboBox creates a combo box to display the fields of a layer.
The layer can be either manually given or dynamically set by connecting the signal QgsMapLayerComboBox.layerChanged to the slot setLayer.
%End

void setFilters( QgsPointCloudAttributeProxyModel::Filters filters );
%Docstring
Sets ``filters`` to allow filtering available attributes according to the attribute properties.

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

QgsPointCloudAttributeProxyModel::Filters filters() const;
%Docstring
Returns the current filters used for filtering available attributes.

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

void setAllowEmptyAttributeName( bool allowEmpty );
@@ -22,11 +22,17 @@ QgsPointCloudAttributeComboBox::QgsPointCloudAttributeComboBox( QWidget *parent
: QComboBox( parent )
{
mAttributeModel = new QgsPointCloudAttributeModel( this );
setModel( mAttributeModel );
mProxyModel = new QgsPointCloudAttributeProxyModel( mAttributeModel, this );
setModel( mProxyModel );

connect( this, static_cast < void ( QComboBox::* )( int ) > ( &QComboBox::activated ), this, &QgsPointCloudAttributeComboBox::indexChanged );
}

void QgsPointCloudAttributeComboBox::setFilters( QgsPointCloudAttributeProxyModel::Filters filters )
{
mProxyModel->setFilters( filters );
}

void QgsPointCloudAttributeComboBox::setAllowEmptyAttributeName( bool allowEmpty )
{
mAttributeModel->setAllowEmptyAttributeName( allowEmpty );
@@ -64,7 +70,15 @@ void QgsPointCloudAttributeComboBox::setAttribute( const QString &name )
QModelIndex idx = mAttributeModel->indexFromName( name );
if ( idx.isValid() )
{
setCurrentIndex( idx.row() );
QModelIndex proxyIdx = mProxyModel->mapFromSource( idx );
if ( proxyIdx.isValid() )
{
setCurrentIndex( proxyIdx.row() );
}
else
{
setCurrentIndex( -1 );
}
}
else
{
@@ -79,13 +93,13 @@ QString QgsPointCloudAttributeComboBox::currentAttribute() const
{
int i = currentIndex();

const QModelIndex modelIndex = mAttributeModel->index( i, 0 );
if ( !modelIndex.isValid() )
const QModelIndex proxyIndex = mProxyModel->index( i, 0 );
if ( !proxyIndex.isValid() )
{
return QString();
}

return mAttributeModel->data( modelIndex, QgsPointCloudAttributeModel::AttributeNameRole ).toString();
return mProxyModel->data( proxyIndex, QgsPointCloudAttributeModel::AttributeNameRole ).toString();
}

void QgsPointCloudAttributeComboBox::indexChanged( int i )
@@ -35,6 +35,7 @@ class QgsPointCloudLayer;
class GUI_EXPORT QgsPointCloudAttributeComboBox : public QComboBox
{
Q_OBJECT
Q_PROPERTY( QgsPointCloudAttributeProxyModel::Filters filters READ filters WRITE setFilters )
Q_PROPERTY( bool allowEmptyAttributeName READ allowEmptyAttributeName WRITE setAllowEmptyAttributeName )

public:
@@ -45,6 +46,18 @@ class GUI_EXPORT QgsPointCloudAttributeComboBox : public QComboBox
*/
explicit QgsPointCloudAttributeComboBox( QWidget *parent SIP_TRANSFERTHIS = nullptr );

/**
* Sets \a filters to allow filtering available attributes according to the attribute properties.
* \see filters()
*/
void setFilters( QgsPointCloudAttributeProxyModel::Filters filters );

/**
* Returns the current filters used for filtering available attributes.
* \see setFilters()
*/
QgsPointCloudAttributeProxyModel::Filters filters() const { return mProxyModel->filters(); }

/**
* Sets whether an optional empty attribute ("not set") option is shown in the combo box.
* \see allowEmptyAttributeName()
@@ -106,6 +119,7 @@ class GUI_EXPORT QgsPointCloudAttributeComboBox : public QComboBox

private:
QgsPointCloudAttributeModel *mAttributeModel = nullptr;
QgsPointCloudAttributeProxyModel *mProxyModel = nullptr;
};

#endif // QGSPOINTCLOUDATTRIBUTECOMBOBOX_H
@@ -14,10 +14,9 @@

from qgis.core import (
QgsProviderRegistry,
QgsFields,
QgsPointCloudLayer,
QgsPointCloudAttributeCollection,
QgsField,
QgsPointCloudAttributeProxyModel,
QgsPointCloudAttribute
)
from qgis.gui import QgsPointCloudAttributeComboBox
@@ -84,6 +83,21 @@ def testSetLayer(self):
self.assertEqual(cb.layer(), layer)
self.assertEqual([cb.itemText(i) for i in range(cb.count())], ['X', 'Y', 'Z', 'Intensity', 'ReturnNumber', 'NumberOfReturns', 'ScanDirectionFlag', 'EdgeOfFlightLine', 'Classification', 'ScanAngleRank', 'UserData', 'PointSourceId', 'GpsTime', 'Red', 'Green', 'Blue'])

def testFilter(self):
cb = QgsPointCloudAttributeComboBox()
cb.setAttributes(create_attributes())

self.assertEqual([cb.itemText(i) for i in range(cb.count())], ['x', 'y', 'z', 'cat', 'red'])
cb.setFilters(QgsPointCloudAttributeProxyModel.Numeric)
self.assertEqual([cb.itemText(i) for i in range(cb.count())], ['x', 'y', 'z', 'red'])
self.assertEqual(cb.filters(), QgsPointCloudAttributeProxyModel.Numeric)
cb.setFilters(QgsPointCloudAttributeProxyModel.Char)
self.assertEqual([cb.itemText(i) for i in range(cb.count())], ['cat'])
self.assertEqual(cb.filters(), QgsPointCloudAttributeProxyModel.Char)
cb.setFilters(QgsPointCloudAttributeProxyModel.Char | QgsPointCloudAttributeProxyModel.Int32)
self.assertEqual([cb.itemText(i) for i in range(cb.count())], ['cat', 'red'])
self.assertEqual(cb.filters(), QgsPointCloudAttributeProxyModel.Char | QgsPointCloudAttributeProxyModel.Int32)


if __name__ == '__main__':
unittest.main()

0 comments on commit 3798f14

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