Skip to content

Commit f6c98ec

Browse files
authoredSep 16, 2020
Merge pull request #38730 from 3nids/conf-flag-check-cb
Use a checkable combobox for fields configuration flags
2 parents 40259f6 + ab66767 commit f6c98ec

23 files changed

+1365
-1210
lines changed
 

‎python/core/auto_generated/qgsfield.sip.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ Formats string for display
292292
%End
293293

294294

295+
295296
bool convertCompatible( QVariant &v ) const;
296297
%Docstring
297298
Converts the provided variant to a compatible format

‎python/core/auto_generated/qgsvectorlayer.sip.in

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1968,24 +1968,36 @@ Convenience function that returns the attribute alias if defined or the field na
19681968
Returns a map of field name to attribute alias
19691969
%End
19701970

1971-
QSet<QString> excludeAttributesWms() const;
1971+
QSet<QString> excludeAttributesWms() const /Deprecated/;
19721972
%Docstring
19731973
A set of attributes that are not advertised in WMS requests with QGIS server.
1974+
1975+
.. deprecated:: QGIS 3.16
1976+
use fields().configurationFlags() instead
19741977
%End
19751978

1976-
void setExcludeAttributesWms( const QSet<QString> &att );
1979+
void setExcludeAttributesWms( const QSet<QString> &att ) /Deprecated/;
19771980
%Docstring
19781981
A set of attributes that are not advertised in WMS requests with QGIS server.
1982+
1983+
.. deprecated:: QGIS 3.16
1984+
use setFieldConfigurationFlag instead
19791985
%End
19801986

1981-
QSet<QString> excludeAttributesWfs() const;
1987+
QSet<QString> excludeAttributesWfs() const /Deprecated/;
19821988
%Docstring
19831989
A set of attributes that are not advertised in WFS requests with QGIS server.
1990+
1991+
.. deprecated:: QGIS 3.16
1992+
use fields().configurationFlags() instead
19841993
%End
19851994

1986-
void setExcludeAttributesWfs( const QSet<QString> &att );
1995+
void setExcludeAttributesWfs( const QSet<QString> &att ) /Deprecated/;
19871996
%Docstring
19881997
A set of attributes that are not advertised in WFS requests with QGIS server.
1998+
1999+
.. deprecated:: QGIS 3.16
2000+
use setFieldConfigurationFlag instead
19892001
%End
19902002

19912003
virtual bool deleteAttribute( int attr );
@@ -2294,6 +2306,7 @@ can also be set. Setting an empty expression will clear any existing expression
22942306

22952307

22962308

2309+
22972310
void setEditorWidgetSetup( int index, const QgsEditorWidgetSetup &setup );
22982311
%Docstring
22992312
\copydoc editorWidgetSetup

‎python/gui/auto_generated/qgscheckablecombobox.sip.in

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ no items selected.
6262
:param text: default text
6363

6464
.. seealso:: :py:func:`defaultText`
65+
%End
66+
67+
void addItemWithCheckState( const QString &text, Qt::CheckState state, const QVariant &userData = QVariant() );
68+
%Docstring
69+
Adds an item to the combobox with the given ``text``, check ``state`` (stored in the Qt.CheckStateRole)
70+
and containing the specified ``userData`` (stored in the Qt.UserRole).
71+
The item is appended to the list of existing items.
72+
73+
.. versionadded:: 3.16
6574
%End
6675

6776
QStringList checkedItems() const;
@@ -113,6 +122,7 @@ Toggles the item check state
113122
.. seealso:: :py:func:`setItemCheckState`
114123
%End
115124

125+
116126
virtual void hidePopup();
117127

118128
%Docstring

‎src/core/qgsfield.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,23 @@ QString QgsField::displayString( const QVariant &v ) const
346346
return v.toString();
347347
}
348348

