Navigation Menu

Skip to content

Commit

Permalink
Move layer notes utils to a new QgsLayerNotesUtils class so that they…
Browse files Browse the repository at this point in the history
… are accessible from core
  • Loading branch information
nyalldawson committed May 4, 2021
1 parent d07348d commit 3bdb2bb
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 62 deletions.
57 changes: 57 additions & 0 deletions python/core/auto_generated/qgslayernotesutils.sip.in
@@ -0,0 +1,57 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgslayernotesutils.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/




class QgsLayerNotesUtils
{
%Docstring(signature="appended")

Contains utility functions for working with layer notes.

.. versionadded:: 3.20
%End

%TypeHeaderCode
#include "qgslayernotesutils.h"
%End
public:

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

The returned string is a HTML formatted set of user notations for the layer.
%End

static void setLayerNotes( QgsMapLayer *layer, const QString &notes );
%Docstring
Sets the ``notes`` for the specified ``layer``, where ``notes`` is a HTML formatted string.
%End

static bool layerHasNotes( QgsMapLayer *layer );
%Docstring
Returns ``True`` if the specified ``layer`` has notes available.
%End

static void removeNotes( QgsMapLayer *layer );
%Docstring
Removes any notes for the specified ``layer``.
%End

};


/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgslayernotesutils.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
1 change: 1 addition & 0 deletions python/core/core_auto.sip
Expand Up @@ -94,6 +94,7 @@
%Include auto_generated/qgsinterval.sip
%Include auto_generated/qgsjsonutils.sip
%Include auto_generated/qgslayerdefinition.sip
%Include auto_generated/qgslayernotesutils.sip
%Include auto_generated/qgslegendrenderer.sip
%Include auto_generated/qgslegendsettings.sip
%Include auto_generated/qgslegendstyle.sip
Expand Down
7 changes: 4 additions & 3 deletions src/app/qgsapplayertreeviewmenuprovider.cpp
Expand Up @@ -46,6 +46,7 @@
#include "qgspointcloudlayer.h"
#include "qgsvectorlayerlabeling.h"
#include "qgslayernotesmanager.h"
#include "qgslayernotesutils.h"

#include <QMessageBox>

Expand Down Expand Up @@ -673,13 +674,13 @@ QMenu *QgsAppLayerTreeViewMenuProvider::createContextMenu()
}
}

QAction *notes = new QAction( QgsLayerNotesManager::layerHasNotes( layer ) ? tr( "Edit Layer Notes…" ) : tr( "Add Layer Notes…" ), menu );
QAction *notes = new QAction( QgsLayerNotesUtils::layerHasNotes( layer ) ? tr( "Edit Layer Notes…" ) : tr( "Add Layer Notes…" ), menu );
connect( notes, &QAction::triggered, this, [layer ]
{
QgsLayerNotesManager::editLayerNotes( layer, QgisApp::instance() );
} );
menu->addAction( notes );
if ( QgsLayerNotesManager::layerHasNotes( layer ) )
if ( QgsLayerNotesUtils::layerHasNotes( layer ) )
{
QAction *notes = new QAction( tr( "Remove Layer Notes" ), menu );
connect( notes, &QAction::triggered, this, [layer ]
Expand All @@ -688,7 +689,7 @@ QMenu *QgsAppLayerTreeViewMenuProvider::createContextMenu()
tr( "Remove Layer Notes" ),
tr( "Are you sure you want to remove all notes for the layer “%1”?" ).arg( layer->name() ),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) == QMessageBox::Yes )
QgsLayerNotesManager::removeNotes( layer );
QgsLayerNotesUtils::removeNotes( layer );
} );
menu->addAction( notes );
}
Expand Down
38 changes: 3 additions & 35 deletions src/app/qgslayernotesmanager.cpp
Expand Up @@ -14,54 +14,22 @@
***************************************************************************/

#include "qgslayernotesmanager.h"
#include "qgslayernotesutils.h"
#include "qgsmaplayer.h"
#include "qgsrichtexteditor.h"
#include "qgsgui.h"
#include <QDialogButtonBox>
#include <QPushButton>

QString QgsLayerNotesManager::layerNotes( QgsMapLayer *layer )
{
if ( !layer )
return nullptr;

return layer->customProperty( QStringLiteral( "userNotes" ) ).toString();
}

void QgsLayerNotesManager::setLayerNotes( QgsMapLayer *layer, const QString &notes )
{
if ( !layer )
return;

if ( notes.isEmpty() )
layer->removeCustomProperty( QStringLiteral( "userNotes" ) );
else
layer->setCustomProperty( QStringLiteral( "userNotes" ), notes );
}

bool QgsLayerNotesManager::layerHasNotes( QgsMapLayer *layer )
{
if ( !layer )
return false;

return !layer->customProperty( QStringLiteral( "userNotes" ) ).toString().isEmpty();
}

void QgsLayerNotesManager::removeNotes( QgsMapLayer *layer )
{
if ( layer )
layer->removeCustomProperty( QStringLiteral( "userNotes" ) );
}

void QgsLayerNotesManager::editLayerNotes( QgsMapLayer *layer, QWidget *parent )
{
const QString notes = layerNotes( layer );
const QString notes = QgsLayerNotesUtils::layerNotes( layer );
QgsLayerNotesDialog *editor = new QgsLayerNotesDialog( parent );
editor->setNotes( notes );
editor->setWindowTitle( QObject::tr( "Layer Notes — %1" ).arg( layer->name() ) );
if ( editor->exec() )
{
QgsLayerNotesManager::setLayerNotes( layer, editor->notes() );
QgsLayerNotesUtils::setLayerNotes( layer, editor->notes() );
}
}

