Skip to content
Permalink
Browse files

dataDefinedServerProperties for the overrides of user defined server …

…variables (currently only WMSOnlineResource) as QgsPropertyCollection alongside the ProjectProperties
  • Loading branch information
signedav committed May 15, 2020
1 parent 7c0d3b5 commit 550ee2c0e0caed95756affe753f503bd696e6357
@@ -55,6 +55,13 @@ open within the main QGIS application.
AvoidIntersectionsLayers,
};

enum DataDefinedServerProperty
{
NoProperty,
AllProperties,
WMSOnlineResource,
};

static QgsProject *instance();
%Docstring
Returns the QgsProject singleton instance
@@ -401,18 +408,6 @@ Write a string list entry to the project file.
Keys are '/'-delimited entries, implying
a hierarchy of keys and corresponding values

.. note::

The key string must be valid xml tag names in order to be saved to the file.
%End

bool writeEntry( const QString &scope, const QString &key, const QgsProperty &value );
%Docstring
Write a QgsProperty entry to the project file.

Keys are '/'-delimited entries, implying
a hierarchy of keys and corresponding values

.. note::

The key string must be valid xml tag names in order to be saved to the file.
@@ -430,8 +425,6 @@ implying a hierarchy of keys and corresponding values
int readNumEntry( const QString &scope, const QString &key, int def = 0, bool *ok = 0 ) const;
double readDoubleEntry( const QString &scope, const QString &key, double def = 0, bool *ok = 0 ) const;
bool readBoolEntry( const QString &scope, const QString &key, bool def = false, bool *ok = 0 ) const;
QgsProperty readPropertyEntry( const QString &scope, const QString &key, const QgsProperty &def = QgsProperty(), bool *ok = 0 ) const;


bool removeEntry( const QString &scope, const QString &key );
%Docstring
@@ -1729,6 +1722,22 @@ Registers the objects that require translation into the ``translationContext``.
So there can be created a ts file with these values.

.. versionadded:: 3.4
%End

void setDataDefinedServerProperties( const QgsPropertyCollection &properties );
%Docstring
Sets the data defined properties used for overrides in user defined server
parameters to ``properties``

.. versionadded:: 3.14
%End

QgsPropertyCollection dataDefinedServerProperties() const;
%Docstring
Returns the data defined properties used for overrides in user defined server
parameters

