Skip to content

Commit

Permalink
Raster maptips
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Nov 9, 2022
1 parent 25e94fa commit f040c26
Show file tree
Hide file tree
Showing 18 changed files with 311 additions and 168 deletions.
Expand Up @@ -198,6 +198,16 @@ Sets the expression context variables which are available for expressions trigge
a map tool capture like add feature.

.. versionadded:: 3.0
%End

static QgsExpressionContextScope *mapLayerPositionScope( const QgsPointXY &position ) /Factory/;
%Docstring
Sets the expression context variables which are available for expressions triggered by moving the mouse over a feature
of the currently selected layer.

:param position: map coordinates of the currente pointer position in the CRS of the layer which triggered the action.

.. versionadded:: 3.30
%End

static QgsExpressionContextScope *updateSymbolScope( const QgsSymbol *symbol, QgsExpressionContextScope *symbolScope = 0 );
Expand Down
34 changes: 34 additions & 0 deletions python/core/auto_generated/qgsmaplayer.sip.in
Expand Up @@ -1508,6 +1508,33 @@ Set placeholder image for legend. If the string is empty, a generated legend wil
.. versionadded:: 3.22
%End

QString mapTipTemplate() const;
%Docstring
The mapTip is a pretty, html representation for feature information.

It may also contain embedded expressions.

.. note::

this method was only available for vector layers since QGIS 3.0

.. versionadded:: 3.0
%End

void setMapTipTemplate( const QString &mapTipTemplate );
%Docstring
The mapTip is a pretty, html representation for feature information.

It may also contain embedded expressions.

.. note::

this method was only available for vector layers since QGIS 3.0

.. versionadded:: 3.30
%End


public slots:

void setMinimumScale( double scale );
Expand Down Expand Up @@ -1846,6 +1873,13 @@ Emitted when edited changes have been successfully written to the data provider.
Emitted when modifications has been done on layer

.. versionadded:: 3.22
%End

void mapTipTemplateChanged();
%Docstring
Emitted when the map tip changes

.. versionadded:: 3.0
%End

protected:
Expand Down
27 changes: 0 additions & 27 deletions python/core/auto_generated/raster/qgsrasterlayer.sip.in
Expand Up @@ -448,24 +448,6 @@ to be drawn outside the data extent.
virtual QgsMapLayerElevationProperties *elevationProperties();


QString mapTipTemplate() const;
%Docstring
The mapTip is a pretty, html representation for feature information.

It may also contain embedded expressions.

.. versionadded:: 3.30
%End

void setMapTipTemplate( const QString &mapTip );
%Docstring
The mapTip is a pretty, html representation for feature information.

It may also contain embedded expressions.

.. versionadded:: 3.30
%End

public slots:
void showStatusMessage( const QString &message );

Expand All @@ -485,15 +467,6 @@ Emitted when the layer's subset string has changed.
.. versionadded:: 3.12
%End

void mapTipTemplateChanged();
%Docstring
Emitted when the map tip changes

.. versionadded:: 3.30
%End



protected:
virtual bool readSymbology( const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories );

Expand Down
25 changes: 0 additions & 25 deletions python/core/auto_generated/vector/qgsvectorlayer.sip.in
Expand Up @@ -2569,24 +2569,6 @@ This defines the appearance of the attribute table.
%Docstring
Sets the attribute table configuration object.
This defines the appearance of the attribute table.
%End

QString mapTipTemplate() const;
%Docstring
The mapTip is a pretty, html representation for feature information.

It may also contain embedded expressions.

.. versionadded:: 3.0
%End

void setMapTipTemplate( const QString &mapTipTemplate );
%Docstring
The mapTip is a pretty, html representation for feature information.

It may also contain embedded expressions.

.. versionadded:: 3.0
%End

virtual QgsExpressionContext createExpressionContext() const ${SIP_FINAL};
Expand Down Expand Up @@ -2984,13 +2966,6 @@ to this signal and update the element accordingly.
:param element: The XML element where you can add additional style information to.
:param doc: The XML document that you can use to create new XML nodes.
:param errorMessage: Write error messages into this string.
%End

void mapTipTemplateChanged();
%Docstring
Emitted when the map tip changes

.. versionadded:: 3.0
%End

