Skip to content
Permalink
Browse files

Add QgsPointCloudAttributeComboBox widget

A reusable widget for selecting an attribute from a point cloud layer
  • Loading branch information
nyalldawson committed Nov 9, 2020
1 parent 78dcd2c commit 59601bf17d5b8783d1a559e5f68e41efa8eb1c54
@@ -44,6 +44,13 @@ Constructor for QgsPointCloudAttributeModel, with the specified ``parent`` objec
Sets the ``layer`` associated with the model.

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

QgsPointCloudLayer *layer();
%Docstring
Returns the layer associated with the model.

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

void setAttributes( const QgsPointCloudAttributeCollection &attributes );
@@ -0,0 +1,111 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/qgspointcloudattributecombobox.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/






class QgsPointCloudAttributeComboBox : QComboBox
{
%Docstring
The QgsPointCloudAttributeComboBox is a combo box which displays the list of attributes of a given point cloud layer.
It might be combined with a QgsMapLayerComboBox to automatically update attributes according to a chosen layer.

.. versionadded:: 3.18
%End

%TypeHeaderCode
#include "qgspointcloudattributecombobox.h"
%End
public:

explicit QgsPointCloudAttributeComboBox( QWidget *parent /TransferThis/ = 0 );
%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 setAllowEmptyAttributeName( bool allowEmpty );
%Docstring
Sets whether an optional empty attribute ("not set") option is shown in the combo box.

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

bool allowEmptyAttributeName() const;
%Docstring
Returns ``True`` if the combo box allows the empty field ("not set") choice.

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

QString currentAttribute() const;
%Docstring
Returns the currently selected attribute
%End

QgsPointCloudLayer *layer() const;
%Docstring
Returns the layer currently associated with the combobox.

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

void setAttributes( const QgsPointCloudAttributeCollection &attributes );
%Docstring
Manually sets the ``attributes`` to use for the combo box.

This method should only be used when the combo box ISN'T associated with a :py:func:`~QgsPointCloudAttributeComboBox.layer`
and needs to show the fields from an arbitrary attribute collection instead. Calling
:py:func:`~QgsPointCloudAttributeComboBox.setAttributes` will automatically clear any existing :py:func:`~QgsPointCloudAttributeComboBox.layer`.

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

QgsPointCloudAttributeCollection attributes() const;
%Docstring
Returns the attributes currently shown in the combobox.

This will either be attributes from the associated :py:func:`~QgsPointCloudAttributeComboBox.layer` or the attributes
manually set by a call to :py:func:`~QgsPointCloudAttributeComboBox.setAttributes`.
%End

signals:
void attributeChanged( const QString &name );
%Docstring
Emitted when the currently selected attribute changes.
%End

public slots:

void setLayer( QgsMapLayer *layer );
%Docstring
Sets the layer for which fields are listed in the combobox. If no layer is set
or a non-point cloud layer is set then the combobox will be empty.

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

void setAttribute( const QString &name );
%Docstring
Sets the currently selected attribute by ``name``
%End

protected slots:
void indexChanged( int i );

};

/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/qgspointcloudattributecombobox.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
@@ -156,6 +156,7 @@
%Include auto_generated/qgspasswordlineedit.sip
%Include auto_generated/qgspixmaplabel.sip
%Include auto_generated/qgspluginmanagerinterface.sip
%Include auto_generated/qgspointcloudattributecombobox.sip
%Include auto_generated/qgspresetcolorrampdialog.sip
%Include auto_generated/qgsprevieweffect.sip
%Include auto_generated/qgsprojectionselectiondialog.sip
@@ -29,11 +29,19 @@ QgsPointCloudAttributeModel::QgsPointCloudAttributeModel( QObject *parent )
void QgsPointCloudAttributeModel::setLayer( QgsPointCloudLayer *layer )
{
if ( layer && layer->dataProvider() && layer->dataProvider()->index() )
{
mLayer = layer;
setAttributes( layer->dataProvider()->index()->attributes() );
}
else
setAttributes( QgsPointCloudAttributeCollection() );
}