.. versionadded:: 3.14
%End

};
@@ -442,11 +442,11 @@ QgsProjectProperties::QgsProjectProperties( QgsMapCanvas *mapCanvas, QWidget *pa
mWMSContactPhone->setText( QgsProject::instance()->readEntry( QStringLiteral( "WMSContactPhone" ), QStringLiteral( "/" ), QString() ) );
mWMSAbstract->setPlainText( QgsProject::instance()->readEntry( QStringLiteral( "WMSServiceAbstract" ), QStringLiteral( "/" ), QString() ) );
mWMSOnlineResourceLineEdit->setText( QgsProject::instance()->readEntry( QStringLiteral( "WMSOnlineResource" ), QStringLiteral( "/" ), QString() ) );
mWMSOnlineResourceExpressionButton->setToProperty( QgsProject::instance()->readPropertyEntry( "WMSOnlineResourceExpression", "/" ) );
mWMSUrlLineEdit->setText( QgsProject::instance()->readEntry( QStringLiteral( "WMSUrl" ), QStringLiteral( "/" ), QString() ) );
mWMSKeywordList->setText( QgsProject::instance()->readListEntry( QStringLiteral( "WMSKeywordList" ), QStringLiteral( "/" ) ).join( QStringLiteral( ", " ) ) );

mWMSOnlineResourceExpressionButton->registerExpressionContextGenerator( this );
mWMSOnlineResourceExpressionButton->setToProperty( QgsProject::instance()->dataDefinedServerProperties().property( QgsProject::DataDefinedServerProperty::WMSOnlineResource ) );

// WMS Name validator
QValidator *shortNameValidator = new QRegExpValidator( QgsApplication::shortNameRegExp(), this );
@@ -1183,9 +1183,12 @@ void QgsProjectProperties::apply()
QgsProject::instance()->writeEntry( QStringLiteral( "WMSContactPhone" ), QStringLiteral( "/" ), mWMSContactPhone->text() );
QgsProject::instance()->writeEntry( QStringLiteral( "WMSServiceAbstract" ), QStringLiteral( "/" ), mWMSAbstract->toPlainText() );
QgsProject::instance()->writeEntry( QStringLiteral( "WMSOnlineResource" ), QStringLiteral( "/" ), mWMSOnlineResourceLineEdit->text() );
QgsProject::instance()->writeEntry( QStringLiteral( "WMSOnlineResourceExpression" ), QStringLiteral( "/" ), mWMSOnlineResourceExpressionButton->toProperty() );
QgsProject::instance()->writeEntry( QStringLiteral( "WMSUrl" ), QStringLiteral( "/" ), mWMSUrlLineEdit->text() );

QgsPropertyCollection propertyCollection = QgsProject::instance()->dataDefinedServerProperties();
propertyCollection.setProperty( QgsProject::DataDefinedServerProperty::WMSOnlineResource, mWMSOnlineResourceExpressionButton->toProperty() );
QgsProject::instance()->setDataDefinedServerProperties( propertyCollection );

// WMS Contact Position
int contactPositionIndex = mWMSContactPositionCb->currentIndex();
QString contactPositionText = mWMSContactPositionCb->currentText();
@@ -593,6 +593,16 @@ void QgsProject::registerTranslatableObjects( QgsTranslationContext *translation
}
}

void QgsProject::setDataDefinedServerProperties( const QgsPropertyCollection &properties )
{
mDataDefinedServerProperties = properties;
}

QgsPropertyCollection QgsProject::dataDefinedServerProperties() const
{
return mDataDefinedServerProperties;
}

void QgsProject::setFileName( const QString &name )
{
if ( name == mFile.fileName() )
@@ -883,6 +893,45 @@ void _getProperties( const QDomDocument &doc, QgsProjectPropertyKey &project_pro
}
}

/**
Restore the data defined server properties collection found in "doc" to "dataDefinedServerProperties".
\code{.xml}
<dataDefinedServerProperties>
<Option type="Map">
<Option name="name" type="QString" value=""></Option>
<Option name="properties" type="Map">
<Option name="WMSOnlineResource" type="Map">
<Option name="active" type="bool" value="true"></Option>
<Option name="expression" type="QString" value="'www.mayasbees.ch/'||@project_basename"></Option>
<Option name="type" type="int" value="3"></Option>
</Option>
</Option>
<Option name="type" type="QString" value="collection"></Option>
</Option>
</dataDefinedServerProperties>
\endcode
\param doc xml document
\param dataDefinedServerProperties property collection of the server overrides
*/
void _getDataDefinedServerProperties( const QDomDocument &doc, QgsPropertyCollection &dataDefinedServerProperties, const QgsPropertiesDefinition &dataDefinedServerPropertyDefinitions )
{
// Read data defined server properties
QDomElement ddElem = doc.documentElement().firstChildElement( QStringLiteral( "dataDefinedServerProperties" ) );

if ( ddElem.isNull() ) // no properties found, so we're done
{
return;
}

if ( ! dataDefinedServerProperties.readXml( ddElem, dataDefinedServerPropertyDefinitions ) )
{
QgsDebugMsg( QStringLiteral( "dataDefinedServerProperties.readXml() failed" ) );
}
}

