Skip to content
Permalink
Browse files

Show warnings generated during mapbox gl style conversion after loadi…

…ng vector tiles with default styles
  • Loading branch information
nyalldawson authored and nirvn committed Sep 15, 2020
1 parent 01f942f commit bd9c8a7621554c2a1fa49a7b058a17f539808f45
@@ -100,6 +100,8 @@ Constructs a new vector tile layer

virtual QString loadDefaultStyle( bool &resultFlag /Out/ );



virtual QString loadDefaultMetadata( bool &resultFlag /Out/ );


@@ -2038,6 +2038,24 @@ void QgisApp::handleDropUriList( const QgsMimeDataUtils::UriList &lst )

QgsScopedProxyProgressTask task( tr( "Loading layers" ) );


auto showLayerLoadWarnings = [ = ]( const QString & title, const QString & shortMessage, const QString & longMessage, Qgis::MessageLevel level )
{
QgsMessageBarItem *messageWidget = visibleMessageBar()->createMessage( title, shortMessage );
QPushButton *detailsButton = new QPushButton( tr( "Details" ) );
connect( detailsButton, &QPushButton::clicked, this, [ = ]
{
if ( QgsMessageViewer *dialog = dynamic_cast< QgsMessageViewer * >( QgsMessageOutput::createMessageOutput() ) )
{
dialog->setTitle( title );
dialog->setMessage( longMessage, QgsMessageOutput::MessageHtml );
dialog->showMessage();
}
} );
messageWidget->layout()->addWidget( detailsButton );
return visibleMessageBar()->pushWidget( messageWidget, level, 0 );
};

// insert items in reverse order as each one is inserted on top of previous one
int count = 0;
for ( int i = lst.size() - 1 ; i >= 0 ; i--, count++ )
@@ -2062,8 +2080,27 @@ void QgisApp::handleDropUriList( const QgsMimeDataUtils::UriList &lst )
{
QgsVectorTileLayer *layer = new QgsVectorTileLayer( uri, u.name );
bool ok = false;
layer->loadDefaultStyle( ok );
layer->loadDefaultMetadata( ok );

QString error;
QStringList warnings;
bool res = layer->loadDefaultStyle( error, warnings );
if ( res && !warnings.empty() )
{
QString message = QStringLiteral( "<p>%1</p>" ).arg( tr( "The following warnings were generated while converting the LYR file:" ) );
message += QStringLiteral( "<ul>" );

std::sort( warnings.begin(), warnings.end() );
warnings.erase( std::unique( warnings.begin(), warnings.end() ), warnings.end() );

for ( const QString &w : qgis::as_const( warnings ) )
{
message += QStringLiteral( "<li>%1</li>" ).arg( w.toHtmlEscaped().replace( '\n', QStringLiteral( "<br>" ) ) );
}
message += QStringLiteral( "</ul>" );
showLayerLoadWarnings( tr( "Vector tiles" ), tr( "Style could not be completely converted" ),
message, Qgis::Warning );
}
addMapLayer( layer );
}
else if ( u.layerType == QLatin1String( "plugin" ) )
@@ -294,6 +294,14 @@ void QgsVectorTileLayer::setTransformContext( const QgsCoordinateTransformContex
}

QString QgsVectorTileLayer::loadDefaultStyle( bool &resultFlag )
{
QString error;
QStringList warnings;
resultFlag = loadDefaultStyle( error, warnings );
return error;
}

bool QgsVectorTileLayer::loadDefaultStyle( QString &error, QStringList &warnings )
{
QgsDataSourceUri dsUri;
dsUri.setEncodedUri( mDataSource );
@@ -325,8 +333,8 @@ QString QgsVectorTileLayer::loadDefaultStyle( bool &resultFlag )
case QgsBlockingNetworkRequest::NetworkError:
case QgsBlockingNetworkRequest::TimeoutError:
case QgsBlockingNetworkRequest::ServerExceptionError:
resultFlag = false;
return QObject::tr( "Error retrieving default style" );
error = QObject::tr( "Error retrieving default style" );
return false;
}

const QgsNetworkReplyContent content = networkRequest.reply();
@@ -403,20 +411,21 @@ QString QgsVectorTileLayer::loadDefaultStyle( bool &resultFlag )
QgsMapBoxGlStyleConverter converter;
if ( converter.convert( styleDefinition, &context ) != QgsMapBoxGlStyleConverter::Success )
{
resultFlag = false;
return converter.errorMessage();
warnings = converter.warnings();
error = converter.errorMessage();
return false;
}

setRenderer( converter.renderer() );
setLabeling( converter.labeling() );
resultFlag = true;
return QString();
warnings = converter.warnings();
return true;
}
else
{
QgsMapLayer::loadDefaultStyle( resultFlag );
resultFlag = true;
return QString();
bool resultFlag = false;
error = QgsMapLayer::loadDefaultStyle( resultFlag );
return resultFlag;
}
}

@@ -107,6 +107,20 @@ class CORE_EXPORT QgsVectorTileLayer : public QgsMapLayer

void setTransformContext( const QgsCoordinateTransformContext &transformContext ) override;
QString loadDefaultStyle( bool &resultFlag SIP_OUT ) override;

/**
* Loads the default style for the layer, and returns TRUE if the style was
* sucessfully loaded.
*
* The \a error string will be filled with a translated error message if an error
* occurs during the style load. The \a warnings list will be populated with any
* warning messages generated during the style load (e.g. default style properties
* which could not be converted).
*
* \since QGIS 3.16
*/
bool loadDefaultStyle( QString &error, QStringList &warnings ) SIP_SKIP;

QString loadDefaultMetadata( bool &resultFlag SIP_OUT ) override;

QString encodedSource( const QString &source, const QgsReadWriteContext &context ) const FINAL;

0 comments on commit bd9c8a7

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