349+
QString QgsField::readableConfigurationFlag( QgsField::ConfigurationFlag flag )
350+
{
351+
switch ( flag )
352+
{
353+
case ConfigurationFlag::None:
354+
return QObject::tr( "None" );
355+
case ConfigurationFlag::Searchable:
356+
return QObject::tr( "Searchable" );
357+
case ConfigurationFlag::ExposeViaWms:
358+
return QStringLiteral( "Expose via WMS" );
359+
case ConfigurationFlag::ExposeViaWfs:
360+
return QStringLiteral( "Expose via WFS" );
361+
case ConfigurationFlag::DefaultFlags:
362+
return QObject::tr( "Default flags" );
363+
}
364+
}
365+
349366
/***************************************************************************
350367
* This class is considered CRITICAL and any change MUST be accompanied with
351368
* full unit tests in testqgsfield.cpp.

‎src/core/qgsfield.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,10 @@ class CORE_EXPORT QgsField
8080
#endif
8181
{
8282
None = 0, //!< No flag is defined
83-
Searchable = 0x1, //!< Defines if the field is searchable (used in the locator search for instance)
84-
DefaultFlags = Searchable, //!< Default set of flags for a field
83+
Searchable = 1 << 1, //!< Defines if the field is searchable (used in the locator search for instance)
84+
ExposeViaWms = 1 << 2, //!< Fields is available if layer is served as WMS from QGIS server
85+
ExposeViaWfs = 1 << 3, //!< Fields is available if layer is served as WFS from QGIS server
86+
DefaultFlags = Searchable | ExposeViaWms | ExposeViaWfs, //!< Default set of flags for a field
8587
};
8688
Q_ENUM( ConfigurationFlag )
8789
Q_DECLARE_FLAGS( ConfigurationFlags, ConfigurationFlag )
@@ -327,6 +329,12 @@ class CORE_EXPORT QgsField
327329
//! Formats string for display
328330
QString displayString( const QVariant &v ) const;
329331

332+
/**
333+
* Returns the reabable and translated value of the configuration flag
334+
* \since QGIS 3.16
335+
*/
336+
static QString readableConfigurationFlag( QgsField::ConfigurationFlag flag ) SIP_SKIP;
337+
330338
#ifndef SIP_RUN
331339

