30#include <QRegularExpression>
31#include <QSortFilterProxyModel>
34#include "moc_qgsbrowsertreeview.cpp"
36using namespace Qt::StringLiterals;
40 , mSettingsSection( u
"browser"_s )
42 setEditTriggers( QAbstractItemView::EditKeyPressed );
48 if ( event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter )
49 emit doubleClicked( currentIndex() );
51 QTreeView::keyPressEvent( event );
56 QTreeView::setModel( model );
63 mBrowserModel = model;
71 QTreeView::showEvent( e );
81 QTreeView::hideEvent( e );
84void QgsBrowserTreeView::saveState()
87 const QStringList expandedPaths = expandedPathsList( QModelIndex() );
88 settings.
setValue( expandedPathsKey(), expandedPaths );
92void QgsBrowserTreeView::restoreState()
94 const QgsSettings settings;
95 mExpandPaths = settings.
value( expandedPathsKey(), QVariant() ).toStringList();
98 if ( !mExpandPaths.isEmpty() )
100 QSet<QModelIndex> expandIndexSet;
101 const auto constMExpandPaths = mExpandPaths;
102 for (
const QString &path : constMExpandPaths )
105 if ( expandIndex.isValid() )
108 if ( modelIndex.isValid() )
114 const QModelIndex parentIndex = model()->parent( expandIndex );
116 if ( parentIndex.isValid() )
117 expandIndexSet.insert( parentIndex );
121 expandIndexSet.insert( expandIndex );
130 const auto constExpandIndexSet = expandIndexSet;
131 for (
const QModelIndex &expandIndex : constExpandIndexSet )
133 expandTree( expandIndex );
142void QgsBrowserTreeView::expandTree(
const QModelIndex &index )
150 const QModelIndex parentIndex = model()->parent( index );
151 if ( parentIndex.isValid() )
152 expandTree( parentIndex );
155bool QgsBrowserTreeView::treeExpanded(
const QModelIndex &index )
159 if ( !isExpanded( index ) )
161 const QModelIndex parentIndex = model()->parent( index );
162 if ( parentIndex.isValid() )
163 return treeExpanded( parentIndex );
170 if ( !model() || !index.isValid() )
173 for (
int i = 0; i < model()->rowCount( index ); i++ )
175 const QModelIndex childIndex = model()->index( i, 0, index );
176 if ( isExpanded( childIndex ) )
188 if ( pathParts.isEmpty() )
192 QVector<QgsDirectoryItem *> initialDirectoryItemCandidates;
193 const QVector<QgsDataItem *> rootItems = mBrowserModel->rootItems();
198 initialDirectoryItemCandidates << dirItem;
200 else if (
QgsFavoritesItem *favoritesItem = qobject_cast<QgsFavoritesItem *>( item ) )
202 const QVector<QgsDataItem *> favoriteChildren = favoritesItem->children();
203 for (
QgsDataItem *favoriteChild : favoriteChildren )
205 if (
QgsDirectoryItem *dirItem = qobject_cast<QgsDirectoryItem *>( favoriteChild ) )
207 initialDirectoryItemCandidates << dirItem;
214 QString currentCandidatePath;
215 for (
const QString &thisPart : pathParts )
217 currentCandidatePath += ( currentCandidatePath.isEmpty() || currentCandidatePath.endsWith(
'/' ) ? QString() : u
"/"_s ) + thisPart;
219 auto it = initialDirectoryItemCandidates.begin();
220 while ( it != initialDirectoryItemCandidates.end() )
222 if ( !( *it )->dirPath().startsWith( currentCandidatePath ) )
224 it = initialDirectoryItemCandidates.erase( it );
228 if ( str.startsWith( ( *it )->dirPath() ) )
229 currentDirectoryItem = *it;
235 if ( !currentDirectoryItem )
238 QStringList remainingParts = pathParts;
239 auto it = remainingParts.begin();
240 QDir currentDir = *it;
241 while ( it != remainingParts.end() )
243 if ( currentDirectoryItem->
dirPath().startsWith( currentDir.filePath( *it ) ) )
245 currentDir = QDir( currentDir.filePath( *it ) );
246 it = remainingParts.erase( it );
254 currentDir = QDir( currentDirectoryItem->
dirPath() );
255 QList<QgsDirectoryItem *> pathItems;
257 pathItems << currentDirectoryItem;
259 for (
const QString ¤tFolderName : std::as_const( remainingParts ) )
261 const QString thisPath = currentDir.filePath( currentFolderName );
263 if ( !QFile::exists( thisPath ) )
268 const QVector<QgsDataItem *> children = currentDirectoryItem->
children();
271 if (
QgsDirectoryItem *childDirectoryItem = qobject_cast<QgsDirectoryItem *>( child ) )
273 if ( childDirectoryItem->dirPath() == thisPath )
275 existingChild = childDirectoryItem;
283 pathItems << existingChild;
284 currentDirectoryItem = existingChild;
291 currentDirectoryItem = newDir;
294 currentDir = QDir( thisPath );
300 QModelIndex index = mBrowserModel->findItem( i );
301 if ( QSortFilterProxyModel *proxyModel = qobject_cast<QSortFilterProxyModel *>( model() ) )
303 index = proxyModel->mapFromSource( index );
309 if ( selectPath && lastItem )
315 if ( !mBrowserModel )
318 QModelIndex index = mBrowserModel->findItem( item );
319 if ( !index.isValid() )
322 if ( QSortFilterProxyModel *proxyModel = qobject_cast<QSortFilterProxyModel *>( model() ) )
324 index = proxyModel->mapFromSource( index );
327 setCurrentIndex( index );
334 QTreeView::rowsInserted( parentIndex, start, end );
339 if ( mExpandPaths.isEmpty() )
348 mExpandPaths.removeOne( parentPath );
351 if ( !treeExpanded( parentIndex ) )
353 const auto constMExpandPaths = mExpandPaths;
354 for (
const QString &path : constMExpandPaths )
356 if ( path.startsWith( parentPath +
'/' ) )
357 mExpandPaths.removeOne( path );
362 for (
int i = start; i <= end; i++ )
364 const QModelIndex childIndex = model()->index( i, 0, parentIndex );
366 const QString escapedChildPath = QRegularExpression::escape( childPath );
368 QgsDebugMsgLevel(
"childPath = " + childPath +
" escapedChildPath = " + escapedChildPath, 2 );
369 if ( mExpandPaths.contains( childPath ) || mExpandPaths.indexOf( QRegularExpression(
"^" + escapedChildPath +
"/.*" ) ) != -1 )
373 if ( modelIndex.isValid() )
378 expand( childIndex );
385QString QgsBrowserTreeView::expandedPathsKey()
const
387 return '/' + mSettingsSection +
"/expandedPaths";
390QStringList QgsBrowserTreeView::expandedPathsList(
const QModelIndex &index )
397 for (
int i = 0; i < model()->rowCount( index ); i++ )
399 const QModelIndex childIndex = model()->index( i, 0, index );
400 if ( isExpanded( childIndex ) )
402 const QStringList childrenPaths = expandedPathsList( childIndex );
403 if ( !childrenPaths.isEmpty() )
405 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.
@ Path
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
void expandPath(const QString &path, bool selectPath=false)
Expands out a file path in the view.
void setBrowserModel(QgsBrowserGuiModel *model)
Sets the browser model.
void setModel(QAbstractItemModel *model) override
bool setSelectedItem(QgsDataItem *item)
Sets the item currently selected in the view.
void hideEvent(QHideEvent *e) override
bool hasExpandedDescendant(const QModelIndex &index) const
void rowsInserted(const QModelIndex &parentIndex, int start, int end) override
QgsBrowserGuiModel * browserModel()
Returns the browser model.
Base class for all items in the model.
QVector< QgsDataItem * > children() const
virtual void addChildItem(QgsDataItem *child, bool refresh=false)
Inserts a new child item.
virtual Qgis::BrowserItemCapabilities capabilities2() const
Returns the capabilities for the data item.
A browser item for directories: contains subdirectories and layers.
QString dirPath() const
Returns the full path to the directory the item represents.
A browser item which contains various Favorites directories.
static QStringList splitPathToComponents(const QString &path)
Given a file path, returns a list of all the components leading to that path.
Stores settings for use within QGIS.
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)