QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
Public Types | Public Slots | Signals | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Friends | List of all members
QgsBrowserModel Class Reference

A model for showing available data sources and other items in a structured tree. More...

#include <qgsbrowsermodel.h>

Inheritance diagram for QgsBrowserModel:
Inheritance graph
[legend]

Public Types

enum  ItemDataRole {
  PathRole = Qt::UserRole , CommentRole = Qt::UserRole + 1 , SortRole , ProviderKeyRole ,
  LayerMetadataRole
}
 

Public Slots

void addFavoriteDirectory (const QString &directory, const QString &name=QString())
 Adds a directory to the favorites group. More...
 
void beginInsertItems (QgsDataItem *parent, int first, int last)
 
void beginRemoveItems (QgsDataItem *parent, int first, int last)
 
void endInsertItems ()
 
void endRemoveItems ()
 
void hidePath (QgsDataItem *item)
 Hide the given path in the browser model. More...
 
void initialize ()
 Delayed initialization, needed because the provider registry must be already populated. More...
 
void itemDataChanged (QgsDataItem *item)
 
void itemStateChanged (QgsDataItem *item, Qgis::BrowserItemState oldState)
 Emitted when an item's state is changed. More...
 
void refreshDrives ()
 Refreshes the list of drive items, removing any corresponding to removed drives and adding newly added drives. More...
 
void reload ()
 Reload the whole model. More...
 
void removeFavorite (const QModelIndex &index)
 Removes a favorite directory from its corresponding model index. More...
 
void removeFavorite (QgsFavoriteItem *favorite)
 Removes a favorite item. More...
 
void updateProjectHome ()
 

Signals

void connectionsChanged (const QString &providerKey)
 Emitted when connections for the specified providerKey have changed in the browser. More...
 
void stateChanged (const QModelIndex &index, Qgis::BrowserItemState oldState)
 Emitted when item children fetch was finished. More...
 

Public Member Functions

 QgsBrowserModel (QObject *parent=nullptr)
 Constructor for QgsBrowserModel, with the specified parent object. More...
 
 ~QgsBrowserModel () override
 
bool canFetchMore (const QModelIndex &parent) const override
 
int columnCount (const QModelIndex &parent=QModelIndex()) const override
 
Q_DECL_DEPRECATED void connectItem (QgsDataItem *item)
 
QVariant data (const QModelIndex &index, int role=Qt::DisplayRole) const override
 
QgsDataItemdataItem (const QModelIndex &idx) const
 Returns the data item at the specified index, or nullptr if no item exists at the index. More...
 
QMap< QString, QgsDirectoryItem * > driveItems () const
 Returns a map of the root drive items shown in the browser. More...
 
