QGIS API Documentation
4.3.0-Master (16eddb2670e)
Toggle main menu visibility
Loading...
Searching...
No Matches
src
gui
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 : denis.rouzaud@gmail.com
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
25
QgsFieldComboBox::QgsFieldComboBox
( QWidget *parent )
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
34
void
QgsFieldComboBox::setFilters
(
QgsFieldProxyModel::Filters
filters
)
35
{
36
mFieldProxyModel->setFilters(
filters
);
37
}
38
39
void
QgsFieldComboBox::setAllowEmptyFieldName
(
bool
allowEmpty )
40
{
41
mFieldProxyModel->sourceFieldModel()->setAllowEmptyFieldName( allowEmpty );
42
}
43
44
bool
QgsFieldComboBox::allowEmptyFieldName
()
const
45
{
46
return
mFieldProxyModel->sourceFieldModel()->allowEmptyFieldName();
47
}
48
49
void
QgsFieldComboBox::setLayer
(
QgsMapLayer
*
layer
)
50
{
51
QgsVectorLayer
*vl = qobject_cast<QgsVectorLayer *>(
layer
);
52
mFieldProxyModel->sourceFieldModel()->setLayer( vl );
53
}
54
55
QgsVectorLayer
*
QgsFieldComboBox::layer
()
const
56
{
57
return
mFieldProxyModel->sourceFieldModel()->layer();
58
}
59
60
void
QgsFieldComboBox::setFields
(
const
QgsFields
&
fields
)
61
{
62
mFieldProxyModel->sourceFieldModel()->setFields(
fields
);
63
}
64
65
QgsFields
QgsFieldComboBox::fields
()
const
66
{
67
return
mFieldProxyModel->sourceFieldModel()->fields();
68
}
69
70
void
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
95
QString
QgsFieldComboBox::currentField
()
const
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
109
void
QgsFieldComboBox::indexChanged
(
int
i )
110
{
111
Q_UNUSED( i )
112
const
QString name =
currentField
();
113
emit
fieldChanged
( name );
114
}
QgsFieldComboBox::setFilters
void setFilters(QgsFieldProxyModel::Filters filters)
setFilters allows filtering according to the type of field
Definition
qgsfieldcombobox.cpp:34
QgsFieldComboBox::allowEmptyFieldName
bool allowEmptyFieldName
Definition
qgsfieldcombobox.h:41
QgsFieldComboBox::fieldChanged
void fieldChanged(const QString &fieldName)
Emitted when the currently selected field changes.
QgsFieldComboBox::setFields
void setFields(const QgsFields &fields)
Manually sets the fields to use for the combo box.
Definition
qgsfieldcombobox.cpp:60
QgsFieldComboBox::setLayer
void setLayer(QgsMapLayer *layer)
Sets the layer for which fields are listed in the combobox.
Definition
qgsfieldcombobox.cpp:49
QgsFieldComboBox::filters
QgsFieldProxyModel::Filters filters
Definition
qgsfieldcombobox.h:40
QgsFieldComboBox::currentField
QString currentField() const
Returns the currently selected field.
Definition
qgsfieldcombobox.cpp:95
QgsFieldComboBox::fields
QgsFields fields() const
Returns the fields currently shown in the combobox.
Definition
qgsfieldcombobox.cpp:65
QgsFieldComboBox::layer
QgsVectorLayer * layer() const
Returns the layer currently associated with the combobox.
Definition
qgsfieldcombobox.cpp:55
QgsFieldComboBox::setField
void setField(const QString &fieldName)
setField sets the currently selected field
Definition
qgsfieldcombobox.cpp:70
QgsFieldComboBox::QgsFieldComboBox
QgsFieldComboBox(QWidget *parent=nullptr)
QgsFieldComboBox creates a combo box to display the fields of a layer.
Definition
qgsfieldcombobox.cpp:25
QgsFieldComboBox::indexChanged
void indexChanged(int i)
Definition
qgsfieldcombobox.cpp:109
QgsFieldComboBox::setAllowEmptyFieldName
void setAllowEmptyFieldName(bool allowEmpty)
Sets whether an optional empty field ("not set") option is shown in the combo box.
Definition
qgsfieldcombobox.cpp:39
QgsFieldModel::CustomRole::FieldName
@ FieldName
Return field name if index corresponds to a field.
Definition
qgsfieldmodel.h:56
QgsFieldProxyModel
A proxy model to filter the list of fields of a layer.
Definition
qgsfieldproxymodel.h:31
QgsFieldProxyModel::Filters
QFlags< Filter > Filters
Definition
qgsfieldproxymodel.h:52
QgsFields
Container of fields for a vector layer.
Definition
qgsfields.h:45
QgsMapLayer
Base class for all map layer types.
Definition
qgsmaplayer.h:83
QgsVectorLayer
Represents a vector layer which manages a vector based dataset.
Definition
qgsvectorlayer.h:414
qgsfieldcombobox.h
qgsfieldmodel.h
qgsfieldproxymodel.h
qgsmaplayer.h
qgsvectorlayer.h
Generated on
for QGIS API Documentation by
1.17.0