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 );
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 );
void setModel(QAbstractItemModel *model) override
void setBrowserModel(QgsBrowserModel *model)
Sets the browser model.
This class is a composition of two QSettings instances:
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
QModelIndex findPath(const QString &path, Qt::MatchFlag matchFlag=Qt::MatchExactly)
Returns index of item with given path.
QgsBrowserTreeView(QWidget *parent=nullptr)
Constructor for QgsBrowserTreeView.
Item path used to access path in the tree, see QgsDataItem::mPath.
bool hasExpandedDescendant(const QModelIndex &index) const
#define QgsDebugMsgLevel(str, level)
QgsDataItem * dataItem(const QModelIndex &idx) const
Returns the data item at the specified index, or nullptr if no item exists at the index...
void rowsInserted(const QModelIndex &parentIndex, int start, int end) override
Base class for all items in the model.
void showEvent(QShowEvent *e) override
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
void hideEvent(QHideEvent *e) override
A model for showing available data sources and other items in a structured tree.
QgsBrowserModel * browserModel()
Returns the browser model.
virtual Capabilities capabilities2() const
Returns the capabilities for the data item.