Skip to content

Commit afd2af4

Browse files
committed
Use default style library if none specified
1 parent 5f5294f commit afd2af4

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

src/analysis/processing/qgsalgorithmcategorizeusingstyle.cpp

+17-6
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void QgsCategorizeUsingStyleAlgorithm::initAlgorithm( const QVariantMap & )
3232
QList< int >() << QgsProcessing::TypeVector ) );
3333
addParameter( new QgsProcessingParameterExpression( QStringLiteral( "FIELD" ), QObject::tr( "Categorize using expression" ), QVariant(), QStringLiteral( "INPUT" ) ) );
3434

35-
addParameter( new QgsProcessingParameterFile( QStringLiteral( "STYLE" ), QObject::tr( "Style database" ), QgsProcessingParameterFile::File, QStringLiteral( "xml" ) ) );
35+
addParameter( new QgsProcessingParameterFile( QStringLiteral( "STYLE" ), QObject::tr( "Style database (leave blank to use saved symbols)" ), QgsProcessingParameterFile::File, QStringLiteral( "xml" ), QVariant(), true ) );
3636
addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "CASE_SENSITIVE" ), QObject::tr( "Use case-sensitive match to symbol names" ), false ) );
3737
addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "TOLERANT" ), QObject::tr( "Ignore non-alphanumeric characters while matching" ), false ) );
3838

@@ -77,7 +77,8 @@ QString QgsCategorizeUsingStyleAlgorithm::groupId() const
7777

7878
QString QgsCategorizeUsingStyleAlgorithm::shortHelpString() const
7979
{
80-
return QObject::tr( "Sets a vector layer's renderer to a categorized renderer using matching symbols from a style database.\n\n"
80+
return QObject::tr( "Sets a vector layer's renderer to a categorized renderer using matching symbols from a style database. If no "
81+
"style file is specified, symbols from the user's current style library are used instead.\n\n"
8182
"The specified expression (or field name) is used to create categories for the renderer. A category will be "
8283
"created for each unique value within the layer.\n\n"
8384
"Each category is individually matched to the symbols which exist within the specified QGIS XML style database. Whenever "
@@ -161,10 +162,20 @@ QVariantMap QgsCategorizeUsingStyleAlgorithm::processAlgorithm( const QVariantMa
161162
const bool caseSensitive = parameterAsBool( parameters, QStringLiteral( "CASE_SENSITIVE" ), context );
162163
const bool tolerant = parameterAsBool( parameters, QStringLiteral( "TOLERANT" ), context );
163164

164-
QgsStyle style;
165-
if ( !style.importXml( styleFile ) )
165+
QgsStyle *style = nullptr;
166+
std::unique_ptr< QgsStyle >importedStyle;
167+
if ( !styleFile.isEmpty() )
166168
{
167-
throw QgsProcessingException( QObject::tr( "An error occurred while reading style file: %1" ).arg( style.errorString() ) );
169+
importedStyle = qgis::make_unique< QgsStyle >();
170+
if ( !importedStyle->importXml( styleFile ) )
171+
{
172+
throw QgsProcessingException( QObject::tr( "An error occurred while reading style file: %1" ).arg( importedStyle->errorString() ) );
173+
}
174+
style = importedStyle.get();
175+
}
176+
else
177+
{
178+
style = QgsStyle::defaultStyle();
168179
}
169180

170181
QgsFields nonMatchingCategoryFields;
@@ -211,7 +222,7 @@ QVariantMap QgsCategorizeUsingStyleAlgorithm::processAlgorithm( const QVariantMa
211222

212223
QVariantList unmatchedCategories;
213224
QStringList unmatchedSymbols;
214-
const int matched = mRenderer->matchToSymbols( &style, type, unmatchedCategories, unmatchedSymbols, caseSensitive, tolerant );
225+
const int matched = mRenderer->matchToSymbols( style, type, unmatchedCategories, unmatchedSymbols, caseSensitive, tolerant );
215226

216227
if ( matched > 0 )
217228
{

0 commit comments

Comments
 (0)