24 , mSettingsSection( QStringLiteral(
"browser" ) )
31 QTreeView::setModel( model );
38 mBrowserModel = model;
46 QTreeView::showEvent( e );
56 QTreeView::hideEvent( e );
59 void QgsBrowserTreeView::saveState()
62 QStringList expandedPaths = expandedPathsList( QModelIndex() );
63 settings.
setValue( expandedPathsKey(), expandedPaths );
67 void QgsBrowserTreeView::restoreState()
70 mExpandPaths = settings.
value( expandedPathsKey(), QVariant() ).toStringList();
73 if ( !mExpandPaths.isEmpty() )
75 QSet<QModelIndex> expandIndexSet;
76 Q_FOREACH (
const QString &path, mExpandPaths )
79 if ( expandIndex.isValid() )
82 if ( modelIndex.isValid() )
85 if ( ptr && ( ptr->
capabilities2() & QgsDataItem::Capability::Collapse ) )
88 QModelIndex parentIndex = model()->parent( expandIndex );
90 if ( parentIndex.isValid() )
91 expandIndexSet.insert( parentIndex );
95 expandIndexSet.insert( expandIndex );
104 Q_FOREACH (
const QModelIndex &expandIndex, expandIndexSet )
106 expandTree( expandIndex );
115 void QgsBrowserTreeView::expandTree(
const QModelIndex &index )
123 QModelIndex parentIndex = model()->parent( index );
124 if ( parentIndex.isValid() )
125 expandTree( parentIndex );
128 bool QgsBrowserTreeView::treeExpanded(
const QModelIndex &index )
132 if ( !isExpanded( index ) )
134 QModelIndex parentIndex = model()->parent( index );
135 if ( parentIndex.isValid() )
136 return treeExpanded( parentIndex );
146 for (
int i = 0; i < model()->rowCount( index ); i++ )
148 QModelIndex childIndex = model()->index( i, 0, index );
149 if ( isExpanded( childIndex ) )
161 QTreeView::rowsInserted( parentIndex, start, end );
166 if ( mExpandPaths.isEmpty() )
175 mExpandPaths.removeOne( parentPath );
178 if ( !treeExpanded( parentIndex ) )
180 Q_FOREACH (
const QString &path, mExpandPaths )
182 if ( path.startsWith( parentPath +
'/' ) )
183 mExpandPaths.removeOne( path );
188 for (
int i = start; i <=
end; i++ )
190 QModelIndex childIndex = model()->index( i, 0, parentIndex );
192 QString escapedChildPath = childPath;
193 escapedChildPath.replace(
'|', QLatin1String(
"\\|" ) );
195 QgsDebugMsgLevel(
"childPath = " + childPath +
" escapedChildPath = " + escapedChildPath, 2 );
196 if ( mExpandPaths.contains( childPath ) || mExpandPaths.indexOf( QRegExp(
"^" + escapedChildPath +
"/.*" ) ) != -1 )
199 expand( childIndex );
204 QString QgsBrowserTreeView::expandedPathsKey()
const 206 return '/' + mSettingsSection +
"/expandedPaths";
209 QStringList QgsBrowserTreeView::expandedPathsList(
const QModelIndex &index )
216 for (
int i = 0; i < model()->rowCount( index ); i++ )
218 QModelIndex childIndex = model()->index( i, 0, index );
219 if ( isExpanded( childIndex ) )
221 QStringList childrenPaths = expandedPathsList( childIndex );
222 if ( !childrenPaths.isEmpty() )
224 paths.append( childrenPaths );
void setModel(QAbstractItemModel *model) override
void setBrowserModel(QgsBrowserModel *model)
Set the browser model.
This class is a composition of two QSettings instances:
QModelIndex findPath(const QString &path, Qt::MatchFlag matchFlag=Qt::MatchExactly)
Return 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
void setValue(const QString &key, const QVariant &value, const QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
#define QgsDebugMsgLevel(str, level)
QgsDataItem * dataItem(const QModelIndex &idx) const
void rowsInserted(const QModelIndex &parentIndex, int start, int end) override
Base class for all items in the model.
void showEvent(QShowEvent *e) override
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), const Section section=NoSection) const
Returns the value for setting key.
void hideEvent(QHideEvent *e) override
QgsBrowserModel * browserModel()
Return the browser model.
virtual Capabilities capabilities2() const