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