Skip to content

Commit

Permalink
[FEATURE][vectortiles] Allow users to load MapBox GL Json style
Browse files Browse the repository at this point in the history
configuration files when importing style for vector tile layers

From the standard layer properties -> Load Style dialog users
are now given the option of selecting a JSON MapBox GL style
file.
  • Loading branch information
nyalldawson committed Sep 7, 2020
1 parent ff883e0 commit 77492b1
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 10 deletions.
41 changes: 40 additions & 1 deletion src/app/vectortile/qgsvectortilelayerproperties.cpp
Expand Up @@ -27,7 +27,7 @@
#include "qgsapplication.h"
#include "qgsmetadatawidget.h"
#include "qgsmaplayerloadstyledialog.h"

#include "qgsmapboxglstyleconverter.h"
#include <QFileDialog>
#include <QMenu>
#include <QMessageBox>
Expand Down Expand Up @@ -241,6 +241,45 @@ void QgsVectorTileLayerProperties::loadStyle()
QMessageBox::warning( this, tr( "Load Style" ), message );
}
}
else if ( type.compare( QLatin1String( "json" ), Qt::CaseInsensitive ) == 0 )
{
QFile file( dlg.filePath() );
if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
{
QMessageBox::warning( this, tr( "Load Style" ), tr( "Could not read %1" ).arg( QDir::toNativeSeparators( dlg.filePath() ) ) );
}
else
{
QTextStream in( &file );
const QString content = in.readAll();

QgsMapBoxGlStyleConversionContext context;
// convert automatically from pixel sizes to millimeters, because pixel sizes
// are a VERY edge case in QGIS and don't play nice with hidpi map renders or print layouts
context.setTargetUnit( QgsUnitTypes::RenderMillimeters );
//assume source uses 96 dpi
context.setPixelSizeConversionFactor( 25.4 / 96.0 );

QgsMapBoxGlStyleConverter converter;

if ( converter.convert( content, &context ) != QgsMapBoxGlStyleConverter::Success )
{
QMessageBox::warning( this, tr( "Load Style" ), converter.errorMessage() );
}
else
{
if ( dlg.styleCategories().testFlag( QgsMapLayer::StyleCategory::Symbology ) )
{
mLayer->setRenderer( converter.renderer() );
}
if ( dlg.styleCategories().testFlag( QgsMapLayer::StyleCategory::Labeling ) )
{
mLayer->setLabeling( converter.labeling() );
}
syncToLayer();
}
}
}
activateWindow(); // set focus back to properties dialog
}
}
Expand Down
46 changes: 37 additions & 9 deletions src/gui/qgsmaplayerloadstyledialog.cpp
Expand Up @@ -58,8 +58,9 @@ QgsMapLayerLoadStyleDialog::QgsMapLayerLoadStyleDialog( QgsMapLayer *layer, QWid
connect( mStyleTypeComboBox, qgis::overload<int>::of( &QComboBox::currentIndexChanged ), this, [ = ]( int )
{
QgsVectorLayerProperties::StyleType type = currentStyleType();
mFromFileWidget->setVisible( type != QgsVectorLayerProperties::StyleType::DB );
if ( QgsVectorLayer *vl = qobject_cast< QgsVectorLayer * >( mLayer ) )
QgsVectorLayer *vl = qobject_cast< QgsVectorLayer * >( mLayer );
mFromFileWidget->setVisible( !vl || type != QgsVectorLayerProperties::StyleType::DB );
if ( vl )
{
mFromDbWidget->setVisible( type == QgsVectorLayerProperties::StyleType::DB );
mDeleteButton->setVisible( type == QgsVectorLayerProperties::StyleType::DB && vl->dataProvider()->isDeleteStyleFromDatabaseSupported() );
Expand All @@ -70,7 +71,7 @@ QgsMapLayerLoadStyleDialog::QgsMapLayerLoadStyleDialog( QgsMapLayer *layer, QWid
mDeleteButton->setVisible( false );
}

mStyleCategoriesListView->setEnabled( currentStyleType() != QgsVectorLayerProperties::StyleType::SLD );
mStyleCategoriesListView->setEnabled( !vl || currentStyleType() != QgsVectorLayerProperties::StyleType::SLD );
updateLoadButtonState();
} );
mStyleTypeComboBox->addItem( tr( "From File" ), QgsVectorLayerProperties::QML ); // QML is used as entry, but works for SLD too, see currentStyleType()
Expand All @@ -94,12 +95,32 @@ QgsMapLayerLoadStyleDialog::QgsMapLayerLoadStyleDialog( QgsMapLayer *layer, QWid
mStyleCategoriesListView->setModel( mModel );

// load from file setup
mFileWidget->setFilter( tr( "QGIS Layer Style File, SLD File" ) + QStringLiteral( " (*.qml *.sld)" ) );
switch ( mLayer->type() )
{
case QgsMapLayerType::VectorLayer:
mFileWidget->setFilter( tr( "QGIS Layer Style File, SLD File" ) + QStringLiteral( " (*.qml *.sld)" ) );
break;

case QgsMapLayerType::VectorTileLayer:
mFileWidget->setFilter( tr( "All Styles" ) + QStringLiteral( " (*.qml *.json);;" )
+ tr( "QGIS Layer Style File" ) + QStringLiteral( " (*.qml);;" )
+ tr( "MapBox GL Style JSON File" ) + QStringLiteral( " (*.json)" ) );
break;

case QgsMapLayerType::RasterLayer:
case QgsMapLayerType::MeshLayer:
case QgsMapLayerType::AnnotationLayer:
case QgsMapLayerType::PluginLayer:
break;

}

mFileWidget->setStorageMode( QgsFileWidget::GetFile );
mFileWidget->setDefaultRoot( myLastUsedDir );
connect( mFileWidget, &QgsFileWidget::fileChanged, this, [ = ]( const QString & path )
{
mStyleCategoriesListView->setEnabled( currentStyleType() != QgsVectorLayerProperties::SLD );
QgsVectorLayer *vl = qobject_cast< QgsVectorLayer * >( mLayer );
mStyleCategoriesListView->setEnabled( !vl || currentStyleType() != QgsVectorLayerProperties::SLD );
QgsSettings settings;
QFileInfo tmplFileInfo( path );
settings.setValue( QStringLiteral( "style/lastStyleDir" ), tmplFileInfo.absolutePath() );
Expand Down Expand Up @@ -317,10 +338,17 @@ void QgsMapLayerLoadStyleDialog::deleteStyleFromDB()
void QgsMapLayerLoadStyleDialog::updateLoadButtonState()
{
QgsVectorLayerProperties::StyleType type = currentStyleType();
mLoadButton->setEnabled( ( type == QgsVectorLayerProperties::DB
&& ( mRelatedTable->selectionModel()->hasSelection() || mOthersTable->selectionModel()->hasSelection()
) ) ||
( type != QgsVectorLayerProperties::DB && !mFileWidget->filePath().isEmpty() ) );
if ( mLayer->type() == QgsMapLayerType::VectorLayer )
{
mLoadButton->setEnabled( ( type == QgsVectorLayerProperties::DB
&& ( mRelatedTable->selectionModel()->hasSelection() || mOthersTable->selectionModel()->hasSelection()
) ) ||
( type != QgsVectorLayerProperties::DB && !mFileWidget->filePath().isEmpty() ) );
}
else
{
mLoadButton->setEnabled( !mFileWidget->filePath().isEmpty() );
}
}

void QgsMapLayerLoadStyleDialog::showHelp()
Expand Down

0 comments on commit 77492b1

Please sign in to comment.