QGIS API Documentation 4.3.0-Master (16649ce5cc6)
Loading...
Searching...
No Matches
qgsprocessingdefaultstyleregistry.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsprocessingdefaultstyleregistry.cpp
3 ------------------------
4 begin : Septeber 2026
5 copyright : (C) 2026 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 "qgsprocessingutils.h"
21
22#include <QDir>
23#include <QFile>
24#include <QString>
25
26#include "moc_qgsprocessingdefaultstyleregistry.cpp"
27
28using namespace Qt::StringLiterals;
29
31 : QObject( parent )
32{}
33
34void QgsProcessingDefaultStyleRegistry::setDefaultStyleForOutput( const QString &algorithmId, const QString &outputName, const QString &qmlPath )
35{
36 if ( qmlPath.isEmpty() )
37 {
38 if ( mStyles[algorithmId].contains( outputName ) )
39 {
40 mStyles[algorithmId].remove( outputName );
41 if ( mStyles[algorithmId].isEmpty() )
42 {
43 mStyles.remove( algorithmId );
44 }
45 }
46 }
47 else
48 {
49 mStyles[algorithmId][outputName] = qmlPath;
50 }
51}
52
53QString QgsProcessingDefaultStyleRegistry::defaultStyleForOutput( const QString &algorithmId, const QString &outputName ) const
54{
55 return mStyles.value( algorithmId ).value( outputName );
56}
57
58QString QgsProcessingDefaultStyleRegistry::configFilePath()
59{
60 const QDir dir( QgsProcessingUtils::userFolder() );
61 return dir.filePath( u"processing_qgis_styles.conf"_s );
62}
63
65{
66 const QString filePath = configFilePath();
67 if ( !QFile::exists( filePath ) )
68 return;
69
70 QFile file( filePath );
71 if ( !file.open( QIODevice::ReadOnly ) )
72 return;
73
74 QTextStream in( &file );
75 const QString originalFileContent = in.readAll();
76 file.close();
77
78 const QStringList lines = originalFileContent.split( '\n' );
79 for ( const QString &line : lines )
80 {
81 const QStringList lineParts = line.split( '|' );
82 if ( lineParts.size() < 3 )
83 continue;
84
85 const QString algorithmId = lineParts.at( 0 );
86 const QString outputName = lineParts.at( 1 );
87 if ( algorithmId.isEmpty() || outputName.isEmpty() )
88 continue;
89
90 const QString qmlFilePath = lineParts.mid( 2 ).join( '|' );
91
92 mStyles[algorithmId][outputName] = qmlFilePath;
93 }
94}
95
97{
98 const QString filePath = configFilePath();
99
100 QFile file( filePath );
101 if ( !file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
102 {
103 file.close();
104 return;
105 }
106
107 QTextStream out( &file );
108 for ( auto algIt = mStyles.constBegin(); algIt != mStyles.constEnd(); ++algIt )
109 {
110 for ( auto outputIt = algIt->constBegin(); outputIt != algIt->constEnd(); ++outputIt )
111 {
112 out << algIt.key() << '|' << outputIt.key() << '|' << outputIt.value() << "\n";
113 }
114 }
115
116 file.close();
117}
void loadStyles()
Loads all saved styles from the configuration file.
QString defaultStyleForOutput(const QString &algorithmId, const QString &outputName) const
Returns the QML path to the default style to use for the specified output from an algorithm,...
void saveStyles()
Saves registered styles to the configuration file.
void setDefaultStyleForOutput(const QString &algorithmId, const QString &outputName, const QString &qmlPath)
Sets the default style for an output from an algorithm.
QgsProcessingDefaultStyleRegistry(QObject *parent=nullptr)
Constructor for QgsProcessingDefaultStyleRegistry.
static QString userFolder()
Returns the location for Processing files stored within the user's current QGIS profile.