QGIS API Documentation 3.99.0-Master (357b655ed83)
Loading...
Searching...
No Matches
qgsprojectstylealgorithms.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsprojectstylealgorithms.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
19
20#include "qgsstyle.h"
21
22#include <QString>
23
24using namespace Qt::StringLiterals;
25
27
28//
29// QgsProjectToStyleVisitor
30//
31
32QgsSaveToStyleVisitor::QgsSaveToStyleVisitor( QgsStyle *style, const QList<QgsStyle::StyleEntity> &objects )
33 : mStyle( style )
34 , mObjects( objects )
35{
36}
37
38bool QgsSaveToStyleVisitor::visit( const QgsStyleEntityVisitorInterface::StyleLeaf &entity )
39{
40 if ( mObjects.empty() || mObjects.contains( entity.entity->type() ) )
41 {
42 const QString name = QString( mParentNames.join( ' ' ) + ' ' + entity.description ).trimmed();
43 QString candidate = name;
44 int i = 1;
45 bool exists = true;
46 while ( exists )
47 {
48 exists = mStyle->allNames( entity.entity->type() ).contains( candidate );
49 if ( !exists )
50 break;
51
52 i++;
53 candidate = name + u" (%1)"_s.arg( i );
54 }
55 mStyle->addEntity( candidate, entity.entity, true );
56 }
57 return true;
58}
59
60bool QgsSaveToStyleVisitor::visitEnter( const QgsStyleEntityVisitorInterface::Node &node )
61{
62 switch ( node.type )
63 {
71 break;
72
78 mParentNames << node.description;
79 break;
80 }
81 return true;
82}
83
84bool QgsSaveToStyleVisitor::visitExit( const QgsStyleEntityVisitorInterface::Node &node )
85{
86 switch ( node.type )
87 {
95 break;
96
102 mParentNames.pop_back();
103 break;
104 }
105 return true;
106}
107
108//
109// QgsStyleFromProjectAlgorithm
110//
111
112QgsStyleFromProjectAlgorithm::QgsStyleFromProjectAlgorithm() = default;
113
114QgsStyleFromProjectAlgorithm::~QgsStyleFromProjectAlgorithm() = default;
115
116void QgsStyleFromProjectAlgorithm::initAlgorithm( const QVariantMap & )
117{
118 addParameter( new QgsProcessingParameterFile( u"INPUT"_s, QObject::tr( "Input project (leave blank to use current)" ), Qgis::ProcessingFileParameterBehavior::File, QString(), QVariant(), true, QObject::tr( "QGIS files" ) + u" (*.qgs *.qgz *.QGS)"_s ) );
119
120 addParameter( new QgsProcessingParameterFileDestination( u"OUTPUT"_s, QObject::tr( "Output style database" ), QObject::tr( "Style files (*.xml)" ) ) );
121
122 const QStringList options = QStringList()
123 << QObject::tr( "Symbols" )
124 << QObject::tr( "Color ramps" )
125 << QObject::tr( "Text formats" )
126 << QObject::tr( "Label settings" );
127 addParameter( new QgsProcessingParameterEnum( u"OBJECTS"_s, QObject::tr( "Objects to extract" ), options, true, QVariantList() << 0 << 1 << 2 << 3 ) );
128 addOutput( new QgsProcessingOutputNumber( u"SYMBOLS"_s, QObject::tr( "Symbol count" ) ) );
129 addOutput( new QgsProcessingOutputNumber( u"COLORRAMPS"_s, QObject::tr( "Color ramp count" ) ) );
130 addOutput( new QgsProcessingOutputNumber( u"TEXTFORMATS"_s, QObject::tr( "Text format count" ) ) );
131 addOutput( new QgsProcessingOutputNumber( u"LABELSETTINGS"_s, QObject::tr( "Label settings count" ) ) );
132}
133
134QString QgsStyleFromProjectAlgorithm::name() const
135{
136 return u"stylefromproject"_s;
137}
138
139QString QgsStyleFromProjectAlgorithm::displayName() const
140{
141 return QObject::tr( "Create style database from project" );
142}
143
144QStringList QgsStyleFromProjectAlgorithm::tags() const
145{
146 return QObject::tr( "symbols,color,ramps,colors,formats,labels,text,fonts" ).split( ',' );
147}
148
149QString QgsStyleFromProjectAlgorithm::group() const
150{
151 return QObject::tr( "Cartography" );
152}
153
154QString QgsStyleFromProjectAlgorithm::groupId() const
155{
156 return u"cartography"_s;
157}
158
159QString QgsStyleFromProjectAlgorithm::shortHelpString() const
160{
161 return QObject::tr( "This algorithm extracts all style objects (including symbols, color ramps, text formats and label settings) from a QGIS project.\n\n"
162 "The extracted symbols are saved to a QGIS style database (XML format), which can be managed and imported via the Style Manager dialog." );
163}
164
165QString QgsStyleFromProjectAlgorithm::shortDescription() const
166{
167 return QObject::tr( "Creates a style database by extracting all symbols, color ramps, text formats and label settings from a QGIS project." );
168}
169
170QgsStyleFromProjectAlgorithm *QgsStyleFromProjectAlgorithm::createInstance() const
171{
172 return new QgsStyleFromProjectAlgorithm();
173}
174
175bool QgsStyleFromProjectAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
176{
177 mProjectPath = parameterAsFile( parameters, u"INPUT"_s, context );
178 if ( mProjectPath.isEmpty() && !context.project() )
179 return false;
180
181 const QList<int> selectedObjects = parameterAsEnums( parameters, u"OBJECTS"_s, context );
182 if ( selectedObjects.contains( 0 ) )
183 mObjects << QgsStyle::SymbolEntity;
184 if ( selectedObjects.contains( 1 ) )
185 mObjects << QgsStyle::ColorrampEntity;
186 if ( selectedObjects.contains( 2 ) )
187 mObjects << QgsStyle::TextFormatEntity;
188 if ( selectedObjects.contains( 3 ) )
190
191 mStyle = std::make_unique<QgsStyle>();
192 mStyle->createMemoryDatabase();
193
194 if ( mProjectPath.isEmpty() )
195 {
196 // using current project -- not thread safe, so prepare in the main thread
197 QgsSaveToStyleVisitor visitor( mStyle.get(), mObjects );
198 context.project()->accept( &visitor );
199 }
200 return true;
201}
202
203QVariantMap QgsStyleFromProjectAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
204{
205 if ( !mProjectPath.isEmpty() )
206 {
207 // load project from path
210 {
211 throw QgsProcessingException( QObject::tr( "Could not read project %1" ).arg( mProjectPath ) );
212 }
213
214 QgsSaveToStyleVisitor visitor( mStyle.get(), mObjects );
215 p.accept( &visitor );
216 }
217
218 const QString file = parameterAsString( parameters, u"OUTPUT"_s, context );
219 if ( !mStyle->exportXml( file ) )
220 {
221 throw QgsProcessingException( QObject::tr( "Error saving style database as %1" ).arg( file ) );
222 }
223
224 QVariantMap results;
225 results.insert( u"OUTPUT"_s, file );
226 results.insert( u"SYMBOLS"_s, mStyle->symbolCount() );
227 results.insert( u"COLORRAMPS"_s, mStyle->colorRampCount() );
228 results.insert( u"TEXTFORMATS"_s, mStyle->textFormatCount() );
229 results.insert( u"LABELSETTINGS"_s, mStyle->labelSettingsCount() );
230 return results;
231}
232
@ File
Parameter is a single file.
Definition qgis.h:3860
@ DontLoad3DViews
Skip loading 3D views.
Definition qgis.h:4401
@ DontUpgradeAnnotations
Don't upgrade old annotation items to QgsAnnotationItem.
Definition qgis.h:4404
@ DontResolveLayers
Don't resolve layer paths (i.e. don't load any layer content). Dramatically improves project read tim...
Definition qgis.h:4397
QFlags< ProjectCapability > ProjectCapabilities
Flags which control project capabilities.
Definition qgis.h:4437
Contains information about the context in which a processing algorithm is executed.
QgsProject * project() const
Returns the project in which the algorithm is being executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
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...
An input file or folder parameter for processing algorithms.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:113
bool accept(QgsStyleEntityVisitorInterface *visitor) const
Accepts the specified style entity visitor, causing it to visit all style entities associated with th...
virtual QgsStyle::StyleEntity type() const =0
Returns the type of style entity.
@ LayoutItem
Individual item in a print layout.
@ SymbolRule
Rule based symbology or label child rule.
A database of saved style entities, including symbols, color ramps, text formats and others.
Definition qgsstyle.h:89
@ 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
Contains information relating to a node (i.e.
QString description
A string describing the node.
QgsStyleEntityVisitorInterface::NodeType type
Node type.
Contains information relating to the style entity currently being visited.
QString description
A string describing the style entity.
const QgsStyleEntityInterface * entity
Reference to style entity being visited.