QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 <QAbstractItemModel>
20 #include <QIcon>
21 #include <QMimeData>
22 #include <QMovie>
23 #include <QFuture>
24 #include <QFutureWatcher>
25 
26 #include "qgsdataitem.h"
27 
33 #ifndef SIP_RUN
34 class CORE_EXPORT QgsBrowserWatcher : public QFutureWatcher<QVector <QgsDataItem *> >
35 {
36  Q_OBJECT
37 
38  public:
40 
41  QgsDataItem *item() const { return mItem; }
42 
43  signals:
44  void finished( QgsDataItem *item, const QVector <QgsDataItem *> &items );
45 
46  private:
47  QgsDataItem *mItem = nullptr;
48 };
49 #endif
50 
65 class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
66 {
67  Q_OBJECT
68 
69  public:
70 
78  explicit QgsBrowserModel( QObject *parent = nullptr );
79 
80  ~QgsBrowserModel() override;
81 
83  {
84  PathRole = Qt::UserRole,
85  CommentRole = Qt::UserRole + 1,
87  };
88  // implemented methods from QAbstractItemModel for read-only access
89 
90  Qt::ItemFlags flags( const QModelIndex &index ) const override;
91  QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
92  bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
93  QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
94  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
95  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
96  QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
97  QModelIndex parent( const QModelIndex &index ) const override;
98  QStringList mimeTypes() const override;
99  QMimeData *mimeData( const QModelIndexList &indexes ) const override;
100  bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent ) override;
101  bool hasChildren( const QModelIndex &parent = QModelIndex() ) const override;
102  bool canFetchMore( const QModelIndex &parent ) const override;
103  void fetchMore( const QModelIndex &parent ) override;
104 
113  QModelIndex findItem( QgsDataItem *item, QgsDataItem *parent = nullptr ) const;
114 
119  QgsDataItem *dataItem( const QModelIndex &idx ) const;
120 
122  void refresh( const QString &path );
123 
125  void refresh( const QModelIndex &index = QModelIndex() );
126 
135  QModelIndex findPath( const QString &path, Qt::MatchFlag matchFlag = Qt::MatchExactly );
136 
138  static QModelIndex findPath( QAbstractItemModel *model, const QString &path, Qt::MatchFlag matchFlag = Qt::MatchExactly ) SIP_SKIP;
139 
149  QModelIndex findUri( const QString &uri, QModelIndex index = QModelIndex() );
150 
154  Q_DECL_DEPRECATED void connectItem( QgsDataItem *item ) SIP_DEPRECATED;
155 
161  bool initialized() const { return mInitialized; }
162 
171  QMap<QString, QgsDirectoryItem *> driveItems() const;
172 
173  signals:
174 
176  void stateChanged( const QModelIndex &index, QgsDataItem::State oldState );
177 
182  void connectionsChanged();
183 
184  public slots:
186  void reload();
187 
194  void refreshDrives();
195 
196  void beginInsertItems( QgsDataItem *parent, int first, int last );
197  void endInsertItems();
198  void beginRemoveItems( QgsDataItem *parent, int first, int last );
199  void endRemoveItems();
200  void itemDataChanged( QgsDataItem *item );
201  void itemStateChanged( QgsDataItem *item, QgsDataItem::State oldState );
202 
212  void addFavoriteDirectory( const QString &directory, const QString &name = QString() );
213 
219  void removeFavorite( const QModelIndex &index );
220 
227  void removeFavorite( QgsFavoriteItem *favorite ) SIP_SKIP;
228 
229  void updateProjectHome();
230 
232  void hidePath( QgsDataItem *item );
233 
238  void initialize();
239 
240 
241  protected:
243  void addRootItems();
244  void removeRootItems();
245 
246  QVector<QgsDataItem *> mRootItems;
247  QgsFavoritesItem *mFavorites = nullptr;
248  QgsDirectoryItem *mProjectHome = nullptr;
249 
250  private:
251  bool mInitialized = false;
252  QMap< QString, QgsDirectoryItem * > mDriveItems;
253 
254  void setupItemConnections( QgsDataItem *item );
255 
256  void removeRootItem( QgsDataItem *item );
257 
258  friend class TestQgsBrowserModel;
259  friend class TestQgsBrowserProxyModel;
260 };
261 
262 #endif // QGSBROWSERMODEL_H
bool initialized() const
Returns true if the model has been initialized.
QgsDataItem * item() const
QVector< QgsDataItem * > mRootItems
#define SIP_SKIP
Definition: qgis_sip.h:119
#define SIP_DEPRECATED
Definition: qgis_sip.h:99
A directory: contains subdirectories and layers.
Definition: qgsdataitem.h:590
Base class for all items in the model.
Definition: qgsdataitem.h:49
Custom sort role, see QgsDataItem::sortKey()
Contains various Favorites directories.
Definition: qgsdataitem.h:699
A model for showing available data sources and other items in a structured tree.