QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 #include <QAbstractItemModel>
21 #include <QIcon>
22 #include <QMimeData>
23 #include <QMovie>
24 #include <QFuture>
25 #include <QFutureWatcher>
26 
28 class QgsDataItem;
29 class QgsDirectoryItem;
30 class QgsFavoriteItem;
31 class QgsFavoritesItem;
32 
38 #ifndef SIP_RUN
39 class CORE_EXPORT QgsBrowserWatcher : public QFutureWatcher<QVector <QgsDataItem *> >
40 {
41  Q_OBJECT
42 
43  public:
45 
46  QgsDataItem *item() const { return mItem; }
47 
48  signals:
49  void finished( QgsDataItem *item, const QVector <QgsDataItem *> &items );
50 
51  private:
52  QgsDataItem *mItem = nullptr;
53 };
54 #endif
55 
76 class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
77 {
78  Q_OBJECT
79 
80  public:
81 
89  explicit QgsBrowserModel( QObject *parent = nullptr );
90 
91  ~QgsBrowserModel() override;
92 
94  {
95  PathRole = Qt::UserRole,
96  CommentRole = Qt::UserRole + 1,
99  };
100  // implemented methods from QAbstractItemModel for read-only access
101 
102  Qt::ItemFlags flags( const QModelIndex &index ) const override;
103  QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
104  bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
105  QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
106  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
107  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
108  QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
109  QModelIndex parent( const QModelIndex &index ) const override;
110  QStringList mimeTypes() const override;
111  QMimeData *mimeData( const QModelIndexList &indexes ) const override;
112  bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent ) override;
113  bool hasChildren( const QModelIndex &parent = QModelIndex() ) const override;
114  bool canFetchMore( const QModelIndex &parent ) const override;
115  void fetchMore( const QModelIndex &parent ) override;
116 
125  QModelIndex findItem( QgsDataItem *item, QgsDataItem *parent = nullptr ) const;
126 
131  QgsDataItem *dataItem( const QModelIndex &idx ) const;
132 
134  void refresh( const QString &path );
135 
137  void refresh( const QModelIndex &index = QModelIndex() );
138 
147  QModelIndex findPath( const QString &path, Qt::MatchFlag matchFlag = Qt::MatchExactly );
148 
150  static QModelIndex findPath( QAbstractItemModel *model, const QString &path, Qt::MatchFlag matchFlag = Qt::MatchExactly ) SIP_SKIP;
151 
161  QModelIndex findUri( const QString &uri, QModelIndex index = QModelIndex() );
162 
166  Q_DECL_DEPRECATED void connectItem( QgsDataItem *item ) SIP_DEPRECATED;
167 
173  bool initialized() const { return mInitialized; }
174 
183  QMap<QString, QgsDirectoryItem *> driveItems() const;
184  signals:
185 
187  void stateChanged( const QModelIndex &index, Qgis::BrowserItemState oldState );
188 
194  void connectionsChanged( const QString &providerKey );
195 
196  public slots:
198  void reload();
199 
206  void refreshDrives();
207 
208  void beginInsertItems( QgsDataItem *parent, int first, int last );
209  void endInsertItems();
210  void beginRemoveItems( QgsDataItem *parent, int first, int last );
211  void endRemoveItems();
212  void itemDataChanged( QgsDataItem *item );
213 
217  void itemStateChanged( QgsDataItem *item, Qgis::BrowserItemState oldState );
218 
228  void addFavoriteDirectory( const QString &directory, const QString &name = QString() );
229 
235  void removeFavorite( const QModelIndex &index );
236 
243  void removeFavorite( QgsFavoriteItem *favorite ) SIP_SKIP;
244 
245  void updateProjectHome();
246 
248  void hidePath( QgsDataItem *item );
249 
254  void initialize();
255 
256  protected:
258  void addRootItems();
259  void removeRootItems();
260 
261  QVector<QgsDataItem *> mRootItems;
262  QgsFavoritesItem *mFavorites = nullptr;
263  QgsDirectoryItem *mProjectHome = nullptr;
264 
265  private slots:
266  void dataItemProviderAdded( QgsDataItemProvider *provider );
267  void dataItemProviderWillBeRemoved( QgsDataItemProvider *provider );
268  void onConnectionsChanged( const QString &providerKey );
269 
270  private:
271  bool mInitialized = false;
272  QMap< QString, QgsDirectoryItem * > mDriveItems;
273 
274  void setupItemConnections( QgsDataItem *item );
275 
276  void removeRootItem( QgsDataItem *item );
277 
278  QgsDataItem *addProviderRootItem( QgsDataItemProvider *provider );
279 
280  friend class TestQgsBrowserModel;
281  friend class TestQgsBrowserProxyModel;
282 };
283 
284 #endif // QGSBROWSERMODEL_H
BrowserItemState
Browser item states.
Definition: qgis.h:249
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()
QgsDataItem * item() const
void finished(QgsDataItem *item, const QVector< QgsDataItem * > &items)
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