QGIS API Documentation  3.14.0-Pi (9f7028fd23)
qgsprocessingtoolboxtreeview.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsprocessingtoolboxtreeview.cpp
3  -------------------------------
4  begin : July 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 
18 
20 
21 QgsProcessingToolboxTreeView::QgsProcessingToolboxTreeView( QWidget *parent,
22  QgsProcessingRegistry *registry,
23  QgsProcessingRecentAlgorithmLog *recentLog )
24  : QTreeView( parent )
25 {
26  mModel = new QgsProcessingToolboxProxyModel( this, registry, recentLog );
27  mToolboxModel = mModel->toolboxModel();
28  setModel( mModel );
29 }
30 
31 void QgsProcessingToolboxTreeView::setRegistry( QgsProcessingRegistry *registry, QgsProcessingRecentAlgorithmLog *recentLog )
32 {
33  QgsProcessingToolboxProxyModel *newModel = new QgsProcessingToolboxProxyModel( this, registry, recentLog );
34  mToolboxModel = newModel->toolboxModel();
35  setModel( newModel );
36  mModel->deleteLater();
37  mModel = newModel;
38 }
39 
40 void QgsProcessingToolboxTreeView::setToolboxProxyModel( QgsProcessingToolboxProxyModel *model )
41 {
42  mToolboxModel = mModel->toolboxModel();
43  setModel( model );
44  mModel->deleteLater();
45  mModel = model;
46 }
47 
48 void QgsProcessingToolboxTreeView::setFilterString( const QString &filter )
49 {
50  const QString text = filter.trimmed().toLower();
51  mModel->setFilterString( text );
52  if ( !text.isEmpty() )
53  {
54  expandAll();
55  if ( !selectedAlgorithm() )
56  {
57  // if previously selected item was hidden, auto select the first visible algorithm
58  QModelIndex firstVisibleIndex = findFirstVisibleAlgorithm( QModelIndex() );
59  if ( firstVisibleIndex.isValid() )
60  selectionModel()->setCurrentIndex( firstVisibleIndex, QItemSelectionModel::ClearAndSelect );
61  }
62  }
63  else
64  {
65  collapseAll();
66  }
67 }
68 
69 const QgsProcessingAlgorithm *QgsProcessingToolboxTreeView::algorithmForIndex( const QModelIndex &index )
70 {
71  QModelIndex sourceIndex = mModel->mapToSource( index );
72  if ( mToolboxModel->isAlgorithm( sourceIndex ) )
73  return mToolboxModel->algorithmForIndex( sourceIndex );
74  else
75  return nullptr;
76 }
77 
78 const QgsProcessingAlgorithm *QgsProcessingToolboxTreeView::selectedAlgorithm()
79 {
80  if ( selectionModel()->hasSelection() )
81  {
82  QModelIndex index = selectionModel()->selectedIndexes().at( 0 );
83  return algorithmForIndex( index );
84  }
85  else
86  {
87  return nullptr;
88  }
89 }
90 
91 void QgsProcessingToolboxTreeView::setFilters( QgsProcessingToolboxProxyModel::Filters filters )
92 {
93  mModel->setFilters( filters );
94 }
95 
96 QgsProcessingToolboxProxyModel::Filters QgsProcessingToolboxTreeView::filters() const
97 {
98  return mModel->filters();
99 }
100 
101 void QgsProcessingToolboxTreeView::setInPlaceLayer( QgsVectorLayer *layer )
102 {
103  mModel->setInPlaceLayer( layer );
104 }
105 
106 QModelIndex QgsProcessingToolboxTreeView::findFirstVisibleAlgorithm( const QModelIndex &parent )
107 {
108  for ( int r = 0; r < mModel->rowCount( parent ); ++r )
109  {
110  QModelIndex proxyIndex = mModel->index( r, 0, parent );
111  QModelIndex sourceIndex = mModel->mapToSource( proxyIndex );
112  if ( mToolboxModel->isAlgorithm( sourceIndex ) )
113  return proxyIndex;
114 
115  QModelIndex index = findFirstVisibleAlgorithm( proxyIndex );
116  if ( index.isValid() )
117  return index;
118  }
119  return QModelIndex();
120 }
121 
qgsprocessingtoolboxmodel.h
QgsProcessingToolboxProxyModel::setFilterString
void setFilterString(const QString &filter)
Sets a filter string, such that only algorithms matching the specified string will be shown.
Definition: qgsprocessingtoolboxmodel.cpp:708
QgsProcessingToolboxProxyModel::toolboxModel
QgsProcessingToolboxModel * toolboxModel()
Returns the underlying source Processing toolbox model.
Definition: qgsprocessingtoolboxmodel.cpp:685
QgsProcessingToolboxProxyModel
A sort/filter proxy model for providers and algorithms shown within the Processing toolbox,...
Definition: qgsprocessingtoolboxmodel.h:417
QgsProcessingToolboxProxyModel::QgsProcessingToolboxProxyModel
QgsProcessingToolboxProxyModel(QObject *parent=nullptr, QgsProcessingRegistry *registry=nullptr, QgsProcessingRecentAlgorithmLog *recentLog=nullptr)
Constructor for QgsProcessingToolboxProxyModel, with the given parent object.
Definition: qgsprocessingtoolboxmodel.cpp:671
qgsprocessingtoolboxtreeview.h
QgsProcessingAlgorithm
Definition: qgsprocessingalgorithm.h:51
QgsVectorLayer
Definition: qgsvectorlayer.h:385
QgsProcessingRegistry
Definition: qgsprocessingregistry.h:39