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