QGIS API Documentation
3.26.3-Buenos Aires (65e4edfdad)
|
Go to the documentation of this file.
16 #include <QApplication>
18 #include <QtConcurrentMap>
20 #include <QStorageInfo>
22 #include <QFutureWatcher>
40 #define PROJECT_HOME_PREFIX "project:"
41 #define HOME_PREFIX "home:"
44 class QgsBrowserWatcher :
public QFutureWatcher<QVector <QgsDataItem *> >
50 : QFutureWatcher(
nullptr )
58 void finished(
QgsDataItem *item,
const QVector <QgsDataItem *> &items );
68 return QString::localeAwareCompare( a->
name(), b->
name() ) < 0;
72 : QAbstractItemModel( parent )
76 this, &QgsBrowserModel::dataItemProviderAdded );
78 this, &QgsBrowserModel::dataItemProviderWillBeRemoved );
97 beginRemoveRows( QModelIndex(), idx, idx );
107 beginInsertRows( QModelIndex(), 0, 0 );
120 QStringLiteral(
"special:Home" ) );
122 setupItemConnections( item );
134 const auto drives { QDir::drives() };
135 for (
const QFileInfo &drive : drives )
137 const QString path = drive.absolutePath();
142 const QString driveName = QStorageInfo( path ).displayName();
143 const QString name = driveName.isEmpty() || driveName == path ? path : QStringLiteral(
"%1 (%2)" ).arg( path, driveName );
146 item->
setSortKey( QStringLiteral(
" 3 %1" ).arg( path ) );
147 mDriveItems.insert( path, item );
149 setupItemConnections( item );
154 QString path = QString(
"/Volumes" );
156 setupItemConnections( vols );
161 QMultiMap<int, QgsDataItem *> providerMap;
166 if (
QgsDataItem *item = addProviderRootItem( pr ) )
168 providerMap.insert( pr->capabilities(), item );
173 const auto constUniqueKeys = providerMap.uniqueKeys();
174 for (
int key : constUniqueKeys )
176 QList<QgsDataItem *> providerGroup = providerMap.values( key );
177 if ( providerGroup.size() > 1 )
179 std::sort( providerGroup.begin(), providerGroup.end(), cmpByDataItemName_ );
182 const auto constProviderGroup = providerGroup;
207 if (
QgsDataItem *item = addProviderRootItem( provider ) )
220 if ( item->providerKey() == provider->
name() )
222 removeRootItem( item );
228 void QgsBrowserModel::onConnectionsChanged(
const QString &providerKey )
233 if ( item->providerKey() == providerKey )
251 if ( ! mInitialized )
261 if ( !
index.isValid() )
262 return Qt::ItemFlags();
264 Qt::ItemFlags
flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
271 return Qt::ItemFlags();
275 flags |= Qt::ItemIsDragEnabled;
279 flags |= Qt::ItemIsDropEnabled;
284 flags |= Qt::ItemIsEditable;
291 if ( !
index.isValid() )
299 else if ( role == Qt::DisplayRole || role == Qt::EditRole )
307 else if ( role == Qt::ToolTipRole )
311 else if ( role == Qt::DecorationRole &&
index.column() == 0 )
323 QgsLayerItem *lyrItem = qobject_cast<QgsLayerItem *>( item );
341 if ( !
index.isValid() )
360 return item->
rename( value.toString() );
370 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
372 return QVariant(
"header" );
413 return findPath(
this, path, matchFlag );
419 return QModelIndex();
422 bool foundChild =
true;
428 for (
int i = 0; i < model->rowCount(
index ); i++ )
430 QModelIndex idx = model->index( i, 0,
index );
432 QString itemPath = model->data( idx,
PathRole ).toString();
433 if ( itemPath == path )
440 if ( path.startsWith( itemPath +
'/' ) )
449 if ( matchFlag == Qt::MatchStartsWith )
453 return QModelIndex();
458 for (
int i = 0; i < this->
rowCount( index ); i++ )
462 if ( qobject_cast<QgsLayerItem *>(
dataItem( idx ) ) )
464 QString itemUri = qobject_cast<QgsLayerItem *>(
dataItem( idx ) )->uri();
466 if ( itemUri == uri )
473 QModelIndex childIdx =
findUri( uri, idx );
474 if ( childIdx.isValid() )
477 return QModelIndex();
496 const QList< QFileInfo > drives = QDir::drives();
498 const QStringList existingDrives = mDriveItems.keys();
499 for (
const QString &drivePath : existingDrives )
501 bool stillExists =
false;
502 for (
const QFileInfo &drive : drives )
504 if ( drivePath == drive.absolutePath() )
516 removeRootItem( driveItem );
519 for (
const QFileInfo &drive : drives )
521 const QString path = drive.absolutePath();
527 if ( !mDriveItems.contains( path ) )
529 const QString driveName = QStorageInfo( path ).displayName();
530 const QString name = driveName.isEmpty() || driveName == path ? path : QStringLiteral(
"%1 (%2)" ).arg( path, driveName );
533 item->
setSortKey( QStringLiteral(
" 3 %1" ).arg( path ) );
535 mDriveItems.insert( path, item );
536 setupItemConnections( item );
547 if ( column < 0 || column >=
columnCount() || row < 0 )
548 return QModelIndex();
553 return item ? createIndex( row, column, item ) : QModelIndex();
560 return QModelIndex();
569 for (
int i = 0; i < items.size(); i++ )
571 if ( items[i] == item )
572 return createIndex( i, 0, item );
574 QModelIndex childIndex =
findItem( item, items[i] );
575 if ( childIndex.isValid() )
578 return QModelIndex();
585 if ( !idx.isValid() )
588 beginInsertRows( idx, first, last );
600 if ( !idx.isValid() )
602 beginRemoveRows( idx, first, last );
613 if ( !idx.isValid() )
615 emit dataChanged( idx, idx );
622 if ( !idx.isValid() )
624 QgsDebugMsgLevel( QStringLiteral(
"item %1 state changed %2 -> %3" ).arg( item->
path() ).arg( qgsEnumValueToKey< Qgis::BrowserItemState >( oldState ) ).arg( qgsEnumValueToKey< Qgis::BrowserItemState >( item->
state() ) ), 4 );
628 void QgsBrowserModel::setupItemConnections(
QgsDataItem *item )
645 if ( collectionItem )
654 types << QStringLiteral(
"application/x-vnd.qgis.qgis.uri" );
661 const auto constIndexes = indexes;
662 for (
const QModelIndex &
index : constIndexes )
664 if (
index.isValid() )
672 uri.filePath = ptr->
path();
697 void *v = idx.internalPointer();
768 QStringList hiddenItems = settings.
value( QStringLiteral(
"browser/hiddenPaths" ),
769 QStringList() ).toStringList();
770 int idx = hiddenItems.indexOf( item->
path() );
773 hiddenItems.removeAt( idx );
777 hiddenItems << item->
path();
779 settings.
setValue( QStringLiteral(
"browser/hiddenPaths" ), hiddenItems );
786 removeRootItem( item );
791 void QgsBrowserModel::removeRootItem(
QgsDataItem *item )
794 beginRemoveRows( QModelIndex(), i, i );
797 if ( !mDriveItems.key( dirItem ).isEmpty() )
799 mDriveItems.remove( mDriveItems.key( dirItem ) );
822 setupItemConnections( item );
828 #include "qgsbrowsermodel.moc"
int columnCount(const QModelIndex &parent=QModelIndex()) const override
QModelIndex findUri(const QString &uri, QModelIndex index=QModelIndex())
Returns index of layer item with given uri.
void addRootItems()
Populates the model.
virtual int capabilities() const =0
Returns combination of flags from QgsDataProvider::DataCapabilities.
void providerAdded(QgsDataItemProvider *provider)
Emitted when a new data item provider has been added.
void refresh(const QString &path)
Refresh item specified by path.
void connectionsChanged(const QString &providerKey)
Emitted when connections for the specified providerKey have changed in the browser.
void reload()
Reload the whole model.
A Collection: logical collection of layers or subcollections, e.g. GRASS location/mapset,...
void beginRemoveItems(QgsDataItem *parent, int first, int last)
void initialize()
Delayed initialization, needed because the provider registry must be already populated.
QModelIndex parent(const QModelIndex &index) const override
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
static bool hiddenPath(const QString &path)
Check if the given path is hidden from the browser model.
@ ProviderKeyRole
Data item provider key that created the item, see QgsDataItem::providerKey()
@ NotPopulated
Children not yet created.
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
#define QgsDebugMsgLevel(str, level)
BrowserItemState
Browser item states.
virtual QVariant sortKey() const
Returns the sorting key for the item.
void stateChanged(const QModelIndex &index, Qgis::BrowserItemState oldState)
Emitted when item children fetch was finished.
virtual void refresh(const QVector< QgsDataItem * > &children)
Refresh the items from a specified list of child items.
Qgis::BrowserItemState state() const
void removeDirectory(QgsDirectoryItem *item)
Removes an existing directory from the favorites group.
void itemDataChanged(QgsDataItem *item)
void setProviderKey(const QString &value)
Sets the provider key that created this item (e.g.
@ SortRole
Custom sort role, see QgsDataItem::sortKey()
static QgsProject * instance()
Returns the QgsProject singleton instance.
QVector< QgsDataItem * > mRootItems
A directory item showing the current project directory.
@ Layer
Represents a map layer.
void itemStateChanged(QgsDataItem *item, Qgis::BrowserItemState oldState)
Emitted when an item's state is changed.
bool hasChildren(const QModelIndex &parent=QModelIndex()) const override
QString name() const
Returns the name of the item (the displayed text for the item).
This class is a composition of two QSettings instances:
void dataChanged(QgsDataItem *item)
static void deleteLater(QVector< QgsDataItem * > &items)
virtual QgsDataItem * createDataItem(const QString &path, QgsDataItem *parentItem)=0
Create a new instance of QgsDataItem (or nullptr) for given path and parent item.
QList< QgsMimeDataUtils::Uri > UriList
@ CommentRole
Item comment.
void beginRemoveItems(QgsDataItem *parent, int first, int last)
void homePathChanged()
Emitted when the home path of the project changes.
QModelIndex findPath(const QString &path, Qt::MatchFlag matchFlag=Qt::MatchExactly)
Returns index of item with given path.
void addDirectory(const QString &directory, const QString &name=QString())
Adds a new directory to the favorites group.
QgsDirectoryItem * mProjectHome
virtual QString name()=0
Human-readable name of the provider name.
#define PROJECT_HOME_PREFIX
#define Q_NOWARN_DEPRECATED_POP
QMap< QString, QgsDirectoryItem * > driveItems() const
Returns a map of the root drive items shown in the browser.
@ ItemRepresentsFile
Item's path() directly represents a file on disk (since QGIS 3.22)
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 removeFavorite(const QModelIndex &index)
Removes a favorite directory from its corresponding model index.
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
A directory: contains subdirectories and layers.
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
QgsDataItem * parent() const
Gets item parent.
QList< QgsDataItemProvider * > providers() const
Returns the list of available providers.
This is the interface for those who want to add custom data items to the browser tree.
Q_DECL_DEPRECATED void connectItem(QgsDataItem *item)
@ Populated
Children created.
static QgsDataItemProviderRegistry * dataItemProviderRegistry()
Returns the application's data item provider registry, which keeps a list of data item providers that...
virtual Q_DECL_DEPRECATED bool acceptDrop()
Returns whether the item accepts drag and dropped layers - e.g.
QgsFavoritesItem * mFavorites
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
virtual bool hasDragEnabled() const
Returns true if the item may be dragged.
@ PathRole
Item path used to access path in the tree, see QgsDataItem::mPath.
void hidePath(QgsDataItem *item)
Hide the given path in the browser model.
void connectionsChanged(const QString &providerKey=QString())
Emitted when the connections of the provider with the specified providerKey have changed.
void stateChanged(QgsDataItem *item, Qgis::BrowserItemState oldState)
Emitted when an item's state is changed.
void beginInsertItems(QgsDataItem *parent, int first, int last)
A directory item showing the a single favorite directory.
QgsBrowserModel(QObject *parent=nullptr)
Constructor for QgsBrowserModel, with the specified parent object.
static QMimeData * encodeUriList(const UriList &layers)
Encodes a URI list to a new QMimeData object.
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
void refreshDrives()
Refreshes the list of drive items, removing any corresponding to removed drives and adding newly adde...
int rowCount(const QModelIndex &parent=QModelIndex()) const override
void providerWillBeRemoved(QgsDataItemProvider *provider)
Emitted when a data item provider is about to be removed.
virtual QString comments() const
Returns comments of the layer.
QMimeData * mimeData(const QModelIndexList &indexes) const override
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
QStringList mimeTypes() const override
virtual void populate(const QVector< QgsDataItem * > &children)
@ Populating
Creating children in separate thread (populating or refreshing)
virtual Q_DECL_DEPRECATED bool handleDrop(const QMimeData *, Qt::DropAction)
Attempts to process the mime data dropped on this item.
void addFavoriteDirectory(const QString &directory, const QString &name=QString())
Adds a directory to the favorites group.
QVector< QgsDataItem * > children() const
Item that represents a layer that can be opened with one of the providers.
@ Rename
Item can be renamed.
~QgsBrowserModel() override
QModelIndex findItem(QgsDataItem *item, QgsDataItem *parent=nullptr) const
Returns the model index corresponding to the specified data item.
virtual QgsMimeDataUtils::UriList mimeUris() const
Returns mime URIs for the data item, most data providers will only return a single URI but some data ...
virtual Qgis::BrowserItemCapabilities capabilities2() const
Returns the capabilities for the data item.
QString providerKey() const
Returns the provider key that created this item (e.g.
Contains various Favorites directories.
Base class for all items in the model.
#define Q_NOWARN_DEPRECATED_PUSH
virtual void deleteChildItem(QgsDataItem *child)
Removes and deletes a child item, emitting relevant signals to the model.
QgsDataItem * dataItem(const QModelIndex &idx) const
Returns the data item at the specified index, or nullptr if no item exists at the index.
void setSortKey(const QVariant &key)
Sets a custom sorting key for the item.
Qt::ItemFlags flags(const QModelIndex &index) const override
Qgis::BrowserItemType type() const
void fetchMore(const QModelIndex &parent) override
bool canFetchMore(const QModelIndex &parent) const override
void beginInsertItems(QgsDataItem *parent, int first, int last)