/**
Get the project title
@@ -1278,6 +1327,9 @@ bool QgsProject::readProjectFile( const QString &filename, QgsProject::ReadFlags
// now get any properties
_getProperties( *doc, mProperties );

// now get the data defined server properties
_getDataDefinedServerProperties( *doc, mDataDefinedServerProperties, dataDefinedServerPropertyDefinitions() );

QgsDebugMsgLevel( QString::number( mProperties.count() ) + " properties read", 2 );

#if 0
@@ -2198,6 +2250,10 @@ bool QgsProject::writeProjectFile( const QString &filename )
mProperties.writeXml( QStringLiteral( "properties" ), qgisNode, *doc );
}

QDomElement ddElem = doc->createElement( QStringLiteral( "dataDefinedServerProperties" ) );
mDataDefinedServerProperties.writeXml( ddElem, dataDefinedServerPropertyDefinitions() );
qgisNode.appendChild( ddElem );

mMapThemeCollection->writeXml( *doc );

mTransformContext.writeXml( qgisNode, context );
@@ -2359,17 +2415,6 @@ bool QgsProject::writeEntry( const QString &scope, const QString &key, const QSt
return success;
}

bool QgsProject::writeEntry( const QString &scope, const QString &key, const QgsProperty &value )
{
bool propertiesModified;
bool success = addKey_( scope, key, &mProperties, value.toVariant(), propertiesModified );

if ( propertiesModified )
setDirty( true );

return success;
}

QStringList QgsProject::readListEntry( const QString &scope,
const QString &key,
const QStringList &def,
@@ -2488,26 +2533,6 @@ bool QgsProject::readBoolEntry( const QString &scope, const QString &key, bool d
return def;
}

QgsProperty QgsProject::readPropertyEntry( const QString &scope, const QString &key, const QgsProperty &def, bool *ok ) const
{
QgsProjectProperty *property = findKey_( scope, key, mProperties );

if ( property )
{
QgsProperty propertyValue;
QVariant value = property->value();
bool loaded = propertyValue.loadVariant( value );
if ( ok )
*ok = loaded;

if ( loaded )
return propertyValue;
}

return def;
}


bool QgsProject::removeEntry( const QString &scope, const QString &key )
{
if ( findKey_( scope, key, mProperties ) )
@@ -3395,6 +3420,18 @@ bool QgsProject::saveAuxiliaryStorage( const QString &filename )
}
}

QgsPropertiesDefinition &QgsProject::dataDefinedServerPropertyDefinitions()
{
static QgsPropertiesDefinition sPropertyDefinitions
{
{
QgsProject::DataDefinedServerProperty::WMSOnlineResource,
QgsPropertyDefinition( "WMSOnlineResource", QObject::tr( "WMS Online Resource" ), QgsPropertyDefinition::String )
},
};
return sPropertyDefinitions;
}

const QgsAuxiliaryStorage *QgsProject::auxiliaryStorage() const
{
return mAuxiliaryStorage.get();
@@ -144,6 +144,21 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
};
Q_ENUM( AvoidIntersectionsMode )

/**
* Data defined properties.
* Overrides of user defined server parameters are stored in a
* property collection and they can be retrieved using the
* indexes specified in this enum.
*
* \since QGIS 3.14
*/
enum DataDefinedServerProperty
{
NoProperty = 0, //!< No property
AllProperties = 1, //!< All properties for item
WMSOnlineResource = 2, //!< Alias
};

//! Returns the QgsProject singleton instance
static QgsProject *instance();

@@ -423,16 +438,6 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
*/
bool writeEntry( const QString &scope, const QString &key, const QStringList &value );

/**
* Write a QgsProperty entry to the project file.
*
* Keys are '/'-delimited entries, implying
* a hierarchy of keys and corresponding values
*
* \note The key string must be valid xml tag names in order to be saved to the file.
*/
bool writeEntry( const QString &scope, const QString &key, const QgsProperty &value );

