QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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 QgsProcessingToolboxModelNode *mParent = nullptr;
133 QList<QgsProcessingToolboxModelNode *> mChildren;
134
135};
136
143class GUI_EXPORT QgsProcessingToolboxModelRecentNode : public QgsProcessingToolboxModelNode
144{
145 Q_OBJECT
146
147 public:
148
149 QgsProcessingToolboxModelRecentNode() = default;
150
151 NodeType nodeType() const override { return NodeType::Recent; }
152
153};
154
161class GUI_EXPORT QgsProcessingToolboxModelFavoriteNode : public QgsProcessingToolboxModelNode
162{
163 Q_OBJECT
164
165 public:
166
167 QgsProcessingToolboxModelFavoriteNode() = default;
168
169 NodeType nodeType() const override { return NodeType::Favorite; }
170
171};
172
179class GUI_EXPORT QgsProcessingToolboxModelProviderNode : public QgsProcessingToolboxModelNode
180{
181 Q_OBJECT
182
183 public:
184
189 QgsProcessingToolboxModelProviderNode( QgsProcessingProvider *provider );
190
191 NodeType nodeType() const override { return NodeType::Provider; }
192
196 QgsProcessingProvider *provider();
197
201 QString providerId() const { return mProviderId; }
202
203 private:
204
205 // NOTE: we store both the provider ID and a pointer to the provider here intentionally.
206 // We store the provider pointer to avoid having to lookup the provider from the registry
207 // every time the node is used (which kills performance in the filter proxy model), but
208 // we also store the provider id string in order to identify the provider that the node
209 // is linked to for cleanups after the provider is removed.
210 QString mProviderId;
211 QPointer< QgsProcessingProvider > mProvider;
212
213};
214
221class GUI_EXPORT QgsProcessingToolboxModelGroupNode : public QgsProcessingToolboxModelNode
222{
223 Q_OBJECT
224
225 public:
226
234 QgsProcessingToolboxModelGroupNode( const QString &id, const QString &name );
235
236 NodeType nodeType() const override { return NodeType::Group; }
237
241 QString id() const { return mId; }
242
246 QString name() const { return mName; }
247
248 private:
249 QString mId;
250 QString mName;
251
252};
253
260class GUI_EXPORT QgsProcessingToolboxModelAlgorithmNode : public QgsProcessingToolboxModelNode
261{
262 Q_OBJECT
263
264 public:
265
270 QgsProcessingToolboxModelAlgorithmNode( const QgsProcessingAlgorithm *algorithm );
271
272 NodeType nodeType() const override { return NodeType::Algorithm; }
273
277 const QgsProcessingAlgorithm *algorithm() const;
278
279 private:
280
281 const QgsProcessingAlgorithm *mAlgorithm = nullptr;
282
283};
284
286
296class GUI_EXPORT QgsProcessingToolboxModel : public QAbstractItemModel
297{
298 Q_OBJECT
299
300 public:
301
302 // *INDENT-OFF*
303
311 {
312 NodeType SIP_MONKEYPATCH_COMPAT_NAME(RoleNodeType) = Qt::UserRole,
313 AlgorithmFlags SIP_MONKEYPATCH_COMPAT_NAME(RoleAlgorithmFlags),
314 AlgorithmId SIP_MONKEYPATCH_COMPAT_NAME(RoleAlgorithmId),
315 AlgorithmName SIP_MONKEYPATCH_COMPAT_NAME(RoleAlgorithmName),
316 AlgorithmShortDescription SIP_MONKEYPATCH_COMPAT_NAME(RoleAlgorithmShortDescription),
317 AlgorithmTags SIP_MONKEYPATCH_COMPAT_NAME(RoleAlgorithmTags),
318 ProviderFlags SIP_MONKEYPATCH_COMPAT_NAME(RoleProviderFlags),
319 };
320 Q_ENUM( CustomRole )
321 // *INDENT-ON*
322
323
337 QgsProcessingToolboxModel( QObject *parent SIP_TRANSFERTHIS = nullptr, QgsProcessingRegistry *registry = nullptr,
338 QgsProcessingRecentAlgorithmLog *recentLog = nullptr,
339 QgsProcessingFavoriteAlgorithmManager *favoriteManager = nullptr );
340
341 Qt::ItemFlags flags( const QModelIndex &index ) const override;
342 QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
343 int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
344 int columnCount( const QModelIndex & = QModelIndex() ) const override;
345 QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
346 QModelIndex parent( const QModelIndex &index ) const override;
347 QMimeData *mimeData( const QModelIndexList &indexes ) const override;
348
353 QgsProcessingToolboxModelNode *index2node( const QModelIndex &index ) const;
354
359 QModelIndex node2index( QgsProcessingToolboxModelNode *node ) const;
360
368 QgsProcessingProvider *providerForIndex( const QModelIndex &index ) const;
369
377 QString providerIdForIndex( const QModelIndex &index ) const;
378
386 const QgsProcessingAlgorithm *algorithmForIndex( const QModelIndex &index ) const;
387
393 bool isAlgorithm( const QModelIndex &index ) const;
394
399 QModelIndex indexForProvider( const QString &providerId ) const;
400
404 QModelIndex indexOfParentTreeNode( QgsProcessingToolboxModelNode *parentNode ) const;
405
406 signals:
407
412
417
418 private slots:
419
420 void rebuild();
421 void repopulateRecentAlgorithms( bool resetting = false );
422 void repopulateFavoriteAlgorithms( bool resetting = false );
423 void providerAdded( const QString &id );
424 void providerRemoved( const QString &id );
425
426 private:
427
428 QPointer< QgsProcessingRegistry > mRegistry;
429 QPointer< QgsProcessingRecentAlgorithmLog > mRecentLog;
430 QPointer< QgsProcessingFavoriteAlgorithmManager > mFavoriteManager;
431
432 std::unique_ptr< QgsProcessingToolboxModelGroupNode > mRootNode;
433 QgsProcessingToolboxModelRecentNode *mRecentNode = nullptr;
434 QgsProcessingToolboxModelFavoriteNode *mFavoriteNode = nullptr;
435
436 void addProvider( QgsProcessingProvider *provider );
437
442 static bool isTopLevelProvider( const QString &providerId );
443
447 static QString toolTipForAlgorithm( const QgsProcessingAlgorithm *algorithm );
448
449};
450
451
460class GUI_EXPORT QgsProcessingToolboxProxyModel: public QSortFilterProxyModel
461{
462 Q_OBJECT
463
464 public:
465
466 // *INDENT-OFF*
467
470 {
471 Toolbox SIP_MONKEYPATCH_COMPAT_NAME( FilterToolbox ) = 1 << 1,
472 Modeler SIP_MONKEYPATCH_COMPAT_NAME( FilterModeler ) = 1 << 2,
473 InPlace SIP_MONKEYPATCH_COMPAT_NAME( FilterInPlace ) = 1 << 3,
474 ShowKnownIssues SIP_MONKEYPATCH_COMPAT_NAME( FilterShowKnownIssues ) = 1 << 4,
475 };
476 Q_ENUM( Filter )
477 Q_DECLARE_FLAGS( Filters, Filter )
478 Q_FLAG( Filters )
479 // *INDENT-ON*
480
495 explicit QgsProcessingToolboxProxyModel( QObject *parent SIP_TRANSFERTHIS = nullptr,
496 QgsProcessingRegistry *registry = nullptr,
497 QgsProcessingRecentAlgorithmLog *recentLog = nullptr,
498 QgsProcessingFavoriteAlgorithmManager *favoriteManager = nullptr );
499
503 QgsProcessingToolboxModel *toolboxModel();
504
509 const QgsProcessingToolboxModel *toolboxModel() const SIP_SKIP;
510
515 void setFilters( QgsProcessingToolboxProxyModel::Filters filters );
516
521 Filters filters() const { return mFilters; }
522
526 void setInPlaceLayer( QgsVectorLayer *layer );
527
537 void setFilterString( const QString &filter );
538
544 QString filterString() const { return mFilterString; }
545
546 bool filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const override;
547 bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override;
548 QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
549
550 private:
551
552 QgsProcessingToolboxModel *mModel = nullptr;
553 Filters mFilters = Filters();
554 QString mFilterString;
555 QPointer<QgsVectorLayer> mInPlaceLayer;
556};
558
559#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)