QGIS API Documentation 4.3.0-Master (90cb4ecf9ef)
Loading...
Searching...
No Matches
qgsprocessingprojectmodelprovider.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsprocessingprojectmodelprovider.cpp
3 ------------------------
4 begin : August 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 "qgsapplication.h"
21#include "qgsmessagelog.h"
24#include "qgsproject.h"
25#include "qgsruntimeprofiler.h"
26#include "qgsxmlutils.h"
27
28#include <QString>
29
30#include "moc_qgsprocessingprojectmodelprovider.cpp"
31
32using namespace Qt::StringLiterals;
33
35 : QgsProcessingProvider( parent )
36 , mProject( project )
37{
38 // must reload models if providers list is changed - previously unavailable algorithms
39 // which models depend on may now be available
40 connect( QgsApplication::processingRegistry(), &QgsProcessingRegistry::providerAdded, this, &QgsProcessingProjectModelProvider::onProviderAdded );
41
42 if ( mProject )
43 {
47 }
48}
49
51{
52 return tr( "Project models" );
53}
54
56{
57 return tr( "Models embedded in the current project" );
58}
59
64
66{
67 return QgsApplication::getThemeIcon( u"/mIconQgsProjectFile.svg"_s );
68}
69
71{
72 return QgsApplication::iconPath( u"mIconQgsProjectFile.svg"_s );
73}
74
79
81{
82 QgsScopedRuntimeProfile profiler( u"Project Provider"_s );
84 return true;
85}
86
88{
89 mModelDefinitions.clear();
91}
92
93void QgsProcessingProjectModelProvider::addModel( const QgsProcessingModelAlgorithm &model )
94{
95 const QVariant definition = model.toVariant();
96 mModelDefinitions.insert( model.name(), definition );
98}
99
100void QgsProcessingProjectModelProvider::removeModel( const QgsProcessingModelAlgorithm *model )
101{
102 if ( !model )
103 return;
104
105 if ( mModelDefinitions.contains( model->name() ) )
106 {
107 mModelDefinitions.remove( model->name() );
109 }
110}
111
112void QgsProcessingProjectModelProvider::readProject( const QDomDocument &document )
113{
114 mModelDefinitions.clear();
115 const QDomNodeList projectModelsNodes = document.elementsByTagName( u"projectModels"_s );
116 if ( !projectModelsNodes.isEmpty() )
117 {
118 const QDomNode projectModelsNode = projectModelsNodes.at( 0 );
119 const QDomNodeList modelNodes = projectModelsNode.childNodes();
120 for ( int i = 0; i < modelNodes.count(); ++i )
121 {
122 const QDomElement modelElement = modelNodes.at( i ).toElement();
123 const QVariant definition = QgsXmlUtils::readVariant( modelElement );
124 QgsProcessingModelAlgorithm algorithm;
125 if ( algorithm.loadVariant( definition ) )
126 {
127 mModelDefinitions.insert( algorithm.name(), definition );
128 }
129 }
130 }
131
133}
134
136{
137 const QDomNodeList qgisNodes = document.elementsByTagName( u"qgis"_s );
138 if ( qgisNodes.isEmpty() )
139 return;
140
141 QDomNode qgisNode = qgisNodes.at( 0 );
142 QDomElement projectModelsNode = document.createElement( u"projectModels"_s );
143
144 const QList< const QgsProcessingAlgorithm * > algs = algorithms();
145 for ( const QgsProcessingAlgorithm *algorithm : std::as_const( algs ) )
146 {
147 auto model = dynamic_cast< const QgsProcessingModelAlgorithm *>( algorithm );
148 if ( !model )
149 continue;
150
151 const QVariant definition = model->toVariant();
152 const QDomElement element = QgsXmlUtils::writeVariant( definition, document );
153 projectModelsNode.appendChild( element );
154 }
155
156 qgisNode.appendChild( projectModelsNode );
157}
158
160{
161 if ( mIsLoading )
162 return;
163
164 mIsLoading = true;
165
166 for ( auto it = mModelDefinitions.constBegin(); it != mModelDefinitions.constEnd(); ++it )
167 {
168 auto algorithm = std::make_unique< QgsProcessingModelAlgorithm >();
169 if ( algorithm->loadVariant( it.value() ) )
170 {
171 addAlgorithm( algorithm.release() );
172 }
173 else
174 {
175 QgsMessageLog::logMessage( tr( "Could not load model from project" ), tr( "Processing" ), Qgis::MessageLevel::Critical );
176 }
177 }
178
179 mIsLoading = false;
180}
181
182void QgsProcessingProjectModelProvider::onProviderAdded( const QString &providerId )
183{
184 if ( providerId == id() )
185 return;
186
188}
@ Critical
Critical/error message.
Definition qgis.h:163
static QgsProcessingRegistry * processingRegistry()
Returns the application's processing registry, used for managing processing providers,...
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static QString iconPath(const QString &iconFile)
Returns path to the desired icon file.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE(), Qgis::StringFormat format=Qgis::StringFormat::PlainText)
Adds a message to the log instance (and creates it if necessary).
Abstract base class for processing algorithms.
QString longName() const override
Returns the longer version of the provider name, which can include extra details such as version numb...
void writeProject(QDomDocument &document)
Writes project model definitions into a project DOM document document.
void loadAlgorithms() override
Loads all algorithms belonging to this provider.
void readProject(const QDomDocument &document)
Reads project model definitions from a project DOM document document.
QString id() const override
Returns the unique provider id, used for identifying the provider.
void removeModel(const QgsProcessingModelAlgorithm *model)
Removes a model from the provider.
QIcon icon() const override
Returns an icon for the provider.
QString name() const override
Returns the provider name, which is used to describe the provider within the GUI.
bool supportsNonFileBasedOutput() const override
Returns true if the provider supports non-file based outputs (such as memory layers or direct databas...
void addModel(const QgsProcessingModelAlgorithm &model)
Adds a model to the provider.
QString svgIconPath() const override
Returns a path to an SVG version of the provider's icon.
void clear()
Clears all models from the provider.
QgsProcessingProjectModelProvider(QgsProject *project, QObject *parent=nullptr)
Constructor for QgsProcessingProjectModelProvider, storing models in the specified project.
const QgsProcessingAlgorithm * algorithm(const QString &name) const
Returns the matching algorithm by name, or nullptr if no matching algorithm is contained by this prov...
QgsProcessingProvider(QObject *parent=nullptr)
Constructor for QgsProcessingProvider.
void refreshAlgorithms()
Refreshes the algorithms available from the provider, causing it to re-populate with all associated a...
bool addAlgorithm(QgsProcessingAlgorithm *algorithm)
Adds an algorithm to the provider.
QList< const QgsProcessingAlgorithm * > algorithms() const
Returns a list of algorithms supplied by this provider.
void providerAdded(const QString &id)
Emitted when a provider has been added to the registry.
static const QString PROJECT_PROVIDER_ID
Constant used for the project model provider's ID.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:114
void cleared()
Emitted when the project is cleared (and additionally when an open project is cleared just before a n...
void readProject(const QDomDocument &document)
Emitted when a project is being read.
void writeProject(QDomDocument &document)
Emitted when the project is being written.
Scoped object for logging of the runtime for a single operation or group of operations.
static QDomElement writeVariant(const QVariant &value, QDomDocument &doc)
Write a QVariant to a QDomElement.
static QVariant readVariant(const QDomElement &element)
Read a QVariant from a QDomElement.