Skip to content
Permalink
Browse files

create dialog for saving different styles

  • Loading branch information
3nids authored and nyalldawson committed Sep 14, 2018
1 parent 20a70ee commit cb9774022f91cd6fb2055cddb575689a62af3835
@@ -110,9 +110,9 @@ QgsMeshLayer cannot be copied.
virtual QgsMapLayerRenderer *createMapRenderer( QgsRenderContext &rendererContext ) /Factory/;

virtual bool readSymbology( const QDomNode &node, QString &errorMessage,
QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllCategories );
QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories );
virtual bool writeSymbology( QDomNode &node, QDomDocument &doc, QString &errorMessage,
const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllCategories ) const;
const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories ) const;
virtual QString encodedSource( const QString &source, const QgsReadWriteContext &context ) const;

virtual QString decodedSource( const QString &source, const QString &provider, const QgsReadWriteContext &context ) const;
@@ -89,7 +89,7 @@ This is the base class for all map layer types (vector, raster).
AttributeTable,
Rendering,
CustomProperties,
AllCategories
AllStyleCategories
};
typedef QFlags<QgsMapLayer::StyleCategory> StyleCategories;

@@ -824,7 +824,7 @@ Retrieve a named style for this layer from a sqlite database.
%End

virtual bool importNamedStyle( QDomDocument &doc, QString &errorMsg /Out/,
QgsMapLayer::StyleCategories categories = QgsMapLayer::AllCategories );
QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories );
%Docstring
Import the properties of this layer from a QDomDocument

@@ -838,13 +838,14 @@ Import the properties of this layer from a QDomDocument
.. versionadded:: 2.8
%End

virtual void exportNamedStyle( QDomDocument &doc, QString &errorMsg /Out/, QgsReadWriteContext &context,
QgsMapLayer::StyleCategories categories = QgsMapLayer::AllCategories ) const;
virtual void exportNamedStyle( QDomDocument &doc, QString &errorMsg /Out/, const QgsReadWriteContext &context = QgsReadWriteContext(),
QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories ) const;
%Docstring
Export the properties of this layer as named style in a QDomDocument

:param doc: the target QDomDocument
:param errorMsg: this QString will be initialized on error
:param context: read write context
:param categories: the style categories to export
during the execution of writeSymbology
%End
@@ -923,25 +924,27 @@ Attempts to style the layer using the formatting from an SLD type file.


virtual bool readSymbology( const QDomNode &node, QString &errorMessage,
QgsReadWriteContext &context, StyleCategories categories = AllCategories ) = 0;
QgsReadWriteContext &context, StyleCategories categories = AllStyleCategories ) = 0;
%Docstring
Read the symbology for the current layer from the DOM node supplied.

:param node: node that will contain the symbology definition for this layer.
:param errorMessage: reference to string that will be updated with any error messages
:param context: reading context (used for transform from relative to absolute paths)
:param categories: the style categories to be read

:return: true in case of success.
%End

virtual bool readStyle( const QDomNode &node, QString &errorMessage,
QgsReadWriteContext &context, StyleCategories categories = AllCategories );
QgsReadWriteContext &context, StyleCategories categories = AllStyleCategories );
%Docstring
Read the style for the current layer from the DOM node supplied.

:param node: node that will contain the style definition for this layer.
:param errorMessage: reference to string that will be updated with any error messages
:param context: reading context (used for transform from relative to absolute paths)
:param categories: the style categories to be read

:return: true in case of success.

@@ -953,14 +956,15 @@ Read the style for the current layer from the DOM node supplied.
%End

virtual bool writeSymbology( QDomNode &node, QDomDocument &doc, QString &errorMessage, const QgsReadWriteContext &context,
StyleCategories categories = AllCategories ) const = 0;
StyleCategories categories = AllStyleCategories ) const = 0;
%Docstring
Write the style for the layer into the docment provided.

:param node: the node that will have the style element added to it.
:param doc: the document that will have the QDomNode added.
:param errorMessage: reference to string that will be updated with any error messages
:param context: writing context (used for transform from absolute to relative paths)
:param categories: the style categories to be written

.. note::

@@ -970,14 +974,15 @@ Write the style for the layer into the docment provided.
%End

virtual bool writeStyle( QDomNode &node, QDomDocument &doc, QString &errorMessage, const QgsReadWriteContext &context,
StyleCategories categories = AllCategories ) const;
StyleCategories categories = AllStyleCategories ) const;
%Docstring
Write just the symbology information for the layer into the document

:param node: the node that will have the style element added to it.
:param doc: the document that will have the QDomNode added.
:param errorMessage: reference to string that will be updated with any error messages
:param context: writing context (used for transform from absolute to relative paths)
:param categories: the style categories to be written

:return: true in case of success.

@@ -1546,15 +1551,15 @@ Write style manager's configuration (if exists). To be called by subclasses.

void writeCommonStyle( QDomElement &layerElement, QDomDocument &document,
const QgsReadWriteContext &context,
StyleCategories categories = AllCategories ) const;
StyleCategories categories = AllStyleCategories ) const;
%Docstring
Write style data common to all layer types

