QGIS API Documentation  3.24.2-Tisler (13c1a02865)
qgsbrowsermodel.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsbrowsermodel.h
3  ---------------------
4  begin : July 2011
5  copyright : (C) 2011 by Martin Dobias
6  email : wonder dot sk at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 #ifndef QGSBROWSERMODEL_H
16 #define QGSBROWSERMODEL_H
17 
18 #include "qgis_core.h"
19 #include "qgis.h"
20 
21 #include <QAbstractItemModel>
22 #include <QIcon>
23 #include <QMimeData>
24 #include <QMovie>
25 
26 class QgsDataItem;
28 class QgsDirectoryItem;
29 class QgsFavoriteItem;
30 class QgsFavoritesItem;
31 
52 class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
53 {
54  Q_OBJECT
55 
56  public:
57 
65  explicit QgsBrowserModel( QObject *parent = nullptr );
66 
67  ~QgsBrowserModel() override;
68 
70  {
71  PathRole = Qt::UserRole,
72  CommentRole = Qt::UserRole + 1,
75  };
76  // implemented methods from QAbstractItemModel for read-only access
77 
78  Qt::ItemFlags flags( const QModelIndex &index ) const override;
79  QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
80  bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
81  QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
82  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
83  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
84  QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
85  QModelIndex parent( const QModelIndex &index ) const override;
86  QStringList mimeTypes() const override;
87  QMimeData *mimeData( const QModelIndexList &indexes ) const override;
88  bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent ) override;
89  bool hasChildren( const QModelIndex &parent = QModelIndex() ) const override;
90  bool canFetchMore( const QModelIndex &parent ) const override;
91  void fetchMore( const QModelIndex &parent ) override;
92 
101  QModelIndex findItem( QgsDataItem *item, QgsDataItem *parent = nullptr ) const;
102 
107  QgsDataItem *dataItem( const QModelIndex &idx ) const;
108 
110  void refresh( const QString &path );
111 
113  void refresh( const QModelIndex &index = QModelIndex() );
114 
123  QModelIndex findPath( const QString &path, Qt::MatchFlag matchFlag = Qt::MatchExactly );
124 
126  static QModelIndex findPath( QAbstractItemModel *model, const QString &path, Qt::MatchFlag matchFlag = Qt::MatchExactly ) SIP_SKIP;
127 
137  QModelIndex findUri( const QString &uri, QModelIndex index = QModelIndex() );
138 
142  Q_DECL_DEPRECATED void connectItem( QgsDataItem *item ) SIP_DEPRECATED;
143 
149  bool initialized() const { return mInitialized; }
150 
159  QMap<QString, QgsDirectoryItem *> driveItems() const;
160  signals:
161 
163  void stateChanged( const QModelIndex &index, Qgis::BrowserItemState oldState );
164 
170  void connectionsChanged( const QString &providerKey );
171 
172  public slots:
174  void reload();
175 
182  void refreshDrives();
183 
184  void beginInsertItems( QgsDataItem *parent, int first, int last );
185  void endInsertItems();
186  void beginRemoveItems( QgsDataItem *parent, int first, int last );
187  void endRemoveItems();
188  void itemDataChanged( QgsDataItem *item );
189 
193  void itemStateChanged( QgsDataItem *item, Qgis::BrowserItemState oldState );
194 
204  void addFavoriteDirectory( const QString &directory, const QString &name = QString() );
205 
211  void removeFavorite( const QModelIndex &index );
212 
219  void removeFavorite( QgsFavoriteItem *favorite ) SIP_SKIP;
220 
221  void updateProjectHome();
222 
224  void hidePath( QgsDataItem *item );
225 
230  void initialize();
231 
232  protected:
234  void addRootItems();
235  void removeRootItems();
236 
237  QVector<QgsDataItem *> mRootItems;
238  QgsFavoritesItem *mFavorites = nullptr;
239  QgsDirectoryItem *mProjectHome = nullptr;
240 
241  private slots:
242  void dataItemProviderAdded( QgsDataItemProvider *provider );
243  void dataItemProviderWillBeRemoved( QgsDataItemProvider *provider );
244  void onConnectionsChanged( const QString &providerKey );
245 
246  private:
247  bool mInitialized = false;
248  QMap< QString, QgsDirectoryItem * > mDriveItems;
249 
250  void setupItemConnections( QgsDataItem *item );
251 
252  void removeRootItem( QgsDataItem *item );
253 
254  QgsDataItem *addProviderRootItem( QgsDataItemProvider *provider );
255 
256  friend class TestQgsBrowserModel;
257  friend class TestQgsBrowserProxyModel;
258 };
259 
260 #endif // QGSBROWSERMODEL_H
BrowserItemState
Browser item states.
Definition: qgis.h:281
A model for showing available data sources and other items in a structured tree.
void connectionsChanged(const QString &providerKey)
Emitted when connections for the specified providerKey have changed in the browser.
QVector< QgsDataItem * > mRootItems
void stateChanged(const QModelIndex &index, Qgis::BrowserItemState oldState)
Emitted when item children fetch was finished.
bool initialized() const
Returns true if the model has been initialized.
@ ProviderKeyRole
Data item provider key that created the item, see QgsDataItem::providerKey()
@ SortRole
Custom sort role, see QgsDataItem::sortKey()
This is the interface for those who want to add custom data items to the browser tree.
Base class for all items in the model.
Definition: qgsdataitem.h:46
A directory: contains subdirectories and layers.
A directory item showing the a single favorite directory.
Contains various Favorites directories.
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
#define SIP_SKIP
Definition: qgis_sip.h:126