Skip to content

Commit

Permalink
(optionally) Store layer notes in QML/QLR and allow copying/pasting
Browse files Browse the repository at this point in the history
when copying styles between layers

Sponsored by Alta Ehf
  • Loading branch information
nyalldawson committed May 4, 2021
1 parent 7ec1abb commit f5449a1
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 7 deletions.
4 changes: 2 additions & 2 deletions python/core/auto_generated/qgslayernotesutils.sip.in
Expand Up @@ -23,7 +23,7 @@ Contains utility functions for working with layer notes.
%End
public:

static QString layerNotes( QgsMapLayer *layer );
static QString layerNotes( const QgsMapLayer *layer );
%Docstring
Returns the notes for the specified ``layer``.

Expand All @@ -35,7 +35,7 @@ The returned string is a HTML formatted set of user notations for the layer.
Sets the ``notes`` for the specified ``layer``, where ``notes`` is a HTML formatted string.
%End

static bool layerHasNotes( QgsMapLayer *layer );
static bool layerHasNotes( const QgsMapLayer *layer );
%Docstring
Returns ``True`` if the specified ``layer`` has notes available.
%End
Expand Down
1 change: 1 addition & 0 deletions python/core/auto_generated/qgsmaplayer.sip.in
Expand Up @@ -108,6 +108,7 @@ This is the base class for all map layer types (vector, raster).
Temporal,
Legend,
Elevation,
Notes,
AllStyleCategories
};
typedef QFlags<QgsMapLayer::StyleCategory> StyleCategories;
Expand Down
4 changes: 2 additions & 2 deletions src/core/qgslayernotesutils.cpp
Expand Up @@ -16,7 +16,7 @@
#include "qgslayernotesutils.h"
#include "qgsmaplayer.h"

QString QgsLayerNotesUtils::layerNotes( QgsMapLayer *layer )
QString QgsLayerNotesUtils::layerNotes( const QgsMapLayer *layer )
{
if ( !layer )
return nullptr;
Expand All @@ -35,7 +35,7 @@ void QgsLayerNotesUtils::setLayerNotes( QgsMapLayer *layer, const QString &notes
layer->setCustomProperty( QStringLiteral( "userNotes" ), notes );
}

bool QgsLayerNotesUtils::layerHasNotes( QgsMapLayer *layer )
bool QgsLayerNotesUtils::layerHasNotes( const QgsMapLayer *layer )
{
if ( !layer )
return false;
Expand Down
4 changes: 2 additions & 2 deletions src/core/qgslayernotesutils.h
Expand Up @@ -37,7 +37,7 @@ class CORE_EXPORT QgsLayerNotesUtils
*
* The returned string is a HTML formatted set of user notations for the layer.
*/
static QString layerNotes( QgsMapLayer *layer );
static QString layerNotes( const QgsMapLayer *layer );

/**
* Sets the \a notes for the specified \a layer, where \a notes is a HTML formatted string.
Expand All @@ -47,7 +47,7 @@ class CORE_EXPORT QgsLayerNotesUtils
/**
* Returns TRUE if the specified \a layer has notes available.
*/
static bool layerHasNotes( QgsMapLayer *layer );
static bool layerHasNotes( const QgsMapLayer *layer );

/**
* Removes any notes for the specified \a layer.
Expand Down
17 changes: 17 additions & 0 deletions src/core/qgsmaplayer.cpp
Expand Up @@ -60,6 +60,7 @@
#include "qgsmaplayertemporalproperties.h"
#include "qgsmaplayerelevationproperties.h"
#include "qgsprovidermetadata.h"
#include "qgslayernotesutils.h"

QString QgsMapLayer::extensionPropertyType( QgsMapLayer::PropertyType type )
{
Expand Down Expand Up @@ -621,6 +622,12 @@ void QgsMapLayer::writeCommonStyle( QDomElement &layerElement, QDomDocument &doc
properties->writeXml( layerElement, document, context );
}

if ( categories.testFlag( Notes ) && QgsLayerNotesUtils::layerHasNotes( this ) )
{
QDomElement notesElem = document.createElement( QStringLiteral( "userNotes" ) );
notesElem.setAttribute( QStringLiteral( "value" ), QgsLayerNotesUtils::layerNotes( this ) );
layerElement.appendChild( notesElem );
}

// custom properties
if ( categories.testFlag( CustomProperties ) )
Expand Down Expand Up @@ -1750,6 +1757,16 @@ void QgsMapLayer::readCommonStyle( const QDomElement &layerElement, const QgsRea
if ( QgsMapLayerElevationProperties *properties = elevationProperties() )
properties->readXml( layerElement.toElement(), context );
}

if ( categories.testFlag( Notes ) )
{
const QDomElement notesElem = layerElement.firstChildElement( QStringLiteral( "userNotes" ) );
if ( !notesElem.isNull() )
{
const QString notes = notesElem.attribute( QStringLiteral( "value" ) );
QgsLayerNotesUtils::setLayerNotes( this, notes );
}
}
}

QUndoStack *QgsMapLayer::undoStack()
Expand Down
3 changes: 2 additions & 1 deletion src/core/qgsmaplayer.h
Expand Up @@ -183,8 +183,9 @@ class CORE_EXPORT QgsMapLayer : public QObject
Temporal = 1 << 14, //!< Temporal properties (since QGIS 3.14)
Legend = 1 << 15, //!< Legend settings (since QGIS 3.16)
Elevation = 1 << 16, //!< Elevation settings (since QGIS 3.18)
Notes = 1 << 17, //!< Layer user notes (since QGIS 3.20)
AllStyleCategories = LayerConfiguration | Symbology | Symbology3D | Labeling | Fields | Forms | Actions |
MapTips | Diagrams | AttributeTable | Rendering | CustomProperties | GeometryOptions | Relations | Temporal | Legend | Elevation,
MapTips | Diagrams | AttributeTable | Rendering | CustomProperties | GeometryOptions | Relations | Temporal | Legend | Elevation | Notes,
};
Q_ENUM( StyleCategory )
Q_DECLARE_FLAGS( StyleCategories, StyleCategory )
Expand Down
11 changes: 11 additions & 0 deletions src/gui/qgsmaplayerstylecategoriesmodel.cpp
Expand Up @@ -286,6 +286,17 @@ QVariant QgsMapLayerStyleCategoriesModel::data( const QModelIndex &index, int ro
}
break;

case QgsMapLayer::StyleCategory::Notes:
switch ( role )
{
case Qt::DisplayRole:
case Qt::ToolTipRole:
return tr( "Notes" );
case Qt::DecorationRole:
return QIcon(); // TODO
}
break;

case QgsMapLayer::StyleCategory::AllStyleCategories:
switch ( role )
{
Expand Down

0 comments on commit f5449a1

Please sign in to comment.