30#include <QRegularExpression>
31#include <QSortFilterProxyModel>
33#include "moc_qgsbrowsertreeview.cpp"
37 , mSettingsSection( QStringLiteral(
"browser" ) )
39 setEditTriggers( QAbstractItemView::EditKeyPressed );
45 if ( event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter )
46 emit doubleClicked( currentIndex() );
48 QTreeView::keyPressEvent( event );
53 QTreeView::setModel( model );
60 mBrowserModel = model;
68 QTreeView::showEvent( e );
78 QTreeView::hideEvent( e );
81void QgsBrowserTreeView::saveState()
84 const QStringList expandedPaths = expandedPathsList( QModelIndex() );
85 settings.
setValue( expandedPathsKey(), expandedPaths );
89void QgsBrowserTreeView::restoreState()
91 const QgsSettings settings;
92 mExpandPaths = settings.
value( expandedPathsKey(), QVariant() ).toStringList();
95 if ( !mExpandPaths.isEmpty() )
97 QSet<QModelIndex> expandIndexSet;
98 const auto constMExpandPaths = mExpandPaths;
99 for (
const QString &path : constMExpandPaths )
102 if ( expandIndex.isValid() )
105 if ( modelIndex.isValid() )
111 const QModelIndex parentIndex = model()->parent( expandIndex );
113 if ( parentIndex.isValid() )
114 expandIndexSet.insert( parentIndex );
118 expandIndexSet.insert( expandIndex );
127 const auto constExpandIndexSet = expandIndexSet;
128 for (
const QModelIndex &expandIndex : constExpandIndexSet )
130 expandTree( expandIndex );
139void QgsBrowserTreeView::expandTree(
const QModelIndex &index )
147 const QModelIndex parentIndex = model()->parent( index );
148 if ( parentIndex.isValid() )
149 expandTree( parentIndex );
152bool QgsBrowserTreeView::treeExpanded(
const QModelIndex &index )
156 if ( !isExpanded( index ) )
158 const QModelIndex parentIndex = model()->parent( index );
159 if ( parentIndex.isValid() )
160 return treeExpanded( parentIndex );
167 if ( !model() || !index.isValid() )
170 for (
int i = 0; i < model()->rowCount( index ); i++ )
172 const QModelIndex childIndex = model()->index( i, 0, index );
173 if ( isExpanded( childIndex ) )
185 if ( pathParts.isEmpty() )
189 QVector<QgsDirectoryItem *> initialDirectoryItemCandidates;
190 const QVector<QgsDataItem *> rootItems = mBrowserModel->rootItems();
195 initialDirectoryItemCandidates << dirItem;
197 else if (
QgsFavoritesItem *favoritesItem = qobject_cast<QgsFavoritesItem *>( item ) )
199 const QVector<QgsDataItem *> favoriteChildren = favoritesItem->children();
200 for (
QgsDataItem *favoriteChild : favoriteChildren )
202 if (
QgsDirectoryItem *dirItem = qobject_cast<QgsDirectoryItem *>( favoriteChild ) )
204 initialDirectoryItemCandidates << dirItem;
211 QString currentCandidatePath;
212 for (
const QString &thisPart : pathParts )
214 currentCandidatePath += ( currentCandidatePath.isEmpty() || currentCandidatePath.endsWith(
'/' ) ? QString() : QStringLiteral(
"/" ) ) + thisPart;
216 auto it = initialDirectoryItemCandidates.begin();
217 while ( it != initialDirectoryItemCandidates.end() )
219 if ( !( *it )->dirPath().startsWith( currentCandidatePath ) )
221 it = initialDirectoryItemCandidates.erase( it );
225 if ( str.startsWith( ( *it )->dirPath() ) )
226 currentDirectoryItem = *it;
232 if ( !currentDirectoryItem )
235 QStringList remainingParts = pathParts;
236 auto it = remainingParts.begin();
237 QDir currentDir = *it;
238 while ( it != remainingParts.end() )
240 if ( currentDirectoryItem->
dirPath().startsWith( currentDir.filePath( *it ) ) )
242 currentDir = QDir( currentDir.filePath( *it ) );
243 it = remainingParts.erase( it );
251 currentDir = QDir( currentDirectoryItem->
dirPath() );
252 QList<QgsDirectoryItem *> pathItems;
254 pathItems << currentDirectoryItem;
256 for (
const QString ¤tFolderName : std::as_const( remainingParts ) )
258 const QString thisPath = currentDir.filePath( currentFolderName );
260 if ( !QFile::exists( thisPath ) )
265 const QVector<QgsDataItem *> children = currentDirectoryItem->
children();
268 if (
QgsDirectoryItem *childDirectoryItem = qobject_cast<QgsDirectoryItem *>( child ) )
270 if ( childDirectoryItem->dirPath() == thisPath )
272 existingChild = childDirectoryItem;
280 pathItems << existingChild;
281 currentDirectoryItem = existingChild;
288 currentDirectoryItem = newDir;
291 currentDir = QDir( thisPath );
297 QModelIndex index = mBrowserModel->findItem( i );
298 if ( QSortFilterProxyModel *proxyModel = qobject_cast<QSortFilterProxyModel *>( model() ) )
300 index = proxyModel->mapFromSource( index );
306 if ( selectPath && lastItem )
312 if ( !mBrowserModel )
315 QModelIndex index = mBrowserModel->findItem( item );
316 if ( !index.isValid() )
319 if ( QSortFilterProxyModel *proxyModel = qobject_cast<QSortFilterProxyModel *>( model() ) )
321 index = proxyModel->mapFromSource( index );
324 setCurrentIndex( index );
331 QTreeView::rowsInserted( parentIndex, start, end );
336 if ( mExpandPaths.isEmpty() )
345 mExpandPaths.removeOne( parentPath );
348 if ( !treeExpanded( parentIndex ) )
350 const auto constMExpandPaths = mExpandPaths;
351 for (
const QString &path : constMExpandPaths )
353 if ( path.startsWith( parentPath +
'/' ) )
354 mExpandPaths.removeOne( path );
359 for (
int i = start; i <= end; i++ )
361 const QModelIndex childIndex = model()->index( i, 0, parentIndex );
363 const QString escapedChildPath = QRegularExpression::escape( childPath );
365 QgsDebugMsgLevel(
"childPath = " + childPath +
" escapedChildPath = " + escapedChildPath, 2 );
366 if ( mExpandPaths.contains( childPath ) || mExpandPaths.indexOf( QRegularExpression(
"^" + escapedChildPath +
"/.*" ) ) != -1 )
370 if ( modelIndex.isValid() )
375 expand( childIndex );
382QString QgsBrowserTreeView::expandedPathsKey()
const
384 return '/' + mSettingsSection +
"/expandedPaths";
387QStringList QgsBrowserTreeView::expandedPathsList(
const QModelIndex &index )
394 for (
int i = 0; i < model()->rowCount( index ); i++ )
396 const QModelIndex childIndex = model()->index( i, 0, index );
397 if ( isExpanded( childIndex ) )
399 const QStringList childrenPaths = expandedPathsList( childIndex );
400 if ( !childrenPaths.isEmpty() )
402 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)