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 Q_FOREACH (
const QString &path, mExpandPaths )
80 if ( expandIndex.isValid() )
83 if ( modelIndex.isValid() )
86 if ( ptr && ( ptr->
capabilities2() & QgsDataItem::Capability::Collapse ) )
89 QModelIndex parentIndex = model()->parent( expandIndex );
91 if ( parentIndex.isValid() )
92 expandIndexSet.insert( parentIndex );
96 expandIndexSet.insert( expandIndex );
105 Q_FOREACH (
const QModelIndex &expandIndex, expandIndexSet )
107 expandTree( expandIndex );
116 void QgsBrowserTreeView::expandTree(
const QModelIndex &index )
124 QModelIndex parentIndex = model()->parent( index );
125 if ( parentIndex.isValid() )
126 expandTree( parentIndex );
129 bool QgsBrowserTreeView::treeExpanded(
const QModelIndex &index )
133 if ( !isExpanded( index ) )
135 QModelIndex parentIndex = model()->parent( index );
136 if ( parentIndex.isValid() )
137 return treeExpanded( parentIndex );
147 for (
int i = 0; i < model()->rowCount( index ); i++ )
149 QModelIndex childIndex = model()->index( i, 0, index );
150 if ( isExpanded( childIndex ) )
162 QTreeView::rowsInserted( parentIndex, start, end );
167 if ( mExpandPaths.isEmpty() )
176 mExpandPaths.removeOne( parentPath );
179 if ( !treeExpanded( parentIndex ) )
181 Q_FOREACH (
const QString &path, mExpandPaths )
183 if ( path.startsWith( parentPath +
'/' ) )
184 mExpandPaths.removeOne( path );
189 for (
int i = start; i <= end; i++ )
191 QModelIndex childIndex = model()->index( i, 0, parentIndex );
193 QString escapedChildPath = childPath;
194 escapedChildPath.replace(
'|', QLatin1String(
"\\|" ) );
196 QgsDebugMsgLevel(
"childPath = " + childPath +
" escapedChildPath = " + escapedChildPath, 2 );
197 if ( mExpandPaths.contains( childPath ) || mExpandPaths.indexOf( QRegExp(
"^" + escapedChildPath +
"/.*" ) ) != -1 )
201 if ( modelIndex.isValid() )
204 if ( !ptr || !( ptr->
capabilities2() & QgsDataItem::Capability::Collapse ) )
206 expand( childIndex );
213 QString QgsBrowserTreeView::expandedPathsKey()
const 215 return '/' + mSettingsSection +
"/expandedPaths";
218 QStringList QgsBrowserTreeView::expandedPathsList(
const QModelIndex &index )
225 for (
int i = 0; i < model()->rowCount( index ); i++ )
227 QModelIndex childIndex = model()->index( i, 0, index );
228 if ( isExpanded( childIndex ) )
230 QStringList childrenPaths = expandedPathsList( childIndex );
231 if ( !childrenPaths.isEmpty() )
233 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 a 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.