Skip to content

Commit

Permalink
Merge pull request #38730 from 3nids/conf-flag-check-cb
Browse files Browse the repository at this point in the history
Use a checkable combobox for fields configuration flags
  • Loading branch information
3nids committed Sep 16, 2020
2 parents 40259f6 + ab66767 commit f6c98ec
Show file tree
Hide file tree
Showing 23 changed files with 1,365 additions and 1,210 deletions.
1 change: 1 addition & 0 deletions python/core/auto_generated/qgsfield.sip.in
Expand Up @@ -292,6 +292,7 @@ Formats string for display
%End



bool convertCompatible( QVariant &v ) const;
%Docstring
Converts the provided variant to a compatible format
Expand Down
21 changes: 17 additions & 4 deletions python/core/auto_generated/qgsvectorlayer.sip.in
Expand Up @@ -1968,24 +1968,36 @@ Convenience function that returns the attribute alias if defined or the field na
Returns a map of field name to attribute alias
%End

QSet<QString> excludeAttributesWms() const;
QSet<QString> excludeAttributesWms() const /Deprecated/;
%Docstring
A set of attributes that are not advertised in WMS requests with QGIS server.

.. deprecated:: QGIS 3.16
use fields().configurationFlags() instead
%End

void setExcludeAttributesWms( const QSet<QString> &att );
void setExcludeAttributesWms( const QSet<QString> &att ) /Deprecated/;
%Docstring
A set of attributes that are not advertised in WMS requests with QGIS server.

.. deprecated:: QGIS 3.16
use setFieldConfigurationFlag instead
%End

QSet<QString> excludeAttributesWfs() const;
QSet<QString> excludeAttributesWfs() const /Deprecated/;
%Docstring
A set of attributes that are not advertised in WFS requests with QGIS server.

.. deprecated:: QGIS 3.16
use fields().configurationFlags() instead
%End

void setExcludeAttributesWfs( const QSet<QString> &att );
void setExcludeAttributesWfs( const QSet<QString> &att ) /Deprecated/;
%Docstring
A set of attributes that are not advertised in WFS requests with QGIS server.

.. deprecated:: QGIS 3.16
use setFieldConfigurationFlag instead
%End

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




void setEditorWidgetSetup( int index, const QgsEditorWidgetSetup &setup );
%Docstring
\copydoc editorWidgetSetup
Expand Down
10 changes: 10 additions & 0 deletions python/gui/auto_generated/qgscheckablecombobox.sip.in
Expand Up @@ -62,6 +62,15 @@ no items selected.
:param text: default text

.. seealso:: :py:func:`defaultText`
%End

void addItemWithCheckState( const QString &text, Qt::CheckState state, const QVariant &userData = QVariant() );
%Docstring
Adds an item to the combobox with the given ``text``, check ``state`` (stored in the Qt.CheckStateRole)
and containing the specified ``userData`` (stored in the Qt.UserRole).
The item is appended to the list of existing items.

.. versionadded:: 3.16
%End

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


virtual void hidePopup();

%Docstring
Expand Down
17 changes: 17 additions & 0 deletions src/core/qgsfield.cpp
Expand Up @@ -346,6 +346,23 @@ QString QgsField::displayString( const QVariant &v ) const
return v.toString();
}

QString QgsField::readableConfigurationFlag( QgsField::ConfigurationFlag flag )
{
switch ( flag )
{
case ConfigurationFlag::None:
return QObject::tr( "None" );
case ConfigurationFlag::Searchable:
return QObject::tr( "Searchable" );
case ConfigurationFlag::ExposeViaWms:
return QStringLiteral( "Expose via WMS" );
case ConfigurationFlag::ExposeViaWfs:
return QStringLiteral( "Expose via WFS" );
case ConfigurationFlag::DefaultFlags:
return QObject::tr( "Default flags" );
}
}

/***************************************************************************
* This class is considered CRITICAL and any change MUST be accompanied with
* full unit tests in testqgsfield.cpp.
Expand Down
12 changes: 10 additions & 2 deletions src/core/qgsfield.h
Expand Up @@ -80,8 +80,10 @@ class CORE_EXPORT QgsField
#endif
{
None = 0, //!< No flag is defined
Searchable = 0x1, //!< Defines if the field is searchable (used in the locator search for instance)
DefaultFlags = Searchable, //!< Default set of flags for a field
Searchable = 1 << 1, //!< Defines if the field is searchable (used in the locator search for instance)
ExposeViaWms = 1 << 2, //!< Fields is available if layer is served as WMS from QGIS server
ExposeViaWfs = 1 << 3, //!< Fields is available if layer is served as WFS from QGIS server
DefaultFlags = Searchable | ExposeViaWms | ExposeViaWfs, //!< Default set of flags for a field
};
Q_ENUM( ConfigurationFlag )
Q_DECLARE_FLAGS( ConfigurationFlags, ConfigurationFlag )
Expand Down Expand Up @@ -327,6 +329,12 @@ class CORE_EXPORT QgsField
//! Formats string for display
QString displayString( const QVariant &v ) const;

/**
* Returns the reabable and translated value of the configuration flag
* \since QGIS 3.16
*/
static QString readableConfigurationFlag( QgsField::ConfigurationFlag flag ) SIP_SKIP;

