QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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() << QObject::tr( "Symbols" ) << QObject::tr( "Color ramps" ) << QObject::tr( "Text formats" ) << QObject::tr( "Label settings" );
39 addParameter( new QgsProcessingParameterEnum( u"OBJECTS"_s, QObject::tr( "Objects to combine" ), options, true, QVariantList() << 0 << 1 << 2 << 3 ) );
40 addOutput( new QgsProcessingOutputNumber( u"SYMBOLS"_s, QObject::tr( "Symbol count" ) ) );
41 addOutput( new QgsProcessingOutputNumber( u"COLORRAMPS"_s, QObject::tr( "Color ramp count" ) ) );
42 addOutput( new QgsProcessingOutputNumber( u"TEXTFORMATS"_s, QObject::tr( "Text format count" ) ) );
43 addOutput( new QgsProcessingOutputNumber( u"LABELSETTINGS"_s, QObject::tr( "Label settings count" ) ) );
44}
45
46QString QgsCombineStylesAlgorithm::name() const
47{
48 return u"combinestyles"_s;
49}
50
51QString QgsCombineStylesAlgorithm::displayName() const
52{
53 return QObject::tr( "Combine style databases" );
54}
55
56QStringList QgsCombineStylesAlgorithm::tags() const
57{
58 return QObject::tr( "symbols,colors,ramps,formats,labels,text,fonts,merge" ).split( ',' );
59}
60
61QString QgsCombineStylesAlgorithm::group() const
62{
63 return QObject::tr( "Cartography" );
64}
65
66QString QgsCombineStylesAlgorithm::groupId() const
67{
68 return u"cartography"_s;
69}
70
71QString QgsCombineStylesAlgorithm::shortHelpString() const
72{
73 return QObject::tr(
74 "This algorithm combines multiple QGIS style databases into a single style database. If any symbols exist with duplicate names between the different "
75 "source databases these will be renamed to have unique names in the output combined database."
76 );
77}
78
79QString QgsCombineStylesAlgorithm::shortDescription() const
80{
81 return QObject::tr( "Combines multiple style databases into a single database." );
82}
83
84QgsCombineStylesAlgorithm *QgsCombineStylesAlgorithm::createInstance() const
85{
86 return new QgsCombineStylesAlgorithm();
87}
88
89QVariantMap QgsCombineStylesAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
90{
91 const QStringList inputs = parameterAsFileList( parameters, u"INPUT"_s, context );
92
93 QList<QgsStyle::StyleEntity> objects;
94 const QList<int> selectedObjects = parameterAsEnums( parameters, u"OBJECTS"_s, context );
95 if ( selectedObjects.contains( 0 ) )
96 objects << QgsStyle::SymbolEntity;
97 if ( selectedObjects.contains( 1 ) )
99 if ( selectedObjects.contains( 2 ) )
101 if ( selectedObjects.contains( 3 ) )
103
104 QgsStyle style;
105 style.createMemoryDatabase();
106
107 int i = 0;
108 QMap<QgsStyle::StyleEntity, QSet<QString>> usedNames;
109 auto makeUniqueName = [&usedNames]( const QString &sourceName, QgsStyle::StyleEntity type ) -> QString {
110 QString candidate = sourceName;
111 int i = 1;
112 bool exists = true;
113 while ( exists )
114 {
115 exists = usedNames[type].contains( candidate );
116 if ( !exists )
117 break;
118
119 i++;
120 candidate = sourceName + u" (%1)"_s.arg( i );
121 }
122
123 usedNames[type].insert( candidate );
124 return candidate;
125 };
126
127 for ( const QString &source : inputs )
128 {
129 if ( feedback )
130 {
131 feedback->setProgress( 100 * i / static_cast<double>( inputs.count() ) );
132 feedback->pushInfo( QObject::tr( "Importing %1" ).arg( source ) );
133 }
134
135 QgsStyle sourceStyle;
136 sourceStyle.createMemoryDatabase();
137 if ( !sourceStyle.importXml( source ) )
138 {
139 if ( feedback )
140 {
141 feedback->reportError( QObject::tr( "Could not read %1" ).arg( source ) );
142 }
143 i++;
144 continue;
145 }
146
147 if ( objects.contains( QgsStyle::SymbolEntity ) )
148 {
149 const QStringList symbolNames = sourceStyle.symbolNames();
150 for ( const QString &name : symbolNames )
151 {
152 const QString newName = makeUniqueName( name, QgsStyle::SymbolEntity );
153 style.addSymbol( newName, sourceStyle.symbol( name ), true );
154 style.tagSymbol( QgsStyle::SymbolEntity, newName, sourceStyle.tagsOfSymbol( QgsStyle::SymbolEntity, name ) );
155 }
156 }
157 if ( objects.contains( QgsStyle::ColorrampEntity ) )
158 {
159 const QStringList colorRampNames = sourceStyle.colorRampNames();
160 for ( const QString &name : colorRampNames )
161 {
162 const QString newName = makeUniqueName( name, QgsStyle::ColorrampEntity );
163 style.addColorRamp( newName, sourceStyle.colorRamp( name ), true );
164 style.tagSymbol( QgsStyle::ColorrampEntity, newName, sourceStyle.tagsOfSymbol( QgsStyle::ColorrampEntity, name ) );
165 }
166 }
167 if ( objects.contains( QgsStyle::TextFormatEntity ) )
168 {
169 const QStringList formatNames = sourceStyle.textFormatNames();
170 for ( const QString &name : formatNames )
171 {
172 const QString newName = makeUniqueName( name, QgsStyle::TextFormatEntity );
173 style.addTextFormat( newName, sourceStyle.textFormat( name ), true );
174 style.tagSymbol( QgsStyle::TextFormatEntity, newName, sourceStyle.tagsOfSymbol( QgsStyle::TextFormatEntity, name ) );
175 }
176 }
177 if ( objects.contains( QgsStyle::LabelSettingsEntity ) )
178 {
179 const QStringList formatNames = sourceStyle.labelSettingsNames();
180 for ( const QString &name : formatNames )
181 {
182 const QString newName = makeUniqueName( name, QgsStyle::LabelSettingsEntity );
183 style.addLabelSettings( newName, sourceStyle.labelSettings( name ), true );
185 }
186 }
187
188 i++;
189 }
190 if ( feedback )
191 {
192 feedback->setProgress( 100 );
193 feedback->pushInfo( QObject::tr( "Writing output file" ) );
194 }
195
196 const QString file = parameterAsString( parameters, u"OUTPUT"_s, context );
197 if ( !style.exportXml( file ) )
198 {
199 throw QgsProcessingException( QObject::tr( "Error saving style database as %1" ).arg( file ) );
200 }
201
202 QVariantMap results;
203 results.insert( u"OUTPUT"_s, file );
204 results.insert( u"SYMBOLS"_s, style.symbolCount() );
205 results.insert( u"COLORRAMPS"_s, style.colorRampCount() );
206 results.insert( u"TEXTFORMATS"_s, style.textFormatCount() );
207 results.insert( u"LABELSETTINGS"_s, style.labelSettingsCount() );
208 return results;
209}
210
@ File
Files (i.e. non map layer sources, such as text files).
Definition qgis.h:3652
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:65
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:506
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:345
QgsSymbol * symbol(const QString &name)
Returns a NEW copy of symbol.
Definition qgsstyle.cpp:317
int labelSettingsCount() const
Returns count of label settings in the style.
StyleEntity
Enum for Entities involved in a style.
Definition qgsstyle.h:204
@ LabelSettingsEntity
Label settings.
Definition qgsstyle.h:210
@ TextFormatEntity
Text formats.
Definition qgsstyle.h:209
@ SymbolEntity
Symbols.
Definition qgsstyle.h:205
@ ColorrampEntity
Color ramps.
Definition qgsstyle.h:207
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:511
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:495
int symbolCount()
Returns count of symbols in style.
Definition qgsstyle.cpp:334
bool createMemoryDatabase()
Creates a temporary memory database.
Definition qgsstyle.cpp:564
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:370
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:226
QStringList symbolNames() const
Returns a list of names of symbols.
Definition qgsstyle.cpp:339
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:392