/**
* Key value accessors
*
@@ -445,8 +450,6 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
int readNumEntry( const QString &scope, const QString &key, int def = 0, bool *ok = nullptr ) const;
double readDoubleEntry( const QString &scope, const QString &key, double def = 0, bool *ok = nullptr ) const;
bool readBoolEntry( const QString &scope, const QString &key, bool def = false, bool *ok = nullptr ) const;
QgsProperty readPropertyEntry( const QString &scope, const QString &key, const QgsProperty &def = QgsProperty(), bool *ok = nullptr ) const;


//! Remove the given key
bool removeEntry( const QString &scope, const QString &key );
@@ -1753,6 +1756,22 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
*/
void registerTranslatableObjects( QgsTranslationContext *translationContext );

/**
* Sets the data defined properties used for overrides in user defined server
* parameters to \a properties
*
* \since QGIS 3.14
*/
void setDataDefinedServerProperties( const QgsPropertyCollection &properties );

/**
* Returns the data defined properties used for overrides in user defined server
* parameters
*
* \since QGIS 3.14
*/
QgsPropertyCollection dataDefinedServerProperties() const;

private slots:
void onMapLayersAdded( const QList<QgsMapLayer *> &layers );
void onMapLayersRemoved( const QList<QgsMapLayer *> &layers );
@@ -1831,6 +1850,9 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
//! Save auxiliary storage to database
bool saveAuxiliaryStorage( const QString &filename = QString() );

//! Returns the property definition used for a data defined server property
static QgsPropertiesDefinition &dataDefinedServerPropertyDefinitions();

std::unique_ptr< QgsMapLayerStore > mLayerStore;

QString mErrorMessage;
@@ -1900,6 +1922,8 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
int mDirtyBlockCount = 0;
bool mTrustLayerMetadata = false;

QgsPropertyCollection mDataDefinedServerProperties;

QgsCoordinateTransformContext mTransformContext;

QgsProjectMetadata mMetadata;
@@ -88,8 +88,8 @@ bool QgsProjectPropertyValue::readXml( const QDomNode &keyNode )
return false;

case QVariant::Map:
mValue = QgsXmlUtils::readVariant( subkeyElement.firstChild().toElement() ).toMap();
break;
QgsDebugMsg( QStringLiteral( "no support for QVariant::Map" ) );
return false;

case QVariant::List:
QgsDebugMsg( QStringLiteral( "no support for QVariant::List" ) );
@@ -258,11 +258,6 @@ bool QgsProjectPropertyValue::writeXml( QString const &nodeName,
valueElement.appendChild( stringListElement );
}
}
else if ( QVariant::Map == mValue.type() )
{
QDomElement element = QgsXmlUtils::writeVariant( mValue, document );
valueElement.appendChild( element );
}
else // we just plop the value in as plain ole text
{
QDomText valueText = document.createTextNode( mValue.toString() );
@@ -67,11 +67,11 @@ QString QgsServerProjectUtils::owsServiceOnlineResource( const QgsProject &proje
{
QString wmsOnlineResource = project.readEntry( QStringLiteral( "WMSOnlineResource" ), QStringLiteral( "/" ) );

QgsProperty wmsOnlineResourceExpression = project.readPropertyEntry( QStringLiteral( "WMSOnlineResourceExpression" ), QStringLiteral( "/" ) );
if ( wmsOnlineResourceExpression.isActive() && ! wmsOnlineResourceExpression.expressionString().isEmpty() )
QgsProperty wmsOnlineResourceProperty = project.dataDefinedServerProperties().property( QgsProject::DataDefinedServerProperty::WMSOnlineResource );
if ( wmsOnlineResourceProperty.isActive() && ! wmsOnlineResourceProperty.expressionString().isEmpty() )
{
QgsExpressionContext context = project.createExpressionContext();
return wmsOnlineResourceExpression.valueAsString( context, wmsOnlineResource );
return wmsOnlineResourceProperty.valueAsString( context, wmsOnlineResource );
}

return wmsOnlineResource;

0 comments on commit 550ee2c

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