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