QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 
22  : QComboBox( parent )
23 {
24  mFieldProxyModel = new QgsFieldProxyModel( this );
25  setModel( mFieldProxyModel );
26 
27  connect( this, SIGNAL( activated( int ) ), this, SLOT( indexChanged( int ) ) );
28 }
29 
30 void QgsFieldComboBox::setFilters( const QgsFieldProxyModel::Filters& filters )
31 {
32  mFieldProxyModel->setFilters( filters );
33 }
34 
36 {
37  QgsVectorLayer* vl = dynamic_cast<QgsVectorLayer*>( layer );
38  if ( vl )
39  {
40  setLayer( vl );
41  }
42 }
43 
45 {
46  mFieldProxyModel->sourceFieldModel()->setLayer( layer );
47 }
48 
50 {
51  return mFieldProxyModel->sourceFieldModel()->layer();
52 }
53 
54 void QgsFieldComboBox::setField( const QString& fieldName )
55 {
56  QModelIndex idx = mFieldProxyModel->sourceFieldModel()->indexFromName( fieldName );
57  if ( idx.isValid() )
58  {
59  QModelIndex proxyIdx = mFieldProxyModel->mapFromSource( idx );
60  if ( proxyIdx.isValid() )
61  {
62  setCurrentIndex( proxyIdx.row() );
63  emit fieldChanged( currentField() );
64  return;
65  }
66  }
67  setCurrentIndex( -1 );
68 }
69 
71 {
72  int i = currentIndex();
73 
74  const QModelIndex proxyIndex = mFieldProxyModel->index( i, 0 );
75  if ( !proxyIndex.isValid() )
76  {
77  return "";
78  }
79 
80  QString name = mFieldProxyModel->data( proxyIndex, QgsFieldModel::FieldNameRole ).toString();
81  return name;
82 }
83 
85 {
86  Q_UNUSED( i );
88  emit fieldChanged( name );
89 }
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const
QgsFieldProxyModel * setFilters(const QgsFieldProxyModel::Filters &filters)
Set flags that affect how fields are filtered in the model.
Base class for all map layer types.
Definition: qgsmaplayer.h:49
QgsFieldModel * sourceFieldModel()
Returns the QgsFieldModel used in this QSortFilterProxyModel.
QString currentField() const
return the currently selected field
void indexChanged(int i)
void setLayer(QgsVectorLayer *layer)
set the layer of which the fields are listed
QgsVectorLayer * layer()
returns the currently used layer
Definition: qgsfieldmodel.h:69
const char * name() const
bool isValid() const
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 currently used layer.
void setLayer(QgsVectorLayer *layer)
set the layer of whch fields are displayed
int row() const
void activated(int index)
void fieldChanged(const QString &fieldName)
the signal is emitted when the currently selected field changes
QgsFieldProxyModel::Filters filters() const
currently used filter on list of fields
void setModel(QAbstractItemModel *model)
virtual QModelIndex mapFromSource(const QModelIndex &sourceIndex) const
void setCurrentIndex(int index)
QgsFieldComboBox(QWidget *parent=nullptr)
QgsFieldComboBox creates a combo box to display the fields of a layer.
void setFilters(const QgsFieldProxyModel::Filters &filters)
setFilters allows fitering according to the type of field
QModelIndex indexFromName(const QString &fieldName)
return the index corresponding to a given fieldName
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Represents a vector layer which manages a vector based data sets.
QString toString() const
virtual QVariant data(const QModelIndex &index, int role) const