QGIS API Documentation 3.39.0-Master (3aed037ce22)
Loading...
Searching...
No Matches
qgsprocessingtoolboxmodel.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsprocessingtoolboxmodel.h
3 ---------------------------
4 begin : May 2018
5 copyright : (C) 2018 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#ifndef QGSPROCESSINGTOOLBOXMODEL_H
17#define QGSPROCESSINGTOOLBOXMODEL_H
18
19#include "qgis.h"
20#include "qgis_gui.h"
21#include <QAbstractItemModel>
22#include <QSortFilterProxyModel>
23#include <QPointer>
24
25class QgsVectorLayer;
29class QgsProcessingToolboxModelGroupNode;
30class QgsProcessingRecentAlgorithmLog;
31class QgsProcessingFavoriteAlgorithmManager;
32
34
41class GUI_EXPORT QgsProcessingToolboxModelNode : public QObject
42{
43 Q_OBJECT
44
45#ifdef SIP_RUN
47 if ( sipCpp->inherits( "QgsProcessingToolboxModelNode" ) )
48 {
49 sipType = sipType_QgsProcessingToolboxModelNode;
50 QgsProcessingToolboxModelNode *node = qobject_cast<QgsProcessingToolboxModelNode *>( sipCpp );
51 if ( node->nodeType() == QgsProcessingToolboxModelNode::NodeType::Provider )
52 sipType = sipType_QgsProcessingToolboxModelProviderNode;
53 else if ( node->nodeType() == QgsProcessingToolboxModelNode::NodeType::Group )
54 sipType = sipType_QgsProcessingToolboxModelGroupNode;
55 else if ( node->nodeType() == QgsProcessingToolboxModelNode::NodeType::Algorithm )
56 sipType = sipType_QgsProcessingToolboxModelAlgorithmNode;
57 else if ( node->nodeType() == QgsProcessingToolboxModelNode::NodeType::Recent )
58 sipType = sipType_QgsProcessingToolboxModelRecentNode;
59 else if ( node->nodeType() == QgsProcessingToolboxModelNode::NodeType::Favorite )
60 sipType = sipType_QgsProcessingToolboxModelFavoriteNode;
61 }
62 else
63 sipType = 0;
65#endif
66
67 public:
68
69 // *INDENT-OFF*
70
72 enum class NodeType SIP_MONKEYPATCH_SCOPEENUM_UNNEST( QgsProcessingToolboxModelNode, NodeType ) : int
73 {
74 Provider SIP_MONKEYPATCH_COMPAT_NAME( NodeProvider ) = 0,
75 Group SIP_MONKEYPATCH_COMPAT_NAME( NodeGroup ),
76 Algorithm SIP_MONKEYPATCH_COMPAT_NAME( NodeAlgorithm ),
77 Recent SIP_MONKEYPATCH_COMPAT_NAME( NodeRecent ),
78 Favorite,
79 };
80 Q_ENUM( NodeType )
81 // *INDENT-ON*
82
83 ~QgsProcessingToolboxModelNode() override;
84
88 virtual NodeType nodeType() const = 0;
89
93 QgsProcessingToolboxModelNode *parent() { return mParent; }
94
98 QList<QgsProcessingToolboxModelNode *> children() { return mChildren; }
99
104 QList<QgsProcessingToolboxModelNode *> children() const { return mChildren; } SIP_SKIP
105
110 QgsProcessingToolboxModelNode *takeChild( QgsProcessingToolboxModelNode *node );
111
117 QgsProcessingToolboxModelGroupNode *getChildGroupNode( const QString &id );
118
123 void addChildNode( QgsProcessingToolboxModelNode *node SIP_TRANSFER );
124
128 void deleteChildren();
129
130 private:
131
132 NodeType mNodeType = NodeType::Provider;
133 QgsProcessingToolboxModelNode *mParent = nullptr;
134 QList<QgsProcessingToolboxModelNode *> mChildren;
135
136};
137
144class GUI_EXPORT QgsProcessingToolboxModelRecentNode : public QgsProcessingToolboxModelNode
145{
146 Q_OBJECT
147
148 public:
149
150 QgsProcessingToolboxModelRecentNode() = default;
151
152 NodeType nodeType() const override { return NodeType::Recent; }
153
154};
155
162class GUI_EXPORT QgsProcessingToolboxModelFavoriteNode : public QgsProcessingToolboxModelNode
163{
164 Q_OBJECT
165
166 public:
167
168 QgsProcessingToolboxModelFavoriteNode() = default;
169
170 NodeType nodeType() const override { return NodeType::Favorite; }
171
172};
173
180class GUI_EXPORT QgsProcessingToolboxModelProviderNode : public QgsProcessingToolboxModelNode
181{
182 Q_OBJECT
183
184 public:
185
190 QgsProcessingToolboxModelProviderNode( QgsProcessingProvider *provider );
191
192 NodeType nodeType() const override { return NodeType::Provider; }
193
197 QgsProcessingProvider *provider();
198
202 QString providerId() const { return mProviderId; }
203
204 private:
205
206 // NOTE: we store both the provider ID and a pointer to the provider here intentionally.
207 // We store the provider pointer to avoid having to lookup the provider from the registry
208 // every time the node is used (which kills performance in the filter proxy model), but
209 // we also store the provider id string in order to identify the provider that the node
210 // is linked to for cleanups after the provider is removed.
211 QString mProviderId;
212 QPointer< QgsProcessingProvider > mProvider;
213
214};
215
222class GUI_EXPORT QgsProcessingToolboxModelGroupNode : public QgsProcessingToolboxModelNode
223{
224 Q_OBJECT
225
226 public:
227
235 QgsProcessingToolboxModelGroupNode( const QString &id, const QString &name );
236
237 NodeType nodeType() const override { return NodeType::Group; }
238
242 QString id() const { return mId; }
243
247 QString name() const { return mName; }
248
249 private:
250 QString mId;
251 QString mName;
252
253};
254
261class GUI_EXPORT QgsProcessingToolboxModelAlgorithmNode : public QgsProcessingToolboxModelNode
262{
263 Q_OBJECT
264
265 public:
266
271 QgsProcessingToolboxModelAlgorithmNode( const QgsProcessingAlgorithm *algorithm );
272
273 NodeType nodeType() const override { return NodeType::Algorithm; }
274
278 const QgsProcessingAlgorithm *algorithm() const;
279
280 private:
281
282 const QgsProcessingAlgorithm *mAlgorithm = nullptr;
283
284};
285
287
297class GUI_EXPORT QgsProcessingToolboxModel : public QAbstractItemModel
298{
299 Q_OBJECT
300
301 public:
302
303 // *INDENT-OFF*
304
312 {
313 NodeType SIP_MONKEYPATCH_COMPAT_NAME(RoleNodeType) = Qt::UserRole,
314 AlgorithmFlags SIP_MONKEYPATCH_COMPAT_NAME(RoleAlgorithmFlags),
315 AlgorithmId SIP_MONKEYPATCH_COMPAT_NAME(RoleAlgorithmId),
316 AlgorithmName SIP_MONKEYPATCH_COMPAT_NAME(RoleAlgorithmName),
317 AlgorithmShortDescription SIP_MONKEYPATCH_COMPAT_NAME(RoleAlgorithmShortDescription),
318 AlgorithmTags SIP_MONKEYPATCH_COMPAT_NAME(RoleAlgorithmTags),
319 ProviderFlags SIP_MONKEYPATCH_COMPAT_NAME(RoleProviderFlags),
320 };
321 Q_ENUM( CustomRole )
322 // *INDENT-ON*
323
324
338 QgsProcessingToolboxModel( QObject *parent SIP_TRANSFERTHIS = nullptr, QgsProcessingRegistry *registry = nullptr,
339 QgsProcessingRecentAlgorithmLog *recentLog = nullptr,
340 QgsProcessingFavoriteAlgorithmManager *favoriteManager = nullptr );
341
342 Qt::ItemFlags flags( const QModelIndex &index ) const override;
343 QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
344 int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
345 int columnCount( const QModelIndex & = QModelIndex() ) const override;
346 QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
347 QModelIndex parent( const QModelIndex &index ) const override;
348 QMimeData *mimeData( const QModelIndexList &indexes ) const override;
349
354 QgsProcessingToolboxModelNode *index2node( const QModelIndex &index ) const;
355
360 QModelIndex node2index( QgsProcessingToolboxModelNode *node ) const;
361
369 QgsProcessingProvider *providerForIndex( const QModelIndex &index ) const;
370
378 QString providerIdForIndex( const QModelIndex &index ) const;
379
387 const QgsProcessingAlgorithm *algorithmForIndex( const QModelIndex &index ) const;
388
394 bool isAlgorithm( const QModelIndex &index ) const;
395
400 QModelIndex indexForProvider( const QString &providerId ) const;
401
405 QModelIndex indexOfParentTreeNode( QgsProcessingToolboxModelNode *parentNode ) const;
406
407 signals:
408
413
418
419 private slots:
420
421 void rebuild();
422 void repopulateRecentAlgorithms( bool resetting = false );
423 void repopulateFavoriteAlgorithms( bool resetting = false );
424 void providerAdded( const QString &id );
425 void providerRemoved( const QString &id );
426
427 private:
428
429 QPointer< QgsProcessingRegistry > mRegistry;
430 QPointer< QgsProcessingRecentAlgorithmLog > mRecentLog;
431 QPointer< QgsProcessingFavoriteAlgorithmManager > mFavoriteManager;
432
433 std::unique_ptr< QgsProcessingToolboxModelGroupNode > mRootNode;
434 QgsProcessingToolboxModelRecentNode *mRecentNode = nullptr;
435 QgsProcessingToolboxModelFavoriteNode *mFavoriteNode = nullptr;
436
437 void addProvider( QgsProcessingProvider *provider );
438
443 static bool isTopLevelProvider( const QString &providerId );
444
448 static QString toolTipForAlgorithm( const QgsProcessingAlgorithm *algorithm );
449
450};
451
452
461class GUI_EXPORT QgsProcessingToolboxProxyModel: public QSortFilterProxyModel
462{
463 Q_OBJECT
464
465 public:
466
467 // *INDENT-OFF*
468
471 {
472 Toolbox SIP_MONKEYPATCH_COMPAT_NAME( FilterToolbox ) = 1 << 1,
473 Modeler SIP_MONKEYPATCH_COMPAT_NAME( FilterModeler ) = 1 << 2,
474 InPlace SIP_MONKEYPATCH_COMPAT_NAME( FilterInPlace ) = 1 << 3,
475 ShowKnownIssues SIP_MONKEYPATCH_COMPAT_NAME( FilterShowKnownIssues ) = 1 << 4,
476 };
477 Q_ENUM( Filter )
478 Q_DECLARE_FLAGS( Filters, Filter )
479 Q_FLAG( Filters )
480 // *INDENT-ON*
481
496 explicit QgsProcessingToolboxProxyModel( QObject *parent SIP_TRANSFERTHIS = nullptr,
497 QgsProcessingRegistry *registry = nullptr,
498 QgsProcessingRecentAlgorithmLog *recentLog = nullptr,
499 QgsProcessingFavoriteAlgorithmManager *favoriteManager = nullptr );
500
504 QgsProcessingToolboxModel *toolboxModel();
505
510 const QgsProcessingToolboxModel *toolboxModel() const SIP_SKIP;
511
516 void setFilters( QgsProcessingToolboxProxyModel::Filters filters );
517
522 Filters filters() const { return mFilters; }
523
527 void setInPlaceLayer( QgsVectorLayer *layer );
528
538 void setFilterString( const QString &filter );
539
545 QString filterString() const { return mFilterString; }
546
547 bool filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const override;
548 bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override;
549 QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
550
551 private:
552
553 QgsProcessingToolboxModel *mModel = nullptr;
554 Filters mFilters = Filters();
555 QString mFilterString;
556 QPointer<QgsVectorLayer> mInPlaceLayer;
557};
559
560#endif // QGSPROCESSINGTOOLBOXMODEL_H
Abstract base class for processing algorithms.
Abstract base class for processing providers.
Registry for various processing components, including providers, algorithms and various parameters an...
A model for providers and algorithms shown within the Processing toolbox.
void recentAlgorithmAdded()
Emitted whenever recent algorithms are added to the model.
void favoriteAlgorithmAdded()
Emitted whenever favorite algorithms are added to the model.
A sort/filter proxy model for providers and algorithms shown within the Processing toolbox,...
QString filterString() const
Returns the current filter string, if set.
Filter
Available filter flags for filtering the model.
Represents a vector layer which manages a vector based data sets.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into allowing algorithms to be written in pure substantial changes are required in order to port existing x Processing algorithms for QGIS x The most significant changes are outlined not GeoAlgorithm For algorithms which operate on features one by consider subclassing the QgsProcessingFeatureBasedAlgorithm class This class allows much of the boilerplate code for looping over features from a vector layer to be bypassed and instead requires implementation of a processFeature method Ensure that your algorithm(or algorithm 's parent class) implements the new pure virtual createInstance(self) call
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition qgis_sip.h:191
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:53
#define SIP_ENUM_BASETYPE(type)
Definition qgis_sip.h:278
#define SIP_SKIP
Definition qgis_sip.h:126
#define SIP_MONKEYPATCH_SCOPEENUM_UNNEST(OUTSIDE_CLASS, FORMERNAME)
Definition qgis_sip.h:271
#define SIP_TRANSFER
Definition qgis_sip.h:36
#define SIP_END
Definition qgis_sip.h:208
#define SIP_MONKEYPATCH_COMPAT_NAME(FORMERNAME)
Definition qgis_sip.h:273
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsTextRendererUtils::CurvedTextFlags)