.. versionadded:: 3.0
%End

void readCommonStyle( const QDomElement &layerElement, const QgsReadWriteContext &context,
StyleCategories categories = AllCategories );
StyleCategories categories = AllStyleCategories );
%Docstring
Read style data common to all layer types

@@ -871,7 +871,7 @@ Returns the current auxiliary layer.


virtual bool readSymbology( const QDomNode &layerNode, QString &errorMessage,
QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllCategories );
QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories );
%Docstring
Read the symbology for the current layer from the Dom node supplied.

@@ -883,7 +883,7 @@ Read the symbology for the current layer from the Dom node supplied.
%End

virtual bool readStyle( const QDomNode &node, QString &errorMessage,
QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllCategories );
QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories );
%Docstring
Read the style for the current layer from the Dom node supplied.

@@ -895,7 +895,7 @@ Read the style for the current layer from the Dom node supplied.
%End

virtual bool writeSymbology( QDomNode &node, QDomDocument &doc, QString &errorMessage,
const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllCategories ) const;
const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories ) const;
%Docstring
Write the symbology for the layer into the docment provided.

@@ -908,7 +908,7 @@ Write the symbology for the layer into the docment provided.
%End

virtual bool writeStyle( QDomNode &node, QDomDocument &doc, QString &errorMessage,
const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllCategories ) const;
const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories ) const;
%Docstring
Write just the style information for the layer into the document

@@ -348,16 +348,16 @@ Draws a preview of the rasterlayer into a QImage
void showStatusMessage( const QString &message );

protected:
virtual bool readSymbology( const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllCategories );
virtual bool readSymbology( const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories );

virtual bool readStyle( const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllCategories );
virtual bool readStyle( const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories );

virtual bool readXml( const QDomNode &layer_node, QgsReadWriteContext &context );

virtual bool writeSymbology( QDomNode &, QDomDocument &doc, QString &errorMessage,
const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllCategories ) const;
const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories ) const;
virtual bool writeStyle( QDomNode &node, QDomDocument &doc, QString &errorMessage,
const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllCategories ) const;
const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories ) const;
virtual bool writeXml( QDomNode &layer_node, QDomDocument &doc, const QgsReadWriteContext &context ) const;