Expand Down
22 changes: 0 additions & 22 deletions src/app/qgslayernotesmanager.h
Expand Up @@ -27,28 +27,6 @@ class QgsLayerNotesManager
{
public:

/**
* Returns the notes for the specified \a layer.
*
* The returned string is a HTML formatted set of user notations for the layer.
*/
static QString layerNotes( QgsMapLayer *layer );

/**
* Sets the \a notes for the specified \a layer, where \a notes is a HTML formatted string.
*/
static void setLayerNotes( QgsMapLayer *layer, const QString &notes );

/**
* Returns TRUE if the specified \a layer has notes available.
*/
static bool layerHasNotes( QgsMapLayer *layer );

/**
* Removes any notes for the specified \a layer.
*/
static void removeNotes( QgsMapLayer *layer );

/**
* Shows a dialog allowing users to edit the notes for the specified \a layer.
*/
Expand Down
5 changes: 3 additions & 2 deletions src/app/qgslayertreeviewnotesindicator.cpp
Expand Up @@ -20,6 +20,7 @@
#include "qgslayertreeutils.h"
#include "qgsvectorlayer.h"
#include "qgslayernotesmanager.h"
#include "qgslayernotesutils.h"
#include "qgisapp.h"

QgsLayerTreeViewNotesIndicatorProvider::QgsLayerTreeViewNotesIndicatorProvider( QgsLayerTreeView *view )
Expand Down Expand Up @@ -56,7 +57,7 @@ bool QgsLayerTreeViewNotesIndicatorProvider::acceptLayer( QgsMapLayer *layer )
if ( !layer )
return false;

return QgsLayerNotesManager::layerHasNotes( layer );
return QgsLayerNotesUtils::layerHasNotes( layer );
}

QString QgsLayerTreeViewNotesIndicatorProvider::iconName( QgsMapLayer *layer )
Expand All @@ -67,6 +68,6 @@ QString QgsLayerTreeViewNotesIndicatorProvider::iconName( QgsMapLayer *layer )

QString QgsLayerTreeViewNotesIndicatorProvider::tooltipText( QgsMapLayer *layer )
{
return QgsLayerNotesManager::layerNotes( layer );
return QgsLayerNotesUtils::layerNotes( layer );
}

2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -347,6 +347,7 @@ set(QGIS_CORE_SRCS
qgsinterval.cpp
qgsjsonutils.cpp
qgslayerdefinition.cpp
qgslayernotesutils.cpp
qgslegendrenderer.cpp
qgslegendsettings.cpp
qgslegendstyle.cpp
Expand Down Expand Up @@ -949,6 +950,7 @@ set(QGIS_CORE_HDRS
qgsinterval.h
qgsjsonutils.h
qgslayerdefinition.h
qgslayernotesutils.h
qgslegendrenderer.h
qgslegendsettings.h
qgslegendstyle.h
Expand Down
50 changes: 50 additions & 0 deletions src/core/qgslayernotesutils.cpp
@@ -0,0 +1,50 @@
/***************************************************************************
qgslayernotesutils.cpp
--------------------------------------
Date : April 2021
Copyright : (C) 2021 by Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgslayernotesutils.h"
#include "qgsmaplayer.h"

QString QgsLayerNotesUtils::layerNotes( QgsMapLayer *layer )
{
if ( !layer )
return nullptr;

return layer->customProperty( QStringLiteral( "userNotes" ) ).toString();
}

void QgsLayerNotesUtils::setLayerNotes( QgsMapLayer *layer, const QString &notes )
{
if ( !layer )
return;

if ( notes.isEmpty() )
layer->removeCustomProperty( QStringLiteral( "userNotes" ) );
else
layer->setCustomProperty( QStringLiteral( "userNotes" ), notes );
}

bool QgsLayerNotesUtils::layerHasNotes( QgsMapLayer *layer )
{
if ( !layer )
return false;

return !layer->customProperty( QStringLiteral( "userNotes" ) ).toString().isEmpty();
}

void QgsLayerNotesUtils::removeNotes( QgsMapLayer *layer )
{
if ( layer )
layer->removeCustomProperty( QStringLiteral( "userNotes" ) );
}
60 changes: 60 additions & 0 deletions src/core/qgslayernotesutils.h
@@ -0,0 +1,60 @@
/***************************************************************************
qgslayernotesutils.h
--------------------------------------
Date : April 2021
Copyright : (C) 2021 by Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSLAYERNOTESUTILS_H
#define QGSLAYERNOTESUTILS_H

#include "qgis_core.h"
#include <QString>

class QgsMapLayer;

/**
* \ingroup core
*
* \brief Contains utility functions for working with layer notes.
*
* \since QGIS 3.20
*/
class CORE_EXPORT QgsLayerNotesUtils
{
public:

/**
* Returns the notes for the specified \a layer.
*
* The returned string is a HTML formatted set of user notations for the layer.
*/
static QString layerNotes( QgsMapLayer *layer );

/**
* Sets the \a notes for the specified \a layer, where \a notes is a HTML formatted string.
*/
static void setLayerNotes( QgsMapLayer *layer, const QString &notes );

/**
* Returns TRUE if the specified \a layer has notes available.
*/
static bool layerHasNotes( QgsMapLayer *layer );

/**
* Removes any notes for the specified \a layer.
*/
static void removeNotes( QgsMapLayer *layer );

};


#endif // QGSLAYERNOTESUTILS_H

0 comments on commit 3bdb2bb

Please sign in to comment.