Skip to content

Commit baa4cbb

Browse files
authored
Merge pull request #6281 from 3nids/appmessage
allow to bring message to message bar from the reading XML when loading layers.
2 parents f271300 + 4e2eb0e commit baa4cbb

35 files changed

+463
-51
lines changed

python/core/__init__.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,28 @@ def __exit__(self, ex_type, ex_value, traceback):
210210
self.layer.rollBack()
211211
return False
212212

213+
# Python class to mimic QgsReadWriteContextCategoryPopper C++ class
214+
215+
216+
class ReadWriteContextEnterCategory():
217+
def __init__(self, context, category_name, details=None):
218+
self.context = context
219+
self.category_name = category_name
220+
self.details = details
221+
self.popper = None
222+
223+
def __enter__(self):
224+
self.popper = self.context._enterCategory(self.category_name, self.details)
225+
return self.context
226+
227+
def __exit__(self, ex_type, ex_value, traceback):
228+
del self.popper
229+
return True
230+
231+
232+
# Inject the context manager into QgsReadWriteContext class as a member
233+
QgsReadWriteContext.enterCategory = ReadWriteContextEnterCategory
234+
213235

214236
class QgsTaskWrapper(QgsTask):
215237

python/core/qgis.sip.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ typedef unsigned long long qgssize;
213213

214214

215215

