QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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 
19 #include <QKeyEvent>
20 
22 
23 QgsProcessingToolboxTreeView::QgsProcessingToolboxTreeView( QWidget *parent,
24  QgsProcessingRegistry *registry,
25  QgsProcessingRecentAlgorithmLog *recentLog )
26  : QTreeView( parent )
27 {
28  mModel = new QgsProcessingToolboxProxyModel( this, registry, recentLog );
29  mToolboxModel = mModel->toolboxModel();
30  setModel( mModel );
31 }
32 
33 void QgsProcessingToolboxTreeView::setRegistry( QgsProcessingRegistry *registry, QgsProcessingRecentAlgorithmLog *recentLog )
34 {
35  QgsProcessingToolboxProxyModel *newModel = new QgsProcessingToolboxProxyModel( this, registry, recentLog );
36  mToolboxModel = newModel->toolboxModel();
37  setModel( newModel );
38  mModel->deleteLater();
39  mModel = newModel;
40 }
41 
42 void QgsProcessingToolboxTreeView::setToolboxProxyModel( QgsProcessingToolboxProxyModel *model )
43 {
44  mToolboxModel = mModel->toolboxModel();
45  setModel( model );
46  mModel->deleteLater();
47  mModel = model;
48 }
49 
50 void QgsProcessingToolboxTreeView::setFilterString( const QString &filter )
51 {
52  const QString text = filter.trimmed().toLower();
53  mModel->setFilterString( text );
54  if ( !text.isEmpty() )
55  {
56  expandAll();
57  if ( !selectedAlgorithm() )
58  {
59  // if previously selected item was hidden, auto select the first visible algorithm
60  const QModelIndex firstVisibleIndex = findFirstVisibleAlgorithm( QModelIndex() );
61  if ( firstVisibleIndex.isValid() )
62  selectionModel()->setCurrentIndex( firstVisibleIndex, QItemSelectionModel::ClearAndSelect );
63  }
64  }
65  else
66  {
67  collapseAll();
68  }
69 }
70 
71 const QgsProcessingAlgorithm *QgsProcessingToolboxTreeView::algorithmForIndex( const QModelIndex &index )
72 {
73  const QModelIndex sourceIndex = mModel->mapToSource( index );
74  if ( mToolboxModel->isAlgorithm( sourceIndex ) )
75  return mToolboxModel->algorithmForIndex( sourceIndex );
76  else
77  return nullptr;
78 }
79 
80 const QgsProcessingAlgorithm *QgsProcessingToolboxTreeView::selectedAlgorithm()
81 {
82  if ( selectionModel()->hasSelection() )
83  {
84  const QModelIndex index = selectionModel()->selectedIndexes().at( 0 );
85  return algorithmForIndex( index );
86  }
87  else
88  {
89  return nullptr;
90  }
91 }
92 
93 void QgsProcessingToolboxTreeView::setFilters( QgsProcessingToolboxProxyModel::Filters filters )
94 {
95  mModel->setFilters( filters );
96 }
97 
98 QgsProcessingToolboxProxyModel::Filters QgsProcessingToolboxTreeView::filters() const
99 {
100  return mModel->filters();
101 }
102 
103 void QgsProcessingToolboxTreeView::setInPlaceLayer( QgsVectorLayer *layer )
104 {
105  mModel->setInPlaceLayer( layer );
106 }
107 
108 QModelIndex QgsProcessingToolboxTreeView::findFirstVisibleAlgorithm( const QModelIndex &parent )
109 {
110  for ( int r = 0; r < mModel->rowCount( parent ); ++r )
111  {
112  QModelIndex proxyIndex = mModel->index( r, 0, parent );
113  const QModelIndex sourceIndex = mModel->mapToSource( proxyIndex );
114  if ( mToolboxModel->isAlgorithm( sourceIndex ) )
115  return proxyIndex;
116 
117  QModelIndex index = findFirstVisibleAlgorithm( proxyIndex );
118  if ( index.isValid() )
119  return index;
120  }
121  return QModelIndex();
122 }
123 
124 void QgsProcessingToolboxTreeView::keyPressEvent( QKeyEvent *event )
125 {
126  if ( event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter )
127  emit doubleClicked( currentIndex() );
128  else
129  QTreeView::keyPressEvent( event );
130 }
131 
Abstract base class for processing algorithms.
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,...
void setFilterString(const QString &filter)
Sets a filter string, such that only algorithms matching the specified string will be shown.
QgsProcessingToolboxModel * toolboxModel()
Returns the underlying source Processing toolbox model.
Represents a vector layer which manages a vector based data sets.