QGIS API Documentation 3.99.0-Master (09f76ad7019)
Loading...
Searching...
No Matches
qgsstylealgorithms.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsstylealgorithms.cpp
3 ---------------------
4 begin : July 2019
5 copyright : (C) 2019 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#include "qgsstylealgorithms.h"
19
20#include "qgsstyle.h"
21
22#include <QString>
23
24using namespace Qt::StringLiterals;
25
27
28QgsCombineStylesAlgorithm::QgsCombineStylesAlgorithm() = default;
29
30QgsCombineStylesAlgorithm::~QgsCombineStylesAlgorithm() = default;
31
32void QgsCombineStylesAlgorithm::initAlgorithm( const QVariantMap & )
33{
34 addParameter( new QgsProcessingParameterMultipleLayers( u"INPUT"_s, QObject::tr( "Input databases" ), Qgis::ProcessingSourceType::File ) );
35
36 addParameter( new QgsProcessingParameterFileDestination( u"OUTPUT"_s, QObject::tr( "Output style database" ), QObject::tr( "Style files (*.xml)" ) ) );
37
38 const QStringList options = QStringList()
39 << QObject::tr( "Symbols" )
40 << QObject::tr( "Color ramps" )
41 << QObject::tr( "Text formats" )
42 << QObject::tr( "Label settings" );
43 addParameter( new QgsProcessingParameterEnum( u"OBJECTS"_s, QObject::tr( "Objects to combine" ), options, true, QVariantList() << 0 << 1 << 2 << 3 ) );
44 addOutput( new QgsProcessingOutputNumber( u"SYMBOLS"_s, QObject::tr( "Symbol count" ) ) );
45 addOutput( new QgsProcessingOutputNumber( u"COLORRAMPS"_s, QObject::tr( "Color ramp count" ) ) );
46 addOutput( new QgsProcessingOutputNumber( u"TEXTFORMATS"_s, QObject::tr( "Text format count" ) ) );
47 addOutput( new QgsProcessingOutputNumber( u"LABELSETTINGS"_s, QObject::tr( "Label settings count" ) ) );
48}
49
50QString QgsCombineStylesAlgorithm::name() const
51{
52 return u"combinestyles"_s;
53}
54
55QString QgsCombineStylesAlgorithm::displayName() const
56{
57 return QObject::tr( "Combine style databases" );
58}
59
60QStringList QgsCombineStylesAlgorithm::tags() const
61{
62 return QObject::tr( "symbols,colors,ramps,formats,labels,text,fonts,merge" ).split( ',' );
63}
64
65QString QgsCombineStylesAlgorithm::group() const
66{
67 return QObject::tr( "Cartography" );
68}
69
70QString QgsCombineStylesAlgorithm::groupId() const
71{
72 return u"cartography"_s;
73}
74
75QString QgsCombineStylesAlgorithm::shortHelpString() const
76{
77 return QObject::tr( "This algorithm combines multiple QGIS style databases into a single style database. If any symbols exist with duplicate names between the different "
78 "source databases these will be renamed to have unique names in the output combined database." );
79}
80
81QString QgsCombineStylesAlgorithm::shortDescription() const
82{
83 return QObject::tr( "Combines multiple style databases into a single database." );
84}
85
86QgsCombineStylesAlgorithm *QgsCombineStylesAlgorithm::createInstance() const
87{
88 return new QgsCombineStylesAlgorithm();
89}
90
91QVariantMap QgsCombineStylesAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
92{
93 const QStringList inputs = parameterAsFileList( parameters, u"INPUT"_s, context );
94
95 QList<QgsStyle::StyleEntity> objects;
96 const QList<int> selectedObjects = parameterAsEnums( parameters, u"OBJECTS"_s, context );
97 if ( selectedObjects.contains( 0 ) )
98 objects << QgsStyle::SymbolEntity;
99 if ( selectedObjects.contains( 1 ) )
100 objects << QgsStyle::ColorrampEntity;
101 if ( selectedObjects.contains( 2 ) )
103 if ( selectedObjects.contains( 3 ) )
105
106 QgsStyle style;
107 style.createMemoryDatabase();
108
109 int i = 0;
110 QMap<QgsStyle::StyleEntity, QSet<QString>> usedNames;
111 auto makeUniqueName = [&usedNames]( const QString &sourceName, QgsStyle::StyleEntity type ) -> QString {
112 QString candidate = sourceName;
113 int i = 1;
114 bool exists = true;
115 while ( exists )
116 {
117 exists = usedNames[type].contains( candidate );
118 if ( !exists )
119 break;
120
121 i++;
122 candidate = sourceName + u" (%1)"_s.arg( i );
123 }
124
125 usedNames[type].insert( candidate );
126 return candidate;
127 };
128
129 for ( const QString &source : inputs )
130 {
131 if ( feedback )
132 {
133 feedback->setProgress( 100 * i / static_cast<double>( inputs.count() ) );
134 feedback->pushInfo( QObject::tr( "Importing %1" ).arg( source ) );
135 }
136
137 QgsStyle sourceStyle;
138 sourceStyle.createMemoryDatabase();
139 if ( !sourceStyle.importXml( source ) )
140 {
141 if ( feedback )
142 {
143 feedback->reportError( QObject::tr( "Could not read %1" ).arg( source ) );
144 }
145 i++;
146 continue;
147 }
148
149 if ( objects.contains( QgsStyle::SymbolEntity ) )
150 {
151 const QStringList symbolNames = sourceStyle.symbolNames();
152 for ( const QString &name : symbolNames )
153 {
154 const QString newName = makeUniqueName( name, QgsStyle::SymbolEntity );
155 style.addSymbol( newName, sourceStyle.symbol( name ), true );
156 style.tagSymbol( QgsStyle::SymbolEntity, newName, sourceStyle.tagsOfSymbol( QgsStyle::SymbolEntity, name ) );
157 }
158 }
159 if ( objects.contains( QgsStyle::ColorrampEntity ) )
160 {
161 const QStringList colorRampNames = sourceStyle.colorRampNames();
162 for ( const QString &name : colorRampNames )
163 {
164 const QString newName = makeUniqueName( name, QgsStyle::ColorrampEntity );
165 style.addColorRamp( newName, sourceStyle.colorRamp( name ), true );
166 style.tagSymbol( QgsStyle::ColorrampEntity, newName, sourceStyle.tagsOfSymbol( QgsStyle::ColorrampEntity, name ) );
167 }
168 }
169 if ( objects.contains( QgsStyle::TextFormatEntity ) )
170 {
171 const QStringList formatNames = sourceStyle.textFormatNames();
172 for ( const QString &name : formatNames )
173 {
174 const QString newName = makeUniqueName( name, QgsStyle::TextFormatEntity );
175 style.addTextFormat( newName, sourceStyle.textFormat( name ), true );
176 style.tagSymbol( QgsStyle::TextFormatEntity, newName, sourceStyle.tagsOfSymbol( QgsStyle::TextFormatEntity, name ) );
177 }
178 }
179 if ( objects.contains( QgsStyle::LabelSettingsEntity ) )
180 {
181 const QStringList formatNames = sourceStyle.labelSettingsNames();
182 for ( const QString &name : formatNames )
183 {
184 const QString newName = makeUniqueName( name, QgsStyle::LabelSettingsEntity );
185 style.addLabelSettings( newName, sourceStyle.labelSettings( name ), true );
187 }
188 }
189
190 i++;
191 }
192 if ( feedback )
193 {
194 feedback->setProgress( 100 );
195 feedback->pushInfo( QObject::tr( "Writing output file" ) );
196 }
197
198 const QString file = parameterAsString( parameters, u"OUTPUT"_s, context );
199 if ( !style.exportXml( file ) )
200 {
201 throw QgsProcessingException( QObject::tr( "Error saving style database as %1" ).arg( file ) );
202 }
203
204 QVariantMap results;
205 results.insert( u"OUTPUT"_s, file );
206 results.insert( u"SYMBOLS"_s, style.symbolCount() );
207 results.insert( u"COLORRAMPS"_s, style.colorRampCount() );
208 results.insert( u"TEXTFORMATS"_s, style.textFormatCount() );
209 results.insert( u"LABELSETTINGS"_s, style.labelSettingsCount() );
210 return results;
211}
212
@ File
Files (i.e. non map layer sources, such as text files).
Definition qgis.h:3609
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:63
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
A numeric output for processing algorithms.
An enum based parameter for processing algorithms, allowing for selection from predefined values.
A generic file based destination parameter, for specifying the destination path for a file (non-map l...
A parameter for processing algorithms which accepts multiple map layers.
A database of saved style entities, including symbols, color ramps, text formats and others.
Definition qgsstyle.h:89
int colorRampCount()
Returns count of color ramps.
Definition qgsstyle.cpp:510
QgsTextFormat textFormat(const QString &name) const
Returns the text format with the specified name.
bool tagSymbol(StyleEntity type, const QString &symbol, const QStringList &tags)
Tags the symbol with the tags in the list.
QStringList textFormatNames() const
Returns a list of names of text formats in the style.
bool addColorRamp(const QString &name, QgsColorRamp *colorRamp, bool update=false)
Adds a color ramp to the style.
Definition qgsstyle.cpp:348
QgsSymbol * symbol(const QString &name)
Returns a NEW copy of symbol.
Definition qgsstyle.cpp:320
int labelSettingsCount() const
Returns count of label settings in the style.
StyleEntity
Enum for Entities involved in a style.
Definition qgsstyle.h:205
@ LabelSettingsEntity
Label settings.
Definition qgsstyle.h:211
@ TextFormatEntity
Text formats.
Definition qgsstyle.h:210
@ SymbolEntity
Symbols.
Definition qgsstyle.h:206
@ ColorrampEntity
Color ramps.
Definition qgsstyle.h:208
QStringList tagsOfSymbol(StyleEntity type, const QString &symbol)
Returns the tags associated with the symbol.
QStringList colorRampNames() const
Returns a list of names of color ramps.
Definition qgsstyle.cpp:515
int textFormatCount() const
Returns count of text formats in the style.
bool exportXml(const QString &filename)
Exports the style as a XML file.
QgsColorRamp * colorRamp(const QString &name) const
Returns a new copy of the specified color ramp.
Definition qgsstyle.cpp:499
int symbolCount()
Returns count of symbols in style.
Definition qgsstyle.cpp:337
bool createMemoryDatabase()
Creates a temporary memory database.
Definition qgsstyle.cpp:568
QStringList labelSettingsNames() const
Returns a list of names of label settings in the style.
bool addTextFormat(const QString &name, const QgsTextFormat &format, bool update=false)
Adds a text format with the specified name to the style.
Definition qgsstyle.cpp:373
QgsPalLayerSettings labelSettings(const QString &name) const
Returns the label settings with the specified name.
bool addSymbol(const QString &name, QgsSymbol *symbol, bool update=false)
Adds a symbol to style and takes symbol's ownership.
Definition qgsstyle.cpp:228
QStringList symbolNames() const
Returns a list of names of symbols.
Definition qgsstyle.cpp:342
bool importXml(const QString &filename)
Imports the symbols and colorramps into the default style database from the given XML file.
bool addLabelSettings(const QString &name, const QgsPalLayerSettings &settings, bool update=false)
Adds label settings with the specified name to the style.
Definition qgsstyle.cpp:395