1 /***************************************************************************
2    qgspointcloudattributecombobox.cpp
3     --------------------------------------
4     begin                : November 2020
5     copyright            : (C) 2020 by Nyall Dawson
6     email                : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 *                                                                         *
9 *   This program is free software; you can redistribute it and/or modify  *
10 *   it under the terms of the GNU General Public License as published by  *
11 *   the Free Software Foundation; either version 2 of the License, or     *
12 *   (at your option) any later version.                                   *
13 *                                                                         *
14 ***************************************************************************/
15 
16 #include "qgspointcloudattributecombobox.h"
17 #include "qgsfieldproxymodel.h"
18 #include "qgsmaplayer.h"
19 #include "qgspointcloudlayer.h"
20 
QgsPointCloudAttributeComboBox(QWidget * parent)21 QgsPointCloudAttributeComboBox::QgsPointCloudAttributeComboBox( QWidget *parent )
22   : QComboBox( parent )
23 {
24   mAttributeModel = new QgsPointCloudAttributeModel( this );
25   mProxyModel = new QgsPointCloudAttributeProxyModel( mAttributeModel, this );
26   setModel( mProxyModel );
27 
28   connect( this, static_cast < void ( QComboBox::* )( int ) > ( &QComboBox::activated ), this, &QgsPointCloudAttributeComboBox::indexChanged );
29 }
30 
setFilters(QgsPointCloudAttributeProxyModel::Filters filters)31 void QgsPointCloudAttributeComboBox::setFilters( QgsPointCloudAttributeProxyModel::Filters filters )
32 {
33   mProxyModel->setFilters( filters );
34 }
35 
setAllowEmptyAttributeName(bool allowEmpty)36 void QgsPointCloudAttributeComboBox::setAllowEmptyAttributeName( bool allowEmpty )
37 {
38   mAttributeModel->setAllowEmptyAttributeName( allowEmpty );
39 }
40 
allowEmptyAttributeName() const41 bool QgsPointCloudAttributeComboBox::allowEmptyAttributeName() const
42 {
43   return mAttributeModel->allowEmptyAttributeName();
44 }
45 
setLayer(QgsMapLayer * layer)46 void QgsPointCloudAttributeComboBox::setLayer( QgsMapLayer *layer )
47 {
48   QgsPointCloudLayer *pcl = qobject_cast<QgsPointCloudLayer *>( layer );
49   mAttributeModel->setLayer( pcl );
50 }
51 
layer() const52 QgsPointCloudLayer *QgsPointCloudAttributeComboBox::layer() const
53 {
54   return mAttributeModel->layer();
55 }
56 
setAttributes(const QgsPointCloudAttributeCollection & attributes)57 void QgsPointCloudAttributeComboBox::setAttributes( const QgsPointCloudAttributeCollection &attributes )
58 {
59   mAttributeModel->setAttributes( attributes );
60 }
61 
attributes() const62 QgsPointCloudAttributeCollection QgsPointCloudAttributeComboBox::attributes() const
63 {
64   return mAttributeModel->attributes();
65 }
66 
setAttribute(const QString & name)67 void QgsPointCloudAttributeComboBox::setAttribute( const QString &name )
68 {
69   const QString prevAttribute = currentAttribute();
70   const QModelIndex idx = mAttributeModel->indexFromName( name );
71   if ( idx.isValid() )
72   {
73     const QModelIndex proxyIdx = mProxyModel->mapFromSource( idx );
74     if ( proxyIdx.isValid() )
75     {
76       setCurrentIndex( proxyIdx.row() );
77     }
78     else
79     {
80       setCurrentIndex( -1 );
81     }
82   }
83   else
84   {
85     setCurrentIndex( -1 );
86   }
87 
88   if ( prevAttribute != currentAttribute() )
89     emit attributeChanged( currentAttribute() );
90 }
91 
currentAttribute() const92 QString QgsPointCloudAttributeComboBox::currentAttribute() const
93 {
94   const int i = currentIndex();
95 
96   const QModelIndex proxyIndex = mProxyModel->index( i, 0 );
97   if ( !proxyIndex.isValid() )
98   {
99     return QString();
100   }
101 
102   return mProxyModel->data( proxyIndex, QgsPointCloudAttributeModel::AttributeNameRole ).toString();
103 }
104 
indexChanged(int i)105 void QgsPointCloudAttributeComboBox::indexChanged( int i )
106 {
107   Q_UNUSED( i )
108   const QString name = currentAttribute();
109   emit attributeChanged( name );
110 }
111