QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
29 
35 #ifndef SIP_RUN
36 class CORE_EXPORT QgsBrowserWatcher : public QFutureWatcher<QVector <QgsDataItem *> >
37 {
38  Q_OBJECT
39 
40  public:
42 
43  QgsDataItem *item() const { return mItem; }
44 
45  signals:
46  void finished( QgsDataItem *item, const QVector <QgsDataItem *> &items );
47 
48  private:
49  QgsDataItem *mItem = nullptr;
50 };
51 #endif
52 
73 class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
74 {
75  Q_OBJECT
76 
77  public:
78 
86  explicit QgsBrowserModel( QObject *parent = nullptr );
87 
88  ~QgsBrowserModel() override;
89 
91  {
92  PathRole = Qt::UserRole,
93  CommentRole = Qt::UserRole + 1,
96  };
97  // implemented methods from QAbstractItemModel for read-only access
98 
99  Qt::ItemFlags flags( const QModelIndex &index ) const override;
100  QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
101  bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
102  QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
103  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
104  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
105  QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
106  QModelIndex parent( const QModelIndex &index ) const override;
107  QStringList mimeTypes() const override;
108  QMimeData *mimeData( const QModelIndexList &indexes ) const override;
109  bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent ) override;
110  bool hasChildren( const QModelIndex &parent = QModelIndex() ) const override;
111  bool canFetchMore( const QModelIndex &parent ) const override;
112  void fetchMore( const QModelIndex &parent ) override;
113 
122  QModelIndex findItem( QgsDataItem *item, QgsDataItem *parent = nullptr ) const;
123 
128  QgsDataItem *dataItem( const QModelIndex &idx ) const;
129 
131  void refresh( const QString &path );
132 
134  void refresh( const QModelIndex &index = QModelIndex() );
135 
144  QModelIndex findPath( const QString &path, Qt::MatchFlag matchFlag = Qt::MatchExactly );
145 
147  static QModelIndex findPath( QAbstractItemModel *model, const QString &path, Qt::MatchFlag matchFlag = Qt::MatchExactly ) SIP_SKIP;
148 
158  QModelIndex findUri( const QString &uri, QModelIndex index = QModelIndex() );
159 
163  Q_DECL_DEPRECATED void connectItem( QgsDataItem *item ) SIP_DEPRECATED;
164 
170  bool initialized() const { return mInitialized; }
171 
180  QMap<QString, QgsDirectoryItem *> driveItems() const;
181  signals:
182 
184  void stateChanged( const QModelIndex &index, QgsDataItem::State oldState );
185 
191  void connectionsChanged( const QString &providerKey );
192 
193  public slots:
195  void reload();
196 
203  void refreshDrives();
204 
205  void beginInsertItems( QgsDataItem *parent, int first, int last );
206  void endInsertItems();
207  void beginRemoveItems( QgsDataItem *parent, int first, int last );
208  void endRemoveItems();
209  void itemDataChanged( QgsDataItem *item );
210  void itemStateChanged( QgsDataItem *item, QgsDataItem::State oldState );
211 
221  void addFavoriteDirectory( const QString &directory, const QString &name = QString() );
222 
228  void removeFavorite( const QModelIndex &index );
229 
236  void removeFavorite( QgsFavoriteItem *favorite ) SIP_SKIP;
237 
238  void updateProjectHome();
239 
241  void hidePath( QgsDataItem *item );
242 
247  void initialize();
248 
249  protected:
251  void addRootItems();
252  void removeRootItems();
253 
254  QVector<QgsDataItem *> mRootItems;
255  QgsFavoritesItem *mFavorites = nullptr;
256  QgsDirectoryItem *mProjectHome = nullptr;
257 
258  private slots:
259  void dataItemProviderAdded( QgsDataItemProvider *provider );
260  void dataItemProviderWillBeRemoved( QgsDataItemProvider *provider );
261  void onConnectionsChanged( const QString &providerKey );
262 
263  private:
264  bool mInitialized = false;
265  QMap< QString, QgsDirectoryItem * > mDriveItems;
266 
267  void setupItemConnections( QgsDataItem *item );
268 
269  void removeRootItem( QgsDataItem *item );
270 
271  QgsDataItem *addProviderRootItem( QgsDataItemProvider *provider );
272 
273  friend class TestQgsBrowserModel;
274  friend class TestQgsBrowserProxyModel;
275 };
276 
277 #endif // QGSBROWSERMODEL_H
QgsBrowserModel::ItemDataRole
ItemDataRole
Definition: qgsbrowsermodel.h:90
QgsBrowserModel::ProviderKeyRole
@ ProviderKeyRole
Data item provider key that created the item,.
Definition: qgsbrowsermodel.h:95
qgsdataitem.h
QgsBrowserModel::SortRole
@ SortRole
Custom sort role, see QgsDataItem::sortKey()
Definition: qgsbrowsermodel.h:94
QgsBrowserModel::mRootItems
QVector< QgsDataItem * > mRootItems
Definition: qgsbrowsermodel.h:254
QgsBrowserWatcher::item
QgsDataItem * item() const
Definition: qgsbrowsermodel.h:43
SIP_DEPRECATED
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsDirectoryItem
Definition: qgsdataitem.h:671
QgsDataItemProvider
Definition: qgsdataitemprovider.h:45
QgsFavoritesItem
Definition: qgsdataitem.h:808
QgsDataItem
Definition: qgsdataitem.h:49
QgsBrowserModel::initialized
bool initialized() const
Returns true if the model has been initialized.
Definition: qgsbrowsermodel.h:170
QgsDataItem::State
State
Definition: qgsdataitem.h:120
QgsBrowserWatcher
Definition: qgsbrowsermodel.h:36
QgsBrowserModel
Definition: qgsbrowsermodel.h:73