From d421b857e535aa5de7d00de084978f2e6947fbb3 Mon Sep 17 00:00:00 2001 From: Denis Rouzaud Date: Tue, 6 Feb 2018 14:57:04 -0400 Subject: [PATCH] QgsReadWriteContext can store message app will show them in message bar when loading layers --- python/core/qgsproject.sip.in | 4 +++- python/core/qgsreadwritecontext.sip.in | 10 ++++++++++ src/app/qgisapp.cpp | 10 ++++++++++ src/app/qgisapp.h | 2 ++ src/core/qgsproject.cpp | 5 +++++ src/core/qgsproject.h | 4 +++- src/core/qgsreadwritecontext.h | 8 ++++++++ 7 files changed, 41 insertions(+), 2 deletions(-) diff --git a/python/core/qgsproject.sip.in b/python/core/qgsproject.sip.in index c96e7ab20e7..3b2cd4609fb 100644 --- a/python/core/qgsproject.sip.in +++ b/python/core/qgsproject.sip.in @@ -910,7 +910,9 @@ Emitted when a layer from a projects was read. :param n: number of layers %End - void loadingLayer( const QString & ); + void loadingLayer( const QString &layerName ); + + void loadingLayerMessages( const QString &layerName, const QList> &messages ); void nonIdentifiableLayersChanged( QStringList nonIdentifiableLayers ); %Docstring diff --git a/python/core/qgsreadwritecontext.sip.in b/python/core/qgsreadwritecontext.sip.in index a92aed4bce2..427a18518af 100644 --- a/python/core/qgsreadwritecontext.sip.in +++ b/python/core/qgsreadwritecontext.sip.in @@ -35,6 +35,16 @@ Returns path resolver for conversion between relative and absolute paths void setPathResolver( const QgsPathResolver &resolver ); %Docstring Sets up path resolver for conversion between relative and absolute paths +%End + + void pushMessage( Qgis::MessageLevel level, const QString &message ); +%Docstring +append a message to the context +%End + + QList> takeMessages(); +%Docstring +return the stored messages and remove them %End }; diff --git a/src/app/qgisapp.cpp b/src/app/qgisapp.cpp index 9b8725fabd8..8cd799de1dd 100644 --- a/src/app/qgisapp.cpp +++ b/src/app/qgisapp.cpp @@ -3230,6 +3230,8 @@ void QgisApp::setupConnections() this, &QgisApp::showProgress ); connect( QgsProject::instance(), &QgsProject::loadingLayer, this, &QgisApp::showStatusMessage ); + connect( QgsProject::instance(), &QgsProject::loadingLayerMessages, + this, &QgisApp::loadingLayerMessages ); connect( QgsProject::instance(), &QgsProject::readProject, this, &QgisApp::readProject ); connect( QgsProject::instance(), &QgsProject::writeProject, @@ -11238,6 +11240,14 @@ void QgisApp::showStatusMessage( const QString &message ) mStatusBar->showMessage( message ); } +void QgisApp::loadingLayerMessages( const QString &layerName, const QList > &messages ) +{ + for ( const auto message : messages ) + { + messageBar()->pushMessage( layerName, message.second, message.first ); + } +} + void QgisApp::displayMapToolMessage( const QString &message, Qgis::MessageLevel level ) { // remove previous message diff --git a/src/app/qgisapp.h b/src/app/qgisapp.h index c82d1b580c9..db5da5a3c3e 100644 --- a/src/app/qgisapp.h +++ b/src/app/qgisapp.h @@ -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> &messages ); + //! set the active layer bool setActiveLayer( QgsMapLayer * ); diff --git a/src/core/qgsproject.cpp b/src/core/qgsproject.cpp index c0a82c9bccf..0c1c55f9c21 100644 --- a/src/core/qgsproject.cpp +++ b/src/core/qgsproject.cpp @@ -716,6 +716,11 @@ bool QgsProject::_getMapLayers( const QDomDocument &doc, QList &broken { returnStatus = false; } + const auto messages = context.takeMessages(); + if ( messages.count() ) + { + emit loadingLayerMessages( tr( "Loading layer %1" ).arg( name ), messages ); + } } emit layerLoaded( i + 1, nl.count() ); i++; diff --git a/src/core/qgsproject.h b/src/core/qgsproject.h index ce3bcd52c59..e3d8f88bd1b 100644 --- a/src/core/qgsproject.h +++ b/src/core/qgsproject.h @@ -887,7 +887,9 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera */ void layerLoaded( int i, int n ); - void loadingLayer( const QString & ); + void loadingLayer( const QString &layerName ); + + void loadingLayerMessages( const QString &layerName, const QList> &messages ); //! Emitted when the list of layer which are excluded from map identification changes void nonIdentifiableLayersChanged( QStringList nonIdentifiableLayers ); diff --git a/src/core/qgsreadwritecontext.h b/src/core/qgsreadwritecontext.h index 8fb408db745..296dc74c835 100644 --- a/src/core/qgsreadwritecontext.h +++ b/src/core/qgsreadwritecontext.h @@ -19,6 +19,7 @@ #define QGSREADWRITECONTEXT_H #include "qgspathresolver.h" +#include "qgis.h" /** * \class QgsReadWriteContext @@ -41,8 +42,15 @@ class CORE_EXPORT QgsReadWriteContext //! Sets up path resolver for conversion between relative and absolute paths void setPathResolver( const QgsPathResolver &resolver ) { mPathResolver = resolver; } + //! append a message to the context + void pushMessage( Qgis::MessageLevel level, const QString &message ) {mMessages.append( qMakePair( level, message ) );} + + //! return the stored messages and remove them + QList> takeMessages() {return mMessages; mMessages.clear();} + private: QgsPathResolver mPathResolver; + QList> mMessages; }; #endif // QGSREADWRITECONTEXT_H