216+
217+
218+
219+
216220
/************************************************************************
217221
* This file has been generated automatically from *
218222
* *

python/core/qgseditformconfig.sip.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ Set type of feature form pop-up suppression after feature creation (overrides ap
252252
%End
253253

254254

255-
void readXml( const QDomNode &node, const QgsReadWriteContext &context );
255+
void readXml( const QDomNode &node, QgsReadWriteContext &context );
256256
%Docstring
257257
Read XML information
258258
Deserialize on project load

python/core/qgslayerdefinition.sip.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ files also store the layer tree info for the exported layers, including group in
2929
%Docstring
3030
Loads the QLR at path into QGIS. New layers are added to given project into layer tree specified by rootGroup
3131
%End
32-
static bool loadLayerDefinition( QDomDocument doc, QgsProject *project, QgsLayerTreeGroup *rootGroup, QString &errorMessage /Out/, const QgsReadWriteContext &context );
32+
static bool loadLayerDefinition( QDomDocument doc, QgsProject *project, QgsLayerTreeGroup *rootGroup, QString &errorMessage /Out/, QgsReadWriteContext &context );
3333
%Docstring
3434
Loads the QLR from the XML document. New layers are added to given project into layer tree specified by rootGroup
3535
%End
@@ -54,7 +54,7 @@ This is a low-level routine that does not write layer tree.
5454
.. seealso:: :py:func:`exportLayerDefinition`
5555
%End
5656

57-
static QList<QgsMapLayer *> loadLayerDefinitionLayers( QDomDocument &document, const QgsReadWriteContext &context ) /Factory/;
57+
static QList<QgsMapLayer *> loadLayerDefinitionLayers( QDomDocument &document, QgsReadWriteContext &context ) /Factory/;
5858
%Docstring
5959
Creates new layers from a layer definition document.
6060
This is a low-level routine that does not resolve layer ID conflicts, dependencies and joins

python/core/qgsmaplayer.sip.in

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ Returns true if the layer is considered a spatial layer, ie it has some form of
452452
.. versionadded:: 2.16
453453
%End
454454

455-
bool readLayerXml( const QDomElement &layerElement, const QgsReadWriteContext &context );
455+
bool readLayerXml( const QDomElement &layerElement, QgsReadWriteContext &context );
456456
%Docstring
457457
Sets state from Dom document
458458

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

826826

827827

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

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

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

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

1364-
virtual bool readXml( const QDomNode &layer_node, const QgsReadWriteContext &context );
1364+
virtual bool readXml( const QDomNode &layer_node, QgsReadWriteContext &context );
13651365
%Docstring
13661366
Called by readLayerXML(), used by children to read state specific to them from
13671367
project files.

python/core/qgsproject.sip.in

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,20 @@ Emitted when a layer from a projects was read.
910910
:param n: number of layers
911911
%End
912912

913-
void loadingLayer( const QString & );
913+
void loadingLayer( const QString &layerName );
914+
%Docstring
915+
Emitted when a layer is loaded
916+
%End
917+
918+
void loadingLayerMessageReceived( const QString &layerName, const QList<QgsReadWriteContext::ReadWriteMessage> &messages );
919+
%Docstring
920+
Emitted when loading layers has produced some messages
921+
922+
:param layerName: the layer name
923+
:param messages: a list of pairs of Qgis.MessageLevel and messages
924+
925+
.. versionadded:: 3.2
926+
%End
914927

915928
void nonIdentifiableLayersChanged( QStringList nonIdentifiableLayers );
916929
%Docstring

python/core/qgsreadwritecontext.sip.in

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010

1111

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

26+
struct ReadWriteMessage
27+
{
28+
ReadWriteMessage( const QString &message, Qgis::MessageLevel level = Qgis::Warning, const QStringList &categories = QStringList() );
29+
%Docstring
30+
Construct a container for QgsReadWriteContext error or warning messages
31+
%End
32+
33+
QString message() const;
34+
%Docstring
35+
Return the message string
36+
%End
37+
38+
Qgis::MessageLevel level() const;
39+
%Docstring
40+
Return the message level
41+
%End
42+
43+
QStringList categories() const;
44+
%Docstring
45+
Return the stack of categories of the message
46+
%End
47+
48+
};
49+
2550
QgsReadWriteContext();
2651
%Docstring
2752
Constructor for QgsReadWriteContext.
2853
%End
2954

55+
~QgsReadWriteContext();
56+
3057
const QgsPathResolver &pathResolver() const;
3158
%Docstring
3259
Returns path resolver for conversion between relative and absolute paths
@@ -37,6 +64,63 @@ Returns path resolver for conversion between relative and absolute paths
3764
Sets up path resolver for conversion between relative and absolute paths
3865
%End
3966

67+
void pushMessage( const QString &message, Qgis::MessageLevel level = Qgis::Warning );
68+
%Docstring
69+
Append a message to the context
70+
71+
.. versionadded:: 3.2
72+
%End
73+
74+
QgsReadWriteContextCategoryPopper enterCategory( const QString &category, const QString &details = QString() ) /PyName=_enterCategory/;
75+
%Docstring
76+
Push a category to the stack
77+
78+
.. note::
79+
80+
The return value should always be used so category can be automatically left.
81+
82+
.. note::
83+
84+
It is not aimed at being used in Python. Instead use the context manager.
85+
.. code-block:: python
86+
87+
context = QgsReadWriteContext()
88+
with QgsReadWriteContext.enterCategory(context, category, details):
89+
# do something
90+
91+
.. versionadded:: 3.2
92+
%End
93+
94+
QList<QgsReadWriteContext::ReadWriteMessage> takeMessages();
95+
%Docstring
96+
Return the stored messages and remove them
97+
98+
.. versionadded:: 3.2
99+
%End
100+
101+
102+
};
103+
104+
105+
class QgsReadWriteContextCategoryPopper
106+
{
107+
%Docstring
108+
QgsReadWriteContextCategoryPopper allows entering a context category
109+
and takes care of leaving this category on deletion of the class.
110+
This would happen when it gets out of scope.
111+
112+
.. versionadded:: 3.2
113+
%End
114+
115+
%TypeHeaderCode
116+
#include "qgsreadwritecontext.h"
117+
%End
118+
public:
119+
QgsReadWriteContextCategoryPopper( QgsReadWriteContext *context );
120+
%Docstring
121+
Creates a popper
122+
%End
123+
~QgsReadWriteContextCategoryPopper();
40124
};
41125

42126
/************************************************************************

python/core/qgsvectorlayer.sip.in

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ Return the provider type for this layer
728728
virtual QString sourceName() const;
729729

730730

731-
virtual bool readXml( const QDomNode &layer_node, const QgsReadWriteContext &context );
731+
virtual bool readXml( const QDomNode &layer_node, QgsReadWriteContext &context );
732732

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

851851

852-
virtual bool readSymbology( const QDomNode &layerNode, QString &errorMessage, const QgsReadWriteContext &context );
852+
virtual bool readSymbology( const QDomNode &layerNode, QString &errorMessage, QgsReadWriteContext &context );
853853

854854
%Docstring
855855
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.
861861
:return: true in case of success.
862862
%End
863863

864-
virtual bool readStyle( const QDomNode &node, QString &errorMessage, const QgsReadWriteContext &context );
864+
virtual bool readStyle( const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context );
865865

866866
%Docstring
867867
Read the style for the current layer from the Dom node supplied.

python/core/raster/qgsrasterlayer.sip.in

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,19 +354,19 @@ Time stamp of data source in the moment when data/metadata were loaded by provid
354354
void showStatusMessage( const QString &message );
355355

356356
protected:
357-
virtual bool readSymbology( const QDomNode &node, QString &errorMessage, const QgsReadWriteContext &context );
357+
virtual bool readSymbology( const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context );
358358

359359
%Docstring
360360
Read the symbology for the current layer from the Dom node supplied
361361
%End
362362

363-
virtual bool readStyle( const QDomNode &node, QString &errorMessage, const QgsReadWriteContext &context );
363+
virtual bool readStyle( const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context );
364364

365365
%Docstring
366366
Read the style information for the current layer from the Dom node supplied
367367
%End
368368

369-
virtual bool readXml( const QDomNode &layer_node, const QgsReadWriteContext &context );
369+
virtual bool readXml( const QDomNode &layer_node, QgsReadWriteContext &context );
370370

371371
%Docstring
372372
Reads layer specific state from project file Dom node

python/gui/qgsmessagebar.sip.in

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ convenience method for pushing a message to the bar
7676
void pushMessage( const QString &title, const QString &text, Qgis::MessageLevel level = Qgis::Info, int duration = 5 );
7777
%Docstring
7878
convenience method for pushing a message with title to the bar
79+
%End
80+
81+
void pushMessage( const QString &title, const QString &text, const QString &showMore, Qgis::MessageLevel level = Qgis::Info, int duration = 5 );
82+
%Docstring
83+
convenience method for pushing a message to the bar with a detail text which be shown when pressing a "more" button
7984
%End
8085

8186
QgsMessageBarItem *currentItem();

0 commit comments

Comments
 (0)