QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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 
25 class QgsVectorLayer;
29 class QgsProcessingToolboxModelGroupNode;
30 class QgsProcessingRecentAlgorithmLog;
31 
33 
40 class GUI_EXPORT QgsProcessingToolboxModelNode : public QObject
41 {
42  Q_OBJECT
43 
44 #ifdef SIP_RUN
46  if ( sipCpp->inherits( "QgsProcessingToolboxModelNode" ) )
47  {
48  sipType = sipType_QgsProcessingToolboxModelNode;
49  QgsProcessingToolboxModelNode *node = qobject_cast<QgsProcessingToolboxModelNode *>( sipCpp );
50  if ( node->nodeType() == QgsProcessingToolboxModelNode::NodeProvider )
51  sipType = sipType_QgsProcessingToolboxModelProviderNode;
52  else if ( node->nodeType() == QgsProcessingToolboxModelNode::NodeGroup )
53  sipType = sipType_QgsProcessingToolboxModelGroupNode;
54  else if ( node->nodeType() == QgsProcessingToolboxModelNode::NodeAlgorithm )
55  sipType = sipType_QgsProcessingToolboxModelAlgorithmNode;
56  else if ( node->nodeType() == QgsProcessingToolboxModelNode::NodeRecent )
57  sipType = sipType_QgsProcessingToolboxModelRecentNode;
58  }
59  else
60  sipType = 0;
61  SIP_END
62 #endif
63 
64  public:
65 
67  enum NodeType
68  {
69  NodeProvider = 0,
70  NodeGroup,
71  NodeAlgorithm,
72  NodeRecent,
73  };
74 
75  ~QgsProcessingToolboxModelNode() override;
76 
80  virtual NodeType nodeType() const = 0;
81 
85  QgsProcessingToolboxModelNode *parent() { return mParent; }
86 
90  QList<QgsProcessingToolboxModelNode *> children() { return mChildren; }
91 
96  QList<QgsProcessingToolboxModelNode *> children() const { return mChildren; } SIP_SKIP
97 
102  QgsProcessingToolboxModelNode *takeChild( QgsProcessingToolboxModelNode *node );
103 
109  QgsProcessingToolboxModelGroupNode *getChildGroupNode( const QString &id );
110 
115  void addChildNode( QgsProcessingToolboxModelNode *node SIP_TRANSFER );
116 
120  void deleteChildren();
121 
122  private:
123 
124  NodeType mNodeType = NodeProvider;
125  QgsProcessingToolboxModelNode *mParent = nullptr;
126  QList<QgsProcessingToolboxModelNode *> mChildren;
127 
128 };
129 
136 class GUI_EXPORT QgsProcessingToolboxModelRecentNode : public QgsProcessingToolboxModelNode
137 {
138  Q_OBJECT
139 
140  public:
141 
145  QgsProcessingToolboxModelRecentNode() = default;
146 
147  NodeType nodeType() const override { return NodeRecent; }
148 
149 };
150 
157 class GUI_EXPORT QgsProcessingToolboxModelProviderNode : public QgsProcessingToolboxModelNode
158 {
159  Q_OBJECT
160 
161  public:
162 
167  QgsProcessingToolboxModelProviderNode( QgsProcessingProvider *provider );
168 
169  NodeType nodeType() const override { return NodeProvider; }
170 
174  QgsProcessingProvider *provider();
175 
179  QString providerId() const { return mProviderId; }
180 
181  private:
182 
183  // NOTE: we store both the provider ID and a pointer to the provider here intentionally.
184  // We store the provider pointer to avoid having to lookup the provider from the registry
185  // every time the node is used (which kills performance in the filter proxy model), but
186  // we also store the provider id string in order to identify the provider that the node
187  // is linked to for cleanups after the provider is removed.
188  QString mProviderId;
189  QPointer< QgsProcessingProvider > mProvider;
190 
191 };
192 
199 class GUI_EXPORT QgsProcessingToolboxModelGroupNode : public QgsProcessingToolboxModelNode
200 {
201  Q_OBJECT
202 
203  public:
204 
212  QgsProcessingToolboxModelGroupNode( const QString &id, const QString &name );
213 
214  NodeType nodeType() const override { return NodeGroup; }
215 
219  QString id() const { return mId; }
220 
224  QString name() const { return mName; }
225 
226  private:
227  QString mId;
228  QString mName;
229 
230 };
231 
238 class GUI_EXPORT QgsProcessingToolboxModelAlgorithmNode : public QgsProcessingToolboxModelNode
239 {
240  Q_OBJECT
241 
242  public:
243 
248  QgsProcessingToolboxModelAlgorithmNode( const QgsProcessingAlgorithm *algorithm );
249 
250  NodeType nodeType() const override { return NodeAlgorithm; }
251 
255  const QgsProcessingAlgorithm *algorithm() const;
256 
257  private:
258 
259  const QgsProcessingAlgorithm *mAlgorithm = nullptr;
260 
261 };
262 
264 
274 class GUI_EXPORT QgsProcessingToolboxModel : public QAbstractItemModel
275 {
276  Q_OBJECT
277 
278  public:
279 
281  enum Roles
282  {
283  RoleNodeType = Qt::UserRole,
290  };
291 
303  QgsProcessingToolboxModel( QObject *parent SIP_TRANSFERTHIS = nullptr, QgsProcessingRegistry *registry = nullptr,
304  QgsProcessingRecentAlgorithmLog *recentLog = nullptr );
305 
306  Qt::ItemFlags flags( const QModelIndex &index ) const override;
307  QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
308  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
309  int columnCount( const QModelIndex & = QModelIndex() ) const override;
310  QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
311  QModelIndex parent( const QModelIndex &index ) const override;
312  QMimeData *mimeData( const QModelIndexList &indexes ) const override;
313 
318  QgsProcessingToolboxModelNode *index2node( const QModelIndex &index ) const;
319 
324  QModelIndex node2index( QgsProcessingToolboxModelNode *node ) const;
325 
333  QgsProcessingProvider *providerForIndex( const QModelIndex &index ) const;
334 
342  QString providerIdForIndex( const QModelIndex &index ) const;
343 
351  const QgsProcessingAlgorithm *algorithmForIndex( const QModelIndex &index ) const;
352 
358  bool isAlgorithm( const QModelIndex &index ) const;
359 
364  QModelIndex indexForProvider( const QString &providerId ) const;
365 
369  QModelIndex indexOfParentTreeNode( QgsProcessingToolboxModelNode *parentNode ) const;
370 
371  signals:
372 
377 
378  private slots:
379 
380  void rebuild();
381  void repopulateRecentAlgorithms( bool resetting = false );
382  void providerAdded( const QString &id );
383  void providerRemoved( const QString &id );
384 
385  private:
386 
387  QPointer< QgsProcessingRegistry > mRegistry;
388  QPointer< QgsProcessingRecentAlgorithmLog > mRecentLog;
389 
390  std::unique_ptr< QgsProcessingToolboxModelGroupNode > mRootNode;
391  QgsProcessingToolboxModelRecentNode *mRecentNode = nullptr;
392 
393  void addProvider( QgsProcessingProvider *provider );
394 
399  static bool isTopLevelProvider( const QString &providerId );
400 
404  static QString toolTipForAlgorithm( const QgsProcessingAlgorithm *algorithm );
405 
406 };
407 
408 
417 class GUI_EXPORT QgsProcessingToolboxProxyModel: public QSortFilterProxyModel
418 {
419  Q_OBJECT
420 
421  public:
422 
424  enum Filter
425  {
426  FilterToolbox = 1 << 1,
427  FilterModeler = 1 << 2,
428  FilterInPlace = 1 << 3,
429  FilterShowKnownIssues = 1 << 4,
430  };
431  Q_DECLARE_FLAGS( Filters, Filter )
432  Q_FLAG( Filters )
433 
434 
445  explicit QgsProcessingToolboxProxyModel( QObject *parent SIP_TRANSFERTHIS = nullptr,
446  QgsProcessingRegistry *registry = nullptr,
447  QgsProcessingRecentAlgorithmLog *recentLog = nullptr );
448 
452  QgsProcessingToolboxModel *toolboxModel();
453 
458  const QgsProcessingToolboxModel *toolboxModel() const SIP_SKIP;
459 
464  void setFilters( QgsProcessingToolboxProxyModel::Filters filters );
465 
470  Filters filters() const { return mFilters; }
471 
475  void setInPlaceLayer( QgsVectorLayer *layer );
476 
486  void setFilterString( const QString &filter );
487 
493  QString filterString() const { return mFilterString; }
494 
495  bool filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const override;
496  bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override;
497  QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
498 
499  private:
500 
501  QgsProcessingToolboxModel *mModel = nullptr;
502  Filters mFilters = Filters();
503  QString mFilterString;
504  QPointer<QgsVectorLayer> mInPlaceLayer;
505 };
506 
507 
508 #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.
Roles
Custom roles used by the model.
@ RoleProviderFlags
Returns the node's provider flags.
@ RoleAlgorithmShortDescription
Short algorithm description, for algorithm nodes.
@ RoleAlgorithmTags
List of algorithm tags, for algorithm nodes.
@ RoleAlgorithmId
Algorithm ID, for algorithm nodes.
@ RoleAlgorithmFlags
Returns the node's algorithm flags, for algorithm nodes.
@ RoleAlgorithmName
Untranslated algorithm name, for algorithm nodes.
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:177
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_END
Definition: qgis_sip.h:194