30#include <QApplication>
33#include <QFutureWatcher>
34#include <QStorageInfo>
39#include "moc_qgsbrowsermodel.cpp"
41using namespace Qt::StringLiterals;
43#define PROJECT_HOME_PREFIX "project:"
44#define HOME_PREFIX "home:"
47class QgsBrowserWatcher :
public QFutureWatcher<QVector <QgsDataItem *> >
53 : QFutureWatcher(
nullptr )
61 void finished(
QgsDataItem *item,
const QVector <QgsDataItem *> &items );
71 return QString::localeAwareCompare( a->
name(), b->
name() ) < 0;
75 : QAbstractItemModel(
parent )
79 this, &QgsBrowserModel::dataItemProviderAdded );
81 this, &QgsBrowserModel::dataItemProviderWillBeRemoved );
100 beginRemoveRows( QModelIndex(), idx, idx );
110 beginInsertRows( QModelIndex(), 0, 0 );
125 setupItemConnections( item );
137 const auto drives { QDir::drives() };
138 for (
const QFileInfo &drive : drives )
140 const QString path = drive.absolutePath();
145 const QString driveName = QStorageInfo( path ).displayName();
146 const QString name = driveName.isEmpty() || driveName == path ? path : u
"%1 (%2)"_s.arg( path, driveName );
150 mDriveItems.insert( path, item );
152 setupItemConnections( item );
157 QString path = QString(
"/Volumes" );
159 setupItemConnections( vols );
164 QMultiMap<int, QgsDataItem *> providerMap;
169 if (
QgsDataItem *item = addProviderRootItem( pr ) )
171 providerMap.insert( pr->capabilities(), item );
176 const auto constUniqueKeys = providerMap.uniqueKeys();
177 for (
int key : constUniqueKeys )
179 QList<QgsDataItem *> providerGroup = providerMap.values( key );
180 if ( providerGroup.size() > 1 )
182 std::sort( providerGroup.begin(), providerGroup.end(), cmpByDataItemName_ );
185 const auto constProviderGroup = providerGroup;
210 if (
QgsDataItem *item = addProviderRootItem( provider ) )
221 for ( QgsDataItem *item : constMRootItems )
223 if ( item->providerKey() == provider->
name() )
225 removeRootItem( item );
231void QgsBrowserModel::onConnectionsChanged(
const QString &providerKey )
234 for ( QgsDataItem *item : std::as_const(
mRootItems ) )
236 if ( item->providerKey() == providerKey )
254 if ( ! mInitialized )
264 if ( !
index.isValid() )
265 return Qt::ItemFlags();
267 Qt::ItemFlags
flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
274 return Qt::ItemFlags();
278 flags |= Qt::ItemIsDragEnabled;
282 flags |= Qt::ItemIsDropEnabled;
287 flags |= Qt::ItemIsEditable;
294 if ( !
index.isValid() )
302 else if ( role == Qt::DisplayRole || role == Qt::EditRole )
310 else if ( role == Qt::ToolTipRole )
314 else if ( role == Qt::DecorationRole &&
index.column() == 0 )
326 QgsLayerItem *lyrItem = qobject_cast<QgsLayerItem *>( item );
335 QgsLayerItem *lyrItem = qobject_cast<QgsLayerItem *>( item );
353 if ( !
index.isValid() )
372 return item->
rename( value.toString() );
382 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
384 return QVariant(
"header" );
425 return findPath(
this, path, matchFlag );
431 return QModelIndex();
434 bool foundChild =
true;
440 for (
int i = 0; i < model->rowCount(
index ); i++ )
442 QModelIndex idx = model->index( i, 0,
index );
445 if ( itemPath == path )
452 if ( path.startsWith( itemPath +
'/' ) )
461 if ( matchFlag == Qt::MatchStartsWith )
465 return QModelIndex();
470 for (
int i = 0; i < this->
rowCount( index ); i++ )
474 if ( qobject_cast<QgsLayerItem *>(
dataItem( idx ) ) )
476 QString itemUri = qobject_cast<QgsLayerItem *>(
dataItem( idx ) )->uri();
478 if ( itemUri == uri )
485 QModelIndex childIdx =
findUri( uri, idx );
486 if ( childIdx.isValid() )
489 return QModelIndex();
508 const QList< QFileInfo > drives = QDir::drives();
510 const QStringList existingDrives = mDriveItems.keys();
511 for (
const QString &drivePath : existingDrives )
513 bool stillExists =
false;
514 for (
const QFileInfo &drive : drives )
516 if ( drivePath == drive.absolutePath() )
528 removeRootItem( driveItem );
531 for (
const QFileInfo &drive : drives )
533 const QString path = drive.absolutePath();
539 if ( !mDriveItems.contains( path ) )
541 const QString driveName = QStorageInfo( path ).displayName();
542 const QString name = driveName.isEmpty() || driveName == path ? path : u
"%1 (%2)"_s.arg( path, driveName );
547 mDriveItems.insert( path, item );
548 setupItemConnections( item );
559 if ( column < 0 || column >=
columnCount() || row < 0 )
560 return QModelIndex();
565 return item ? createIndex( row, column, item ) : QModelIndex();
572 return QModelIndex();
581 for (
int i = 0; i < items.size(); i++ )
583 if ( items[i] == item )
584 return createIndex( i, 0, item );
586 QModelIndex childIndex =
findItem( item, items[i] );
587 if ( childIndex.isValid() )
590 return QModelIndex();
597 if ( !idx.isValid() )
600 beginInsertRows( idx, first, last );
612 if ( !idx.isValid() )
614 beginRemoveRows( idx, first, last );
625 if ( !idx.isValid() )
627 emit dataChanged( idx, idx );
634 if ( !idx.isValid() )
640void QgsBrowserModel::setupItemConnections(
QgsDataItem *item )
657 if ( collectionItem )
666 types << u
"application/x-vnd.qgis.qgis.uri"_s;
673 const auto constIndexes = indexes;
674 for (
const QModelIndex &
index : constIndexes )
676 if (
index.isValid() )
680 if ( uris.isEmpty() )
694 uri.filePath = ptr->
path();
719 void *v = idx.internalPointer();
790 QStringList hiddenItems = settings.
value( u
"browser/hiddenPaths"_s,
791 QStringList() ).toStringList();
792 int idx = hiddenItems.indexOf( item->
path() );
795 hiddenItems.removeAt( idx );
799 hiddenItems << item->
path();
801 settings.
setValue( u
"browser/hiddenPaths"_s, hiddenItems );
808 removeRootItem( item );
813void QgsBrowserModel::removeRootItem(
QgsDataItem *item )
816 beginRemoveRows( QModelIndex(), i, i );
819 if ( !mDriveItems.key( dirItem ).isEmpty() )
821 mDriveItems.remove( mDriveItems.key( dirItem ) );
844 setupItemConnections( item );
850#include "qgsbrowsermodel.moc"
@ NoCapabilities
No capabilities.
BrowserItemState
Browser item states.
@ NotPopulated
Children not yet created.
@ Populating
Creating children in separate thread (populating or refreshing).
@ Populated
Children created.
@ Rename
Item can be renamed.
@ ItemRepresentsFile
Item's path() directly represents a file on disk.
QFlags< DataItemProviderCapability > DataItemProviderCapabilities
Capabilities for data item providers.
@ Layer
Represents a map layer.
static QgsDataItemProviderRegistry * dataItemProviderRegistry()
Returns the application's data item provider registry, which keeps a list of data item providers that...
Q_DECL_DEPRECATED void connectItem(QgsDataItem *item)
void connectionsChanged(const QString &providerKey)
Emitted when connections for the specified providerKey have changed in the browser.
bool hasChildren(const QModelIndex &parent=QModelIndex()) const override
QgsFavoritesItem * mFavorites
QgsDataItem * dataItem(const QModelIndex &idx) const
Returns the data item at the specified index, or nullptr if no item exists at the index.
bool canFetchMore(const QModelIndex &parent) const override
void hidePath(QgsDataItem *item)
Hide the given path in the browser model.
void refreshDrives()
Refreshes the list of drive items, removing any corresponding to removed drives and adding newly adde...
void reload()
Reload the whole model.
Qt::ItemFlags flags(const QModelIndex &index) const override
void itemStateChanged(QgsDataItem *item, Qgis::BrowserItemState oldState)
Emitted when an item's state is changed.
void beginInsertItems(QgsDataItem *parent, int first, int last)
QModelIndex findPath(const QString &path, Qt::MatchFlag matchFlag=Qt::MatchExactly)
Returns index of item with given path.
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
void refresh(const QString &path)
Refresh item specified by path.
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
int columnCount(const QModelIndex &parent=QModelIndex()) const override
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
QVector< QgsDataItem * > mRootItems
QMap< QString, QgsDirectoryItem * > driveItems() const
Returns a map of the root drive items shown in the browser.
~QgsBrowserModel() override
int rowCount(const QModelIndex &parent=QModelIndex()) const override
void beginRemoveItems(QgsDataItem *parent, int first, int last)
QStringList mimeTypes() const override
QModelIndex parent(const QModelIndex &index) const override
QModelIndex findItem(QgsDataItem *item, QgsDataItem *parent=nullptr) const
Returns the model index corresponding to the specified data item.
void initialize()
Delayed initialization, needed because the provider registry must be already populated.
QMimeData * mimeData(const QModelIndexList &indexes) const override
void addFavoriteDirectory(const QString &directory, const QString &name=QString())
Adds a directory to the favorites group.
QgsBrowserModel(QObject *parent=nullptr)
Constructor for QgsBrowserModel, with the specified parent object.
QgsDirectoryItem * mProjectHome
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
void stateChanged(const QModelIndex &index, Qgis::BrowserItemState oldState)
Emitted when item children fetch was finished.
void itemDataChanged(QgsDataItem *item)
void addRootItems()
Populates the model.
QModelIndex findUri(const QString &uri, QModelIndex index=QModelIndex())
Returns index of layer item with given uri.
@ Sort
Custom sort role, see QgsDataItem::sortKey().
@ Path
Item path used to access path in the tree, see QgsDataItem::mPath.
@ ProviderKey
Data item provider key that created the item, see QgsDataItem::providerKey().
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
void removeFavorite(const QModelIndex &index)
Removes a favorite directory from its corresponding model index.
void fetchMore(const QModelIndex &parent) override
A browser item for collections of data.
QList< QgsDataItemProvider * > providers() const
Returns the list of available providers.
void providerWillBeRemoved(QgsDataItemProvider *provider)
Emitted when a data item provider is about to be removed.
void providerAdded(QgsDataItemProvider *provider)
Emitted when a new data item provider has been added.
Interface for providers that add custom data items to the browser tree.
virtual QString name()=0
Human-readable name of the provider name.
virtual QgsDataItem * createDataItem(const QString &path, QgsDataItem *parentItem)=0
Create a new instance of QgsDataItem (or nullptr) for given path and parent item.
virtual Qgis::DataItemProviderCapabilities capabilities() const =0
Returns combination of flags from QgsDataProvider::DataCapabilities.
Base class for all items in the model.
void stateChanged(QgsDataItem *item, Qgis::BrowserItemState oldState)
Emitted when an item's state is changed.
void setSortKey(const QVariant &key)
Sets a custom sorting key for the item.
bool hasChildren() const
Returns whether this item has children.
int rowCount() const
Returns the number of rows of this item.
virtual void deleteChildItem(QgsDataItem *child)
Removes and deletes a child item, emitting relevant signals to the model.
virtual QVariant sortKey() const
Returns the sorting key for the item.
virtual Q_DECL_DEPRECATED bool handleDrop(const QMimeData *, Qt::DropAction)
Attempts to process the mime data dropped on this item.
virtual void refresh(const QVector< QgsDataItem * > &children)
Refresh the items from a specified list of child items.
void dataChanged(QgsDataItem *item)
Emitted when data changes for an item.
static void deleteLater(QVector< QgsDataItem * > &items)
void endRemoveItems()
Emitted after child items have been removed from this data item.
QVector< QgsDataItem * > children() const
void beginRemoveItems(QgsDataItem *parent, int first, int last)
Emitted before child items are removed from this data item.
virtual Q_DECL_DEPRECATED bool acceptDrop()
Returns whether the item accepts drag and dropped layers - e.g.
Qgis::BrowserItemType type() const
Qgis::BrowserItemState state() const
virtual bool hasDragEnabled() const
Returns true if the item may be dragged.
virtual Q_DECL_DEPRECATED bool rename(const QString &name)
Sets a new name for the item, and returns true if the item was successfully renamed.
void connectionsChanged(const QString &providerKey=QString())
Emitted when the connections of the provider with the specified providerKey have changed.
virtual Q_DECL_DEPRECATED QgsMimeDataUtils::Uri mimeUri() const
Returns mime URI for the data item.
QString name() const
Returns the name of the item (the displayed text for the item).
void beginInsertItems(QgsDataItem *parent, int first, int last)
Emitted before child items are added to this data item.
QgsDataItem * parent() const
Gets item parent.
virtual QgsMimeDataUtils::UriList mimeUris() const
Returns mime URIs for the data item, most data providers will only return a single URI but some data ...
QString providerKey() const
Returns the provider key that created this item (e.g.
void endInsertItems()
Emitted after child items have been added to this data item.
virtual Qgis::BrowserItemCapabilities capabilities2() const
Returns the capabilities for the data item.
void setProviderKey(const QString &value)
Sets the provider key that created this item (e.g.
virtual void populate(const QVector< QgsDataItem * > &children)
A browser item for directories: contains subdirectories and layers.
static bool hiddenPath(const QString &path)
Check if the given path is hidden from the browser model.
A directory item showing a single favorite directory.
A browser item which contains various Favorites directories.
A browser item that represents a layer that can be opened with one of the providers.
virtual QString comments() const
Returns comments of the layer.
const QgsLayerMetadata & layerMetadata() const
Returns layer's metadata, it may be a default constructed metadata if metadata is not explicitly set.
QList< QgsMimeDataUtils::Uri > UriList
static QMimeData * encodeUriList(const UriList &layers)
Encodes a URI list to a new QMimeData object.
A directory item showing the current project directory.
static QgsProject * instance()
Returns the QgsProject singleton instance.
void homePathChanged()
Emitted when the home path of the project changes.
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.
#define Q_NOWARN_DEPRECATED_POP
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
#define Q_NOWARN_DEPRECATED_PUSH
#define PROJECT_HOME_PREFIX
#define QgsDebugMsgLevel(str, level)