Skip to content

Commit cb97740

Browse files
3nidsnyalldawson
authored andcommitted
create dialog for saving different styles
1 parent 20a70ee commit cb97740

18 files changed

+446
-177
lines changed

python/core/auto_generated/mesh/qgsmeshlayer.sip.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ QgsMeshLayer cannot be copied.
110110
virtual QgsMapLayerRenderer *createMapRenderer( QgsRenderContext &rendererContext ) /Factory/;
111111

112112
virtual bool readSymbology( const QDomNode &node, QString &errorMessage,
113-
QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllCategories );
113+
QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories );
114114
virtual bool writeSymbology( QDomNode &node, QDomDocument &doc, QString &errorMessage,
115-
const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllCategories ) const;
115+
const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories ) const;
116116
virtual QString encodedSource( const QString &source, const QgsReadWriteContext &context ) const;
117117

118118
virtual QString decodedSource( const QString &source, const QString &provider, const QgsReadWriteContext &context ) const;

python/core/auto_generated/qgsmaplayer.sip.in

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ This is the base class for all map layer types (vector, raster).
8989
AttributeTable,
9090
Rendering,
9191
CustomProperties,
92-
AllCategories
92+
AllStyleCategories
9393
};
9494
typedef QFlags<QgsMapLayer::StyleCategory> StyleCategories;
9595

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

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

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

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

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

924925

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

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

934936
:return: true in case of success.
935937
%End
936938

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

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

946949
:return: true in case of success.
947950

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

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

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

965969
.. note::
966970

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

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

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

982987
:return: true in case of success.
983988

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

15471552
void writeCommonStyle( QDomElement &layerElement, QDomDocument &document,
15481553
const QgsReadWriteContext &context,
1549-
StyleCategories categories = AllCategories ) const;
1554+
StyleCategories categories = AllStyleCategories ) const;
15501555
%Docstring
15511556
Write style data common to all layer types
15521557

15531558
.. versionadded:: 3.0
15541559
%End
15551560

15561561
void readCommonStyle( const QDomElement &layerElement, const QgsReadWriteContext &context,
1557-
StyleCategories categories = AllCategories );
1562+
StyleCategories categories = AllStyleCategories );
15581563
%Docstring
15591564
Read style data common to all layer types
15601565

python/core/auto_generated/qgsvectorlayer.sip.in

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,7 @@ Returns the current auxiliary layer.
871871

872872

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

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

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

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

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

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

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

python/core/auto_generated/raster/qgsrasterlayer.sip.in

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -348,16 +348,16 @@ Draws a preview of the rasterlayer into a QImage
348348
void showStatusMessage( const QString &message );
349349

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

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

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

357357
virtual bool writeSymbology( QDomNode &, QDomDocument &doc, QString &errorMessage,
358-
const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllCategories ) const;
358+
const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories ) const;
359359
virtual bool writeStyle( QDomNode &node, QDomDocument &doc, QString &errorMessage,
360-
const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllCategories ) const;
360+
const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories ) const;
361361
virtual bool writeXml( QDomNode &layer_node, QDomDocument &doc, const QgsReadWriteContext &context ) const;
362362

363363
virtual QString encodedSource( const QString &source, const QgsReadWriteContext &context ) const;

