QGIS API Documentation 4.3.0-Master (b46232e4f9a)
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
19
20#include <QKeyEvent>
21
22#include "moc_qgsprocessingtoolboxtreeview.cpp"
23
25
26QgsProcessingToolboxTreeView::QgsProcessingToolboxTreeView( QWidget *parent, QgsProcessingRegistry *registry, QgsProcessingRecentAlgorithmLog *recentLog, 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
72void QgsProcessingToolboxTreeView::reset()
73{
74 QTreeView::reset();
75
76 if ( !mModel->filterString().isEmpty() )
77 {
78 expandAll();
79 }
80}
81
82const QgsProcessingAlgorithm *QgsProcessingToolboxTreeView::algorithmForIndex( const QModelIndex &index )
83{
84 const QModelIndex sourceIndex = mModel->mapToSource( index );
85 if ( mToolboxModel->isAlgorithm( sourceIndex ) )
86 return mToolboxModel->algorithmForIndex( sourceIndex );
87 else
88 return nullptr;
89}
90
91const QgsProcessingAlgorithm *QgsProcessingToolboxTreeView::selectedAlgorithm()
92{
93 if ( selectionModel()->hasSelection() )
94 {
95 const QModelIndex index = selectionModel()->selectedIndexes().at( 0 );
96 return algorithmForIndex( index );
97 }
98 else
99 {
100 return nullptr;
101 }
102}
103
104const QgsProcessingParameterType *QgsProcessingToolboxTreeView::parameterTypeForIndex( const QModelIndex &index )
105{
106 const QModelIndex sourceIndex = mModel->mapToSource( index );
107 if ( mToolboxModel->isParameter( sourceIndex ) )
108 return mToolboxModel->parameterTypeForIndex( sourceIndex );
109 else
110 return nullptr;
111}
112
113const QgsProcessingParameterType *QgsProcessingToolboxTreeView::selectedParameterType()
114{
115 if ( selectionModel()->hasSelection() )
116 {
117 const QModelIndex index = selectionModel()->selectedIndexes().at( 0 );
118 return parameterTypeForIndex( index );
119 }
120 else
121 {
122 return nullptr;
123 }
124}
125
126void QgsProcessingToolboxTreeView::setFilters( QgsProcessingToolboxProxyModel::Filters filters )
127{
128 mModel->setFilters( filters );
129}
130
131QgsProcessingToolboxProxyModel::Filters QgsProcessingToolboxTreeView::filters() const
132{
133 return mModel->filters();
134}
135
136void QgsProcessingToolboxTreeView::setFilterParameter( const QgsProcessingParameterDefinition *parameterDefinition )
137{
138 mModel->setFilterParameter( parameterDefinition );
139}
140
141void QgsProcessingToolboxTreeView::setFilterOutput( const QgsProcessingOutputDefinition *outputDefinition )
142{
143 mModel->setFilterOutput( outputDefinition );
144}
145
146
147void QgsProcessingToolboxTreeView::setInPlaceLayer( QgsVectorLayer *layer )
148{
149 mModel->setInPlaceLayer( layer );
150}
151
152QModelIndex QgsProcessingToolboxTreeView::findFirstVisibleAlgorithm( const QModelIndex &parent )
153{
154 for ( int r = 0; r < mModel->rowCount( parent ); ++r )
155 {
156 QModelIndex proxyIndex = mModel->index( r, 0, parent );
157 const QModelIndex sourceIndex = mModel->mapToSource( proxyIndex );
158 if ( mToolboxModel->isAlgorithm( sourceIndex ) )
159 return proxyIndex;
160
161 QModelIndex index = findFirstVisibleAlgorithm( proxyIndex );
162 if ( index.isValid() )
163 return index;
164 }
165 return QModelIndex();
166}
167
168void QgsProcessingToolboxTreeView::keyPressEvent( QKeyEvent *event )
169{
170 if ( event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter )
171 emit doubleClicked( currentIndex() );
172 else
173 QTreeView::keyPressEvent( event );
174}
175
Abstract base class for processing algorithms.
Base class for the definition of processing outputs.
Base class for the definition of processing parameters.
Makes metadata of processing parameters available.
Registry for various processing components, including providers, algorithms and various parameters an...
A 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 dataset.