Skip to content
Permalink
Browse files

use a readable string, fix flag keys, fix opening old project file

  • Loading branch information
3nids committed Sep 14, 2020
1 parent d273f78 commit de4a293ec5d24589563bdf92fe25680dc99ef53c
@@ -291,6 +291,13 @@ Sets the alias for the field (the friendly displayed name of the field ).
Formats string for display
%End

static QString readableConfigurationFlag( ConfigurationFlag flag );
%Docstring
Returns the reabable and translated value of the configuration flag

.. versionadded:: 3.16
%End


bool convertCompatible( QVariant &v ) const;
%Docstring
@@ -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::Wms:
return QStringLiteral( "WMS" );
case ConfigurationFlag::Wfs:
return QStringLiteral( "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.
@@ -80,9 +80,9 @@ 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)
Wms = 0x2, //!< Fields is available if layer is served as WMS
Wfs = 0x3, //!< Fields is available if layer is served as WFS
Searchable = 1 << 1, //!< Defines if the field is searchable (used in the locator search for instance)
Wms = 1 << 2, //!< Fields is available if layer is served as WMS
Wfs = 1 << 3, //!< Fields is available if layer is served as WFS
DefaultFlags = Searchable | Wms | Wfs, //!< Default set of flags for a field
};
Q_ENUM( ConfigurationFlag )
@@ -329,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( ConfigurationFlag flag );

#ifndef SIP_RUN

