Skip to content

Commit

Permalink
Merge pull request #36427 from signedav/ressource_expression
Browse files Browse the repository at this point in the history
[Server] Expression override for online resource in service capabilities
  • Loading branch information
m-kuhn committed May 15, 2020
2 parents ad3c1cd + 54739e0 commit bf485ec
Show file tree
Hide file tree
Showing 8 changed files with 373 additions and 177 deletions.
24 changes: 23 additions & 1 deletion python/core/auto_generated/qgsproject.sip.in
Expand Up @@ -55,6 +55,13 @@ open within the main QGIS application.
AvoidIntersectionsLayers, AvoidIntersectionsLayers,
}; };


enum DataDefinedServerProperty
{
NoProperty,
AllProperties,
WMSOnlineResource,
};

static QgsProject *instance(); static QgsProject *instance();
%Docstring %Docstring
Returns the QgsProject singleton instance Returns the QgsProject singleton instance
Expand Down Expand Up @@ -419,7 +426,6 @@ implying a hierarchy of keys and corresponding values
double readDoubleEntry( const QString &scope, const QString &key, double 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; bool readBoolEntry( const QString &scope, const QString &key, bool def = false, bool *ok = 0 ) const;



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


.. versionadded:: 3.4 .. 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 %End


}; };
Expand Down
17 changes: 17 additions & 0 deletions src/app/qgsprojectproperties.cpp
Expand Up @@ -445,6 +445,9 @@ QgsProjectProperties::QgsProjectProperties( QgsMapCanvas *mapCanvas, QWidget *pa
mWMSUrlLineEdit->setText( QgsProject::instance()->readEntry( QStringLiteral( "WMSUrl" ), QStringLiteral( "/" ), QString() ) ); mWMSUrlLineEdit->setText( QgsProject::instance()->readEntry( QStringLiteral( "WMSUrl" ), QStringLiteral( "/" ), QString() ) );
mWMSKeywordList->setText( QgsProject::instance()->readListEntry( QStringLiteral( "WMSKeywordList" ), QStringLiteral( "/" ) ).join( QStringLiteral( ", " ) ) ); 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 // WMS Name validator
QValidator *shortNameValidator = new QRegExpValidator( QgsApplication::shortNameRegExp(), this ); QValidator *shortNameValidator = new QRegExpValidator( QgsApplication::shortNameRegExp(), this );
mWMSName->setValidator( shortNameValidator ); mWMSName->setValidator( shortNameValidator );
Expand Down Expand Up @@ -994,6 +997,16 @@ void QgsProjectProperties::setSelectedCrs( const QgsCoordinateReferenceSystem &c
crsChanged( projectionSelector->crs() ); crsChanged( projectionSelector->crs() );
} }


QgsExpressionContext QgsProjectProperties::createExpressionContext() const
{
QgsExpressionContext context;
context
<< QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope( QgsProject::instance() );

return context;
}

