QGIS API Documentation 4.3.0-Master (16eddb2670e)
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 QVariantMap definition = model.toVariant().toMap();
96 definition.insert( u"qgis_version"_s, Qgis::version() );
97 mModelDefinitions.insert( model.name(), definition );
99}
100
101void QgsProcessingProjectModelProvider::removeModel( const QgsProcessingModelAlgorithm *model )
102{
103 if ( !model )
104 return;
105
106 if ( mModelDefinitions.contains( model->name() ) )
107 {
108 mModelDefinitions.remove( model->name() );
110 }
111}
112
113void QgsProcessingProjectModelProvider::readProject( const QDomDocument &document )
114{
115 mModelDefinitions.clear();
116 const QDomNodeList projectModelsNodes = document.elementsByTagName( u"projectModels"_s );
117 if ( !projectModelsNodes.isEmpty() )
118 {
119 const QDomNode projectModelsNode = projectModelsNodes.at( 0 );
120 const QDomNodeList modelNodes = projectModelsNode.childNodes();
121 for ( int i = 0; i < modelNodes.count(); ++i )
122 {
123 const QDomElement modelElement = modelNodes.at( i ).toElement();
124 const QVariant definition = QgsXmlUtils::readVariant( modelElement );
125 QgsProcessingModelAlgorithm algorithm;
126 if ( algorithm.loadVariant( definition ) )
127 {
128 mModelDefinitions.insert( algorithm.name(), definition );
129 }
130 }
131 }
132
134}
135
137{
138 const QDomNodeList qgisNodes = document.elementsByTagName( u"qgis"_s );
139 if ( qgisNodes.isEmpty() )
140 return;
141
142 QDomNode qgisNode = qgisNodes.at( 0 );
143 QDomElement projectModelsNode = document.createElement( u"projectModels"_s );
144
145 const QList< const QgsProcessingAlgorithm * > algs = algorithms();
146 for ( const QgsProcessingAlgorithm *algorithm : std::as_const( algs ) )
147 {
148 auto model = dynamic_cast< const QgsProcessingModelAlgorithm *>( algorithm );
149 if ( !model )
150 continue;
151
152 const QVariant definition = model->toVariant();
153 const QDomElement element = QgsXmlUtils::writeVariant( definition, document );
154 projectModelsNode.appendChild( element );
155 }
156
157 qgisNode.appendChild( projectModelsNode );
158}
159
161{
162 if ( mIsLoading )
163 return;
164
165 mIsLoading = true;
166
167 for ( auto it = mModelDefinitions.constBegin(); it != mModelDefinitions.constEnd(); ++it )
168 {
169 auto algorithm = std::make_unique< QgsProcessingModelAlgorithm >();
170 if ( algorithm->loadVariant( it.value() ) )
171 {
172 addAlgorithm( algorithm.release() );
173 }
174 else
175 {
176 QgsMessageLog::logMessage( tr( "Could not load model from project" ), tr( "Processing" ), Qgis::MessageLevel::Critical );
177 }
178 }
179
180 mIsLoading = false;
181}
182
183void QgsProcessingProjectModelProvider::onProviderAdded( const QString &providerId )
184{
185 if ( providerId == id() )
186 return;
187
189}
static QString version()
Version string.
Definition qgis.cpp:682
@ 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.