QGIS API Documentation
3.14.0-Pi (9f7028fd23)
|
Go to the documentation of this file.
16 #include <QApplication>
18 #include <QtConcurrentMap>
33 #define PROJECT_HOME_PREFIX "project:"
34 #define HOME_PREFIX "home:"
37 : QFutureWatcher( nullptr )
45 return QString::localeAwareCompare( a->
name(), b->
name() ) < 0;
49 : QAbstractItemModel( parent )
53 this, &QgsBrowserModel::dataItemProviderAdded );
55 this, &QgsBrowserModel::dataItemProviderWillBeRemoved );
74 beginRemoveRows( QModelIndex(), idx, idx );
84 beginInsertRows( QModelIndex(), 0, 0 );
97 QStringLiteral(
"special:Home" ) );
99 setupItemConnections( item );
111 const auto drives { QDir::drives() };
112 for (
const QFileInfo &drive : drives )
114 const QString path = drive.absolutePath();
120 item->
setSortKey( QStringLiteral(
" 3 %1" ).arg( path ) );
121 mDriveItems.insert( path, item );
123 setupItemConnections( item );
128 QString path = QString(
"/Volumes" );
134 QMap<int, QgsDataItem *> providerMap;
139 if (
QgsDataItem *item = addProviderRootItem( pr ) )
141 providerMap.insertMulti( pr->capabilities(), item );
146 const auto constUniqueKeys = providerMap.uniqueKeys();
147 for (
int key : constUniqueKeys )
149 QList<QgsDataItem *> providerGroup = providerMap.values( key );
150 if ( providerGroup.size() > 1 )
152 std::sort( providerGroup.begin(), providerGroup.end(), cmpByDataItemName_ );
155 const auto constProviderGroup = providerGroup;
180 if (
QgsDataItem *item = addProviderRootItem( provider ) )
193 if ( item->providerKey() == provider->
name() )
195 removeRootItem( item );
201 void QgsBrowserModel::onConnectionsChanged(
const QString &providerKey )
206 if ( item->providerKey() == providerKey )
224 if ( ! mInitialized )
234 if ( !
index.isValid() )
235 return Qt::ItemFlags();
237 Qt::ItemFlags
flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
244 return Qt::ItemFlags();
248 flags |= Qt::ItemIsDragEnabled;
252 flags |= Qt::ItemIsDropEnabled;
256 flags |= Qt::ItemIsEditable;
263 if ( !
index.isValid() )
271 else if ( role == Qt::DisplayRole || role == Qt::EditRole )
279 else if ( role == Qt::ToolTipRole )
283 else if ( role == Qt::DecorationRole &&
index.column() == 0 )
295 QgsLayerItem *lyrItem = qobject_cast<QgsLayerItem *>( item );
313 if ( !
index.isValid() )
331 return item->
rename( value.toString() );
341 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
343 return QVariant(
"header" );
384 return findPath(
this, path, matchFlag );
390 return QModelIndex();
393 bool foundChild =
true;
399 for (
int i = 0; i < model->rowCount(
index ); i++ )
401 QModelIndex idx = model->index( i, 0,
index );
403 QString itemPath = model->data( idx,
PathRole ).toString();
404 if ( itemPath == path )
411 if ( path.startsWith( itemPath +
'/' ) )
420 if ( matchFlag == Qt::MatchStartsWith )
424 return QModelIndex();
429 for (
int i = 0; i < this->
rowCount( index ); i++ )
433 if ( qobject_cast<QgsLayerItem *>(
dataItem( idx ) ) )
435 QString itemUri = qobject_cast<QgsLayerItem *>(
dataItem( idx ) )->uri();
437 if ( itemUri == uri )
444 QModelIndex childIdx =
findUri( uri, idx );
445 if ( childIdx.isValid() )
448 return QModelIndex();
467 const QList< QFileInfo > drives = QDir::drives();
469 const QStringList existingDrives = mDriveItems.keys();
470 for (
const QString &drivePath : existingDrives )
472 bool stillExists =
false;
473 for (
const QFileInfo &drive : drives )
475 if ( drivePath == drive.absolutePath() )
487 removeRootItem( driveItem );
490 for (
const QFileInfo &drive : drives )
492 const QString path = drive.absolutePath();
498 if ( !mDriveItems.contains( path ) )
501 item->
setSortKey( QStringLiteral(
" 3 %1" ).arg( path ) );
503 mDriveItems.insert( path, item );
504 setupItemConnections( item );
515 if ( column < 0 || column >=
columnCount() || row < 0 )
516 return QModelIndex();
521 return item ? createIndex( row, column, item ) : QModelIndex();
528 return QModelIndex();
537 for (
int i = 0; i < items.size(); i++ )
539 if ( items[i] == item )
540 return createIndex( i, 0, item );
542 QModelIndex childIndex =
findItem( item, items[i] );
543 if ( childIndex.isValid() )
546 return QModelIndex();
553 if ( !idx.isValid() )
556 beginInsertRows( idx, first, last );
568 if ( !idx.isValid() )
570 beginRemoveRows( idx, first, last );
581 if ( !idx.isValid() )
583 emit dataChanged( idx, idx );
590 if ( !idx.isValid() )
592 QgsDebugMsgLevel( QStringLiteral(
"item %1 state changed %2 -> %3" ).arg( item->
path() ).arg( oldState ).arg( item->
state() ), 4 );
596 void QgsBrowserModel::setupItemConnections(
QgsDataItem *item )
613 if ( collectionItem )
622 types << QStringLiteral(
"application/x-vnd.qgis.qgis.uri" );
629 const auto constIndexes = indexes;
630 for (
const QModelIndex &
index : constIndexes )
632 if (
index.isValid() )
659 void *v = idx.internalPointer();
730 QStringList hiddenItems = settings.
value( QStringLiteral(
"browser/hiddenPaths" ),
731 QStringList() ).toStringList();
732 int idx = hiddenItems.indexOf( item->
path() );
735 hiddenItems.removeAt( idx );
739 hiddenItems << item->
path();
741 settings.
setValue( QStringLiteral(
"browser/hiddenPaths" ), hiddenItems );
748 removeRootItem( item );
753 void QgsBrowserModel::removeRootItem(
QgsDataItem *item )
756 beginRemoveRows( QModelIndex(), i, i );
759 if ( !mDriveItems.key( dirItem ).isEmpty() )
761 mDriveItems.remove( mDriveItems.key( dirItem ) );
784 setupItemConnections( item );
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.
bool isValid() const
Returns whether the object contains valid data.
void connectionsChanged(const QString &providerKey)
Emitted when connections for the specified providerKey have changed in the browser.
void reload()
Reload the whole model.
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,.
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
#define QgsDebugMsgLevel(str, level)
virtual Capabilities capabilities2() const
Returns the capabilities for the data item.
virtual QVariant sortKey() const
Returns the sorting key for the item.
virtual void refresh(const QVector< QgsDataItem * > &children)
Refresh the items from a specified list of child items.
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
bool hasChildren(const QModelIndex &parent=QModelIndex()) const override
QString name() const
Returns the name of the item (the displayed text for the item).
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
virtual QgsMimeDataUtils::Uri mimeUri() const
Returns mime URI for the data item.
QMap< QString, QgsDirectoryItem * > driveItems() const
Returns a map of the root drive items shown in the browser.
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
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.
void stateChanged(const QModelIndex &index, QgsDataItem::State oldState)
Emitted when item children fetch was finished.
Q_DECL_DEPRECATED void connectItem(QgsDataItem *item)
@ Rename
Item can be renamed.
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.
@ Populating
Creating children in separate thread (populating or refreshing)
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.
QgsBrowserWatcher(QgsDataItem *item)
@ NotPopulated
Children not yet created.
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 beginInsertItems(QgsDataItem *parent, int first, int last)
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
void itemStateChanged(QgsDataItem *item, QgsDataItem::State oldState)
virtual void populate(const QVector< QgsDataItem * > &children)
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
@ Populated
Children created.
~QgsBrowserModel() override
QModelIndex findItem(QgsDataItem *item, QgsDataItem *parent=nullptr) const
Returns the model index corresponding to the specified data item.
QString providerKey() const
Returns the provider key that created this item (e.g.
#define Q_NOWARN_DEPRECATED_PUSH
virtual void deleteChildItem(QgsDataItem *child)
Removes and deletes a child item, emitting relevant signals to the model.
void stateChanged(QgsDataItem *item, QgsDataItem::State oldState)
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
void fetchMore(const QModelIndex &parent) override
bool canFetchMore(const QModelIndex &parent) const override
void beginInsertItems(QgsDataItem *parent, int first, int last)