void displayExpressionChanged();
Expand Down
2 changes: 1 addition & 1 deletion resources/function_help/json/raster_attributes
Expand Up @@ -8,7 +8,7 @@
"description": "the name or id of a raster layer"
}, {
"arg": "band",
"description": "the band number to sample the value from and for the associated attribute table lookup."
"description": "the band number to sample the value from and for the associated attribute table lookup. "
}, {
"arg": "value",
"description": "raster value"
Expand Down
4 changes: 2 additions & 2 deletions src/app/qgisapp.cpp
Expand Up @@ -14420,8 +14420,8 @@ void QgisApp::showMapTip()
QgsMapLayer *mypLayer = mMapCanvas->currentLayer();
if ( mypLayer )
{
// only process vector layers
if ( mypLayer->type() == QgsMapLayerType::VectorLayer )
// only process vector and raster layers
if ( mypLayer->type() == QgsMapLayerType::VectorLayer || mypLayer->type() == QgsMapLayerType::RasterLayer )
{
// Show the maptip if the maptips button is depressed
if ( mMapTipsVisible )
Expand Down
7 changes: 7 additions & 0 deletions src/core/expression/qgsexpressioncontextutils.cpp
Expand Up @@ -535,6 +535,13 @@ QgsExpressionContextScope *QgsExpressionContextUtils::mapToolCaptureScope( const
return scope;
}

QgsExpressionContextScope *QgsExpressionContextUtils::mapLayerPositionScope( const QgsPointXY &position )
{
QgsExpressionContextScope *scope = new QgsExpressionContextScope( QObject::tr( "Map Layer Position" ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layer_position" ), QVariant::fromValue( QgsGeometry::fromPointXY( position ) ) ) );
return scope;
}

QgsExpressionContextScope *QgsExpressionContextUtils::updateSymbolScope( const QgsSymbol *symbol, QgsExpressionContextScope *symbolScope )
{
if ( !symbolScope )
Expand Down
9 changes: 9 additions & 0 deletions src/core/expression/qgsexpressioncontextutils.h
Expand Up @@ -188,6 +188,15 @@ class CORE_EXPORT QgsExpressionContextUtils
*/
static QgsExpressionContextScope *mapToolCaptureScope( const QList<QgsPointLocator::Match> &matches ) SIP_FACTORY;

/**
* Sets the expression context variables which are available for expressions triggered by moving the mouse over a feature
* of the currently selected layer.
* \param position map coordinates of the currente pointer position in the CRS of the layer which triggered the action.
*
* \since QGIS 3.30
*/
static QgsExpressionContextScope *mapLayerPositionScope( const QgsPointXY &position ) SIP_FACTORY;

/**
* Updates a symbol scope related to a QgsSymbol to an expression context.
* \param symbol symbol to extract properties from
Expand Down
14 changes: 14 additions & 0 deletions src/core/qgsmaplayer.cpp
Expand Up @@ -816,6 +816,20 @@ void QgsMapLayer::writeStyleManager( QDomNode &layerNode, QDomDocument &doc ) co
}
}

QString QgsMapLayer::mapTipTemplate() const
{
return mMapTipTemplate;
}

void QgsMapLayer::setMapTipTemplate( const QString &mapTip )
{
if ( mMapTipTemplate == mapTip )
return;

mMapTipTemplate = mapTip;
emit mapTipTemplateChanged();
}

bool QgsMapLayer::isValid() const
{
return mValid;
Expand Down
32 changes: 32 additions & 0 deletions src/core/qgsmaplayer.h
Expand Up @@ -80,6 +80,7 @@ class CORE_EXPORT QgsMapLayer : public QObject
Q_PROPERTY( QgsMapLayerType type READ type CONSTANT )
Q_PROPERTY( bool isValid READ isValid NOTIFY isValidChanged )
Q_PROPERTY( double opacity READ opacity WRITE setOpacity NOTIFY opacityChanged )
Q_PROPERTY( QString mapTipTemplate READ mapTipTemplate WRITE setMapTipTemplate NOTIFY mapTipTemplateChanged )

#ifdef SIP_RUN
SIP_CONVERT_TO_SUBCLASS_CODE
Expand Down Expand Up @@ -1523,6 +1524,27 @@ class CORE_EXPORT QgsMapLayer : public QObject
*/
void setLegendPlaceholderImage( const QString &imgPath ) { mLegendPlaceholderImage = imgPath; }

/**
* The mapTip is a pretty, html representation for feature information.
*
* It may also contain embedded expressions.
*
* \note this method was only available for vector layers since QGIS 3.0
* \since QGIS 3.0
*/
QString mapTipTemplate() const;

/**
* The mapTip is a pretty, html representation for feature information.
*
* It may also contain embedded expressions.
*
* \note this method was only available for vector layers since QGIS 3.0
* \since QGIS 3.30
*/
void setMapTipTemplate( const QString &mapTipTemplate );


public slots:

/**
Expand Down Expand Up @@ -1807,6 +1829,13 @@ class CORE_EXPORT QgsMapLayer : public QObject
*/
void layerModified();

/**
* Emitted when the map tip changes
*
* \since QGIS 3.0
*/
void mapTipTemplateChanged();

private slots:

void onNotified( const QString &message );
Expand Down Expand Up @@ -2127,6 +2156,9 @@ class CORE_EXPORT QgsMapLayer : public QObject
//! Path to placeholder image for layer legend. If the string is empty, a generated legend is shown
QString mLegendPlaceholderImage;

//! Maptip template
QString mMapTipTemplate;

friend class QgsVectorLayer;
friend class TestQgsMapLayer;
};
Expand Down
28 changes: 13 additions & 15 deletions src/core/raster/qgsrasterlayer.cpp
Expand Up @@ -168,6 +168,7 @@ QgsRasterLayer *QgsRasterLayer::clone() const
QgsMapLayer::clone( layer );
layer->mElevationProperties = mElevationProperties->clone();
layer->mElevationProperties->setParent( layer );
layer->setMapTipTemplate( mapTipTemplate() );

// do not clone data provider which is the first element in pipe
for ( int i = 1; i < mPipe->size(); i++ )
Expand Down Expand Up @@ -1213,21 +1214,6 @@ QgsMapLayerElevationProperties *QgsRasterLayer::elevationProperties()
return mElevationProperties;
}

QString QgsRasterLayer::mapTipTemplate() const
{
return mMapTipTemplate;
}

void QgsRasterLayer::setMapTipTemplate( const QString &mapTip )
{
if ( mMapTipTemplate == mapTip )
return;

mMapTipTemplate = mapTip;
emit mapTipTemplateChanged();

}

void QgsRasterLayer::setContrastEnhancement( QgsContrastEnhancement::ContrastEnhancementAlgorithm algorithm, QgsRasterMinMaxOrigin::Limits limits, const QgsRectangle &extent, int sampleSize, bool generateLookupTableFlag )
{
setContrastEnhancement( algorithm,
Expand Down Expand Up @@ -2144,6 +2130,9 @@ bool QgsRasterLayer::readSymbology( const QDomNode &layer_node, QString &errorMe
if ( !elemDataDefinedProperties.isNull() )
mPipe->dataDefinedProperties().readXml( elemDataDefinedProperties, QgsRasterPipe::propertyDefinitions() );

if ( categories.testFlag( MapTips ) )
setMapTipTemplate( layer_node.namedItem( QStringLiteral( "mapTip" ) ).toElement().text() );

readCustomProperties( layer_node );

emit rendererChanged();
Expand Down Expand Up @@ -2344,6 +2333,15 @@ bool QgsRasterLayer::writeSymbology( QDomNode &layer_node, QDomDocument &documen
QDomElement layerElement = layer_node.toElement();
writeCommonStyle( layerElement, document, context, categories );

// save map tip
if ( categories.testFlag( MapTips ) )
{
QDomElement mapTipElem = document.createElement( QStringLiteral( "mapTip" ) );
QDomText mapTipText = document.createTextNode( mapTipTemplate() );
mapTipElem.appendChild( mapTipText );
layer_node.toElement().appendChild( mapTipElem );
}

// Store pipe members into pipe element, in future, it will be
// possible to add custom filters into the pipe
QDomElement pipeElement = document.createElement( QStringLiteral( "pipe" ) );
Expand Down
28 changes: 0 additions & 28 deletions src/core/raster/qgsrasterlayer.h
Expand Up @@ -494,24 +494,6 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer, public QgsAbstractProfile
QgsMapLayerTemporalProperties *temporalProperties() override;
QgsMapLayerElevationProperties *elevationProperties() override;

/**
* The mapTip is a pretty, html representation for feature information.
*
* It may also contain embedded expressions.
*
* \since QGIS 3.30
*/
QString mapTipTemplate() const;

/**
* The mapTip is a pretty, html representation for feature information.
*
* It may also contain embedded expressions.
*
* \since QGIS 3.30
*/
void setMapTipTemplate( const QString &mapTip );

public slots:
void showStatusMessage( const QString &message );

Expand All @@ -530,15 +512,6 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer, public QgsAbstractProfile
*/
void subsetStringChanged();

/**
* Emitted when the map tip changes
*
* \since QGIS 3.30
*/
void mapTipTemplateChanged();



protected:
bool readSymbology( const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories ) override;
bool readStyle( const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories ) override;
Expand Down Expand Up @@ -638,7 +611,6 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer, public QgsAbstractProfile
QDomDocument mOriginalStyleDocument;
QDomElement mOriginalStyleElement;

QString mMapTipTemplate;
};

// clazy:excludeall=qstring-allocations
Expand Down

0 comments on commit f040c26

Please sign in to comment.