24 , mSettingsSection( QStringLiteral(
"browser" ) )
26 setEditTriggers( QAbstractItemView::EditKeyPressed );
32 QTreeView::setModel( model );
39 mBrowserModel = model;
47 QTreeView::showEvent( e );
57 QTreeView::hideEvent( e );
60 void QgsBrowserTreeView::saveState()
63 QStringList expandedPaths = expandedPathsList( QModelIndex() );
64 settings.
setValue( expandedPathsKey(), expandedPaths );
68 void QgsBrowserTreeView::restoreState()
71 mExpandPaths = settings.
value( expandedPathsKey(), QVariant() ).toStringList();
74 if ( !mExpandPaths.isEmpty() )
76 QSet<QModelIndex> expandIndexSet;
77 const auto constMExpandPaths = mExpandPaths;
78 for (
const QString &path : constMExpandPaths )
81 if ( expandIndex.isValid() )
84 if ( modelIndex.isValid() )
87 if ( ptr && ( ptr->
capabilities2() & QgsDataItem::Capability::Collapse ) )
90 QModelIndex parentIndex = model()->parent( expandIndex );
92 if ( parentIndex.isValid() )
93 expandIndexSet.insert( parentIndex );
97 expandIndexSet.insert( expandIndex );
106 const auto constExpandIndexSet = expandIndexSet;
107 for (
const QModelIndex &expandIndex : constExpandIndexSet )
109 expandTree( expandIndex );
118 void QgsBrowserTreeView::expandTree(
const QModelIndex &index )
126 QModelIndex parentIndex = model()->parent( index );
127 if ( parentIndex.isValid() )
128 expandTree( parentIndex );
131 bool QgsBrowserTreeView::treeExpanded(
const QModelIndex &index )
135 if ( !isExpanded( index ) )
137 QModelIndex parentIndex = model()->parent( index );
138 if ( parentIndex.isValid() )
139 return treeExpanded( parentIndex );
146 if ( !model() || !index.isValid() )
149 for (
int i = 0; i < model()->rowCount( index ); i++ )
151 QModelIndex childIndex = model()->index( i, 0, index );
152 if ( isExpanded( childIndex ) )
164 QTreeView::rowsInserted( parentIndex, start, end );
169 if ( mExpandPaths.isEmpty() )
178 mExpandPaths.removeOne( parentPath );
181 if ( !treeExpanded( parentIndex ) )
183 const auto constMExpandPaths = mExpandPaths;
184 for (
const QString &path : constMExpandPaths )
186 if ( path.startsWith( parentPath +
'/' ) )
187 mExpandPaths.removeOne( path );
192 for (
int i = start; i <= end; i++ )
194 QModelIndex childIndex = model()->index( i, 0, parentIndex );
196 QString escapedChildPath = childPath;
197 escapedChildPath.replace(
'|', QLatin1String(
"\\|" ) );
199 QgsDebugMsgLevel(
"childPath = " + childPath +
" escapedChildPath = " + escapedChildPath, 2 );
200 if ( mExpandPaths.contains( childPath ) || mExpandPaths.indexOf( QRegExp(
"^" + escapedChildPath +
"/.*" ) ) != -1 )
204 if ( modelIndex.isValid() )
207 if ( !ptr || !( ptr->
capabilities2() & QgsDataItem::Capability::Collapse ) )
209 expand( childIndex );
216 QString QgsBrowserTreeView::expandedPathsKey()
const
218 return '/' + mSettingsSection +
"/expandedPaths";
221 QStringList QgsBrowserTreeView::expandedPathsList(
const QModelIndex &index )
228 for (
int i = 0; i < model()->rowCount( index ); i++ )
230 QModelIndex childIndex = model()->index( i, 0, index );
231 if ( isExpanded( childIndex ) )
233 QStringList childrenPaths = expandedPathsList( childIndex );
234 if ( !childrenPaths.isEmpty() )
236 paths.append( childrenPaths );