332340
/**

‎src/core/qgsvectorlayer.cpp

Lines changed: 31 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,6 @@ QgsVectorLayer *QgsVectorLayer::clone() const
274274
layer->setMapTipTemplate( mapTipTemplate() );
275275
layer->setReadOnly( isReadOnly() );
276276
layer->selectByIds( selectedFeatureIds() );
277-
layer->setExcludeAttributesWms( excludeAttributesWms() );
278-
layer->setExcludeAttributesWfs( excludeAttributesWfs() );
279277
layer->setAttributeTableConfig( attributeTableConfig() );
280278
layer->setFeatureBlendMode( featureBlendMode() );
281279
layer->setOpacity( opacity() );
@@ -2306,29 +2304,6 @@ bool QgsVectorLayer::readSymbology( const QDomNode &layerNode, QString &errorMes
23062304

23072305
updateFields();
23082306

2309-
//Attributes excluded from WMS and WFS
2310-
mExcludeAttributesWMS.clear();
2311-
QDomNode excludeWMSNode = layerNode.namedItem( QStringLiteral( "excludeAttributesWMS" ) );
2312-
if ( !excludeWMSNode.isNull() )
2313-
{
2314-
QDomNodeList attributeNodeList = excludeWMSNode.toElement().elementsByTagName( QStringLiteral( "attribute" ) );
2315-
for ( int i = 0; i < attributeNodeList.size(); ++i )
2316-
{
2317-
mExcludeAttributesWMS.insert( attributeNodeList.at( i ).toElement().text() );
2318-
}
2319-
}
2320-
2321-
mExcludeAttributesWFS.clear();
2322-
QDomNode excludeWFSNode = layerNode.namedItem( QStringLiteral( "excludeAttributesWFS" ) );
2323-
if ( !excludeWFSNode.isNull() )
2324-
{
2325-
QDomNodeList attributeNodeList = excludeWFSNode.toElement().elementsByTagName( QStringLiteral( "attribute" ) );
2326-
for ( int i = 0; i < attributeNodeList.size(); ++i )
2327-
{
2328-
mExcludeAttributesWFS.insert( attributeNodeList.at( i ).toElement().text() );
2329-
}
2330-
}
2331-
23322307
// Load editor widget configuration
23332308
QDomElement widgetsElem = layerNode.namedItem( QStringLiteral( "fieldConfiguration" ) ).toElement();
23342309
QDomNodeList fieldConfigurationElementList = widgetsElem.elementsByTagName( QStringLiteral( "field" ) );
@@ -2351,6 +2326,28 @@ bool QgsVectorLayer::readSymbology( const QDomNode &layerNode, QString &errorMes
23512326
QgsEditorWidgetSetup setup = QgsEditorWidgetSetup( widgetType, optionsMap );
23522327
mFieldWidgetSetups[fieldName] = setup;
23532328
}
2329+
2330+
// Legacy reading for QGIS 3.14 and older projects
2331+
// Attributes excluded from WMS and WFS
2332+
const QList<QPair<QString, QgsField::ConfigurationFlag>> legacyConfig
2333+
{
2334+
qMakePair( QStringLiteral( "excludeAttributesWMS" ), QgsField::ConfigurationFlag::ExposeViaWms ),
2335+
qMakePair( QStringLiteral( "excludeAttributesWFS" ), QgsField::ConfigurationFlag::ExposeViaWfs )
2336+
};
2337+
for ( const auto &config : legacyConfig )
2338+
{
2339+
QDomNode excludeNode = layerNode.namedItem( config.first );
2340+
if ( !excludeNode.isNull() )
2341+
{
2342+
QDomNodeList attributeNodeList = excludeNode.toElement().elementsByTagName( QStringLiteral( "attribute" ) );
2343+
for ( int i = 0; i < attributeNodeList.size(); ++i )
2344+
{
2345+
QString fieldName = attributeNodeList.at( i ).toElement().text();
2346+
int index = mFields.indexFromName( fieldName );
2347+
setFieldConfigurationFlag( index, config.second, false );
2348+
}
2349+
}
2350+
}
23542351
}
23552352

23562353
if ( categories.testFlag( GeometryOptions ) )
@@ -2660,30 +2657,6 @@ bool QgsVectorLayer::writeSymbology( QDomNode &node, QDomDocument &doc, QString
26602657
}
26612658
node.appendChild( aliasElem );
26622659

2663-
//exclude attributes WMS
2664-
QDomElement excludeWMSElem = doc.createElement( QStringLiteral( "excludeAttributesWMS" ) );
2665-
QSet<QString>::const_iterator attWMSIt = mExcludeAttributesWMS.constBegin();
2666-
for ( ; attWMSIt != mExcludeAttributesWMS.constEnd(); ++attWMSIt )
2667-
{
2668-
QDomElement attrElem = doc.createElement( QStringLiteral( "attribute" ) );
2669-
QDomText attrText = doc.createTextNode( *attWMSIt );
2670-
attrElem.appendChild( attrText );
2671-
excludeWMSElem.appendChild( attrElem );
2672-
}
2673-
node.appendChild( excludeWMSElem );
2674-
2675-
//exclude attributes WFS
2676-
QDomElement excludeWFSElem = doc.createElement( QStringLiteral( "excludeAttributesWFS" ) );
2677-
QSet<QString>::const_iterator attWFSIt = mExcludeAttributesWFS.constBegin();
2678-
for ( ; attWFSIt != mExcludeAttributesWFS.constEnd(); ++attWFSIt )
2679-
{
2680-
QDomElement attrElem = doc.createElement( QStringLiteral( "attribute" ) );
2681-
QDomText attrText = doc.createTextNode( *attWFSIt );
2682-
attrElem.appendChild( attrText );
2683-
excludeWFSElem.appendChild( attrElem );
2684-
}
2685-
node.appendChild( excludeWFSElem );
2686-
26872660
//default expressions
26882661
QDomElement defaultsElem = doc.createElement( QStringLiteral( "defaults" ) );
26892662
for ( const QgsField &field : qgis::as_const( mFields ) )
@@ -5487,12 +5460,19 @@ void QgsVectorLayer::setFieldConfigurationFlags( int index, QgsField::Configurat
54875460
if ( index < 0 || index >= mFields.count() )
54885461
return;
54895462

5490-
mFields.at( index ).setConfigurationFlags( flags );
5491-
54925463
mFieldConfigurationFlags.insert( mFields.at( index ).name(), flags );
54935464
updateFields();
54945465
}
54955466

5467+
void QgsVectorLayer::setFieldConfigurationFlag( int index, QgsField::ConfigurationFlag flag, bool active )
5468+
{
5469+
if ( index < 0 || index >= mFields.count() )
5470+
return;
5471+
QgsField::ConfigurationFlags flags = mFields.at( index ).configurationFlags();
5472+
flags.setFlag( flag, active );
5473+
setFieldConfigurationFlags( index, flags );
5474+
}
5475+
54965476
QgsField::ConfigurationFlags QgsVectorLayer::fieldConfigurationFlags( int index ) const
54975477
{
54985478

‎src/core/qgsvectorlayer.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1839,23 +1839,27 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
18391839

18401840
/**
18411841
* A set of attributes that are not advertised in WMS requests with QGIS server.
1842+
* \deprecated since QGIS 3.16, use fields().configurationFlags() instead
18421843
*/
1843-
QSet<QString> excludeAttributesWms() const { return mExcludeAttributesWMS; }
1844+
Q_DECL_DEPRECATED QSet<QString> excludeAttributesWms() const SIP_DEPRECATED { return mExcludeAttributesWMS; }
18441845

18451846
/**
18461847
* A set of attributes that are not advertised in WMS requests with QGIS server.
1848+
* \deprecated since QGIS 3.16, use setFieldConfigurationFlag instead
18471849
*/
1848-
void setExcludeAttributesWms( const QSet<QString> &att ) { mExcludeAttributesWMS = att; }
1850+
Q_DECL_DEPRECATED void setExcludeAttributesWms( const QSet<QString> &att ) SIP_DEPRECATED { mExcludeAttributesWMS = att; }
18491851