#ifndef SIP_RUN

/**
Expand Down
82 changes: 31 additions & 51 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -274,8 +274,6 @@ QgsVectorLayer *QgsVectorLayer::clone() const
layer->setMapTipTemplate( mapTipTemplate() );
layer->setReadOnly( isReadOnly() );
layer->selectByIds( selectedFeatureIds() );
layer->setExcludeAttributesWms( excludeAttributesWms() );
layer->setExcludeAttributesWfs( excludeAttributesWfs() );
layer->setAttributeTableConfig( attributeTableConfig() );
layer->setFeatureBlendMode( featureBlendMode() );
layer->setOpacity( opacity() );
Expand Down Expand Up @@ -2306,29 +2304,6 @@ bool QgsVectorLayer::readSymbology( const QDomNode &layerNode, QString &errorMes

updateFields();

//Attributes excluded from WMS and WFS
mExcludeAttributesWMS.clear();
QDomNode excludeWMSNode = layerNode.namedItem( QStringLiteral( "excludeAttributesWMS" ) );
if ( !excludeWMSNode.isNull() )
{
QDomNodeList attributeNodeList = excludeWMSNode.toElement().elementsByTagName( QStringLiteral( "attribute" ) );
for ( int i = 0; i < attributeNodeList.size(); ++i )
{
mExcludeAttributesWMS.insert( attributeNodeList.at( i ).toElement().text() );
}
}

mExcludeAttributesWFS.clear();
QDomNode excludeWFSNode = layerNode.namedItem( QStringLiteral( "excludeAttributesWFS" ) );
if ( !excludeWFSNode.isNull() )
{
QDomNodeList attributeNodeList = excludeWFSNode.toElement().elementsByTagName( QStringLiteral( "attribute" ) );
for ( int i = 0; i < attributeNodeList.size(); ++i )
{
mExcludeAttributesWFS.insert( attributeNodeList.at( i ).toElement().text() );
}
}

// Load editor widget configuration
QDomElement widgetsElem = layerNode.namedItem( QStringLiteral( "fieldConfiguration" ) ).toElement();
QDomNodeList fieldConfigurationElementList = widgetsElem.elementsByTagName( QStringLiteral( "field" ) );
Expand All @@ -2351,6 +2326,28 @@ bool QgsVectorLayer::readSymbology( const QDomNode &layerNode, QString &errorMes
QgsEditorWidgetSetup setup = QgsEditorWidgetSetup( widgetType, optionsMap );
mFieldWidgetSetups[fieldName] = setup;
}

// Legacy reading for QGIS 3.14 and older projects
// Attributes excluded from WMS and WFS
const QList<QPair<QString, QgsField::ConfigurationFlag>> legacyConfig
{
qMakePair( QStringLiteral( "excludeAttributesWMS" ), QgsField::ConfigurationFlag::ExposeViaWms ),
qMakePair( QStringLiteral( "excludeAttributesWFS" ), QgsField::ConfigurationFlag::ExposeViaWfs )
};
for ( const auto &config : legacyConfig )
{
QDomNode excludeNode = layerNode.namedItem( config.first );
if ( !excludeNode.isNull() )
{
QDomNodeList attributeNodeList = excludeNode.toElement().elementsByTagName( QStringLiteral( "attribute" ) );
for ( int i = 0; i < attributeNodeList.size(); ++i )
{
QString fieldName = attributeNodeList.at( i ).toElement().text();
int index = mFields.indexFromName( fieldName );
setFieldConfigurationFlag( index, config.second, false );
}
}
}
}

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

//exclude attributes WMS
QDomElement excludeWMSElem = doc.createElement( QStringLiteral( "excludeAttributesWMS" ) );
QSet<QString>::const_iterator attWMSIt = mExcludeAttributesWMS.constBegin();
for ( ; attWMSIt != mExcludeAttributesWMS.constEnd(); ++attWMSIt )
{
QDomElement attrElem = doc.createElement( QStringLiteral( "attribute" ) );
QDomText attrText = doc.createTextNode( *attWMSIt );
attrElem.appendChild( attrText );
excludeWMSElem.appendChild( attrElem );
}
node.appendChild( excludeWMSElem );

//exclude attributes WFS
QDomElement excludeWFSElem = doc.createElement( QStringLiteral( "excludeAttributesWFS" ) );
QSet<QString>::const_iterator attWFSIt = mExcludeAttributesWFS.constBegin();
for ( ; attWFSIt != mExcludeAttributesWFS.constEnd(); ++attWFSIt )
{
QDomElement attrElem = doc.createElement( QStringLiteral( "attribute" ) );
QDomText attrText = doc.createTextNode( *attWFSIt );
attrElem.appendChild( attrText );
excludeWFSElem.appendChild( attrElem );
}
node.appendChild( excludeWFSElem );

//default expressions
QDomElement defaultsElem = doc.createElement( QStringLiteral( "defaults" ) );
for ( const QgsField &field : qgis::as_const( mFields ) )
Expand Down Expand Up @@ -5487,12 +5460,19 @@ void QgsVectorLayer::setFieldConfigurationFlags( int index, QgsField::Configurat
if ( index < 0 || index >= mFields.count() )
return;

mFields.at( index ).setConfigurationFlags( flags );

mFieldConfigurationFlags.insert( mFields.at( index ).name(), flags );
updateFields();
}

void QgsVectorLayer::setFieldConfigurationFlag( int index, QgsField::ConfigurationFlag flag, bool active )
{
if ( index < 0 || index >= mFields.count() )
return;
QgsField::ConfigurationFlags flags = mFields.at( index ).configurationFlags();
flags.setFlag( flag, active );
setFieldConfigurationFlags( index, flags );
}

QgsField::ConfigurationFlags QgsVectorLayer::fieldConfigurationFlags( int index ) const
{

Expand Down
18 changes: 14 additions & 4 deletions src/core/qgsvectorlayer.h
Expand Up @@ -1839,23 +1839,27 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte

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

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

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

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

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

/**
* Sets the given configuration \a flag for the field at given \a index to be \a active or not.
* \since QGIS 3.16
*/
void setFieldConfigurationFlag( int index, QgsField::ConfigurationFlag flag, bool active ) SIP_SKIP;