void QgsProjectProperties::apply() void QgsProjectProperties::apply()
{ {
mMapCanvas->enableMapTileRendering( mMapTileRenderingCheckBox->isChecked() ); mMapCanvas->enableMapTileRendering( mMapTileRenderingCheckBox->isChecked() );
Expand Down Expand Up @@ -1172,6 +1185,10 @@ void QgsProjectProperties::apply()
QgsProject::instance()->writeEntry( QStringLiteral( "WMSOnlineResource" ), QStringLiteral( "/" ), mWMSOnlineResourceLineEdit->text() ); QgsProject::instance()->writeEntry( QStringLiteral( "WMSOnlineResource" ), QStringLiteral( "/" ), mWMSOnlineResourceLineEdit->text() );
QgsProject::instance()->writeEntry( QStringLiteral( "WMSUrl" ), QStringLiteral( "/" ), mWMSUrlLineEdit->text() ); 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 // WMS Contact Position
int contactPositionIndex = mWMSContactPositionCb->currentIndex(); int contactPositionIndex = mWMSContactPositionCb->currentIndex();
QString contactPositionText = mWMSContactPositionCb->currentText(); QString contactPositionText = mWMSContactPositionCb->currentText();
Expand Down
3 changes: 2 additions & 1 deletion src/app/qgsprojectproperties.h
Expand Up @@ -42,7 +42,7 @@ class QgsBearingNumericFormat;
\note actual state is stored in QgsProject singleton instance \note actual state is stored in QgsProject singleton instance
*/ */
class APP_EXPORT QgsProjectProperties : public QgsOptionsDialogBase, private Ui::QgsProjectPropertiesBase class APP_EXPORT QgsProjectProperties : public QgsOptionsDialogBase, private Ui::QgsProjectPropertiesBase, public QgsExpressionContextGenerator
{ {
Q_OBJECT Q_OBJECT


Expand All @@ -65,6 +65,7 @@ class APP_EXPORT QgsProjectProperties : public QgsOptionsDialogBase, private Ui:
*/ */
void setSelectedCrs( const QgsCoordinateReferenceSystem &crs ); void setSelectedCrs( const QgsCoordinateReferenceSystem &crs );


QgsExpressionContext createExpressionContext() const override;
public slots: public slots:


/** /**
Expand Down
50 changes: 49 additions & 1 deletion src/core/qgsproject.cpp
Expand Up @@ -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 ) void QgsProject::setFileName( const QString &name )
{ {
if ( name == mFile.fileName() ) if ( name == mFile.fileName() )
Expand Down Expand Up @@ -883,6 +893,26 @@ void _getProperties( const QDomDocument &doc, QgsProjectPropertyKey &project_pro
} }
} }


/**
Returns the data defined server properties collection found in "doc" to "dataDefinedServerProperties".
\param doc xml document
\param dataDefinedServerProperties property collection of the server overrides
\since QGIS 3.14
**/
QgsPropertyCollection getDataDefinedServerProperties( const QDomDocument &doc, const QgsPropertiesDefinition &dataDefinedServerPropertyDefinitions )
{
QgsPropertyCollection ddServerProperties;
// Read data defined server properties
QDomElement ddElem = doc.documentElement().firstChildElement( QStringLiteral( "dataDefinedServerProperties" ) );
if ( !ddElem.isNull() )
{
if ( !ddServerProperties.readXml( ddElem, dataDefinedServerPropertyDefinitions ) )
{
QgsDebugMsg( QStringLiteral( "dataDefinedServerProperties.readXml() failed" ) );
}
}
return ddServerProperties;
}


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


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

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


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


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

mMapThemeCollection->writeXml( *doc ); mMapThemeCollection->writeXml( *doc );


mTransformContext.writeXml( qgisNode, context ); mTransformContext.writeXml( qgisNode, context );
Expand Down Expand Up @@ -2477,7 +2514,6 @@ bool QgsProject::readBoolEntry( const QString &scope, const QString &key, bool d
return def; return def;
} }



bool QgsProject::removeEntry( const QString &scope, const QString &key ) bool QgsProject::removeEntry( const QString &scope, const QString &key )
{ {
if ( findKey_( scope, key, mProperties ) ) if ( findKey_( scope, key, mProperties ) )
Expand Down Expand Up @@ -3365,6 +3401,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 const QgsAuxiliaryStorage *QgsProject::auxiliaryStorage() const
{ {
return mAuxiliaryStorage.get(); return mAuxiliaryStorage.get();
Expand Down
37 changes: 36 additions & 1 deletion src/core/qgsproject.h
Expand Up @@ -144,6 +144,21 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
}; };
Q_ENUM( AvoidIntersectionsMode ) 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 //! Returns the QgsProject singleton instance
static QgsProject *instance(); static QgsProject *instance();


Expand Down Expand Up @@ -436,7 +451,6 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
double readDoubleEntry( const QString &scope, const QString &key, double 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; bool readBoolEntry( const QString &scope, const QString &key, bool def = false, bool *ok = nullptr ) const;



//! Remove the given key //! Remove the given key
bool removeEntry( const QString &scope, const QString &key ); bool removeEntry( const QString &scope, const QString &key );


Expand Down Expand Up @@ -1742,6 +1756,22 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
*/ */
void registerTranslatableObjects( QgsTranslationContext *translationContext ); 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: private slots:
void onMapLayersAdded( const QList<QgsMapLayer *> &layers ); void onMapLayersAdded( const QList<QgsMapLayer *> &layers );
void onMapLayersRemoved( const QList<QgsMapLayer *> &layers ); void onMapLayersRemoved( const QList<QgsMapLayer *> &layers );
Expand Down Expand Up @@ -1820,6 +1850,9 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
//! Save auxiliary storage to database //! Save auxiliary storage to database
bool saveAuxiliaryStorage( const QString &filename = QString() ); 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; std::unique_ptr< QgsMapLayerStore > mLayerStore;


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


QgsPropertyCollection mDataDefinedServerProperties;

QgsCoordinateTransformContext mTransformContext; QgsCoordinateTransformContext mTransformContext;


QgsProjectMetadata mMetadata; QgsProjectMetadata mMetadata;
Expand Down
1 change: 0 additions & 1 deletion src/core/qgsprojectproperty.cpp
Expand Up @@ -218,7 +218,6 @@ bool QgsProjectPropertyValue::readXml( const QDomNode &keyNode )
value_ = QVariant( subkeyElement.text() ).toULongLong(); value_ = QVariant( subkeyElement.text() ).toULongLong();
break; break;
#endif #endif

default : default :
QgsDebugMsg( QStringLiteral( "unsupported value type %1 .. not properly translated to QVariant" ).arg( typeString ) ); QgsDebugMsg( QStringLiteral( "unsupported value type %1 .. not properly translated to QVariant" ).arg( typeString ) );
} }
Expand Down
11 changes: 10 additions & 1 deletion src/server/qgsserverprojectutils.cpp
Expand Up @@ -65,7 +65,16 @@ QStringList QgsServerProjectUtils::owsServiceKeywords( const QgsProject &project


QString QgsServerProjectUtils::owsServiceOnlineResource( const QgsProject &project ) QString QgsServerProjectUtils::owsServiceOnlineResource( const QgsProject &project )
{ {
return project.readEntry( QStringLiteral( "WMSOnlineResource" ), QStringLiteral( "/" ) ); QString wmsOnlineResource = project.readEntry( QStringLiteral( "WMSOnlineResource" ), QStringLiteral( "/" ) );

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

return wmsOnlineResource;
} }


QString QgsServerProjectUtils::owsServiceContactOrganization( const QgsProject &project ) QString QgsServerProjectUtils::owsServiceContactOrganization( const QgsProject &project )
Expand Down

0 comments on commit bf485ec

Please sign in to comment.