Skip to content
Permalink
Browse files

Merge pull request #6281 from 3nids/appmessage

allow to bring message to message bar from the reading XML when loading layers.
  • Loading branch information
3nids committed Feb 28, 2018
2 parents f271300 + 4e2eb0e commit baa4cbb020cc6851ddabb22d300e46caa6deb601
@@ -210,6 +210,28 @@ def __exit__(self, ex_type, ex_value, traceback):
self.layer.rollBack()
return False

# Python class to mimic QgsReadWriteContextCategoryPopper C++ class


class ReadWriteContextEnterCategory():
def __init__(self, context, category_name, details=None):
self.context = context
self.category_name = category_name
self.details = details
self.popper = None

def __enter__(self):
self.popper = self.context._enterCategory(self.category_name, self.details)
return self.context

def __exit__(self, ex_type, ex_value, traceback):
del self.popper
return True


# Inject the context manager into QgsReadWriteContext class as a member
QgsReadWriteContext.enterCategory = ReadWriteContextEnterCategory


class QgsTaskWrapper(QgsTask):

@@ -213,6 +213,10 @@ typedef unsigned long long qgssize;







/************************************************************************
* This file has been generated automatically from *
* *
@@ -252,7 +252,7 @@ Set type of feature form pop-up suppression after feature creation (overrides ap
%End


void readXml( const QDomNode &node, const QgsReadWriteContext &context );
void readXml( const QDomNode &node, QgsReadWriteContext &context );
%Docstring
Read XML information
Deserialize on project load
@@ -29,7 +29,7 @@ files also store the layer tree info for the exported layers, including group in
%Docstring
Loads the QLR at path into QGIS. New layers are added to given project into layer tree specified by rootGroup
%End
static bool loadLayerDefinition( QDomDocument doc, QgsProject *project, QgsLayerTreeGroup *rootGroup, QString &errorMessage /Out/, const QgsReadWriteContext &context );
static bool loadLayerDefinition( QDomDocument doc, QgsProject *project, QgsLayerTreeGroup *rootGroup, QString &errorMessage /Out/, QgsReadWriteContext &context );
%Docstring
Loads the QLR from the XML document. New layers are added to given project into layer tree specified by rootGroup
%End
@@ -54,7 +54,7 @@ This is a low-level routine that does not write layer tree.
.. seealso:: :py:func:`exportLayerDefinition`
%End

static QList<QgsMapLayer *> loadLayerDefinitionLayers( QDomDocument &document, const QgsReadWriteContext &context ) /Factory/;
static QList<QgsMapLayer *> loadLayerDefinitionLayers( QDomDocument &document, QgsReadWriteContext &context ) /Factory/;
%Docstring
Creates new layers from a layer definition document.
This is a low-level routine that does not resolve layer ID conflicts, dependencies and joins
@@ -452,7 +452,7 @@ Returns true if the layer is considered a spatial layer, ie it has some form of
.. versionadded:: 2.16
%End

bool readLayerXml( const QDomElement &layerElement, const QgsReadWriteContext &context );
bool readLayerXml( const QDomElement &layerElement, QgsReadWriteContext &context );
%Docstring
Sets state from Dom document

@@ -825,7 +825,7 @@ the SLD file could not be loaded



virtual bool readSymbology( const QDomNode &node, QString &errorMessage, const QgsReadWriteContext &context ) = 0;
virtual bool readSymbology( const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context ) = 0;
%Docstring
Read the symbology for the current layer from the Dom node supplied.

@@ -836,7 +836,7 @@ Read the symbology for the current layer from the Dom node supplied.
:return: true in case of success.
%End

virtual bool readStyle( const QDomNode &node, QString &errorMessage, const QgsReadWriteContext &context );
virtual bool readStyle( const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context );
%Docstring
Read the style for the current layer from the Dom node supplied.

@@ -1361,7 +1361,7 @@ Set the extent
Set whether layer is valid or not - should be used in constructor.
%End

virtual bool readXml( const QDomNode &layer_node, const QgsReadWriteContext &context );
virtual bool readXml( const QDomNode &layer_node, QgsReadWriteContext &context );
%Docstring
Called by readLayerXML(), used by children to read state specific to them from
project files.
@@ -910,7 +910,20 @@ Emitted when a layer from a projects was read.
:param n: number of layers
%End

void loadingLayer( const QString & );
void loadingLayer( const QString &layerName );
%Docstring
Emitted when a layer is loaded
%End

void loadingLayerMessageReceived( const QString &layerName, const QList<QgsReadWriteContext::ReadWriteMessage> &messages );
%Docstring
Emitted when loading layers has produced some messages

:param layerName: the layer name
:param messages: a list of pairs of Qgis.MessageLevel and messages

.. versionadded:: 3.2
%End

void nonIdentifiableLayersChanged( QStringList nonIdentifiableLayers );
%Docstring
@@ -9,6 +9,7 @@




class QgsReadWriteContext
{
%Docstring
@@ -22,11 +23,37 @@ The class is used as a container of context for various read/write operations on
%End
public:

struct ReadWriteMessage
{
ReadWriteMessage( const QString &message, Qgis::MessageLevel level = Qgis::Warning, const QStringList &categories = QStringList() );
%Docstring
Construct a container for QgsReadWriteContext error or warning messages
%End

QString message() const;
%Docstring
Return the message string
%End

Qgis::MessageLevel level() const;
%Docstring
Return the message level
%End

QStringList categories() const;
%Docstring
Return the stack of categories of the message
%End

};

QgsReadWriteContext();
%Docstring
Constructor for QgsReadWriteContext.
%End

~QgsReadWriteContext();

const QgsPathResolver &pathResolver() const;
%Docstring
Returns path resolver for conversion between relative and absolute paths
@@ -37,6 +64,63 @@ Returns path resolver for conversion between relative and absolute paths
Sets up path resolver for conversion between relative and absolute paths
%End

void pushMessage( const QString &message, Qgis::MessageLevel level = Qgis::Warning );
%Docstring
Append a message to the context

.. versionadded:: 3.2
%End

QgsReadWriteContextCategoryPopper enterCategory( const QString &category, const QString &details = QString() ) /PyName=_enterCategory/;
%Docstring
Push a category to the stack

.. note::

The return value should always be used so category can be automatically left.

.. note::

It is not aimed at being used in Python. Instead use the context manager.
.. code-block:: python

context = QgsReadWriteContext()
with QgsReadWriteContext.enterCategory(context, category, details):
# do something

.. versionadded:: 3.2
%End

QList<QgsReadWriteContext::ReadWriteMessage> takeMessages();
%Docstring
Return the stored messages and remove them

.. versionadded:: 3.2
%End


};


class QgsReadWriteContextCategoryPopper
{
%Docstring
QgsReadWriteContextCategoryPopper allows entering a context category
and takes care of leaving this category on deletion of the class.
This would happen when it gets out of scope.

.. versionadded:: 3.2
%End

%TypeHeaderCode
#include "qgsreadwritecontext.h"
%End
public:
QgsReadWriteContextCategoryPopper( QgsReadWriteContext *context );
%Docstring
Creates a popper
%End
~QgsReadWriteContextCategoryPopper();
};

/************************************************************************
@@ -728,7 +728,7 @@ Return the provider type for this layer
virtual QString sourceName() const;


virtual bool readXml( const QDomNode &layer_node, const QgsReadWriteContext &context );
virtual bool readXml( const QDomNode &layer_node, QgsReadWriteContext &context );

%Docstring
Reads vector layer specific state from project file Dom node.
@@ -849,7 +849,7 @@ Returns the current auxiliary layer.
%End


virtual bool readSymbology( const QDomNode &layerNode, QString &errorMessage, const QgsReadWriteContext &context );
virtual bool readSymbology( const QDomNode &layerNode, QString &errorMessage, QgsReadWriteContext &context );

%Docstring
Read the symbology for the current layer from the Dom node supplied.
@@ -861,7 +861,7 @@ Read the symbology for the current layer from the Dom node supplied.
:return: true in case of success.
%End

virtual bool readStyle( const QDomNode &node, QString &errorMessage, const QgsReadWriteContext &context );
virtual bool readStyle( const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context );

%Docstring
Read the style for the current layer from the Dom node supplied.
@@ -354,19 +354,19 @@ Time stamp of data source in the moment when data/metadata were loaded by provid
void showStatusMessage( const QString &message );

protected:
virtual bool readSymbology( const QDomNode &node, QString &errorMessage, const QgsReadWriteContext &context );
virtual bool readSymbology( const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context );

%Docstring
Read the symbology for the current layer from the Dom node supplied
%End

virtual bool readStyle( const QDomNode &node, QString &errorMessage, const QgsReadWriteContext &context );
virtual bool readStyle( const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context );

%Docstring
Read the style information for the current layer from the Dom node supplied
%End

virtual bool readXml( const QDomNode &layer_node, const QgsReadWriteContext &context );
virtual bool readXml( const QDomNode &layer_node, QgsReadWriteContext &context );

%Docstring
Reads layer specific state from project file Dom node
@@ -76,6 +76,11 @@ convenience method for pushing a message to the bar
void pushMessage( const QString &title, const QString &text, Qgis::MessageLevel level = Qgis::Info, int duration = 5 );
%Docstring
convenience method for pushing a message with title to the bar
%End

void pushMessage( const QString &title, const QString &text, const QString &showMore, Qgis::MessageLevel level = Qgis::Info, int duration = 5 );
%Docstring
convenience method for pushing a message to the bar with a detail text which be shown when pressing a "more" button
%End

QgsMessageBarItem *currentItem();
@@ -847,6 +847,8 @@ sub detect_non_method_member{
$LINE =~ s/^(\s*template\s*<)(?:class|typename) (\w+>)(.*)$/$1$2$3/;
$LINE =~ s/\s*\boverride\b//;
$LINE =~ s/\s*\bextern \b//;
$LINE =~ s/\s*\bMAYBE_UNUSED \b//;
$LINE =~ s/\s*\bNODISCARD \b//;
$LINE =~ s/^(\s*)?(const )?(virtual |static )?inline /$1$2$3/;
$LINE =~ s/\bconstexpr\b/const/;
$LINE =~ s/\bnullptr\b/0/g;
@@ -3230,6 +3230,8 @@ void QgisApp::setupConnections()
this, &QgisApp::showProgress );
connect( QgsProject::instance(), &QgsProject::loadingLayer,
this, &QgisApp::showStatusMessage );
connect( QgsProject::instance(), &QgsProject::loadingLayerMessageReceived,
this, &QgisApp::loadingLayerMessages );
connect( QgsProject::instance(), &QgsProject::readProject,
this, &QgisApp::readProject );
connect( QgsProject::instance(), &QgsProject::writeProject,
@@ -8928,8 +8930,10 @@ void QgisApp::duplicateVectorStyle( QgsVectorLayer *srcLayer, QgsVectorLayer *de
rootNode.setAttribute( QStringLiteral( "version" ), Qgis::QGIS_VERSION );
doc.appendChild( rootNode );
QString errorMsg;
srcLayer->writeSymbology( rootNode, doc, errorMsg, QgsReadWriteContext() );
destLayer->readSymbology( rootNode, errorMsg, QgsReadWriteContext() );
QgsReadWriteContext writeContext = QgsReadWriteContext();
srcLayer->writeSymbology( rootNode, doc, errorMsg, writeContext );
QgsReadWriteContext readContext = QgsReadWriteContext();
destLayer->readSymbology( rootNode, errorMsg, readContext );
}
}

@@ -11236,6 +11240,14 @@ void QgisApp::showStatusMessage( const QString &message )
mStatusBar->showMessage( message );
}

void QgisApp::loadingLayerMessages( const QString &layerName, const QList<QgsReadWriteContext::ReadWriteMessage> &messages )
{
for ( const auto message : messages )
{
messageBar()->pushMessage( layerName, message.message(), message.categories().join( '\n' ), message.level() );
}
}

void QgisApp::displayMapToolMessage( const QString &message, Qgis::MessageLevel level )
{
// remove previous message
@@ -926,6 +926,8 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
void showProgress( int progress, int totalSteps );
void showStatusMessage( const QString &message );

void loadingLayerMessages( const QString &layerName, const QList<QgsReadWriteContext::ReadWriteMessage> &messages );

//! set the active layer
bool setActiveLayer( QgsMapLayer * );

@@ -542,14 +542,16 @@ QgsMapLayerStyleCommand::QgsMapLayerStyleCommand( QgsMapLayer *layer, const QStr
void QgsMapLayerStyleCommand::undo()
{
QString error;
mLayer->readStyle( mLastState, error, QgsReadWriteContext() );
QgsReadWriteContext context = QgsReadWriteContext();
mLayer->readStyle( mLastState, error, context );
mLayer->triggerRepaint();
}

void QgsMapLayerStyleCommand::redo()
{
QString error;
mLayer->readStyle( mXml, error, QgsReadWriteContext() );
QgsReadWriteContext context = QgsReadWriteContext();
mLayer->readStyle( mXml, error, context );
mLayer->triggerRepaint();
}

0 comments on commit baa4cbb

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