Skip to content
Permalink
Browse files

[vectortiles] When importing style from QML, give option to pick

categories to import

Allows importing just symbology or just labels from a stored
vector tile style
  • Loading branch information
nyalldawson committed Sep 7, 2020
1 parent 144ef62 commit c359c9e2722b2ba4da838bf481e77592ad5a263a
@@ -26,6 +26,7 @@
#include "qgsnative.h"
#include "qgsapplication.h"
#include "qgsmetadatawidget.h"
#include "qgsmaplayerloadstyledialog.h"

#include <QFileDialog>
#include <QMenu>
@@ -48,6 +49,7 @@ QgsVectorTileLayerProperties::QgsVectorTileLayerProperties( QgsVectorTileLayer *
mOptsPage_Labeling->layout()->setContentsMargins( 0, 0, 0, 0 );

connect( this, &QDialog::accepted, this, &QgsVectorTileLayerProperties::apply );
connect( this, &QDialog::rejected, this, &QgsVectorTileLayerProperties::onCancel );
connect( buttonBox->button( QDialogButtonBox::Apply ), &QAbstractButton::clicked, this, &QgsVectorTileLayerProperties::apply );
connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsVectorTileLayerProperties::showHelp );

@@ -133,6 +135,20 @@ void QgsVectorTileLayerProperties::apply()
mMetadataWidget->acceptMetadata();
}

void QgsVectorTileLayerProperties::onCancel()
{
if ( mOldStyle.xmlData() != mLayer->styleManager()->style( mLayer->styleManager()->currentStyle() ).xmlData() )
{
// need to reset style to previous - style applied directly to the layer (not in apply())
QString myMessage;
QDomDocument doc( QStringLiteral( "qgis" ) );
int errorLine, errorColumn;
doc.setContent( mOldStyle.xmlData(), false, &myMessage, &errorLine, &errorColumn );
mLayer->importNamedStyle( doc, myMessage );
syncToLayer();
}
}

void QgsVectorTileLayerProperties::syncToLayer()
{
/*
@@ -195,31 +211,37 @@ void QgsVectorTileLayerProperties::saveDefaultStyle()

void QgsVectorTileLayerProperties::loadStyle()
{
QgsSettings settings;
QString lastUsedDir = settings.value( QStringLiteral( "style/lastStyleDir" ), QDir::homePath() ).toString();
QgsSettings settings; // where we keep last used filter in persistent state

QString fileName = QFileDialog::getOpenFileName(
this,
tr( "Load rendering setting from style file" ),
lastUsedDir,
tr( "QGIS Layer Style File" ) + " (*.qml)" );
if ( fileName.isEmpty() )
return;
QString errorMsg;
QStringList ids, names, descriptions;

// ensure the user never omits the extension from the file name
if ( !fileName.endsWith( QLatin1String( ".qml" ), Qt::CaseInsensitive ) )
fileName += QLatin1String( ".qml" );
QgsMapLayerLoadStyleDialog dlg( mLayer );

bool defaultLoadedFlag = false;
QString message = mLayer->loadNamedStyle( fileName, defaultLoadedFlag );
if ( defaultLoadedFlag )
{
settings.setValue( QStringLiteral( "style/lastStyleDir" ), QFileInfo( fileName ).absolutePath() );
syncToLayer();
}
else
if ( dlg.exec() )
{
QMessageBox::information( this, tr( "Load Style" ), message );
mOldStyle = mLayer->styleManager()->style( mLayer->styleManager()->currentStyle() );
QgsMapLayer::StyleCategories categories = dlg.styleCategories();
const QString type = dlg.fileExtension();
if ( type.compare( QLatin1String( "qml" ), Qt::CaseInsensitive ) == 0 )
{
QString message;
bool defaultLoadedFlag = false;
QString filePath = dlg.filePath();
message = mLayer->loadNamedStyle( filePath, defaultLoadedFlag, categories );

//reset if the default style was loaded OK only
if ( defaultLoadedFlag )
{
syncToLayer();
}
else
{
//let the user know what went wrong
QMessageBox::warning( this, tr( "Load Style" ), message );
}
}
activateWindow(); // set focus back to properties dialog
}
}

@@ -20,6 +20,8 @@

#include "ui_qgsvectortilelayerpropertiesbase.h"

#include "qgsmaplayerstylemanager.h"

class QgsMapLayer;
class QgsMapCanvas;
class QgsMessageBar;
@@ -37,6 +39,7 @@ class QgsVectorTileLayerProperties : public QgsOptionsDialogBase, private Ui::Qg

private slots:
void apply();
void onCancel();

void loadDefaultStyle();
void saveDefaultStyle();
@@ -68,6 +71,11 @@ class QgsVectorTileLayerProperties : public QgsOptionsDialogBase, private Ui::Qg
QgsMapCanvas *mMapCanvas = nullptr;
QgsMetadataWidget *mMetadataWidget = nullptr;

/**
* Previous layer style. Used to reset style to previous state if new style
* was loaded but dialog is canceled.
*/
QgsMapLayerStyle mOldStyle;
};

#endif // QGSVECTORTILELAYERPROPERTIES_H

0 comments on commit c359c9e

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