QGIS API Documentation  3.12.1-BucureČ™ti (121cc00ff0)
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  QModelIndex idx = mFieldProxyModel->sourceFieldModel()->indexFromName( fieldName );
71  if ( idx.isValid() )
72  {
73  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  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  QString name = currentField();
110  emit fieldChanged( name );
111 }
Base class for all map layer types.
Definition: qgsmaplayer.h:79
QgsFieldModel * sourceFieldModel()
Returns the QgsFieldModel used in this QSortFilterProxyModel.
QString currentField() const
Returns the currently selected field.
void indexChanged(int i)
bool allowEmptyFieldName
Definition: qgsfieldmodel.h:42
Container of fields for a vector layer.
Definition: qgsfields.h:42
QgsFieldProxyModel * setFilters(QgsFieldProxyModel::Filters filters)
Set flags that affect how fields are filtered in the model.
QgsFields fields() const
Returns the fields currently shown in the combobox.
void setFilters(QgsFieldProxyModel::Filters filters)
setFilters allows filtering according to the type of field
Return field name if index corresponds to a field.
Definition: qgsfieldmodel.h:50
void setFields(const QgsFields &fields)
Manually sets the fields to use for the model.
void setAllowEmptyFieldName(bool allowEmpty)
Sets whether an optional empty field ("not set") option is present in the model.
The QgsFieldProxyModel class provides an easy to use model to display the list of fields of a layer...
void setField(const QString &fieldName)
setField sets the currently selected field
QgsVectorLayer * layer() const
Returns the layer currently associated with the combobox.
void setLayer(QgsVectorLayer *layer)
Set the layer from which fields are displayed.
QgsFields fields() const
Returns the fields currently shown in the model.
void fieldChanged(const QString &fieldName)
Emitted when the currently selected field changes.
void setFields(const QgsFields &fields)
Manually sets the fields to use for the combo box.
QgsFieldProxyModel::Filters filters() const
currently used filter on list of fields
QgsVectorLayer layer
Definition: qgsfieldmodel.h:43
void setLayer(QgsMapLayer *layer)
Sets the layer for which fields are listed in the combobox.
void setAllowEmptyFieldName(bool allowEmpty)
Sets whether an optional empty field ("not set") option is shown in the combo box.
QgsFieldComboBox(QWidget *parent=nullptr)
QgsFieldComboBox creates a combo box to display the fields of a layer.
QModelIndex indexFromName(const QString &fieldName)
Returns the index corresponding to a given fieldName.
Represents a vector layer which manages a vector based data sets.
bool allowEmptyFieldName() const
Returns true if the combo box allows the empty field ("not set") choice.