27 QgsSaveToStyleVisitor::QgsSaveToStyleVisitor(
QgsStyle *style,
const QList<QgsStyle::StyleEntity> &objects )
35 if ( mObjects.empty() || mObjects.contains( entity.
entity->
type() ) )
37 const QString name = ( mParentNames.join(
' ' ) +
' ' + entity.
description ).trimmed();
38 QString candidate = name;
43 exists = mStyle->allNames( entity.
entity->
type() ).contains( candidate );
48 candidate = name + QStringLiteral(
" (%1)" ).arg( i );
50 mStyle->addEntity( candidate, entity.
entity,
true );
97 mParentNames.pop_back();
107 QgsStyleFromProjectAlgorithm::QgsStyleFromProjectAlgorithm() =
default;
109 QgsStyleFromProjectAlgorithm::~QgsStyleFromProjectAlgorithm() =
default;
111 void QgsStyleFromProjectAlgorithm::initAlgorithm(
const QVariantMap & )
114 QString(), QVariant(),
true, QObject::tr(
"QGIS files" ) + QStringLiteral(
" (*.qgs *.qgz *.QGS)" ) ) );
117 QObject::tr(
"Style files (*.xml)" ) ) );
119 QStringList options = QStringList()
120 << QObject::tr(
"Symbols" )
121 << QObject::tr(
"Color ramps" )
122 << QObject::tr(
"Text formats" )
123 << QObject::tr(
"Label settings" );
124 addParameter(
new QgsProcessingParameterEnum( QStringLiteral(
"OBJECTS" ), QObject::tr(
"Objects to extract" ), options,
true, QVariantList() << 0 << 1 << 2 << 3 ) );
131 QString QgsStyleFromProjectAlgorithm::name()
const 133 return QStringLiteral(
"stylefromproject" );
136 QString QgsStyleFromProjectAlgorithm::displayName()
const 138 return QObject::tr(
"Create style database from project" );
141 QStringList QgsStyleFromProjectAlgorithm::tags()
const 143 return QObject::tr(
"symbols,color,ramps,colors,formats,labels,text,fonts" ).split(
',' );
146 QString QgsStyleFromProjectAlgorithm::group()
const 148 return QObject::tr(
"Cartography" );
151 QString QgsStyleFromProjectAlgorithm::groupId()
const 153 return QStringLiteral(
"cartography" );
156 QString QgsStyleFromProjectAlgorithm::shortHelpString()
const 158 return QObject::tr(
"This algorithm extracts all style objects (including symbols, color ramps, text formats and label settings) from a QGIS project.\n\n" 159 "The extracted symbols are saved to a QGIS style database (XML format), which can be managed and imported via the Style Manager dialog." );
162 QString QgsStyleFromProjectAlgorithm::shortDescription()
const 164 return QObject::tr(
"Creates a style database by extracting all symbols, color ramps, text formats and label settings from a QGIS project." );
167 QgsStyleFromProjectAlgorithm *QgsStyleFromProjectAlgorithm::createInstance()
const 169 return new QgsStyleFromProjectAlgorithm();
174 mProjectPath = parameterAsFile( parameters, QStringLiteral(
"INPUT" ), context );
175 if ( mProjectPath.isEmpty() && !context.
project() )
178 const QList< int > selectedObjects = parameterAsEnums( parameters, QStringLiteral(
"OBJECTS" ), context );
179 if ( selectedObjects.contains( 0 ) )
181 if ( selectedObjects.contains( 1 ) )
183 if ( selectedObjects.contains( 2 ) )
185 if ( selectedObjects.contains( 3 ) )
188 mStyle = qgis::make_unique< QgsStyle >();
189 mStyle->createMemoryDatabase();
191 if ( mProjectPath.isEmpty() )
194 QgsSaveToStyleVisitor visitor( mStyle.get(), mObjects );
202 if ( !mProjectPath.isEmpty() )
211 QgsSaveToStyleVisitor visitor( mStyle.get(), mObjects );
215 const QString file = parameterAsString( parameters, QStringLiteral(
"OUTPUT" ), context );
216 if ( !mStyle->exportXml( file ) )
222 results.insert( QStringLiteral(
"OUTPUT" ), file );
223 results.insert( QStringLiteral(
"SYMBOLS" ), mStyle->symbolCount() );
224 results.insert( QStringLiteral(
"COLORRAMPS" ), mStyle->colorRampCount() );
225 results.insert( QStringLiteral(
"TEXTFORMATS" ), mStyle->textFormatCount() );
226 results.insert( QStringLiteral(
"LABELSETTINGS" ), mStyle->labelSettingsCount() );
An input file or folder parameter for processing algorithms.
Base class for providing feedback from a processing algorithm.
A numeric output for processing algorithms.
Individual item in a print layout.
QgsProject * project() const
Returns the project in which the algorithm is being executed.
An individual print layout.
An enum based parameter for processing algorithms, allowing for selection from predefined values...
Parameter is a single file.
Custom exception class for processing related exceptions.
Contains information relating to a node (i.e.
QgsStyleEntityVisitorInterface::NodeType type
Node type.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts, annotations, canvases, etc.
An individual annotation.
A generic file based destination parameter, for specifying the destination path for a file (non-map l...
QString description
A string describing the node.
Rule based symbology or label child rule.
virtual QgsStyle::StyleEntity type() const =0
Returns the type of style entity.
QString description
A string describing the style entity.
bool read(const QString &filename, QgsProject::ReadFlags flags=nullptr)
Reads given project file from the given file.
bool accept(QgsStyleEntityVisitorInterface *visitor) const
Accepts the specified style entity visitor, causing it to visit all style entities associated with th...
Don't resolve layer paths (i.e. don't load any layer content). Dramatically improves project read tim...
const QgsStyleEntityInterface * entity
Reference to style entity being visited.
Contains information about the context in which a processing algorithm is executed.
Contains information relating to the style entity currently being visited.