27 , mSettingsSection( QStringLiteral(
"browser" ) )
29 setEditTriggers( QAbstractItemView::EditKeyPressed );
35 if ( event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter )
36 emit doubleClicked( currentIndex() );
38 QTreeView::keyPressEvent( event );
44 QTreeView::setModel( model );
51 mBrowserModel = model;
59 QTreeView::showEvent( e );
69 QTreeView::hideEvent( e );
72 void QgsBrowserTreeView::saveState()
75 const QStringList expandedPaths = expandedPathsList( QModelIndex() );
76 settings.
setValue( expandedPathsKey(), expandedPaths );
80 void QgsBrowserTreeView::restoreState()
83 mExpandPaths = settings.
value( expandedPathsKey(), QVariant() ).toStringList();
86 if ( !mExpandPaths.isEmpty() )
88 QSet<QModelIndex> expandIndexSet;
89 const auto constMExpandPaths = mExpandPaths;
90 for (
const QString &path : constMExpandPaths )
93 if ( expandIndex.isValid() )
96 if ( modelIndex.isValid() )
102 const QModelIndex parentIndex = model()->parent( expandIndex );
104 if ( parentIndex.isValid() )
105 expandIndexSet.insert( parentIndex );
109 expandIndexSet.insert( expandIndex );
118 const auto constExpandIndexSet = expandIndexSet;
119 for (
const QModelIndex &expandIndex : constExpandIndexSet )
121 expandTree( expandIndex );
130 void QgsBrowserTreeView::expandTree(
const QModelIndex &index )
138 const QModelIndex parentIndex = model()->parent( index );
139 if ( parentIndex.isValid() )
140 expandTree( parentIndex );
143 bool QgsBrowserTreeView::treeExpanded(
const QModelIndex &index )
147 if ( !isExpanded( index ) )
149 const QModelIndex parentIndex = model()->parent( index );
150 if ( parentIndex.isValid() )
151 return treeExpanded( parentIndex );
158 if ( !model() || !index.isValid() )
161 for (
int i = 0; i < model()->rowCount( index ); i++ )
163 const QModelIndex childIndex = model()->index( i, 0, index );
164 if ( isExpanded( childIndex ) )
176 QTreeView::rowsInserted( parentIndex, start, end );
181 if ( mExpandPaths.isEmpty() )
190 mExpandPaths.removeOne( parentPath );
193 if ( !treeExpanded( parentIndex ) )
195 const auto constMExpandPaths = mExpandPaths;
196 for (
const QString &path : constMExpandPaths )
198 if ( path.startsWith( parentPath +
'/' ) )
199 mExpandPaths.removeOne( path );
204 for (
int i = start; i <= end; i++ )
206 const QModelIndex childIndex = model()->index( i, 0, parentIndex );
208 QString escapedChildPath = childPath;
209 escapedChildPath.replace(
'|', QLatin1String(
"\\|" ) );
211 QgsDebugMsgLevel(
"childPath = " + childPath +
" escapedChildPath = " + escapedChildPath, 2 );
212 if ( mExpandPaths.contains( childPath ) || mExpandPaths.indexOf( QRegExp(
"^" + escapedChildPath +
"/.*" ) ) != -1 )
216 if ( modelIndex.isValid() )
221 expand( childIndex );
228 QString QgsBrowserTreeView::expandedPathsKey()
const
230 return '/' + mSettingsSection +
"/expandedPaths";
233 QStringList QgsBrowserTreeView::expandedPathsList(
const QModelIndex &index )
240 for (
int i = 0; i < model()->rowCount( index ); i++ )
242 const QModelIndex childIndex = model()->index( i, 0, index );
243 if ( isExpanded( childIndex ) )
245 const QStringList childrenPaths = expandedPathsList( childIndex );
246 if ( !childrenPaths.isEmpty() )
248 paths.append( childrenPaths );