src/app/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ SET(QGIS_APP_SRCS
138138
qgssvgannotationdialog.cpp
139139
qgsundowidget.cpp
140140
qgsvectorlayerlegendwidget.cpp
141+
qgsvectorlayerloadsavestyledialog.cpp
141142
qgsvectorlayerproperties.cpp
142143
qgsmapthemes.cpp
143144
qgshandlebadlayers.cpp
@@ -377,6 +378,7 @@ SET (QGIS_APP_MOC_HDRS
377378
qgstextannotationdialog.h
378379
qgsundowidget.h
379380
qgsvectorlayerlegendwidget.h
381+
qgsvectorlayerloadsavestyledialog.h
380382
qgsvectorlayerproperties.h
381383
qgsmapthemes.h
382384
qgshandlebadlayers.h

src/app/qgisapp.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -834,15 +834,15 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
834834
* \param sourceLayer The layer where the style will be taken from (defaults to the active layer on the legend)
835835
* \param categories The style categories to copy
836836
*/
837-
void copyStyle( QgsMapLayer *sourceLayer = nullptr, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllCategories );
837+
void copyStyle( QgsMapLayer *sourceLayer = nullptr, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories );
838838
//! pastes style on the clipboard to the active layer
839839

840840
/**
841841
* \param destinationLayer The layer that the clipboard will be pasted to (defaults to the active layer on the legend)
842842
* \param categories The style categories to copy
843843
*/
844844
void pasteStyle( QgsMapLayer *destinationLayer = nullptr,
845-
QgsMapLayer::StyleCategories categories = QgsMapLayer::AllCategories );
845+
QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories );
846846
//! copies group or layer on the clipboard
847847
void copyLayer();
848848
//! pastes group or layer from the clipboard to layer tree

src/app/qgsapplayertreeviewmenuprovider.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,15 +321,15 @@ QMenu *QgsAppLayerTreeViewMenuProvider::createContextMenu()
321321
QMenu *copyStyleMenu = menuStyleManager->addMenu( tr( "Copy Style" ) );
322322
copyStyleMenu->setToolTipsVisible( true );
323323
QList<QgsMapLayer::StyleCategory> categories = qgsEnumMap<QgsMapLayer::StyleCategory>().keys();
324-
categories.move( categories.indexOf( QgsMapLayer::AllCategories ), 0 ); // move All categories to top
324+
categories.move( categories.indexOf( QgsMapLayer::AllStyleCategories ), 0 ); // move All categories to top
325325
for ( QgsMapLayer::StyleCategory category : categories )
326326
{
327327
QgsMapLayer::ReadableStyleCategory readableCategory = QgsMapLayer::readableStyleCategory( category );
328328
QAction *copyAction = new QAction( readableCategory.icon(), readableCategory.name(), copyStyleMenu );
329329
copyAction->setToolTip( readableCategory.toolTip() );
330330
connect( copyAction, &QAction::triggered, this, [ = ]() {app->copyStyle( layer, category );} );
331331
copyStyleMenu->addAction( copyAction );
332-
if ( category == QgsMapLayer::AllCategories )
332+
if ( category == QgsMapLayer::AllStyleCategories )
333333
copyStyleMenu->addSeparator();
334334
}
335335
}
@@ -345,15 +345,15 @@ QMenu *QgsAppLayerTreeViewMenuProvider::createContextMenu()
345345
QMenu *copyStyleMenu = menuStyleManager->addMenu( tr( "Paste Style" ) );
346346
copyStyleMenu->setToolTipsVisible( true );
347347
QList<QgsMapLayer::StyleCategory> categories = qgsEnumMap<QgsMapLayer::StyleCategory>().keys();
348-
categories.move( categories.indexOf( QgsMapLayer::AllCategories ), 0 ); // move All categories to top
348+
categories.move( categories.indexOf( QgsMapLayer::AllStyleCategories ), 0 ); // move All categories to top
349349
for ( QgsMapLayer::StyleCategory category : categories )
350350
{
351351
QgsMapLayer::ReadableStyleCategory readableCategory = QgsMapLayer::readableStyleCategory( category );
352352
QAction *copyAction = new QAction( readableCategory.icon(), readableCategory.name() );
353353
copyAction->setToolTip( readableCategory.toolTip() );
354354
connect( copyAction, &QAction::triggered, this, [ = ]() {app->pasteStyle( layer, category );} );
355355
copyStyleMenu->addAction( copyAction );
356-
if ( category == QgsMapLayer::AllCategories )
356+
if ( category == QgsMapLayer::AllStyleCategories )
357357
copyStyleMenu->addSeparator();
358358
}
359359
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/***************************************************************************
2+
qgsvectorlayerloadsavestyledialog.h
3+
--------------------------------------
4+
Date : September 2018
5+
Copyright : (C) 2018 by Denis Rouzaud
6+
Email : denis@opengis.ch
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
#include <QListWidgetItem>
17+
#include <QMessageBox>
18+
19+
#include "qgsvectorlayerloadsavestyledialog.h"
20+
#include "qgsvectorlayer.h"
21+
#include "qgssettings.h"
22+
#include "qgisapp.h"
23+
#include "qgshelp.h"
24+
25+
QgsVectorLayerLoadSaveStyleDialog::QgsVectorLayerLoadSaveStyleDialog( QgsVectorLayer *layer, Mode mode, QWidget *parent )
26+
: QDialog( parent )
27+
, mLayer( layer )
28+
, mMode( mode )
29+
{
30+
setupUi( this );
31+
32+
QString providerName = mLayer->providerType();
33+
if ( providerName == QLatin1String( "ogr" ) )
34+
{
35+
providerName = mLayer->dataProvider()->storageType();
36+
if ( providerName == QLatin1String( "GPKG" ) )
37+
providerName = QStringLiteral( "GeoPackage" );
38+
}
39+
40+
//QStringLiteral( "style/lastStyleDir" )
41+
42+
QgsSettings settings;
43+
QgsMapLayer::StyleCategories lastStyleCategories = settings.flagValue( QStringLiteral( "style/lastStyleCategories" ), QgsMapLayer::AllStyleCategories );
44+
45+
46+
switch ( mMode )
47+
{
48+
case Save:
49+
mModeLabel->setText( tr( "Save" ) );
50+
mStyleTypeComboBox->addItem( tr( "from file" ), GenericFile );
51+
if ( mLayer->dataProvider()->isSaveAndLoadStyleToDatabaseSupported() )
52+
mStyleTypeComboBox->addItem( tr( "from database (%1)" ).arg( providerName ), DB );
53+
break;
54+
case Load:
55+
mModeLabel->setText( tr( "Load" ) );
56+
mStyleTypeComboBox->addItem( tr( "as QGIS QML style file" ), QML );
57+
mStyleTypeComboBox->addItem( tr( "as SLD style file" ), SLD );
58+
if ( mLayer->dataProvider()->isSaveAndLoadStyleToDatabaseSupported() )
59+
mStyleTypeComboBox->addItem( tr( "in database (%1)" ).arg( providerName ), DB );
60+
break;
61+
}
62+
63+
connect( mStyleTypeComboBox, qgis::overload<int>::of( &QComboBox::currentIndexChanged ), this, [ = ]( int idx ) {mFileWidget->setVisible( mStyleTypeComboBox->itemData( idx ) != DB );} );
64+
65+
for ( QgsMapLayer::StyleCategory category : qgsEnumMap<QgsMapLayer::StyleCategory>().keys() )
66+
{
67+
if ( category == QgsMapLayer::AllStyleCategories )
68+
continue;
69+
70+
QgsMapLayer::ReadableStyleCategory readableCategory = QgsMapLayer::readableStyleCategory( category );
71+
72+
QListWidgetItem *item = new QListWidgetItem( readableCategory.icon(), readableCategory.name(), mStyleCategoriesListWidget );
73+
item->setFlags( item->flags() | Qt::ItemIsUserCheckable );
74+
item->setCheckState( lastStyleCategories.testFlag( category ) ? Qt::Checked : Qt::Unchecked );
75+
item->setData( Qt::UserRole, category );
76+
}
77+
}
78+
79+
void QgsVectorLayerLoadSaveStyleDialog::accept()
80+
{
81+
bool ok = true;
82+
QgsMapLayer::StyleCategories lastStyleCategories;
83+
for ( int row = 0; row < mStyleCategoriesListWidget->count(); ++row )
84+
{
85+
QListWidgetItem *item = mStyleCategoriesListWidget->item( row );
86+
if ( item->checkState() == Qt::Checked )
87+
lastStyleCategories |= static_cast<QgsMapLayer::StyleCategory>( item->data( Qt::UserRole ).toInt() );
88+
}
89+
90+
if ( ok )
91+
{
92+
QgsSettings settings;
93+
settings.setFlagValue( QStringLiteral( "style/lastStyleCategories" ), lastStyleCategories );
94+
close();
95+
}
96+
}

0 commit comments

Comments
 (0)