/**
@@ -2304,28 +2304,6 @@ bool QgsVectorLayer::readSymbology( const QDomNode &layerNode, QString &errorMes

updateFields();

// 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::Wms ),
qMakePair( QStringLiteral( "excludeAttributesWFS" ), QgsField::ConfigurationFlag::Wfs )
};
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 );
}
}
}

// Load editor widget configuration
QDomElement widgetsElem = layerNode.namedItem( QStringLiteral( "fieldConfiguration" ) ).toElement();
QDomNodeList fieldConfigurationElementList = widgetsElem.elementsByTagName( QStringLiteral( "field" ) );
@@ -2348,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::Wms ),
qMakePair( QStringLiteral( "excludeAttributesWFS" ), QgsField::ConfigurationFlag::Wfs )
};
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 ) )
@@ -5460,8 +5460,6 @@ 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();
}
@@ -138,7 +138,7 @@ void QgsCheckableComboBox::setDefaultText( const QString &text )
void QgsCheckableComboBox::addItemWithCheckState( const QString &text, Qt::CheckState state, const QVariant &userData )
{
QComboBox::addItem( text, userData );
setItemCheckState( count(), state );
setItemCheckState( count() - 1, state );
}

QStringList QgsCheckableComboBox::checkedItems() const
@@ -65,15 +65,9 @@ QgsSourceFieldsProperties::QgsSourceFieldsProperties( QgsVectorLayer *layer, QWi
mFieldsList->setHorizontalHeaderItem( AttrLengthCol, new QTableWidgetItem( tr( "Length" ) ) );
mFieldsList->setHorizontalHeaderItem( AttrPrecCol, new QTableWidgetItem( tr( "Precision" ) ) );
mFieldsList->setHorizontalHeaderItem( AttrCommentCol, new QTableWidgetItem( tr( "Comment" ) ) );
const auto wmsWi = new QTableWidgetItem( QStringLiteral( "WMS" ) );
wmsWi->setToolTip( tr( "Defines if this field is available in QGIS Server WMS service" ) );
mFieldsList->setHorizontalHeaderItem( AttrWMSCol, wmsWi );
const auto wfsWi = new QTableWidgetItem( QStringLiteral( "WFS" ) );
wfsWi->setToolTip( tr( "Defines if this field is available in QGIS Server WFS (and OAPIF) service" ) );
mFieldsList->setHorizontalHeaderItem( AttrWFSCol, wfsWi );
const auto searchableWi = new QTableWidgetItem( QStringLiteral( "Searchable" ) );
searchableWi->setToolTip( tr( "Defines if this field is searchable (active layer locator filter)" ) );
mFieldsList->setHorizontalHeaderItem( AttrSearchableCol, searchableWi );
const auto configurationFlagsWi = new QTableWidgetItem( QStringLiteral( "Configuration" ) );
configurationFlagsWi->setToolTip( tr( "Configures the field" ) );
mFieldsList->setHorizontalHeaderItem( AttrConfigurationFlagsCol, configurationFlagsWi );
mFieldsList->setHorizontalHeaderItem( AttrAliasCol, new QTableWidgetItem( tr( "Alias" ) ) );

mFieldsList->setSortingEnabled( true );
@@ -164,6 +158,9 @@ void QgsSourceFieldsProperties::attributeAdded( int idx )

for ( int i = 0; i < mFieldsList->columnCount(); i++ )
{
if ( i == AttrConfigurationFlagsCol )
continue;

switch ( mLayer->fields().fieldOrigin( idx ) )
{
case QgsFields::OriginExpression:
@@ -275,18 +272,17 @@ void QgsSourceFieldsProperties::setRow( int row, int idx, const QgsField &field

// Flags
QgsCheckableComboBox *cb = new QgsCheckableComboBox( mFieldsList );
const QMap<QgsField::ConfigurationFlag, QString> flagList = qgsEnumMap<QgsField::ConfigurationFlag>();
QMap<QgsField::ConfigurationFlag, QString>::const_iterator flagIt;
for ( flagIt = flagList.constBegin(); flagIt != flagList.constEnd(); ++flagIt )
const QList<QgsField::ConfigurationFlag> flagList = qgsEnumMap<QgsField::ConfigurationFlag>().keys();
for ( const QgsField::ConfigurationFlag flag : flagList )
{
if ( flagIt.key() == QgsField::ConfigurationFlag::None || flagIt.key() == QgsField::ConfigurationFlag::DefaultFlags )
if ( flag == QgsField::ConfigurationFlag::None || flag == QgsField::ConfigurationFlag::DefaultFlags )
continue;

cb->addItemWithCheckState( flagIt.value(),
mLayer->fieldConfigurationFlags( idx ).testFlag( flagIt.key() ) ? Qt::Unchecked : Qt::Checked,
QVariant::fromValue( flagIt.key() ) );
cb->addItemWithCheckState( QgsField::readableConfigurationFlag( flag ),
mLayer->fieldConfigurationFlags( idx ).testFlag( flag ) ? Qt::Checked : Qt::Unchecked,
QVariant::fromValue( flag ) );
}
mFieldsList->setCellWidget( row, AttrSearchableCol, cb );
mFieldsList->setCellWidget( row, AttrConfigurationFlagsCol, cb );
}

bool QgsSourceFieldsProperties::addAttribute( const QgsField &field )
@@ -313,7 +309,7 @@ void QgsSourceFieldsProperties::apply()
int idx = mFieldsList->item( i, AttrIdCol )->data( Qt::DisplayRole ).toInt();
QgsField::ConfigurationFlags flags = mLayer->fieldConfigurationFlags( idx );

QgsCheckableComboBox *cb = qobject_cast<QgsCheckableComboBox *>( mFieldsList->cellWidget( i, AttrSearchableCol ) );
QgsCheckableComboBox *cb = qobject_cast<QgsCheckableComboBox *>( mFieldsList->cellWidget( i, AttrConfigurationFlagsCol ) );
if ( cb )
{
QgsCheckableItemModel *model = cb->model();
@@ -80,9 +80,7 @@ class GUI_EXPORT QgsSourceFieldsProperties : public QWidget, private Ui_QgsSourc
AttrLengthCol,
AttrPrecCol,
AttrCommentCol,
AttrWMSCol,
AttrWFSCol,
AttrSearchableCol,
AttrConfigurationFlagsCol,
AttrColCount,
};

0 comments on commit de4a293

Please sign in to comment.
You can’t perform that action at this time.