Skip to content

Commit 85c8c97

Browse files
committed
Allow empty field name ('not set' option) in QgsFieldModel, QgsFieldComboBox
1 parent dd68d81 commit 85c8c97

File tree

9 files changed

+468
-43
lines changed

9 files changed

+468
-43
lines changed

python/gui/qgsfieldcombobox.sip

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ class QgsFieldComboBox : QComboBox
2525
//! currently used filter on list of fields
2626
QgsFieldProxyModel::Filters filters() const;
2727

28+
/**
29+
* Sets whether an optional empty field ("not set") option is shown in the combo box.
30+
* @see allowEmptyFieldName()
31+
* @note added in QGIS 3.0
32+
*/
33+
void setAllowEmptyFieldName( bool allowEmpty );
34+
35+
/**
36+
* Returns true if the combo box allows the empty field ("not set") choice.
37+
* @see setAllowEmptyFieldName()
38+
* @note added in QGIS 3.0
39+
*/
40+
bool allowEmptyFieldName() const;
41+
2842
//! return the currently selected field
2943
QString currentField() const;
3044

python/gui/qgsfieldmodel.sip

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class QgsFieldModel : QAbstractItemModel
1212
%End
1313

1414
public:
15+
16+
//! Roles utilised by the model
1517
enum FieldRoles
1618
{
1719
FieldNameRole, /*!< return field name if index corresponds to a field */
@@ -21,38 +23,85 @@ class QgsFieldModel : QAbstractItemModel
2123
ExpressionValidityRole, /*!< return if expression is valid or not */
2224
FieldTypeRole, /*!< return the field type (if a field, return QVariant if expression) */
2325
FieldOriginRole, /*!< return the field origin (if a field, returns QVariant if expression) */
26+
IsEmptyRole, //!< Return if the index corresponds to the empty value
2427
};
2528

2629
/**
27-
* @brief QgsFieldModel creates a model to display the fields of a given layer
30+
* Constructor for QgsFieldModel - creates a model to display the fields of a given layer.
2831
*/
2932
explicit QgsFieldModel( QObject *parent /TransferThis/ = 0 );
3033

31-
//! return the index corresponding to a given fieldName
34+
/**
35+
* Returns the index corresponding to a given fieldName.
36+
*/
3237
QModelIndex indexFromName( const QString &fieldName );
3338

34-
//! returns the currently used layer
39+
/**
40+
* Sets whether custom expressions are accepted and displayed in the model.
41+
* @see allowExpression()
42+
* @see setExpression()
43+
*/
3544
void setAllowExpression( bool allowExpression );
45+
46+
/**
47+
* Returns true if the model allows custom expressions to be created and displayed.
48+
* @see setAllowExpression()
49+
*/
3650
bool allowExpression();
3751

38-
bool isField( const QString& expression );
52+
/**
53+
* Sets whether an optional empty field ("not set") option is present in the model.
54+
* @see allowEmptyFieldName()
55+
* @note added in QGIS 3.0
56+
*/
57+
void setAllowEmptyFieldName( bool allowEmpty );
58+
59+
/**
60+
* Returns true if the model allows the empty field ("not set") choice.
61+
* @see setAllowEmptyFieldName()
62+
* @note added in QGIS 3.0
63+
*/
64+
bool allowEmptyFieldName() const;
3965

4066
/**
41-
* @brief setExpression sets a single expression to be added after the fields at the end of the model
67+
* Returns true if a string represents a field reference, or false if it is an
68+
* expression consisting of more than direct field reference.
69+
*/
70+
bool isField( const QString& expression ) const;
71+
72+
/**
73+
* Sets a single expression to be added after the fields at the end of the model.
74+
* @see setAllowExpression()
75+
* @see allowExpression()
76+
* @see removeExpression()
4277
*/
4378
void setExpression( const QString &expression );
4479

45-
//! remove expressions from the model
80+
/**
81+
* Removes any custom expression from the model.
82+
* @see setExpression()
83+
* @see allowExpression()
84+
*/
4685
void removeExpression();
4786

48-
//! returns the currently used layer
87+
/**
88+
* Returns the layer associated with the model.
89+
* @see setLayer()
90+
*/
4991
QgsVectorLayer* layer();
5092

5193
public slots:
52-
//! set the layer of whch fields are displayed
94+
/**
95+
* Set the layer from which fields are displayed.
96+
* @see layer()
97+
*/
5398
void setLayer( QgsVectorLayer *layer );
5499

55100
protected slots:
101+
102+
/**
103+
* Called when the model must be updated.
104+
*/
56105
virtual void updateModel();
57106

58107
// QAbstractItemModel interface

src/gui/qgsfieldcombobox.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ void QgsFieldComboBox::setFilters( QgsFieldProxyModel::Filters filters )
3333
mFieldProxyModel->setFilters( filters );
3434
}
3535

36+
void QgsFieldComboBox::setAllowEmptyFieldName( bool allowEmpty )
37+
{
38+
mFieldProxyModel->sourceFieldModel()->setAllowEmptyFieldName( allowEmpty );
39+
}
40+
41+
bool QgsFieldComboBox::allowEmptyFieldName() const
42+
{
43+
return mFieldProxyModel->sourceFieldModel()->allowEmptyFieldName();
44+
}
45+
3646
void QgsFieldComboBox::setLayer( QgsMapLayer *layer )
3747
{
3848
QgsVectorLayer* vl = dynamic_cast<QgsVectorLayer*>( layer );

src/gui/qgsfieldcombobox.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class GUI_EXPORT QgsFieldComboBox : public QComboBox
3535
Q_OBJECT
3636
Q_FLAGS( QgsFieldProxyModel::Filters )
3737
Q_PROPERTY( QgsFieldProxyModel::Filters filters READ filters WRITE setFilters )
38+
Q_PROPERTY( bool allowEmptyFieldName READ allowEmptyFieldName WRITE setAllowEmptyFieldName )
3839

3940
public:
4041

@@ -50,6 +51,20 @@ class GUI_EXPORT QgsFieldComboBox : public QComboBox
5051
//! currently used filter on list of fields
5152
QgsFieldProxyModel::Filters filters() const { return mFieldProxyModel->filters(); }
5253

54+
/**
55+
* Sets whether an optional empty field ("not set") option is shown in the combo box.
56+
* @see allowEmptyFieldName()
57+
* @note added in QGIS 3.0
58+
*/
59+
void setAllowEmptyFieldName( bool allowEmpty );
60+
61+
/**
62+
* Returns true if the combo box allows the empty field ("not set") choice.
63+
* @see setAllowEmptyFieldName()
64+
* @note added in QGIS 3.0
65+
*/
66+
bool allowEmptyFieldName() const;
67+
5368
//! return the currently selected field
5469
QString currentField() const;
5570

0 commit comments

Comments
 (0)