27 , mSettingsSection( QStringLiteral(
"browser" ) )
29 setEditTriggers( QAbstractItemView::EditKeyPressed );
35 if ( event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter )
36 emit doubleClicked( currentIndex() );
38 QTreeView::keyPressEvent( event );
44 QTreeView::setModel( model );
51 mBrowserModel = model;
59 QTreeView::showEvent( e );
69 QTreeView::hideEvent( e );
72 void QgsBrowserTreeView::saveState()
75 const QStringList expandedPaths = expandedPathsList( QModelIndex() );
76 settings.
setValue( expandedPathsKey(), expandedPaths );
80 void QgsBrowserTreeView::restoreState()
83 mExpandPaths = settings.
value( expandedPathsKey(), QVariant() ).toStringList();
86 if ( !mExpandPaths.isEmpty() )
88 QSet<QModelIndex> expandIndexSet;
89 const auto constMExpandPaths = mExpandPaths;
90 for (
const QString &path : constMExpandPaths )
93 if ( expandIndex.isValid() )
96 if ( modelIndex.isValid() )
102 const QModelIndex parentIndex = model()->parent( expandIndex );
104 if ( parentIndex.isValid() )
105 expandIndexSet.insert( parentIndex );
109 expandIndexSet.insert( expandIndex );
118 const auto constExpandIndexSet = expandIndexSet;
119 for (
const QModelIndex &expandIndex : constExpandIndexSet )
121 expandTree( expandIndex );
130 void QgsBrowserTreeView::expandTree(
const QModelIndex &index )
138 const QModelIndex parentIndex = model()->parent( index );
139 if ( parentIndex.isValid() )
140 expandTree( parentIndex );
143 bool QgsBrowserTreeView::treeExpanded(
const QModelIndex &index )
147 if ( !isExpanded( index ) )
149 const QModelIndex parentIndex = model()->parent( index );
150 if ( parentIndex.isValid() )
151 return treeExpanded( parentIndex );
158 if ( !model() || !index.isValid() )
161 for (
int i = 0; i < model()->rowCount( index ); i++ )
163 const QModelIndex childIndex = model()->index( i, 0, index );
164 if ( isExpanded( childIndex ) )
176 QTreeView::rowsInserted( parentIndex, start, end );
181 if ( mExpandPaths.isEmpty() )
190 mExpandPaths.removeOne( parentPath );
193 if ( !treeExpanded( parentIndex ) )
195 const auto constMExpandPaths = mExpandPaths;
196 for (
const QString &path : constMExpandPaths )
198 if ( path.startsWith( parentPath +
'/' ) )
199 mExpandPaths.removeOne( path );
204 for (
int i = start; i <= end; i++ )
206 const QModelIndex childIndex = model()->index( i, 0, parentIndex );
208 QString escapedChildPath = childPath;
209 escapedChildPath.replace(
'|', QLatin1String(
"\\|" ) );
211 QgsDebugMsgLevel(
"childPath = " + childPath +
" escapedChildPath = " + escapedChildPath, 2 );
212 if ( mExpandPaths.contains( childPath ) || mExpandPaths.indexOf( QRegExp(
"^" + escapedChildPath +
"/.*" ) ) != -1 )
216 if ( modelIndex.isValid() )
221 expand( childIndex );
228 QString QgsBrowserTreeView::expandedPathsKey()
const
230 return '/' + mSettingsSection +
"/expandedPaths";
233 QStringList QgsBrowserTreeView::expandedPathsList(
const QModelIndex &index )
240 for (
int i = 0; i < model()->rowCount( index ); i++ )
242 const QModelIndex childIndex = model()->index( i, 0, index );
243 if ( isExpanded( childIndex ) )
245 const QStringList childrenPaths = expandedPathsList( childIndex );
246 if ( !childrenPaths.isEmpty() )
248 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.
void keyPressEvent(QKeyEvent *event) override
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.
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)