QGIS API Documentation 4.3.0-Master (0ff9723465c)
Loading...
Searching...
No Matches
qgsprocessingmodelprovider.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsprocessingmodelprovider.cpp
3 ------------------------
4 begin : September 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 "qgsruntimeprofiler.h"
25
26#include <QDirIterator>
27#include <QString>
28
29#include "moc_qgsprocessingmodelprovider.cpp"
30
31using namespace Qt::StringLiterals;
32
34 : QgsProcessingProvider( parent )
35{
36 // must reload models if providers list is changed - previously unavailable algorithms
37 // which models depend on may now be available
38 connect( QgsApplication::processingRegistry(), &QgsProcessingRegistry::providerAdded, this, &QgsProcessingModelProvider::onProviderAdded );
39}
40
42{
43 return tr( "Models" );
44}
45
50
52{
53 return QgsApplication::getThemeIcon( u"/processingModel.svg"_s );
54}
55
57{
58 return QgsApplication::iconPath( u"processingModel.svg"_s );
59}
60
62{
63 return true;
64}
65
67{
68 QgsScopedRuntimeProfile profiler( u"Model Provider"_s );
70 return true;
71}
72
74{
75 if ( mIsLoading )
76 return;
77
78 mIsLoading = true;
79
80 const QStringList folders = QgsProcessingUtils::modelFolders();
81 for ( const QString &folder : folders )
82 {
83 loadFromFolder( folder );
84 }
85
86 mIsLoading = false;
87}
88
89void QgsProcessingModelProvider::onProviderAdded( const QString &providerId )
90{
91 if ( providerId == id() )
92 return;
93
95}
96
97void QgsProcessingModelProvider::loadFromFolder( const QString &folder )
98{
99 if ( !QFile::exists( folder ) )
100 return;
101
102 QDirIterator it( folder, QDir::Files, QDirIterator::Subdirectories );
103 while ( it.hasNext() )
104 {
105 it.next();
106 const QString modelFile = it.fileName();
107 if ( QFileInfo( modelFile ).suffix().compare( "model3"_L1, Qt::CaseInsensitive ) == 0 )
108 {
109 const QString fullpath = it.filePath();
110 auto modelAlgorithm = std::make_unique<QgsProcessingModelAlgorithm>();
111 if ( modelAlgorithm->fromFile( fullpath ) )
112 {
113 if ( !modelAlgorithm->name().isEmpty() )
114 {
115 modelAlgorithm->setSourceFilePath( fullpath );
116 addAlgorithm( modelAlgorithm.release() );
117 }
118 }
119 else
120 {
121 QgsMessageLog::logMessage( tr( "Could not load model %1" ).arg( modelFile ), tr( "Processing" ), Qgis::MessageLevel::Critical );
122 }
123 }
124 }
125}
@ 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).
QString svgIconPath() const override
Returns a path to an SVG version of the provider's icon.
QString id() const override
Returns the unique provider id, used for identifying the provider.
bool supportsNonFileBasedOutput() const override
Returns true if the provider supports non-file based outputs (such as memory layers or direct databas...
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.
QgsProcessingModelProvider(QObject *parent=nullptr)
Constructor for QgsProcessingModelProvider.
void loadAlgorithms() override
Loads all algorithms belonging to this provider.
bool load() override
Loads the provider.
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.
void providerAdded(const QString &id)
Emitted when a provider has been added to the registry.
static QStringList modelFolders()
Returns the list of locations for reading Processing model files.
static const QString MODEL_PROVIDER_ID
Constant used for the model provider's ID.
Scoped object for logging of the runtime for a single operation or group of operations.