QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgspointcloudattributemodel.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgspointcloudattributemodel.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 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
19 #include "qgspointcloudlayer.h"
20 #include "qgspointcloudindex.h"
21 #include "qgsapplication.h"
22 
24  : QAbstractItemModel( parent )
25 {
26 
27 }
28 
30 {
31  if ( layer )
32  {
33  mLayer = layer;
35  }
36  else
38 }
39 
41 {
42  return mLayer;
43 }
44 
46 {
47  beginResetModel();
48  mAttributes = attributes;
49  endResetModel();
50 }
51 
53 {
54  if ( allowEmpty == mAllowEmpty )
55  return;
56 
57  if ( allowEmpty )
58  {
59  beginInsertRows( QModelIndex(), 0, 0 );
60  mAllowEmpty = true;
61  endInsertRows();
62  }
63  else
64  {
65  beginRemoveRows( QModelIndex(), 0, 0 );
66  mAllowEmpty = false;
67  endRemoveRows();
68  }
69 }
70 
71 QModelIndex QgsPointCloudAttributeModel::indexFromName( const QString &name )
72 {
73  if ( !name.isEmpty() )
74  {
75  const int idx = mAttributes.indexOf( name );
76  if ( idx >= 0 )
77  {
78  if ( mAllowEmpty )
79  return index( 1 + idx, 0 );
80  else
81  return index( idx, 0 );
82  }
83  }
84 
85  if ( mAllowEmpty && name.isEmpty() )
86  return index( 0, 0 );
87 
88  return QModelIndex();
89 }
90 
91 QModelIndex QgsPointCloudAttributeModel::index( int row, int column, const QModelIndex &parent ) const
92 {
93  if ( hasIndex( row, column, parent ) )
94  {
95  return createIndex( row, column, row );
96  }
97 
98  return QModelIndex();
99 }
100 
101 QModelIndex QgsPointCloudAttributeModel::parent( const QModelIndex &child ) const
102 {
103  Q_UNUSED( child )
104  return QModelIndex();
105 }
106 
107 int QgsPointCloudAttributeModel::rowCount( const QModelIndex &parent ) const
108 {
109  if ( parent.isValid() )
110  {
111  return 0;
112  }
113 
114  return ( mAllowEmpty ? 1 : 0 ) + mAttributes.count();
115 }
116 
117 int QgsPointCloudAttributeModel::columnCount( const QModelIndex &parent ) const
118 {
119  Q_UNUSED( parent )
120  return 1;
121 }
122 
123 QVariant QgsPointCloudAttributeModel::data( const QModelIndex &index, int role ) const
124 {
125  if ( !index.isValid() )
126  return QVariant();
127 
128  const bool isEmpty = mAllowEmpty && index.row() == 0;
129  const int fieldOffset = mAllowEmpty ? 1 : 0;
130 
131  if ( !isEmpty && ( index.row() - fieldOffset >= mAttributes.count() ) )
132  return QVariant();
133 
134  switch ( role )
135  {
136  case AttributeNameRole:
137  {
138  if ( isEmpty )
139  {
140  return QVariant();
141  }
142  return mAttributes.at( index.row() - fieldOffset ).name();
143  }
144 
145  case AttributeIndexRole:
146  {
147  if ( isEmpty )
148  {
149  return QVariant();
150  }
151  return index.row() - fieldOffset;
152  }
153 
154  case AttributeSizeRole:
155  {
156  if ( isEmpty )
157  {
158  return QVariant();
159  }
160  return static_cast< int >( mAttributes.at( index.row() - fieldOffset ).size() );
161  }
162 
163  case AttributeTypeRole:
164  {
165  if ( isEmpty )
166  {
167  return QVariant();
168  }
169  return static_cast< int >( mAttributes.at( index.row() - fieldOffset ).type() );
170  }
171 
172  case IsEmptyRole:
173  {
174  return isEmpty;
175  }
176 
177  case IsNumericRole:
178  {
179  if ( isEmpty )
180  {
181  return QVariant();
182  }
183  return QgsPointCloudAttribute::isNumeric( mAttributes.at( index.row() - fieldOffset ).type() );
184  }
185 
186  case Qt::DisplayRole:
187  case Qt::EditRole:
188  case Qt::ToolTipRole:
189  {
190  if ( isEmpty )
191  {
192  return QVariant();
193  }
194  else if ( role == Qt::ToolTipRole )
195  {
196  return attributeToolTip( mAttributes.at( index.row() - fieldOffset ) );
197  }
198  else
199  return mAttributes.at( index.row() - fieldOffset ).name();
200  }
201 
202  case Qt::DecorationRole:
203  {
204  if ( !isEmpty )
205  {
206  return iconForAttributeType( mAttributes.at( index.row() - fieldOffset ).type() );
207  }
208  return QIcon();
209  }
210 
211  default:
212  return QVariant();
213  }
214 }
215 
217 {
218  QString toolTip = QStringLiteral( "<b>%1</b>" ).arg( attribute.name() );
219 
220  toolTip += QStringLiteral( "<br><font style='font-family:monospace; white-space: nowrap;'>%3</font>" ).arg( attribute.displayType() );
221 
222  return toolTip;
223 }
224 
226 {
227  switch ( type )
228  {
235  {
236  return QgsApplication::getThemeIcon( "/mIconFieldInteger.svg" );
237  }
240  {
241  return QgsApplication::getThemeIcon( "/mIconFieldFloat.svg" );
242  }
245  {
246  return QgsApplication::getThemeIcon( "/mIconFieldText.svg" );
247  }
248 
249  }
250  return QIcon();
251 }
252 
253 //
254 // QgsPointCloudAttributeProxyModel
255 //
256 
258  : QSortFilterProxyModel( parent )
259  , mModel( source )
260 {
261  setSourceModel( mModel );
262 }
263 
264 QgsPointCloudAttributeProxyModel *QgsPointCloudAttributeProxyModel::setFilters( QgsPointCloudAttributeProxyModel::Filters filters )
265 {
266  mFilters = filters;
267  invalidateFilter();
268  return this;
269 }
270 
271 bool QgsPointCloudAttributeProxyModel::filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const
272 {
273  const QModelIndex index = sourceModel()->index( source_row, 0, source_parent );
274 
275  if ( mFilters.testFlag( AllTypes ) )
276  return true;
277 
278  const QVariant typeVar = mModel->data( index, QgsPointCloudAttributeModel::AttributeTypeRole );
279  if ( typeVar.isNull() )
280  return true;
281 
282  bool ok;
283  const QgsPointCloudAttribute::DataType type = static_cast< QgsPointCloudAttribute::DataType >( typeVar.toInt( &ok ) );
284  if ( !ok )
285  return true;
286 
287  if ( ( mFilters.testFlag( Char ) && type == QgsPointCloudAttribute::Char ) ||
288  ( mFilters.testFlag( Char ) && type == QgsPointCloudAttribute::UChar ) ||
289  ( mFilters.testFlag( Short ) && type == QgsPointCloudAttribute::Short ) ||
290  ( mFilters.testFlag( Short ) && type == QgsPointCloudAttribute::UShort ) ||
291  ( mFilters.testFlag( Int32 ) && type == QgsPointCloudAttribute::Int32 ) ||
292  ( mFilters.testFlag( Int32 ) && type == QgsPointCloudAttribute::UInt32 ) ||
293  ( mFilters.testFlag( Int32 ) && type == QgsPointCloudAttribute::Int64 ) ||
294  ( mFilters.testFlag( Int32 ) && type == QgsPointCloudAttribute::UInt64 ) ||
295  ( mFilters.testFlag( Float ) && type == QgsPointCloudAttribute::Float ) ||
296  ( mFilters.testFlag( Double ) && type == QgsPointCloudAttribute::Double ) )
297  return true;
298 
299  return false;
300 }
301 
302 bool QgsPointCloudAttributeProxyModel::lessThan( const QModelIndex &left, const QModelIndex &right ) const
303 {
304  // empty field is always first
305  if ( sourceModel()->data( left, QgsPointCloudAttributeModel::IsEmptyRole ).toBool() )
306  return true;
307  else if ( sourceModel()->data( right, QgsPointCloudAttributeModel::IsEmptyRole ).toBool() )
308  return false;
309 
310  // order is attribute order
311  bool lok, rok;
312  const int leftId = sourceModel()->data( left, QgsPointCloudAttributeModel::AttributeIndexRole ).toInt( &lok );
313  const int rightId = sourceModel()->data( right, QgsPointCloudAttributeModel::AttributeIndexRole ).toInt( &rok );
314 
315  if ( !lok )
316  return false;
317  if ( !rok )
318  return true;
319 
320  return leftId < rightId;
321 }
QgsPointCloudAttributeModel::AttributeIndexRole
@ AttributeIndexRole
Attribute index if index corresponds to an attribute.
Definition: qgspointcloudattributemodel.h:46
QgsPointCloudAttribute::displayType
QString displayType() const
Returns the type to use when displaying this field.
Definition: qgspointcloudattribute.cpp:52
QgsPointCloudAttribute::DataType
DataType
Systems of unit measurement.
Definition: qgspointcloudattribute.h:44
QgsPointCloudAttributeModel::AttributeNameRole
@ AttributeNameRole
Attribute name.
Definition: qgspointcloudattributemodel.h:45
QgsPointCloudLayer
Represents a map layer supporting display of point clouds.
Definition: qgspointcloudlayer.h:45
QgsPointCloudAttributeModel::setAttributes
void setAttributes(const QgsPointCloudAttributeCollection &attributes)
Sets the attributes to include in the model.
Definition: qgspointcloudattributemodel.cpp:45
QgsPointCloudAttributeCollection::at
const QgsPointCloudAttribute & at(int index) const
Returns the attribute at the specified index.
Definition: qgspointcloudattribute.h:170
QgsPointCloudAttributeProxyModel::setFilters
QgsPointCloudAttributeProxyModel * setFilters(QgsPointCloudAttributeProxyModel::Filters filters)
Set flags that affect how fields are filtered in the model.
Definition: qgspointcloudattributemodel.cpp:264
QgsPointCloudAttributeModel::parent
QModelIndex parent(const QModelIndex &child) const override
Definition: qgspointcloudattributemodel.cpp:101
QgsPointCloudAttributeModel::attributeToolTip
static QString attributeToolTip(const QgsPointCloudAttribute &attribute)
Returns a HTML formatted tooltip string for a attribute, containing details like the attribute name a...
Definition: qgspointcloudattributemodel.cpp:216
QgsPointCloudAttributeModel::rowCount
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Definition: qgspointcloudattributemodel.cpp:107
QgsPointCloudAttributeProxyModel::Char
@ Char
Character attributes.
Definition: qgspointcloudattributemodel.h:145
QgsPointCloudAttribute::UInt32
@ UInt32
Unsigned int32 4 bytes.
Definition: qgspointcloudattribute.h:51
QgsPointCloudAttributeModel::IsEmptyRole
@ IsEmptyRole
true if the index corresponds to the empty value
Definition: qgspointcloudattributemodel.h:49
QgsPointCloudAttributeModel::layer
QgsPointCloudLayer * layer()
Returns the layer associated with the model.
Definition: qgspointcloudattributemodel.cpp:40
QgsPointCloudAttributeCollection::count
int count() const
Returns the number of attributes present in the collection.
Definition: qgspointcloudattribute.h:165
QgsPointCloudAttribute::name
QString name() const
Returns name of the attribute.
Definition: qgspointcloudattribute.h:64
QgsPointCloudAttributeModel::indexFromName
QModelIndex indexFromName(const QString &name)
Returns the index corresponding to a given attribute name.
Definition: qgspointcloudattributemodel.cpp:71
QgsPointCloudAttribute::Double
@ Double
Double 8 bytes.
Definition: qgspointcloudattribute.h:55
qgsapplication.h
QgsPointCloudAttribute::isNumeric
static bool isNumeric(DataType type)
Returns true if the specified data type is numeric.
Definition: qgspointcloudattribute.cpp:80
QgsPointCloudAttribute::Short
@ Short
Short int 2 bytes.
Definition: qgspointcloudattribute.h:48
QgsPointCloudAttributeProxyModel::Short
@ Short
Short attributes.
Definition: qgspointcloudattributemodel.h:146
QgsPointCloudAttribute::UInt64
@ UInt64
Unsigned int64 8 bytes.
Definition: qgspointcloudattribute.h:53
qgspointcloudlayer.h
QgsPointCloudAttribute::Int32
@ Int32
Int32 4 bytes.
Definition: qgspointcloudattribute.h:50
QgsPointCloudAttribute::UShort
@ UShort
Unsigned short int 2 bytes.
Definition: qgspointcloudattribute.h:49
QgsPointCloudAttributeProxyModel::Double
@ Double
Double attributes.
Definition: qgspointcloudattributemodel.h:149
QgsPointCloudAttributeCollection
Collection of point cloud attributes.
Definition: qgspointcloudattribute.h:141
QgsPointCloudAttributeProxyModel::Int32
@ Int32
Int32 attributes.
Definition: qgspointcloudattributemodel.h:147
QgsPointCloudAttributeProxyModel::Float
@ Float
Float attributes.
Definition: qgspointcloudattributemodel.h:148
QgsPointCloudAttributeProxyModel::QgsPointCloudAttributeProxyModel
QgsPointCloudAttributeProxyModel(QgsPointCloudAttributeModel *source, QObject *parent=nullptr)
Constructor for QgsPointCloudAttributeProxyModel, with the specified source model and parent object.
Definition: qgspointcloudattributemodel.cpp:257
QgsPointCloudAttribute::Char
@ Char
Char 1 byte.
Definition: qgspointcloudattribute.h:46
QgsPointCloudAttribute
Attribute for point cloud data pair of name and size in bytes.
Definition: qgspointcloudattribute.h:40
QgsPointCloudLayer::attributes
QgsPointCloudAttributeCollection attributes() const
Returns the attributes available from the layer.
Definition: qgspointcloudlayer.cpp:696
QgsPointCloudAttributeModel::columnCount
int columnCount(const QModelIndex &parent) const override
Definition: qgspointcloudattributemodel.cpp:117
QgsPointCloudAttributeModel::attributes
QgsPointCloudAttributeCollection attributes() const
Returns the attributes associated with the model.
Definition: qgspointcloudattributemodel.h:85
QgsPointCloudAttributeModel::AttributeSizeRole
@ AttributeSizeRole
Attribute size.
Definition: qgspointcloudattributemodel.h:47
QgsPointCloudAttributeCollection::indexOf
int indexOf(const QString &name) const
Returns the index of the attribute with the specified name.
Definition: qgspointcloudattribute.cpp:181
qgspointcloudindex.h
QgsPointCloudAttributeProxyModel
A proxy model for filtering available attributes from a point cloud attribute model.
Definition: qgspointcloudattributemodel.h:137
QgsPointCloudAttribute::Int64
@ Int64
Int64 8 bytes.
Definition: qgspointcloudattribute.h:52
QgsPointCloudAttribute::size
int size() const
Returns size of the attribute in bytes.
Definition: qgspointcloudattribute.h:67
QgsPointCloudAttributeModel
A model for display of available attributes from a point cloud.
Definition: qgspointcloudattributemodel.h:36
QgsPointCloudAttributeModel::AttributeTypeRole
@ AttributeTypeRole
Attribute type, see QgsPointCloudAttribute::DataType.
Definition: qgspointcloudattributemodel.h:48
QgsPointCloudAttributeModel::setAllowEmptyAttributeName
void setAllowEmptyAttributeName(bool allowEmpty)
Sets whether an optional empty attribute ("not set") option is present in the model.
Definition: qgspointcloudattributemodel.cpp:52
QgsPointCloudAttributeProxyModel::filters
Filters filters() const
Returns the filters controlling displayed attributes.
Definition: qgspointcloudattributemodel.h:177
QgsPointCloudAttributeModel::QgsPointCloudAttributeModel
QgsPointCloudAttributeModel(QObject *parent=nullptr)
Constructor for QgsPointCloudAttributeModel, with the specified parent object.
Definition: qgspointcloudattributemodel.cpp:23
QgsPointCloudAttributeModel::setLayer
void setLayer(QgsPointCloudLayer *layer)
Sets the layer associated with the model.
Definition: qgspointcloudattributemodel.cpp:29
QgsPointCloudAttributeModel::iconForAttributeType
static QIcon iconForAttributeType(QgsPointCloudAttribute::DataType type)
Returns an icon corresponding to an attribute type.
Definition: qgspointcloudattributemodel.cpp:225
qgspointcloudattributemodel.h
QgsPointCloudAttributeModel::IsNumericRole
@ IsNumericRole
true if the index corresponds to a numeric attributre
Definition: qgspointcloudattributemodel.h:50
QgsApplication::getThemeIcon
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
Definition: qgsapplication.cpp:693
QgsPointCloudAttributeProxyModel::AllTypes
@ AllTypes
All attribute types.
Definition: qgspointcloudattributemodel.h:151
QgsPointCloudAttributeModel::data
QVariant data(const QModelIndex &index, int role) const override
Definition: qgspointcloudattributemodel.cpp:123
QgsPointCloudAttribute::UChar
@ UChar
Unsigned char 1 byte.
Definition: qgspointcloudattribute.h:47
QgsPointCloudAttribute::Float
@ Float
Float 4 bytes.
Definition: qgspointcloudattribute.h:54
QgsPointCloudAttributeProxyModel::filterAcceptsRow
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override
Definition: qgspointcloudattributemodel.cpp:271
QgsPointCloudAttributeModel::index
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Definition: qgspointcloudattributemodel.cpp:91
QgsPointCloudAttribute::type
DataType type() const
Returns the data type.
Definition: qgspointcloudattribute.h:74
QgsPointCloudAttributeProxyModel::lessThan
bool lessThan(const QModelIndex &left, const QModelIndex &right) const override
Definition: qgspointcloudattributemodel.cpp:302