QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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;
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,
289  };
290 
302  QgsProcessingToolboxModel( QObject *parent SIP_TRANSFERTHIS = nullptr, QgsProcessingRegistry *registry = nullptr,
303  QgsProcessingRecentAlgorithmLog *recentLog = nullptr );
304 
305  Qt::ItemFlags flags( const QModelIndex &index ) const override;
306  QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
307  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
308  int columnCount( const QModelIndex & = QModelIndex() ) const override;
309  QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
310  QModelIndex parent( const QModelIndex &index ) const override;
311  QMimeData *mimeData( const QModelIndexList &indexes ) const override;
312 
317  QgsProcessingToolboxModelNode *index2node( const QModelIndex &index ) const;
318 
323  QModelIndex node2index( QgsProcessingToolboxModelNode *node ) const;
324 
332  QgsProcessingProvider *providerForIndex( const QModelIndex &index ) const;
333 
341  QString providerIdForIndex( const QModelIndex &index ) const;
342 
350  const QgsProcessingAlgorithm *algorithmForIndex( const QModelIndex &index ) const;
351 
357  bool isAlgorithm( const QModelIndex &index ) const;
358 
363  QModelIndex indexForProvider( const QString &providerId ) const;
364 
368  QModelIndex indexOfParentTreeNode( QgsProcessingToolboxModelNode *parentNode ) const;
369 
370  signals:
371 
375  void recentAlgorithmAdded();
376 
377  private slots:
378 
379  void rebuild();
380  void repopulateRecentAlgorithms( bool resetting = false );
381  void providerAdded( const QString &id );
382  void providerRemoved( const QString &id );
383 
384  private:
385 
386  QPointer< QgsProcessingRegistry > mRegistry;
387  QPointer< QgsProcessingRecentAlgorithmLog > mRecentLog;
388 
389  std::unique_ptr< QgsProcessingToolboxModelGroupNode > mRootNode;
390  QgsProcessingToolboxModelRecentNode *mRecentNode = nullptr;
391 
392  void addProvider( QgsProcessingProvider *provider );
393 
398  static bool isTopLevelProvider( const QString &providerId );
399 
403  static QString toolTipForAlgorithm( const QgsProcessingAlgorithm *algorithm );
404 
405 };
406 
407 
416 class GUI_EXPORT QgsProcessingToolboxProxyModel: public QSortFilterProxyModel
417 {
418  Q_OBJECT
419 
420  public:
421 
423  enum Filter
424  {
425  FilterToolbox = 1 << 1,
426  FilterModeler = 1 << 2,
427  FilterInPlace = 1 << 3,
428  FilterShowKnownIssues = 1 << 4,
429  };
430  Q_DECLARE_FLAGS( Filters, Filter )
431  Q_FLAG( Filters )
432 
433 
444  explicit QgsProcessingToolboxProxyModel( QObject *parent SIP_TRANSFERTHIS = nullptr,
445  QgsProcessingRegistry *registry = nullptr,
446  QgsProcessingRecentAlgorithmLog *recentLog = nullptr );
447 
451  QgsProcessingToolboxModel *toolboxModel();
452 
457  void setFilters( QgsProcessingToolboxProxyModel::Filters filters );
458 
463  Filters filters() const { return mFilters; }
464 
468  void setInPlaceLayer( QgsVectorLayer *layer );
469 
479  void setFilterString( const QString &filter );
480 
486  QString filterString() const { return mFilterString; }
487 
488  bool filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const override;
489  bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override;
490 
491  private:
492 
493  QgsProcessingToolboxModel *mModel = nullptr;
494  Filters mFilters = nullptr;
495  QString mFilterString;
496  QPointer<QgsVectorLayer> mInPlaceLayer;
497 };
498 
499 
500 #endif // QGSPROCESSINGTOOLBOXMODEL_H
Roles
Custom roles used by the model.
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
Filters filters() const
Returns any filters that affect how toolbox content is filtered.
Abstract base class for processing providers.
Abstract base class for processing algorithms.
Untranslated algorithm name, for algorithm nodes.
Returns the node&#39;s algorithm flags, for algorithm nodes.
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_END
Definition: qgis_sip.h:189
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
A model for providers and algorithms shown within the Processing toolbox.
Short algorithm description, for algorithm nodes.
Registry for various processing components, including providers, algorithms and various parameters an...
A sort/filter proxy model for providers and algorithms shown within the Processing toolbox...
QString filterString() const
Returns the current filter string, if set.
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:172
Filter
Available filter flags for filtering the model.
List of algorithm tags, for algorithm nodes.
Represents a vector layer which manages a vector based data sets.