QGIS API Documentation 3.41.0-Master (af5edcb665c)
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 // *INDENT-OFF*
69
71 enum class NodeType SIP_MONKEYPATCH_SCOPEENUM_UNNEST( QgsProcessingToolboxModelNode, NodeType ) : int
72 {
73 Provider SIP_MONKEYPATCH_COMPAT_NAME( NodeProvider ) = 0,
74 Group SIP_MONKEYPATCH_COMPAT_NAME( NodeGroup ),
75 Algorithm SIP_MONKEYPATCH_COMPAT_NAME( NodeAlgorithm ),
76 Recent SIP_MONKEYPATCH_COMPAT_NAME( NodeRecent ),
77 Favorite,
78 };
79 Q_ENUM( NodeType )
80 // *INDENT-ON*
81
82 ~QgsProcessingToolboxModelNode() override;
83
87 virtual NodeType nodeType() const = 0;
88
92 QgsProcessingToolboxModelNode *parent() { return mParent; }
93
97 QList<QgsProcessingToolboxModelNode *> children() { return mChildren; }
98
103 QList<QgsProcessingToolboxModelNode *> children() const SIP_SKIP
104 {
105 return mChildren;
106 }
107
112 QgsProcessingToolboxModelNode *takeChild( QgsProcessingToolboxModelNode *node );
113
119 QgsProcessingToolboxModelGroupNode *getChildGroupNode( const QString &id );
120
125 void addChildNode( QgsProcessingToolboxModelNode *node SIP_TRANSFER );
126
130 void deleteChildren();
131
132 private:
133 QgsProcessingToolboxModelNode *mParent = nullptr;
134 QList<QgsProcessingToolboxModelNode *> mChildren;
135};
136
143class GUI_EXPORT QgsProcessingToolboxModelRecentNode : public QgsProcessingToolboxModelNode
144{
145 Q_OBJECT
146
147 public:
148 QgsProcessingToolboxModelRecentNode() = default;
149
150 NodeType nodeType() const override { return NodeType::Recent; }
151};
152
159class GUI_EXPORT QgsProcessingToolboxModelFavoriteNode : public QgsProcessingToolboxModelNode
160{
161 Q_OBJECT
162
163 public:
164 QgsProcessingToolboxModelFavoriteNode() = default;
165
166 NodeType nodeType() const override { return NodeType::Favorite; }
167};
168
175class GUI_EXPORT QgsProcessingToolboxModelProviderNode : public QgsProcessingToolboxModelNode
176{
177 Q_OBJECT
178
179 public:
184 QgsProcessingToolboxModelProviderNode( QgsProcessingProvider *provider );
185
186 NodeType nodeType() const override { return NodeType::Provider; }
187
191 QgsProcessingProvider *provider();
192
196 QString providerId() const { return mProviderId; }
197
198 private:
199 // NOTE: we store both the provider ID and a pointer to the provider here intentionally.
200 // We store the provider pointer to avoid having to lookup the provider from the registry
201 // every time the node is used (which kills performance in the filter proxy model), but
202 // we also store the provider id string in order to identify the provider that the node
203 // is linked to for cleanups after the provider is removed.
204 QString mProviderId;
205 QPointer<QgsProcessingProvider> mProvider;
206};
207
214class GUI_EXPORT QgsProcessingToolboxModelGroupNode : public QgsProcessingToolboxModelNode
215{
216 Q_OBJECT
217
218 public:
226 QgsProcessingToolboxModelGroupNode( const QString &id, const QString &name );
227
228 NodeType nodeType() const override { return NodeType::Group; }
229
233 QString id() const { return mId; }
234
238 QString name() const { return mName; }
239
240 private:
241 QString mId;
242 QString mName;
243};
244
251class GUI_EXPORT QgsProcessingToolboxModelAlgorithmNode : public QgsProcessingToolboxModelNode
252{
253 Q_OBJECT
254
255 public:
260 QgsProcessingToolboxModelAlgorithmNode( const QgsProcessingAlgorithm *algorithm );
261
262 NodeType nodeType() const override { return NodeType::Algorithm; }
263
267 const QgsProcessingAlgorithm *algorithm() const;
268
269 private:
270 const QgsProcessingAlgorithm *mAlgorithm = nullptr;
271};
272
274
284class GUI_EXPORT QgsProcessingToolboxModel : public QAbstractItemModel
285{
286 Q_OBJECT
287
288 public:
289 // *INDENT-OFF*
290
298 {
299 NodeType SIP_MONKEYPATCH_COMPAT_NAME( RoleNodeType ) = Qt::UserRole,
300 AlgorithmFlags SIP_MONKEYPATCH_COMPAT_NAME( RoleAlgorithmFlags ),
301 AlgorithmId SIP_MONKEYPATCH_COMPAT_NAME( RoleAlgorithmId ),
302 AlgorithmName SIP_MONKEYPATCH_COMPAT_NAME( RoleAlgorithmName ),
303 AlgorithmShortDescription SIP_MONKEYPATCH_COMPAT_NAME( RoleAlgorithmShortDescription ),
304 AlgorithmTags SIP_MONKEYPATCH_COMPAT_NAME( RoleAlgorithmTags ),
305 ProviderFlags SIP_MONKEYPATCH_COMPAT_NAME( RoleProviderFlags ),
306 };
307 Q_ENUM( CustomRole )
308 // *INDENT-ON*
309
310
324 QgsProcessingToolboxModel( QObject *parent SIP_TRANSFERTHIS = nullptr, QgsProcessingRegistry *registry = nullptr, QgsProcessingRecentAlgorithmLog *recentLog = nullptr, QgsProcessingFavoriteAlgorithmManager *favoriteManager = nullptr );
325
326 Qt::ItemFlags flags( const QModelIndex &index ) const override;
327 QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
328 int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
329 int columnCount( const QModelIndex & = QModelIndex() ) const override;
330 QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
331 QModelIndex parent( const QModelIndex &index ) const override;
332 QMimeData *mimeData( const QModelIndexList &indexes ) const override;
333
338 QgsProcessingToolboxModelNode *index2node( const QModelIndex &index ) const;
339
344 QModelIndex node2index( QgsProcessingToolboxModelNode *node ) const;
345
353 QgsProcessingProvider *providerForIndex( const QModelIndex &index ) const;
354
362 QString providerIdForIndex( const QModelIndex &index ) const;
363
371 const QgsProcessingAlgorithm *algorithmForIndex( const QModelIndex &index ) const;
372
378 bool isAlgorithm( const QModelIndex &index ) const;
379
384 QModelIndex indexForProvider( const QString &providerId ) const;
385
389 QModelIndex indexOfParentTreeNode( QgsProcessingToolboxModelNode *parentNode ) const;
390
391 signals:
392
397
402
403 private slots:
404
405 void rebuild();
406 void repopulateRecentAlgorithms( bool resetting = false );
407 void repopulateFavoriteAlgorithms( bool resetting = false );
408 void providerAdded( const QString &id );
409 void providerRemoved( const QString &id );
410
411 private:
412 QPointer<QgsProcessingRegistry> mRegistry;
413 QPointer<QgsProcessingRecentAlgorithmLog> mRecentLog;
414 QPointer<QgsProcessingFavoriteAlgorithmManager> mFavoriteManager;
415
416 std::unique_ptr<QgsProcessingToolboxModelGroupNode> mRootNode;
417 QgsProcessingToolboxModelRecentNode *mRecentNode = nullptr;
418 QgsProcessingToolboxModelFavoriteNode *mFavoriteNode = nullptr;
419
420 void addProvider( QgsProcessingProvider *provider );
421
426 static bool isTopLevelProvider( const QString &providerId );
427
431 static QString toolTipForAlgorithm( const QgsProcessingAlgorithm *algorithm );
432};
433
434
443class GUI_EXPORT QgsProcessingToolboxProxyModel : public QSortFilterProxyModel
444{
445 Q_OBJECT
446
447 public:
448 // *INDENT-OFF*
449
452 {
453 Toolbox SIP_MONKEYPATCH_COMPAT_NAME( FilterToolbox ) = 1 << 1,
454 Modeler SIP_MONKEYPATCH_COMPAT_NAME( FilterModeler ) = 1 << 2,
455 InPlace SIP_MONKEYPATCH_COMPAT_NAME( FilterInPlace ) = 1 << 3,
456 ShowKnownIssues SIP_MONKEYPATCH_COMPAT_NAME( FilterShowKnownIssues ) = 1 << 4,
457 };
458 Q_ENUM( Filter )
459 Q_DECLARE_FLAGS( Filters, Filter )
460 Q_FLAG( Filters )
461 // *INDENT-ON*
462
477 explicit QgsProcessingToolboxProxyModel( QObject *parent SIP_TRANSFERTHIS = nullptr, QgsProcessingRegistry *registry = nullptr, QgsProcessingRecentAlgorithmLog *recentLog = nullptr, QgsProcessingFavoriteAlgorithmManager *favoriteManager = nullptr );
478
482 QgsProcessingToolboxModel *toolboxModel();
483
488 const QgsProcessingToolboxModel *toolboxModel() const SIP_SKIP;
489
494 void setFilters( QgsProcessingToolboxProxyModel::Filters filters );
495
500 Filters filters() const { return mFilters; }
501
505 void setInPlaceLayer( QgsVectorLayer *layer );
506
516 void setFilterString( const QString &filter );
517
523 QString filterString() const { return mFilterString; }
524
525 bool filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const override;
526 bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override;
527 QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
528
529 private:
530 QgsProcessingToolboxModel *mModel = nullptr;
531 Filters mFilters = Filters();
532 QString mFilterString;
533 QPointer<QgsVectorLayer> mInPlaceLayer;
534};
536
537#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)