QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
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
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
18#include "qgsfieldmodel.h"
19#include "qgsfieldproxymodel.h"
20#include "qgsmaplayer.h"
21#include "qgsvectorlayer.h"
22
23#include "moc_qgsfieldcombobox.cpp"
24
26 : QComboBox( parent )
27{
28 mFieldProxyModel = new QgsFieldProxyModel( this );
29 setModel( mFieldProxyModel );
30
31 connect( this, static_cast<void ( QComboBox::* )( int )>( &QComboBox::activated ), this, &QgsFieldComboBox::indexChanged );
32}
33
35{
36 mFieldProxyModel->setFilters( filters );
37}
38
40{
41 mFieldProxyModel->sourceFieldModel()->setAllowEmptyFieldName( allowEmpty );
42}
43
45{
46 return mFieldProxyModel->sourceFieldModel()->allowEmptyFieldName();
47}
48
50{
51 QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layer );
52 mFieldProxyModel->sourceFieldModel()->setLayer( vl );
53}
54
56{
57 return mFieldProxyModel->sourceFieldModel()->layer();
58}
59
61{
62 mFieldProxyModel->sourceFieldModel()->setFields( fields );
63}
64
66{
67 return mFieldProxyModel->sourceFieldModel()->fields();
68}
69
70void QgsFieldComboBox::setField( const QString &fieldName )
71{
72 const QString prevField = currentField();
73 const QModelIndex idx = mFieldProxyModel->sourceFieldModel()->indexFromName( fieldName );
74 if ( idx.isValid() )
75 {
76 const QModelIndex proxyIdx = mFieldProxyModel->mapFromSource( idx );
77 if ( proxyIdx.isValid() )
78 {
79 setCurrentIndex( proxyIdx.row() );
80 }
81 else
82 {
83 setCurrentIndex( -1 );
84 }
85 }
86 else
87 {
88 setCurrentIndex( -1 );
89 }
90
91 if ( prevField != currentField() )
92 emit fieldChanged( currentField() );
93}
94
96{
97 const int i = currentIndex();
98
99 const QModelIndex proxyIndex = mFieldProxyModel->index( i, 0 );
100 if ( !proxyIndex.isValid() )
101 {
102 return QString();
103 }
104
105 QString name = mFieldProxyModel->data( proxyIndex, static_cast<int>( QgsFieldModel::CustomRole::FieldName ) ).toString();
106 return name;
107}
108
110{
111 Q_UNUSED( i )
112 const QString name = currentField();
113 emit fieldChanged( name );
114}
void setFilters(QgsFieldProxyModel::Filters filters)
setFilters allows filtering according to the type of field
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.
void setLayer(QgsMapLayer *layer)
Sets the layer for which fields are listed in the combobox.
QgsFieldProxyModel::Filters filters
QString currentField() const
Returns the currently selected field.
QgsFields fields() const
Returns the fields currently shown in the combobox.
QgsVectorLayer * layer() const
Returns the layer currently associated with the combobox.
void setField(const QString &fieldName)
setField sets the currently selected field
QgsFieldComboBox(QWidget *parent=nullptr)
QgsFieldComboBox creates a combo box to display the fields of a layer.
void setAllowEmptyFieldName(bool allowEmpty)
Sets whether an optional empty field ("not set") option is shown in the combo box.
@ FieldName
Return field name if index corresponds to a field.
A proxy model to filter the list of fields of a layer.
QFlags< Filter > Filters
Container of fields for a vector layer.
Definition qgsfields.h:46
Base class for all map layer types.
Definition qgsmaplayer.h:80
Represents a vector layer which manages a vector based dataset.