/**
* Returns the configuration flags of the field at given index
* \see QgsField::ConfigurationFlag
Expand Down
9 changes: 8 additions & 1 deletion src/gui/qgscheckablecombobox.cpp
Expand Up @@ -77,8 +77,9 @@ void QgsCheckBoxDelegate::paint( QPainter *painter, const QStyleOptionViewItem &
QgsCheckableComboBox::QgsCheckableComboBox( QWidget *parent )
: QComboBox( parent )
, mSeparator( QStringLiteral( ", " ) )
, mModel( new QgsCheckableItemModel( this ) )
{
setModel( new QgsCheckableItemModel( this ) );
setModel( mModel );
setItemDelegate( new QgsCheckBoxDelegate( this ) );

QLineEdit *lineEdit = new QLineEdit( this );
Expand Down Expand Up @@ -134,6 +135,12 @@ void QgsCheckableComboBox::setDefaultText( const QString &text )
}
}

void QgsCheckableComboBox::addItemWithCheckState( const QString &text, Qt::CheckState state, const QVariant &userData )
{
QComboBox::addItem( text, userData );
setItemCheckState( count() - 1, state );
}

QStringList QgsCheckableComboBox::checkedItems() const
{
QStringList items;
Expand Down
17 changes: 17 additions & 0 deletions src/gui/qgscheckablecombobox.h
Expand Up @@ -163,6 +163,14 @@ class GUI_EXPORT QgsCheckableComboBox : public QComboBox
*/
void setDefaultText( const QString &text );

/**
* Adds an item to the combobox with the given \a text, check \a state (stored in the Qt::CheckStateRole)
* and containing the specified \a userData (stored in the Qt::UserRole).
* The item is appended to the list of existing items.
* \since QGIS 3.16
*/
void addItemWithCheckState( const QString &text, Qt::CheckState state, const QVariant &userData = QVariant() );

/**
* Returns currently checked items.
* \see setCheckedItems()
Expand Down Expand Up @@ -201,6 +209,13 @@ class GUI_EXPORT QgsCheckableComboBox : public QComboBox
*/
void toggleItemCheckState( int index );

/**
* Returns the custom item model which handles checking the items
* \see QgsCheckableItemModel
* \since QGIS 3.16
*/
QgsCheckableItemModel *model() const SIP_SKIP {return mModel;}

/**
* Hides the list of items in the combobox if it is currently
* visible and resets the internal state.
Expand Down Expand Up @@ -260,6 +275,8 @@ class GUI_EXPORT QgsCheckableComboBox : public QComboBox
QString mSeparator;
QString mDefaultText;

QgsCheckableItemModel *mModel = nullptr;

bool mSkipHide = false;

QMenu *mContextMenu = nullptr;
Expand Down

0 comments on commit f6c98ec

Please sign in to comment.