virtual QString encodedSource( const QString &source, const QgsReadWriteContext &context ) const;
@@ -138,6 +138,7 @@ SET(QGIS_APP_SRCS
qgssvgannotationdialog.cpp
qgsundowidget.cpp
qgsvectorlayerlegendwidget.cpp
qgsvectorlayerloadsavestyledialog.cpp
qgsvectorlayerproperties.cpp
qgsmapthemes.cpp
qgshandlebadlayers.cpp
@@ -377,6 +378,7 @@ SET (QGIS_APP_MOC_HDRS
qgstextannotationdialog.h
qgsundowidget.h
qgsvectorlayerlegendwidget.h
qgsvectorlayerloadsavestyledialog.h
qgsvectorlayerproperties.h
qgsmapthemes.h
qgshandlebadlayers.h
@@ -834,15 +834,15 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
* \param sourceLayer The layer where the style will be taken from (defaults to the active layer on the legend)
* \param categories The style categories to copy
*/
void copyStyle( QgsMapLayer *sourceLayer = nullptr, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllCategories );
void copyStyle( QgsMapLayer *sourceLayer = nullptr, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories );
//! pastes style on the clipboard to the active layer

/**
* \param destinationLayer The layer that the clipboard will be pasted to (defaults to the active layer on the legend)
* \param categories The style categories to copy
*/
void pasteStyle( QgsMapLayer *destinationLayer = nullptr,
QgsMapLayer::StyleCategories categories = QgsMapLayer::AllCategories );
QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories );
//! copies group or layer on the clipboard
void copyLayer();
//! pastes group or layer from the clipboard to layer tree
@@ -321,15 +321,15 @@ QMenu *QgsAppLayerTreeViewMenuProvider::createContextMenu()
QMenu *copyStyleMenu = menuStyleManager->addMenu( tr( "Copy Style" ) );
copyStyleMenu->setToolTipsVisible( true );
QList<QgsMapLayer::StyleCategory> categories = qgsEnumMap<QgsMapLayer::StyleCategory>().keys();
categories.move( categories.indexOf( QgsMapLayer::AllCategories ), 0 ); // move All categories to top
categories.move( categories.indexOf( QgsMapLayer::AllStyleCategories ), 0 ); // move All categories to top
for ( QgsMapLayer::StyleCategory category : categories )
{
QgsMapLayer::ReadableStyleCategory readableCategory = QgsMapLayer::readableStyleCategory( category );
QAction *copyAction = new QAction( readableCategory.icon(), readableCategory.name(), copyStyleMenu );
copyAction->setToolTip( readableCategory.toolTip() );
connect( copyAction, &QAction::triggered, this, [ = ]() {app->copyStyle( layer, category );} );
copyStyleMenu->addAction( copyAction );
if ( category == QgsMapLayer::AllCategories )
if ( category == QgsMapLayer::AllStyleCategories )
copyStyleMenu->addSeparator();
}
}
@@ -345,15 +345,15 @@ QMenu *QgsAppLayerTreeViewMenuProvider::createContextMenu()
QMenu *copyStyleMenu = menuStyleManager->addMenu( tr( "Paste Style" ) );
copyStyleMenu->setToolTipsVisible( true );
QList<QgsMapLayer::StyleCategory> categories = qgsEnumMap<QgsMapLayer::StyleCategory>().keys();
categories.move( categories.indexOf( QgsMapLayer::AllCategories ), 0 ); // move All categories to top
categories.move( categories.indexOf( QgsMapLayer::AllStyleCategories ), 0 ); // move All categories to top
for ( QgsMapLayer::StyleCategory category : categories )
{
QgsMapLayer::ReadableStyleCategory readableCategory = QgsMapLayer::readableStyleCategory( category );
QAction *copyAction = new QAction( readableCategory.icon(), readableCategory.name() );
copyAction->setToolTip( readableCategory.toolTip() );
connect( copyAction, &QAction::triggered, this, [ = ]() {app->pasteStyle( layer, category );} );
copyStyleMenu->addAction( copyAction );
if ( category == QgsMapLayer::AllCategories )
if ( category == QgsMapLayer::AllStyleCategories )
copyStyleMenu->addSeparator();
}
}
@@ -0,0 +1,96 @@
/***************************************************************************
qgsvectorlayerloadsavestyledialog.h
--------------------------------------
Date : September 2018
Copyright : (C) 2018 by Denis Rouzaud
Email : denis@opengis.ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include <QListWidgetItem>
#include <QMessageBox>

#include "qgsvectorlayerloadsavestyledialog.h"
#include "qgsvectorlayer.h"
#include "qgssettings.h"
#include "qgisapp.h"
#include "qgshelp.h"

QgsVectorLayerLoadSaveStyleDialog::QgsVectorLayerLoadSaveStyleDialog( QgsVectorLayer *layer, Mode mode, QWidget *parent )
: QDialog( parent )
, mLayer( layer )
, mMode( mode )
{
setupUi( this );

QString providerName = mLayer->providerType();
if ( providerName == QLatin1String( "ogr" ) )
{
providerName = mLayer->dataProvider()->storageType();
if ( providerName == QLatin1String( "GPKG" ) )
providerName = QStringLiteral( "GeoPackage" );
}

//QStringLiteral( "style/lastStyleDir" )

QgsSettings settings;
QgsMapLayer::StyleCategories lastStyleCategories = settings.flagValue( QStringLiteral( "style/lastStyleCategories" ), QgsMapLayer::AllStyleCategories );


switch ( mMode )
{
case Save:
mModeLabel->setText( tr( "Save" ) );
mStyleTypeComboBox->addItem( tr( "from file" ), GenericFile );
if ( mLayer->dataProvider()->isSaveAndLoadStyleToDatabaseSupported() )
mStyleTypeComboBox->addItem( tr( "from database (%1)" ).arg( providerName ), DB );
break;
case Load:
mModeLabel->setText( tr( "Load" ) );
mStyleTypeComboBox->addItem( tr( "as QGIS QML style file" ), QML );
mStyleTypeComboBox->addItem( tr( "as SLD style file" ), SLD );
if ( mLayer->dataProvider()->isSaveAndLoadStyleToDatabaseSupported() )
mStyleTypeComboBox->addItem( tr( "in database (%1)" ).arg( providerName ), DB );
break;
}

connect( mStyleTypeComboBox, qgis::overload<int>::of( &QComboBox::currentIndexChanged ), this, [ = ]( int idx ) {mFileWidget->setVisible( mStyleTypeComboBox->itemData( idx ) != DB );} );

for ( QgsMapLayer::StyleCategory category : qgsEnumMap<QgsMapLayer::StyleCategory>().keys() )
{
if ( category == QgsMapLayer::AllStyleCategories )
continue;

QgsMapLayer::ReadableStyleCategory readableCategory = QgsMapLayer::readableStyleCategory( category );

QListWidgetItem *item = new QListWidgetItem( readableCategory.icon(), readableCategory.name(), mStyleCategoriesListWidget );
item->setFlags( item->flags() | Qt::ItemIsUserCheckable );
item->setCheckState( lastStyleCategories.testFlag( category ) ? Qt::Checked : Qt::Unchecked );
item->setData( Qt::UserRole, category );
}
}

void QgsVectorLayerLoadSaveStyleDialog::accept()
{
bool ok = true;
QgsMapLayer::StyleCategories lastStyleCategories;
for ( int row = 0; row < mStyleCategoriesListWidget->count(); ++row )
{
QListWidgetItem *item = mStyleCategoriesListWidget->item( row );
if ( item->checkState() == Qt::Checked )
lastStyleCategories |= static_cast<QgsMapLayer::StyleCategory>( item->data( Qt::UserRole ).toInt() );
}

if ( ok )
{
QgsSettings settings;
settings.setFlagValue( QStringLiteral( "style/lastStyleCategories" ), lastStyleCategories );
close();
}
}

0 comments on commit cb97740

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