24 , mSettingsSection( QStringLiteral(
"browser" ) )
26 setEditTriggers( QAbstractItemView::EditKeyPressed );
33 QTreeView::setModel( model );
40 mBrowserModel = model;
48 QTreeView::showEvent( e );
58 QTreeView::hideEvent( e );
61 void QgsBrowserTreeView::saveState()
64 QStringList expandedPaths = expandedPathsList( QModelIndex() );
65 settings.
setValue( expandedPathsKey(), expandedPaths );
69 void QgsBrowserTreeView::restoreState()
72 mExpandPaths = settings.
value( expandedPathsKey(), QVariant() ).toStringList();
75 if ( !mExpandPaths.isEmpty() )
77 QSet<QModelIndex> expandIndexSet;
78 const auto constMExpandPaths = mExpandPaths;
79 for (
const QString &path : constMExpandPaths )
82 if ( expandIndex.isValid() )
85 if ( modelIndex.isValid() )
88 if ( ptr && ( ptr->
capabilities2() & QgsDataItem::Capability::Collapse ) )
91 QModelIndex parentIndex = model()->parent( expandIndex );
93 if ( parentIndex.isValid() )
94 expandIndexSet.insert( parentIndex );
98 expandIndexSet.insert( expandIndex );
107 const auto constExpandIndexSet = expandIndexSet;
108 for (
const QModelIndex &expandIndex : constExpandIndexSet )
110 expandTree( expandIndex );
119 void QgsBrowserTreeView::expandTree(
const QModelIndex &index )
127 QModelIndex parentIndex = model()->parent( index );
128 if ( parentIndex.isValid() )
129 expandTree( parentIndex );
132 bool QgsBrowserTreeView::treeExpanded(
const QModelIndex &index )
136 if ( !isExpanded( index ) )
138 QModelIndex parentIndex = model()->parent( index );
139 if ( parentIndex.isValid() )
140 return treeExpanded( parentIndex );
147 if ( !model() || !index.isValid() )
150 for (
int i = 0; i < model()->rowCount( index ); i++ )
152 QModelIndex childIndex = model()->index( i, 0, index );
153 if ( isExpanded( childIndex ) )
165 QTreeView::rowsInserted( parentIndex, start, end );
170 if ( mExpandPaths.isEmpty() )
179 mExpandPaths.removeOne( parentPath );
182 if ( !treeExpanded( parentIndex ) )
184 const auto constMExpandPaths = mExpandPaths;
185 for (
const QString &path : constMExpandPaths )
187 if ( path.startsWith( parentPath +
'/' ) )
188 mExpandPaths.removeOne( path );
193 for (
int i = start; i <= end; i++ )
195 QModelIndex childIndex = model()->index( i, 0, parentIndex );
197 QString escapedChildPath = childPath;
198 escapedChildPath.replace(
'|', QLatin1String(
"\\|" ) );
200 QgsDebugMsgLevel(
"childPath = " + childPath +
" escapedChildPath = " + escapedChildPath, 2 );
201 if ( mExpandPaths.contains( childPath ) || mExpandPaths.indexOf( QRegExp(
"^" + escapedChildPath +
"/.*" ) ) != -1 )
205 if ( modelIndex.isValid() )
208 if ( !ptr || !( ptr->
capabilities2() & QgsDataItem::Capability::Collapse ) )
210 expand( childIndex );
217 QString QgsBrowserTreeView::expandedPathsKey()
const
219 return '/' + mSettingsSection +
"/expandedPaths";
222 QStringList QgsBrowserTreeView::expandedPathsList(
const QModelIndex &index )
229 for (
int i = 0; i < model()->rowCount( index ); i++ )
231 QModelIndex childIndex = model()->index( i, 0, index );
232 if ( isExpanded( childIndex ) )
234 QStringList childrenPaths = expandedPathsList( childIndex );
235 if ( !childrenPaths.isEmpty() )
237 paths.append( childrenPaths );
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 Capabilities capabilities2() const
Returns the capabilities for the data item.
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.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
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)