QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
23
24//
25// QgsProjectToStyleVisitor
26//
27
28QgsSaveToStyleVisitor::QgsSaveToStyleVisitor( QgsStyle *style, const QList<QgsStyle::StyleEntity> &objects )
29 : mStyle( style )
30 , mObjects( objects )
31{
32}
33
34bool QgsSaveToStyleVisitor::visit( const QgsStyleEntityVisitorInterface::StyleLeaf &entity )
35{
36 if ( mObjects.empty() || mObjects.contains( entity.entity->type() ) )
37 {
38 const QString name = QString( mParentNames.join( ' ' ) + ' ' + entity.description ).trimmed();
39 QString candidate = name;
40 int i = 1;
41 bool exists = true;
42 while ( exists )
43 {
44 exists = mStyle->allNames( entity.entity->type() ).contains( candidate );
45 if ( !exists )
46 break;
47
48 i++;
49 candidate = name + QStringLiteral( " (%1)" ).arg( i );
50 }
51 mStyle->addEntity( candidate, entity.entity, true );
52 }
53 return true;
54}
55
56bool QgsSaveToStyleVisitor::visitEnter( const QgsStyleEntityVisitorInterface::Node &node )
57{
58 switch ( node.type )
59 {
67 break;
68
74 mParentNames << node.description;
75 break;
76 }
77 return true;
78}
79
80bool QgsSaveToStyleVisitor::visitExit( const QgsStyleEntityVisitorInterface::Node &node )
81{
82 switch ( node.type )
83 {
91 break;
92
98 mParentNames.pop_back();
99 break;
100 }
101 return true;
102}
103
104//
105// QgsStyleFromProjectAlgorithm
106//
107
108QgsStyleFromProjectAlgorithm::QgsStyleFromProjectAlgorithm() = default;
109
110QgsStyleFromProjectAlgorithm::~QgsStyleFromProjectAlgorithm() = default;
111
112void QgsStyleFromProjectAlgorithm::initAlgorithm( const QVariantMap & )
113{
114 addParameter( new QgsProcessingParameterFile( QStringLiteral( "INPUT" ), QObject::tr( "Input project (leave blank to use current)" ), Qgis::ProcessingFileParameterBehavior::File, QString(), QVariant(), true, QObject::tr( "QGIS files" ) + QStringLiteral( " (*.qgs *.qgz *.QGS)" ) ) );
115
116 addParameter( new QgsProcessingParameterFileDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "Output style database" ), QObject::tr( "Style files (*.xml)" ) ) );
117
118 const QStringList options = QStringList()
119 << QObject::tr( "Symbols" )
120 << QObject::tr( "Color ramps" )
121 << QObject::tr( "Text formats" )
122 << QObject::tr( "Label settings" );
123 addParameter( new QgsProcessingParameterEnum( QStringLiteral( "OBJECTS" ), QObject::tr( "Objects to extract" ), options, true, QVariantList() << 0 << 1 << 2 << 3 ) );
124 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "SYMBOLS" ), QObject::tr( "Symbol count" ) ) );
125 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "COLORRAMPS" ), QObject::tr( "Color ramp count" ) ) );
126 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "TEXTFORMATS" ), QObject::tr( "Text format count" ) ) );
127 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "LABELSETTINGS" ), QObject::tr( "Label settings count" ) ) );
128}
129
130QString QgsStyleFromProjectAlgorithm::name() const
131{
132 return QStringLiteral( "stylefromproject" );
133}
134
135QString QgsStyleFromProjectAlgorithm::displayName() const
136{
137 return QObject::tr( "Create style database from project" );
138}
139
140QStringList QgsStyleFromProjectAlgorithm::tags() const
141{
142 return QObject::tr( "symbols,color,ramps,colors,formats,labels,text,fonts" ).split( ',' );
143}
144
145QString QgsStyleFromProjectAlgorithm::group() const
146{
147 return QObject::tr( "Cartography" );
148}
149
150QString QgsStyleFromProjectAlgorithm::groupId() const
151{
152 return QStringLiteral( "cartography" );
153}
154
155QString QgsStyleFromProjectAlgorithm::shortHelpString() const
156{
157 return QObject::tr( "This algorithm extracts all style objects (including symbols, color ramps, text formats and label settings) from a QGIS project.\n\n"
158 "The extracted symbols are saved to a QGIS style database (XML format), which can be managed and imported via the Style Manager dialog." );
159}
160
161QString QgsStyleFromProjectAlgorithm::shortDescription() const
162{
163 return QObject::tr( "Creates a style database by extracting all symbols, color ramps, text formats and label settings from a QGIS project." );
164}
165
166QgsStyleFromProjectAlgorithm *QgsStyleFromProjectAlgorithm::createInstance() const
167{
168 return new QgsStyleFromProjectAlgorithm();
169}
170
171bool QgsStyleFromProjectAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
172{
173 mProjectPath = parameterAsFile( parameters, QStringLiteral( "INPUT" ), context );
174 if ( mProjectPath.isEmpty() && !context.project() )
175 return false;
176
177 const QList<int> selectedObjects = parameterAsEnums( parameters, QStringLiteral( "OBJECTS" ), context );
178 if ( selectedObjects.contains( 0 ) )
179 mObjects << QgsStyle::SymbolEntity;
180 if ( selectedObjects.contains( 1 ) )
181 mObjects << QgsStyle::ColorrampEntity;
182 if ( selectedObjects.contains( 2 ) )
183 mObjects << QgsStyle::TextFormatEntity;
184 if ( selectedObjects.contains( 3 ) )
186
187 mStyle = std::make_unique<QgsStyle>();
188 mStyle->createMemoryDatabase();
189
190 if ( mProjectPath.isEmpty() )
191 {
192 // using current project -- not thread safe, so prepare in the main thread
193 QgsSaveToStyleVisitor visitor( mStyle.get(), mObjects );
194 context.project()->accept( &visitor );
195 }
196 return true;
197}
198
199QVariantMap QgsStyleFromProjectAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
200{
201 if ( !mProjectPath.isEmpty() )
202 {
203 // load project from path
206 {
207 throw QgsProcessingException( QObject::tr( "Could not read project %1" ).arg( mProjectPath ) );
208 }
209
210 QgsSaveToStyleVisitor visitor( mStyle.get(), mObjects );
211 p.accept( &visitor );
212 }
213
214 const QString file = parameterAsString( parameters, QStringLiteral( "OUTPUT" ), context );
215 if ( !mStyle->exportXml( file ) )
216 {
217 throw QgsProcessingException( QObject::tr( "Error saving style database as %1" ).arg( file ) );
218 }
219
220 QVariantMap results;
221 results.insert( QStringLiteral( "OUTPUT" ), file );
222 results.insert( QStringLiteral( "SYMBOLS" ), mStyle->symbolCount() );
223 results.insert( QStringLiteral( "COLORRAMPS" ), mStyle->colorRampCount() );
224 results.insert( QStringLiteral( "TEXTFORMATS" ), mStyle->textFormatCount() );
225 results.insert( QStringLiteral( "LABELSETTINGS" ), mStyle->labelSettingsCount() );
226 return results;
227}
228
@ File
Parameter is a single file.
Definition qgis.h:3789
@ DontLoad3DViews
Skip loading 3D views.
Definition qgis.h:4307
@ DontUpgradeAnnotations
Don't upgrade old annotation items to QgsAnnotationItem.
Definition qgis.h:4310
@ DontResolveLayers
Don't resolve layer paths (i.e. don't load any layer content). Dramatically improves project read tim...
Definition qgis.h:4303
QFlags< ProjectCapability > ProjectCapabilities
Flags which control project capabilities.
Definition qgis.h:4343
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:109
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:88
@ 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
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.