QgsPointCloudLayer *QgsPointCloudAttributeModel::layer()
{
return mLayer;
}

void QgsPointCloudAttributeModel::setAttributes( const QgsPointCloudAttributeCollection &attributes )
{
beginResetModel();
@@ -21,6 +21,7 @@
#include "qgspointcloudattribute.h"

#include <QAbstractItemModel>
#include <QPointer>

class QgsPointCloudLayer;

@@ -59,6 +60,13 @@ class CORE_EXPORT QgsPointCloudAttributeModel : public QAbstractItemModel
*/
void setLayer( QgsPointCloudLayer *layer );

/**
* Returns the layer associated with the model.
*
* \see setLayer()
*/
QgsPointCloudLayer *layer();

/**
* Sets the \a attributes to include in the model.
*
@@ -113,6 +121,7 @@ class CORE_EXPORT QgsPointCloudAttributeModel : public QAbstractItemModel

QgsPointCloudAttributeCollection mAttributes;
bool mAllowEmpty = false;
QPointer< QgsPointCloudLayer > mLayer;
};


@@ -532,6 +532,7 @@ SET(QGIS_GUI_SRCS
qgspasswordlineedit.cpp
qgspixmaplabel.cpp
qgspluginmanagerinterface.cpp
qgspointcloudattributecombobox.cpp
qgspresetcolorrampdialog.cpp
qgsprevieweffect.cpp
qgsprojectionselectiondialog.cpp
@@ -767,6 +768,7 @@ SET(QGIS_GUI_HDRS
qgspasswordlineedit.h
qgspixmaplabel.h
qgspluginmanagerinterface.h
qgspointcloudattributecombobox.h
qgspresetcolorrampdialog.h
qgsprevieweffect.h
qgsprojectionselectiondialog.h
@@ -0,0 +1,96 @@
/***************************************************************************
qgspointcloudattributecombobox.cpp
--------------------------------------
begin : November 2020
copyright : (C) 2020 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgspointcloudattributecombobox.h"
#include "qgsfieldproxymodel.h"
#include "qgsmaplayer.h"
#include "qgspointcloudlayer.h"

QgsPointCloudAttributeComboBox::QgsPointCloudAttributeComboBox( QWidget *parent )
: QComboBox( parent )
{
mAttributeModel = new QgsPointCloudAttributeModel( this );
setModel( mAttributeModel );

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

void QgsPointCloudAttributeComboBox::setAllowEmptyAttributeName( bool allowEmpty )
{
mAttributeModel->setAllowEmptyAttributeName( allowEmpty );
}

bool QgsPointCloudAttributeComboBox::allowEmptyAttributeName() const
{
return mAttributeModel->allowEmptyAttributeName();
}

void QgsPointCloudAttributeComboBox::setLayer( QgsMapLayer *layer )
{
QgsPointCloudLayer *pcl = qobject_cast<QgsPointCloudLayer *>( layer );
mAttributeModel->setLayer( pcl );
}

QgsPointCloudLayer *QgsPointCloudAttributeComboBox::layer() const
{
return mAttributeModel->layer();
}

void QgsPointCloudAttributeComboBox::setAttributes( const QgsPointCloudAttributeCollection &attributes )
{
mAttributeModel->setAttributes( attributes );
}

QgsPointCloudAttributeCollection QgsPointCloudAttributeComboBox::attributes() const
{
return mAttributeModel->attributes();
}

void QgsPointCloudAttributeComboBox::setAttribute( const QString &name )
{
const QString prevAttribute = currentAttribute();
QModelIndex idx = mAttributeModel->indexFromName( name );
if ( idx.isValid() )
{
setCurrentIndex( idx.row() );
}
else
{
setCurrentIndex( -1 );
}

if ( prevAttribute != currentAttribute() )
emit attributeChanged( currentAttribute() );
}

QString QgsPointCloudAttributeComboBox::currentAttribute() const
{
int i = currentIndex();

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

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

void QgsPointCloudAttributeComboBox::indexChanged( int i )
{
Q_UNUSED( i )
const QString name = currentAttribute();
emit attributeChanged( name );
}

0 comments on commit 59601bf

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