QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsfieldcombobox.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsfieldcombobox.cpp
3  --------------------------------------
4  Date : 01.04.2014
5  Copyright : (C) 2014 Denis Rouzaud
6  Email : [email protected]
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 "qgsfieldcombobox.h"
17 #include "qgsfieldproxymodel.h"
18 #include "qgsmaplayer.h"
19 #include "qgsvectorlayer.h"
20 #include "qgsfieldmodel.h"
21 
23  : QComboBox( parent )
24 {
25  mFieldProxyModel = new QgsFieldProxyModel( this );
26  setModel( mFieldProxyModel );
27 
28  connect( this, static_cast < void ( QComboBox::* )( int ) > ( &QComboBox::activated ), this, &QgsFieldComboBox::indexChanged );
29 }
30 
31 void QgsFieldComboBox::setFilters( QgsFieldProxyModel::Filters filters )
32 {
33  mFieldProxyModel->setFilters( filters );
34 }
35 
37 {
38  mFieldProxyModel->sourceFieldModel()->setAllowEmptyFieldName( allowEmpty );
39 }
40 
42 {
43  return mFieldProxyModel->sourceFieldModel()->allowEmptyFieldName();
44 }
45 
47 {
48  QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layer );
49  mFieldProxyModel->sourceFieldModel()->setLayer( vl );
50 }
51 
53 {
54  return mFieldProxyModel->sourceFieldModel()->layer();
55 }
56 
58 {
59  mFieldProxyModel->sourceFieldModel()->setFields( fields );
60 }
61 
63 {
64  return mFieldProxyModel->sourceFieldModel()->fields();
65 }
66 
67 void QgsFieldComboBox::setField( const QString &fieldName )
68 {
69  const QString prevField = currentField();
70  const QModelIndex idx = mFieldProxyModel->sourceFieldModel()->indexFromName( fieldName );
71  if ( idx.isValid() )
72  {
73  const QModelIndex proxyIdx = mFieldProxyModel->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 ( prevField != currentField() )
89  emit fieldChanged( currentField() );
90 }
91 
93 {
94  const int i = currentIndex();
95 
96  const QModelIndex proxyIndex = mFieldProxyModel->index( i, 0 );
97  if ( !proxyIndex.isValid() )
98  {
99  return QString();
100  }
101 
102  QString name = mFieldProxyModel->data( proxyIndex, QgsFieldModel::FieldNameRole ).toString();
103  return name;
104 }
105 
107 {
108  Q_UNUSED( i )
109  const QString name = currentField();
110  emit fieldChanged( name );
111 }
QgsFieldModel::setAllowEmptyFieldName
void setAllowEmptyFieldName(bool allowEmpty)
Sets whether an optional empty field ("not set") option is present in the model.
Definition: qgsfieldmodel.cpp:198
qgsfieldproxymodel.h
QgsFieldComboBox::setAllowEmptyFieldName
void setAllowEmptyFieldName(bool allowEmpty)
Sets whether an optional empty field ("not set") option is shown in the combo box.
Definition: qgsfieldcombobox.cpp:36
QgsFieldModel::layer
QgsVectorLayer layer
Definition: qgsfieldmodel.h:44
QgsFieldComboBox::QgsFieldComboBox
QgsFieldComboBox(QWidget *parent=nullptr)
QgsFieldComboBox creates a combo box to display the fields of a layer.
Definition: qgsfieldcombobox.cpp:22
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:44
QgsFieldComboBox::setFields
void setFields(const QgsFields &fields)
Manually sets the fields to use for the combo box.
Definition: qgsfieldcombobox.cpp:57
QgsFieldModel::setLayer
void setLayer(QgsVectorLayer *layer)
Set the layer from which fields are displayed.
Definition: qgsfieldmodel.cpp:83
QgsFieldModel::setFields
void setFields(const QgsFields &fields)
Manually sets the fields to use for the model.
Definition: qgsfieldmodel.cpp:521
QgsFieldComboBox::layer
QgsVectorLayer * layer() const
Returns the layer currently associated with the combobox.
Definition: qgsfieldcombobox.cpp:52
QgsFieldModel::FieldNameRole
@ FieldNameRole
Return field name if index corresponds to a field.
Definition: qgsfieldmodel.h:51
QgsFieldComboBox::filters
QgsFieldProxyModel::Filters filters
Definition: qgsfieldcombobox.h:41
QgsFieldProxyModel::setFilters
QgsFieldProxyModel * setFilters(QgsFieldProxyModel::Filters filters)
Set flags that affect how fields are filtered in the model.
Definition: qgsfieldproxymodel.cpp:28
qgsfieldcombobox.h
QgsFieldProxyModel
The QgsFieldProxyModel class provides an easy to use model to display the list of fields of a layer.
Definition: qgsfieldproxymodel.h:32
qgsmaplayer.h
QgsFieldModel::allowEmptyFieldName
bool allowEmptyFieldName
Definition: qgsfieldmodel.h:43
QgsFieldComboBox::setLayer
void setLayer(QgsMapLayer *layer)
Sets the layer for which fields are listed in the combobox.
Definition: qgsfieldcombobox.cpp:46
QgsFieldProxyModel::sourceFieldModel
QgsFieldModel * sourceFieldModel()
Returns the QgsFieldModel used in this QSortFilterProxyModel.
Definition: qgsfieldproxymodel.h:62
QgsFieldComboBox::setFilters
void setFilters(QgsFieldProxyModel::Filters filters)
setFilters allows filtering according to the type of field
Definition: qgsfieldcombobox.cpp:31
qgsvectorlayer.h
QgsFieldComboBox::allowEmptyFieldName
bool allowEmptyFieldName
Definition: qgsfieldcombobox.h:42
QgsFieldComboBox::fields
QgsFields fields() const
Returns the fields currently shown in the combobox.
Definition: qgsfieldcombobox.cpp:62
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsMapLayer
Base class for all map layer types. This is the base class for all map layer types (vector,...
Definition: qgsmaplayer.h:72
qgsfieldmodel.h
QgsFieldComboBox::indexChanged
void indexChanged(int i)
Definition: qgsfieldcombobox.cpp:106
QgsFieldModel::fields
QgsFields fields() const
Returns the fields currently shown in the model.
Definition: qgsfieldmodel.cpp:529
QgsFieldModel::indexFromName
QModelIndex indexFromName(const QString &fieldName)
Returns the index corresponding to a given fieldName.
Definition: qgsfieldmodel.cpp:32
QgsFieldComboBox::currentField
QString currentField() const
Returns the currently selected field.
Definition: qgsfieldcombobox.cpp:92
QgsFieldComboBox::fieldChanged
void fieldChanged(const QString &fieldName)
Emitted when the currently selected field changes.
QgsFieldComboBox::setField
void setField(const QString &fieldName)
setField sets the currently selected field
Definition: qgsfieldcombobox.cpp:67