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