16 #include "qgssettings.h"
25 , mSettingsSection( QStringLiteral(
"browser" ) )
27 setEditTriggers( QAbstractItemView::EditKeyPressed );
34 QTreeView::setModel( model );
41 mBrowserModel = model;
49 QTreeView::showEvent( e );
59 QTreeView::hideEvent( e );
62 void QgsBrowserTreeView::saveState()
65 QStringList expandedPaths = expandedPathsList( QModelIndex() );
66 settings.setValue( expandedPathsKey(), expandedPaths );
70 void QgsBrowserTreeView::restoreState()
73 mExpandPaths = settings.value( expandedPathsKey(), QVariant() ).toStringList();
76 if ( !mExpandPaths.isEmpty() )
78 QSet<QModelIndex> expandIndexSet;
79 const auto constMExpandPaths = mExpandPaths;
80 for (
const QString &path : constMExpandPaths )
83 if ( expandIndex.isValid() )
86 if ( modelIndex.isValid() )
92 QModelIndex parentIndex = model()->parent( expandIndex );
94 if ( parentIndex.isValid() )
95 expandIndexSet.insert( parentIndex );
99 expandIndexSet.insert( expandIndex );
108 const auto constExpandIndexSet = expandIndexSet;
109 for (
const QModelIndex &expandIndex : constExpandIndexSet )
111 expandTree( expandIndex );
120 void QgsBrowserTreeView::expandTree(
const QModelIndex &index )
128 QModelIndex parentIndex = model()->parent( index );
129 if ( parentIndex.isValid() )
130 expandTree( parentIndex );
133 bool QgsBrowserTreeView::treeExpanded(
const QModelIndex &index )
137 if ( !isExpanded( index ) )
139 QModelIndex parentIndex = model()->parent( index );
140 if ( parentIndex.isValid() )
141 return treeExpanded( parentIndex );
148 if ( !model() || !index.isValid() )
151 for (
int i = 0; i < model()->rowCount( index ); i++ )
153 QModelIndex childIndex = model()->index( i, 0, index );
154 if ( isExpanded( childIndex ) )
166 QTreeView::rowsInserted( parentIndex, start, end );
171 if ( mExpandPaths.isEmpty() )
180 mExpandPaths.removeOne( parentPath );
183 if ( !treeExpanded( parentIndex ) )
185 const auto constMExpandPaths = mExpandPaths;
186 for (
const QString &path : constMExpandPaths )
188 if ( path.startsWith( parentPath +
'/' ) )
189 mExpandPaths.removeOne( path );
194 for (
int i = start; i <= end; i++ )
196 QModelIndex childIndex = model()->index( i, 0, parentIndex );
198 QString escapedChildPath = childPath;
199 escapedChildPath.replace(
'|', QLatin1String(
"\\|" ) );
201 QgsDebugMsgLevel(
"childPath = " + childPath +
" escapedChildPath = " + escapedChildPath, 2 );
202 if ( mExpandPaths.contains( childPath ) || mExpandPaths.indexOf( QRegExp(
"^" + escapedChildPath +
"/.*" ) ) != -1 )
206 if ( modelIndex.isValid() )
211 expand( childIndex );
218 QString QgsBrowserTreeView::expandedPathsKey()
const
220 return '/' + mSettingsSection +
"/expandedPaths";
223 QStringList QgsBrowserTreeView::expandedPathsList(
const QModelIndex &index )
230 for (
int i = 0; i < model()->rowCount( index ); i++ )
232 QModelIndex childIndex = model()->index( i, 0, index );
233 if ( isExpanded( childIndex ) )
235 QStringList childrenPaths = expandedPathsList( childIndex );
236 if ( !childrenPaths.isEmpty() )
238 paths.append( childrenPaths );
@ Collapse
The collapse/expand status for this items children should be ignored in order to avoid undesired netw...
A model for showing available data sources and other items in a structured tree.
QgsDataItem * dataItem(const QModelIndex &idx) const
Returns the data item at the specified index, or nullptr if no item exists at the index.
QModelIndex findPath(const QString &path, Qt::MatchFlag matchFlag=Qt::MatchExactly)
Returns index of item with given path.
@ PathRole
Item path used to access path in the tree, see QgsDataItem::mPath.
void showEvent(QShowEvent *e) override
QgsBrowserTreeView(QWidget *parent=nullptr)
Constructor for QgsBrowserTreeView.
QgsBrowserGuiModel * browserModel()
Returns the browser model.
void setBrowserModel(QgsBrowserGuiModel *model)
Sets the browser model.
void setModel(QAbstractItemModel *model) override
void hideEvent(QHideEvent *e) override
bool hasExpandedDescendant(const QModelIndex &index) const
void rowsInserted(const QModelIndex &parentIndex, int start, int end) override
Base class for all items in the model.
virtual Qgis::BrowserItemCapabilities capabilities2() const
Returns the capabilities for the data item.
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly,...
#define QgsDebugMsgLevel(str, level)