bool dropMimeData (const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
 
void fetchMore (const QModelIndex &parent) override
 
QModelIndex findItem (QgsDataItem *item, QgsDataItem *parent=nullptr) const
 Returns the model index corresponding to the specified data item. More...
 
QModelIndex findPath (const QString &path, Qt::MatchFlag matchFlag=Qt::MatchExactly)
 Returns index of item with given path. More...
 
QModelIndex findUri (const QString &uri, QModelIndex index=QModelIndex())
 Returns index of layer item with given uri. More...
 
Qt::ItemFlags flags (const QModelIndex &index) const override
 
bool hasChildren (const QModelIndex &parent=QModelIndex()) const override
 
QVariant headerData (int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
 
QModelIndex index (int row, int column, const QModelIndex &parent=QModelIndex()) const override
 
bool initialized () const
 Returns true if the model has been initialized. More...
 
QMimeData * mimeData (const QModelIndexList &indexes) const override
 
QStringList mimeTypes () const override
 
QModelIndex parent (const QModelIndex &index) const override
 
void refresh (const QModelIndex &index=QModelIndex())
 Refresh item children. More...
 
void refresh (const QString &path)
 Refresh item specified by path. More...
 
QVector< QgsDataItem * > rootItems () const
 Returns the root items for the model. More...
 
int rowCount (const QModelIndex &parent=QModelIndex()) const override
 
bool setData (const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
 

Static Public Member Functions

static QModelIndex findPath (QAbstractItemModel *model, const QString &path, Qt::MatchFlag matchFlag=Qt::MatchExactly)
 

Protected Member Functions

void addRootItems ()
 Populates the model. More...
 
void removeRootItems ()
 

Protected Attributes

QgsFavoritesItemmFavorites = nullptr
 
QgsDirectoryItemmProjectHome = nullptr
 
QVector< QgsDataItem * > mRootItems
 

Friends

class TestQgsBrowserModel
 
class TestQgsBrowserProxyModel
 

Detailed Description

A model for showing available data sources and other items in a structured tree.

QgsBrowserModel is the foundation for the QGIS browser panel, and includes items for the different data providers and folders accessible to users.

QgsBrowserModel models are not initially populated and use a deferred initialization approach. After constructing a QgsBrowserModel, a call must be made to initialize() in order to populate the model.

Note
Since QGIS 3.10 it is recommended to use QgsBrowserGuiModel from GUI library. Implementation of data items used from QgsBrowserModel should not trigger any GUI operations such as opening of widgets/dialogs or showing message boxes. Such actions should be implemented in a new QgsDataItemGuiProvider subclass which is used by QgsBrowserGuiModel (but not by QgsBrowserModel).

Definition at line 52 of file qgsbrowsermodel.h.

Member Enumeration Documentation

◆ ItemDataRole

Enumerator
PathRole 

Item path used to access path in the tree, see QgsDataItem::mPath.

CommentRole 

Item comment.

SortRole 

Custom sort role, see QgsDataItem::sortKey()

ProviderKeyRole 

Data item provider key that created the item, see QgsDataItem::providerKey()

Since
QGIS 3.12
LayerMetadataRole 

Definition at line 69 of file qgsbrowsermodel.h.

Constructor & Destructor Documentation

◆ QgsBrowserModel()

QgsBrowserModel::QgsBrowserModel ( QObject *  parent = nullptr)
explicit

Constructor for QgsBrowserModel, with the specified parent object.

Note
QgsBrowserModel models are not initially populated and use a deferred initialization approach. After constructing a QgsBrowserModel, a call must be made to initialize() in order to populate the model.

Definition at line 72 of file qgsbrowsermodel.cpp.

◆ ~QgsBrowserModel()

QgsBrowserModel::~QgsBrowserModel ( )
override

Definition at line 82 of file qgsbrowsermodel.cpp.

Member Function Documentation

◆ addFavoriteDirectory

void QgsBrowserModel::addFavoriteDirectory ( const QString &  directory,
const QString &  name = QString() 
)
slot

Adds a directory to the favorites group.

If name is specified, it will be used as the favorite's name. Otherwise the name will be set to match directory.

See also
removeFavorite()
Since
QGIS 3.0

Definition at line 752 of file qgsbrowsermodel.cpp.

◆ addRootItems()

void QgsBrowserModel::addRootItems ( )
protected

Populates the model.

Definition at line 114 of file qgsbrowsermodel.cpp.

◆ beginInsertItems

void QgsBrowserModel::beginInsertItems ( QgsDataItem parent,
int  first,
int  last 
)
slot

Definition at line 591 of file qgsbrowsermodel.cpp.

◆ beginRemoveItems

void QgsBrowserModel::beginRemoveItems ( QgsDataItem parent,
int  first,
int  last 
)
slot

Definition at line 606 of file qgsbrowsermodel.cpp.

◆ canFetchMore()

bool QgsBrowserModel::canFetchMore ( const QModelIndex &  parent) const
override

Definition at line 713 of file qgsbrowsermodel.cpp.

◆ columnCount()

int QgsBrowserModel::columnCount ( const QModelIndex &  parent = QModelIndex()) const
override

Definition at line 415 of file qgsbrowsermodel.cpp.

◆ connectionsChanged

void QgsBrowserModel::connectionsChanged ( const QString &  providerKey)
signal

Emitted when connections for the specified providerKey have changed in the browser.

Forwarded to the widget and used to notify the provider dialogs of a changed connection.

◆ connectItem()

void QgsBrowserModel::connectItem ( QgsDataItem item)
Deprecated:
Deprecated since QGIS 3.4 – this method has no effect, and is dangerous to call in earlier QGIS versions. Any usage should be removed (and will have no harmful side-effects!).

Definition at line 490 of file qgsbrowsermodel.cpp.

◆ data()

QVariant QgsBrowserModel::data ( const QModelIndex &  index,
int  role = Qt::DisplayRole 
) const
override

Definition at line 290 of file qgsbrowsermodel.cpp.

◆ dataItem()

QgsDataItem * QgsBrowserModel::dataItem ( const QModelIndex &  idx) const

Returns the data item at the specified index, or nullptr if no item exists at the index.

Definition at line 705 of file qgsbrowsermodel.cpp.

◆ driveItems()

QMap< QString, QgsDirectoryItem * > QgsBrowserModel::driveItems ( ) const

Returns a map of the root drive items shown in the browser.

These correspond to the top-level directory items shown, e.g. on Windows the C:\, D:\, etc, and on Linux the "/" root directory.

Since
QGIS 3.6

Definition at line 244 of file qgsbrowsermodel.cpp.

◆ dropMimeData()

bool QgsBrowserModel::dropMimeData ( const QMimeData *  data,
Qt::DropAction  action,
int  row,
int  column,
const QModelIndex &  parent 
)
override

Definition at line 691 of file qgsbrowsermodel.cpp.

◆ endInsertItems

void QgsBrowserModel::endInsertItems ( )
slot

Definition at line 601 of file qgsbrowsermodel.cpp.

◆ endRemoveItems

void QgsBrowserModel::endRemoveItems ( )
slot

Definition at line 614 of file qgsbrowsermodel.cpp.

◆ fetchMore()

void QgsBrowserModel::fetchMore ( const QModelIndex &  parent)
override

Definition at line 721 of file qgsbrowsermodel.cpp.

◆ findItem()

QModelIndex QgsBrowserModel::findItem ( QgsDataItem item,
QgsDataItem parent = nullptr 
) const

Returns the model index corresponding to the specified data item.

If the item was not found, an invalid QModelIndex is returned.

If the parent item is argument is specified, then only items which are children of parent are searched. If no parent is specified, then all items within the model are searched.

Definition at line 575 of file qgsbrowsermodel.cpp.

◆ findPath() [1/2]

QModelIndex QgsBrowserModel::findPath ( const QString &  path,
Qt::MatchFlag  matchFlag = Qt::MatchExactly 
)

Returns index of item with given path.

It only searches in currently fetched items, i.e. it does not fetch children.

Parameters
pathitem path
matchFlagsupported is Qt::MatchExactly and Qt::MatchStartsWith which has reverse meaning, i.e. find item with the longest match from start with path (to get as close/deep as possible to deleted item).
Returns
model index, invalid if item not found

Definition at line 421 of file qgsbrowsermodel.cpp.

◆ findPath() [2/2]

QModelIndex QgsBrowserModel::findPath ( QAbstractItemModel *  model,
const QString &  path,
Qt::MatchFlag  matchFlag = Qt::MatchExactly 
)
static
Note
not available in Python bindings

Definition at line 426 of file qgsbrowsermodel.cpp.

◆ findUri()

QModelIndex QgsBrowserModel::findUri ( const QString &  uri,
QModelIndex  index = QModelIndex() 
)

Returns index of layer item with given uri.

It only searches in currently fetched items, i.e. it does not fetch children.

Parameters
uriitem uri
indexthe current index of the parent (to search for children)
Returns
model index, invalid if item not found
Since
QGIS 3.6

Definition at line 466 of file qgsbrowsermodel.cpp.

◆ flags()

Qt::ItemFlags QgsBrowserModel::flags ( const QModelIndex &  index) const
override

Definition at line 260 of file qgsbrowsermodel.cpp.

◆ hasChildren()

bool QgsBrowserModel::hasChildren ( const QModelIndex &  parent = QModelIndex()) const
override

Definition at line 406 of file qgsbrowsermodel.cpp.

◆ headerData()

QVariant QgsBrowserModel::headerData ( int  section,
Qt::Orientation  orientation,
int  role = Qt::DisplayRole 
) const
override

Definition at line 377 of file qgsbrowsermodel.cpp.

◆ hidePath

void QgsBrowserModel::hidePath ( QgsDataItem item)
slot

Hide the given path in the browser model.

Definition at line 775 of file qgsbrowsermodel.cpp.

◆ index()

QModelIndex QgsBrowserModel::index ( int  row,
int  column,
const QModelIndex &  parent = QModelIndex() 
) const
override

Definition at line 555 of file qgsbrowsermodel.cpp.

◆ initialize

void QgsBrowserModel::initialize ( )
slot

Delayed initialization, needed because the provider registry must be already populated.

See also
initialized()

Definition at line 250 of file qgsbrowsermodel.cpp.

◆ initialized()

bool QgsBrowserModel::initialized ( ) const
inline

Returns true if the model has been initialized.

See also
initialize()

Definition at line 150 of file qgsbrowsermodel.h.

◆ itemDataChanged

void QgsBrowserModel::itemDataChanged ( QgsDataItem item)
slot

Definition at line 619 of file qgsbrowsermodel.cpp.

◆ itemStateChanged

void QgsBrowserModel::itemStateChanged ( QgsDataItem item,
Qgis::BrowserItemState  oldState 
)
slot

Emitted when an item's state is changed.

Definition at line 627 of file qgsbrowsermodel.cpp.

◆ mimeData()

QMimeData * QgsBrowserModel::mimeData ( const QModelIndexList &  indexes) const
override

Definition at line 668 of file qgsbrowsermodel.cpp.

◆ mimeTypes()

QStringList QgsBrowserModel::mimeTypes ( ) const
override

Definition at line 659 of file qgsbrowsermodel.cpp.

◆ parent()

QModelIndex QgsBrowserModel::parent ( const QModelIndex &  index) const
override

Definition at line 566 of file qgsbrowsermodel.cpp.

◆ refresh() [1/2]

void QgsBrowserModel::refresh ( const QModelIndex &  index = QModelIndex())

Refresh item children.

Definition at line 741 of file qgsbrowsermodel.cpp.

◆ refresh() [2/2]

void QgsBrowserModel::refresh ( const QString &  path)

Refresh item specified by path.

Definition at line 734 of file qgsbrowsermodel.cpp.

◆ refreshDrives

void QgsBrowserModel::refreshDrives ( )
slot

Refreshes the list of drive items, removing any corresponding to removed drives and adding newly added drives.

Since
QGIS 3.4

Definition at line 504 of file qgsbrowsermodel.cpp.

◆ reload

void QgsBrowserModel::reload ( )
slot

Reload the whole model.

Definition at line 495 of file qgsbrowsermodel.cpp.

◆ removeFavorite [1/2]

void QgsBrowserModel::removeFavorite ( const QModelIndex &  index)
slot

Removes a favorite directory from its corresponding model index.

See also
addFavoriteDirectory()
Since
QGIS 3.0

Definition at line 758 of file qgsbrowsermodel.cpp.

◆ removeFavorite [2/2]

void QgsBrowserModel::removeFavorite ( QgsFavoriteItem favorite)
slot

Removes a favorite item.

See also
addFavoriteDirectory()
Note
Not available in Python bindings
Since
QGIS 3.6

Definition at line 767 of file qgsbrowsermodel.cpp.

◆ removeRootItems()

void QgsBrowserModel::removeRootItems ( )
protected

Definition at line 191 of file qgsbrowsermodel.cpp.

◆ rootItems()

QVector< QgsDataItem * > QgsBrowserModel::rootItems ( ) const
inline

Returns the root items for the model.

Since
QGIS 3.28

Definition at line 167 of file qgsbrowsermodel.h.

◆ rowCount()

int QgsBrowserModel::rowCount ( const QModelIndex &  parent = QModelIndex()) const
override

Definition at line 388 of file qgsbrowsermodel.cpp.

◆ setData()

bool QgsBrowserModel::setData ( const QModelIndex &  index,
const QVariant &  value,
int  role = Qt::EditRole 
)
override

Definition at line 349 of file qgsbrowsermodel.cpp.

◆ stateChanged

void QgsBrowserModel::stateChanged ( const QModelIndex &  index,
Qgis::BrowserItemState  oldState 
)
signal

Emitted when item children fetch was finished.

◆ updateProjectHome

void QgsBrowserModel::updateProjectHome ( )
slot

Definition at line 87 of file qgsbrowsermodel.cpp.

Friends And Related Function Documentation

◆ TestQgsBrowserModel

friend class TestQgsBrowserModel
friend

Definition at line 265 of file qgsbrowsermodel.h.

◆ TestQgsBrowserProxyModel

friend class TestQgsBrowserProxyModel
friend

Definition at line 266 of file qgsbrowsermodel.h.

Member Data Documentation

◆ mFavorites

QgsFavoritesItem* QgsBrowserModel::mFavorites = nullptr
protected

Definition at line 247 of file qgsbrowsermodel.h.

◆ mProjectHome

QgsDirectoryItem* QgsBrowserModel::mProjectHome = nullptr
protected

Definition at line 248 of file qgsbrowsermodel.h.

◆ mRootItems

QVector<QgsDataItem *> QgsBrowserModel::mRootItems
protected

Definition at line 246 of file qgsbrowsermodel.h.


The documentation for this class was generated from the following files: