QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 
57 void QgsFieldComboBox::setField( const QString &fieldName )
58 {
59  QModelIndex idx = mFieldProxyModel->sourceFieldModel()->indexFromName( fieldName );
60  if ( idx.isValid() )
61  {
62  QModelIndex proxyIdx = mFieldProxyModel->mapFromSource( idx );
63  if ( proxyIdx.isValid() )
64  {
65  setCurrentIndex( proxyIdx.row() );
66  emit fieldChanged( currentField() );
67  return;
68  }
69  }
70  setCurrentIndex( -1 );
71 }
72 
74 {
75  int i = currentIndex();
76 
77  const QModelIndex proxyIndex = mFieldProxyModel->index( i, 0 );
78  if ( !proxyIndex.isValid() )
79  {
80  return QString();
81  }
82 
83  QString name = mFieldProxyModel->data( proxyIndex, QgsFieldModel::FieldNameRole ).toString();
84  return name;
85 }
86 
88 {
89  Q_UNUSED( i )
90  QString name = currentField();
91  emit fieldChanged( name );
92 }
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
QgsFieldProxyModel * setFilters(QgsFieldProxyModel::Filters filters)
Set flags that affect how fields are filtered in the model.
void setFilters(QgsFieldProxyModel::Filters filters)
setFilters allows fitering according to the type of field
Return field name if index corresponds to a field.
Definition: qgsfieldmodel.h:50
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.
void fieldChanged(const QString &fieldName)
Emitted when the currently selected field changes.
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.