18501852
/**
18511853
* A set of attributes that are not advertised in WFS requests with QGIS server.
1854+
* \deprecated since QGIS 3.16, use fields().configurationFlags() instead
18521855
*/
1853-
QSet<QString> excludeAttributesWfs() const { return mExcludeAttributesWFS; }
1856+
Q_DECL_DEPRECATED QSet<QString> excludeAttributesWfs() const SIP_DEPRECATED { return mExcludeAttributesWFS; }
18541857

18551858
/**
18561859
* A set of attributes that are not advertised in WFS requests with QGIS server.
1860+
* \deprecated since QGIS 3.16, use setFieldConfigurationFlag instead
18571861
*/
1858-
void setExcludeAttributesWfs( const QSet<QString> &att ) { mExcludeAttributesWFS = att; }
1862+
Q_DECL_DEPRECATED void setExcludeAttributesWfs( const QSet<QString> &att ) SIP_DEPRECATED { mExcludeAttributesWFS = att; }
18591863

18601864
/**
18611865
* Deletes an attribute field (but does not commit it).
@@ -2122,6 +2126,12 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
21222126
*/
21232127
void setFieldConfigurationFlags( int index, QgsField::ConfigurationFlags flags ) SIP_SKIP;
21242128

2129+
/**
2130+
* Sets the given configuration \a flag for the field at given \a index to be \a active or not.
2131+
* \since QGIS 3.16
2132+
*/
2133+
void setFieldConfigurationFlag( int index, QgsField::ConfigurationFlag flag, bool active ) SIP_SKIP;
2134+
21252135
/**
21262136
* Returns the configuration flags of the field at given index
21272137
* \see QgsField::ConfigurationFlag

‎src/gui/qgscheckablecombobox.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ void QgsCheckBoxDelegate::paint( QPainter *painter, const QStyleOptionViewItem &
7777
QgsCheckableComboBox::QgsCheckableComboBox( QWidget *parent )
7878
: QComboBox( parent )
7979
, mSeparator( QStringLiteral( ", " ) )
80+
, mModel( new QgsCheckableItemModel( this ) )
8081
{
81-
setModel( new QgsCheckableItemModel( this ) );
82+
setModel( mModel );
8283
setItemDelegate( new QgsCheckBoxDelegate( this ) );
8384

8485
QLineEdit *lineEdit = new QLineEdit( this );
@@ -134,6 +135,12 @@ void QgsCheckableComboBox::setDefaultText( const QString &text )
134135
}
135136
}
136137

138+
void QgsCheckableComboBox::addItemWithCheckState( const QString &text, Qt::CheckState state, const QVariant &userData )
139+
{
140+
QComboBox::addItem( text, userData );
141+
setItemCheckState( count() - 1, state );
142+
}
143+
137144
QStringList QgsCheckableComboBox::checkedItems() const
138145
{
139146
QStringList items;

‎src/gui/qgscheckablecombobox.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,14 @@ class GUI_EXPORT QgsCheckableComboBox : public QComboBox
163163
*/
164164
void setDefaultText( const QString &text );
165165

166+
/**
167+
* Adds an item to the combobox with the given \a text, check \a state (stored in the Qt::CheckStateRole)
168+
* and containing the specified \a userData (stored in the Qt::UserRole).
169+
* The item is appended to the list of existing items.
170+
* \since QGIS 3.16
171+
*/
172+
void addItemWithCheckState( const QString &text, Qt::CheckState state, const QVariant &userData = QVariant() );
173+
166174
/**
167175
* Returns currently checked items.
168176
* \see setCheckedItems()
@@ -201,6 +209,13 @@ class GUI_EXPORT QgsCheckableComboBox : public QComboBox
201209
*/
202210
void toggleItemCheckState( int index );
203211

212+
/**
213+
* Returns the custom item model which handles checking the items
214+
* \see QgsCheckableItemModel
215+
* \since QGIS 3.16
216+
*/
217+
QgsCheckableItemModel *model() const SIP_SKIP {return mModel;}
218+
204219
/**
205220
* Hides the list of items in the combobox if it is currently
206221
* visible and resets the internal state.
@@ -260,6 +275,8 @@ class GUI_EXPORT QgsCheckableComboBox : public QComboBox
260275
QString mSeparator;
261276
QString mDefaultText;
262277

278+
QgsCheckableItemModel *mModel = nullptr;
279+
263280
bool mSkipHide = false;
264281

265282
QMenu *mContextMenu = nullptr;

0 commit comments

Comments
 (0)
Please sign in to comment.