QGIS API Documentation  2.9.0-Master
qgsdataitem.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsdataitem.h - Items representing data
3  -------------------
4  begin : 2011-04-01
5  copyright : (C) 2011 Radim Blazek
6  email : radim dot blazek at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 #ifndef QGSDATAITEM_H
18 #define QGSDATAITEM_H
19 
20 #include <QFileSystemWatcher>
21 #include <QFutureWatcher>
22 #include <QIcon>
23 #include <QLibrary>
24 #include <QMovie>
25 #include <QObject>
26 #include <QPixmap>
27 #include <QString>
28 #include <QTreeWidget>
29 #include <QVector>
30 
31 #include "qgsapplication.h"
32 #include "qgsmaplayer.h"
34 
35 class QgsDataProvider;
36 class QgsDataItem;
37 
38 typedef QgsDataItem * dataItem_t( QString, QgsDataItem* );
39 
40 
43 class CORE_EXPORT QgsDataItem : public QObject
44 {
45  Q_OBJECT
46  Q_ENUMS( Type )
47  Q_ENUMS( State )
48  public:
49  enum Type
50  {
55  Favourites
56  };
57 
59  QgsDataItem( QgsDataItem::Type type, QgsDataItem* parent, QString name, QString path );
60  virtual ~QgsDataItem();
61 
62  bool hasChildren();
63 
64  int rowCount();
65 
68  virtual QVector<QgsDataItem*> createChildren();
69 
70  enum State
71  {
74  Populated
75  };
76 
78  State state() const;
79 
84  virtual void setState( State state );
85 
87  bool isPopulated() { return state() == Populated; }
88 
89  // Insert new child using alphabetical order based on mName, emits necessary signal to model before and after, sets parent and connects signals
90  // refresh - refresh populated item, emit signals to model
91  virtual void addChildItem( QgsDataItem *child, bool refresh = false );
92 
93  // remove and delete child item, signals to browser are emitted
94  virtual void deleteChildItem( QgsDataItem * child );
95 
96  // remove child item but don't delete it, signals to browser are emitted
97  // returns pointer to the removed item or null if no such item was found
98  virtual QgsDataItem *removeChildItem( QgsDataItem * child );
99 
100  virtual bool equal( const QgsDataItem *other );
101 
102  virtual QWidget *paramWidget() { return 0; }
103 
104  // list of actions provided by this item - usually used for popup menu on right-click
105  virtual QList<QAction*> actions() { return QList<QAction*>(); }
106 
107  // whether accepts drag&drop'd layers - e.g. for import
108  virtual bool acceptDrop() { return false; }
109 
110  // try to process the data dropped on this item
111  virtual bool handleDrop( const QMimeData * /*data*/, Qt::DropAction /*action*/ ) { return false; }
112 
114  {
115  NoCapabilities = 0,
116  SetCrs = 1 << 0,
117  Fertile = 1 << 1,
118  Fast = 1 << 2
119  };
120  Q_DECLARE_FLAGS( Capabilities, Capability )
121 
122  // This will _write_ selected crs in data source
123  virtual bool setCrs( QgsCoordinateReferenceSystem crs )
124  { Q_UNUSED( crs ); return false; }
125 
127  Q_DECL_DEPRECATED virtual Capability capabilities() { return NoCapabilities; }
128 
129  virtual Capabilities capabilities2() const { return mCapabilities; }
130 
131  virtual void setCapabilities( Capabilities capabilities ) { mCapabilities = capabilities; }
132 
133  // static methods
134 
135  // Find child index in vector of items using '==' operator
136  static int findItem( QVector<QgsDataItem*> items, QgsDataItem * item );
137 
138  // members
139 
140  Type type() const { return mType; }
141 
144  QgsDataItem* parent() const { return mParent; }
147  void setParent( QgsDataItem* parent );
148  QVector<QgsDataItem*> children() const { return mChildren; }
149  virtual QIcon icon();
150  QString name() const { return mName; }
151  void setName( const QString &name ) { mName = name; }
152  QString path() const { return mPath; }
153  void setPath( const QString &path ) { mPath = path; }
155  static QString pathComponent( const QString &component );
156 
157  // Because QIcon (QPixmap) must not be used in outside the GUI thread, it is
158  // not possible to set mIcon in constructor. Either use mIconName/setIconName()
159  // or implement icon().
160  void setIcon( QIcon icon ) { mIcon = icon; }
161  void setIconName( const QString & iconName ) { mIconName = iconName; }
162 
163  void setToolTip( QString msg ) { mToolTip = msg; }
164  QString toolTip() const { return mToolTip; }
165 
166  // deleteLater() items anc clear the vector
167  static void deleteLater( QVector<QgsDataItem*> &items );
168 
170  void moveToThread( QThread * targetThread );
171 
172  protected:
173  virtual void populate( QVector<QgsDataItem*> children );
174  virtual void refresh( QVector<QgsDataItem*> children );
175  QIcon populatingIcon() { return mPopulatingIcon; }
182  bool deferredDelete() { return mDeferredDelete; }
183 
185  Capabilities mCapabilities;
187  QVector<QgsDataItem*> mChildren; // easier to have it always
191  QString mName;
192  // Path is slash ('/') separated chain of item identifiers which are usually item names, but may be differen if it is
193  // necessary to distinguish paths of two providers to the same source (e.g GRASS location and standard directory have the same
194  // name but different paths). Identifiers in path must not contain '/' characters.
195  // The path is used to identify item in tree.
196  QString mPath;
197  QString mToolTip;
198  QString mIconName;
199  QIcon mIcon;
200  static QMap<QString, QIcon> mIconMap;
201 
202  public slots:
210  virtual void deleteLater();
211 
212  // Populate children using children vector created by createChildren()
213  virtual void populate();
214 
216  virtual void depopulate();
217 
218  virtual void refresh();
219 
220  void emitBeginInsertItems( QgsDataItem* parent, int first, int last );
221  void emitEndInsertItems();
222  void emitBeginRemoveItems( QgsDataItem* parent, int first, int last );
223  void emitEndRemoveItems();
224  void emitDataChanged( QgsDataItem* item );
225  void emitDataChanged( );
226  void emitStateChanged( QgsDataItem* item, QgsDataItem::State oldState );
227  virtual void childrenCreated();
228  void setPopulatingIcon();
229 
230  signals:
231  void beginInsertItems( QgsDataItem* parent, int first, int last );
232  void endInsertItems();
233  void beginRemoveItems( QgsDataItem* parent, int first, int last );
234  void endRemoveItems();
235  void dataChanged( QgsDataItem * item );
236  void stateChanged( QgsDataItem * item, QgsDataItem::State oldState );
237 
238  private:
239  static QVector<QgsDataItem*> runCreateChildren( QgsDataItem* item );
240 
241  // Set to true if object has to be deleted when possible (nothing running in threads)
242  bool mDeferredDelete;
243  QFutureWatcher< QVector <QgsDataItem*> > *mFutureWatcher;
244  // number of items currently in loading (populating) state
245  static int mPopulatingCount;
246  static QMovie * mPopulatingMovie;
247  static QIcon mPopulatingIcon;
248 };
249 
250 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsDataItem::Capabilities )
251 
252 
253 class CORE_EXPORT QgsLayerItem : public QgsDataItem
254 {
255  Q_OBJECT
256  public:
258  {
268  Plugin
269  };
270 
271  QgsLayerItem( QgsDataItem* parent, QString name, QString path, QString uri, LayerType layerType, QString providerKey );
272 
273  // --- reimplemented from QgsDataItem ---
274 
275  virtual bool equal( const QgsDataItem *other ) override;
276 
277  // --- New virtual methods for layer item derived classes ---
278 
279  // Returns QgsMapLayer::LayerType
280  QgsMapLayer::LayerType mapLayerType();
281 
282  // Returns layer uri or empty string if layer cannot be created
283  QString uri() { return mUri; }
284 
285  // Returns provider key
286  QString providerKey() { return mProviderKey; }
287 
291  QStringList supportedCRS() { return mSupportedCRS; }
292 
296  QStringList supportedFormats() { return mSupportFormats; }
297 
298  protected:
299 
300  QString mProviderKey;
301  QString mUri;
303  QStringList mSupportedCRS;
304  QStringList mSupportFormats;
305 
306  public:
307  static const QIcon &iconPoint();
308  static const QIcon &iconLine();
309  static const QIcon &iconPolygon();
310  static const QIcon &iconTable();
311  static const QIcon &iconRaster();
312  static const QIcon &iconDefault();
313 
314  virtual QString layerName() const { return name(); }
315 };
316 
317 
319 class CORE_EXPORT QgsDataCollectionItem : public QgsDataItem
320 {
321  Q_OBJECT
322  public:
323  QgsDataCollectionItem( QgsDataItem* parent, QString name, QString path = QString::null );
325 
326  void addChild( QgsDataItem *item ) { mChildren.append( item ); }
327 
328  static const QIcon &iconDir(); // shared icon: open/closed directory
329  static const QIcon &iconDataCollection(); // default icon for data collection
330 };
331 
333 class CORE_EXPORT QgsDirectoryItem : public QgsDataCollectionItem
334 {
335  Q_OBJECT
336  public:
337  enum Column
338  {
346  };
347 
348  QgsDirectoryItem( QgsDataItem* parent, QString name, QString path );
349 
355  QgsDirectoryItem( QgsDataItem* parent, QString name, QString dirPath, QString path );
356  ~QgsDirectoryItem();
357 
358  virtual void setState( State state ) override;
359 
360  QVector<QgsDataItem*> createChildren() override;
361 
362  QString dirPath() const { return mDirPath; }
363  virtual bool equal( const QgsDataItem *other ) override;
364  virtual QIcon icon() override;
365  virtual QWidget *paramWidget() override;
366 
367  /* static QVector<QgsDataProvider*> mProviders; */
370  Q_DECL_DEPRECATED static QVector<QLibrary*> mLibraries;
371 
372  public slots:
373  virtual void childrenCreated() override;
374  void directoryChanged();
375 
376  protected:
377  void init();
378  QString mDirPath;
379 
380  private:
381  QFileSystemWatcher * mFileSystemWatcher;
382  bool mRefreshLater;
383 };
384 
388 class CORE_EXPORT QgsErrorItem : public QgsDataItem
389 {
390  Q_OBJECT
391  public:
392 
393  QgsErrorItem( QgsDataItem* parent, QString error, QString path );
394  ~QgsErrorItem();
395 
396 };
397 
398 
399 // ---------
400 
401 class CORE_EXPORT QgsDirectoryParamWidget : public QTreeWidget
402 {
403  Q_OBJECT
404 
405  public:
406  QgsDirectoryParamWidget( QString path, QWidget* parent = NULL );
407 
408  protected:
409  void mousePressEvent( QMouseEvent* event ) override;
410 
411  public slots:
412  void showHideColumn();
413 };
414 
416 class CORE_EXPORT QgsFavouritesItem : public QgsDataCollectionItem
417 {
418  Q_OBJECT
419  public:
420  QgsFavouritesItem( QgsDataItem* parent, QString name, QString path = QString() );
422 
423  QVector<QgsDataItem*> createChildren() override;
424 
425  void addDirectory( QString favIcon );
426  void removeDirectory( QgsDirectoryItem *item );
427 
428  static const QIcon &iconFavourites();
429 };
430 
432 class CORE_EXPORT QgsZipItem : public QgsDataCollectionItem
433 {
434  Q_OBJECT
435 
436  protected:
437  QString mFilePath;
438  QString mVsiPrefix;
439  QStringList mZipFileList;
440 
441  public:
442  QgsZipItem( QgsDataItem* parent, QString name, QString path );
443  QgsZipItem( QgsDataItem* parent, QString name, QString filePath, QString path );
444  ~QgsZipItem();
445 
446  QVector<QgsDataItem*> createChildren() override;
447  const QStringList & getZipFileList();
448 
450  static QVector<dataItem_t *> mDataItemPtr;
451  static QStringList mProviderNames;
452 
453  static QString vsiPrefix( QString uri ) { return qgsVsiPrefix( uri ); }
454 
455  static QgsDataItem* itemFromPath( QgsDataItem* parent, QString path, QString name );
456  static QgsDataItem* itemFromPath( QgsDataItem* parent, QString filePath, QString name, QString path );
457 
458  static const QIcon &iconZip();
459 
460  private:
461  void init();
462 };
463 
464 #endif // QGSDATAITEM_H
465 
466 
A Collection: logical collection of layers or subcollections, e.g.
Definition: qgsdataitem.h:319
QStringList mSupportFormats
Definition: qgsdataitem.h:304
Contains various Favourites directories.
Definition: qgsdataitem.h:416
virtual void childrenCreated()
static QMap< QString, QIcon > mIconMap
Definition: qgsdataitem.h:200
QVector< QgsDataItem * > children() const
Definition: qgsdataitem.h:148
QString name() const
Definition: qgsdataitem.h:150
LayerType
Layers enum defining the types of layers that can be added to a map.
Definition: qgsmaplayer.h:55
virtual Capabilities capabilities2() const
Definition: qgsdataitem.h:129
QgsDataItem * parent() const
Get item parent.
Definition: qgsdataitem.h:144
QIcon populatingIcon()
Definition: qgsdataitem.h:175
QString providerKey()
Definition: qgsdataitem.h:286
QString mProviderKey
Definition: qgsdataitem.h:300
virtual bool handleDrop(const QMimeData *, Qt::DropAction)
Definition: qgsdataitem.h:111
virtual QIcon icon()
void addChild(QgsDataItem *item)
Definition: qgsdataitem.h:326
QString qgsVsiPrefix(QString path)
Definition: qgis.cpp:269
QString mIconName
Definition: qgsdataitem.h:198
State mState
Definition: qgsdataitem.h:188
Abstract base class for spatial data provider implementations.
void setIcon(QIcon icon)
Definition: qgsdataitem.h:160
QString uri()
Definition: qgsdataitem.h:283
virtual QWidget * paramWidget()
Definition: qgsdataitem.h:102
QString mToolTip
Definition: qgsdataitem.h:197
A zip file: contains layers, using GDAL/OGR VSIFILE mechanism.
Definition: qgsdataitem.h:432
virtual bool equal(const QgsDataItem *other)
QStringList mSupportedCRS
Definition: qgsdataitem.h:303
QString mFilePath
Definition: qgsdataitem.h:437
bool isPopulated()
Definition: qgsdataitem.h:87
QString dirPath() const
Definition: qgsdataitem.h:362
static QString vsiPrefix(QString uri)
Definition: qgsdataitem.h:453
QString mName
Definition: qgsdataitem.h:191
QStringList mZipFileList
Definition: qgsdataitem.h:439
virtual void setCapabilities(Capabilities capabilities)
Definition: qgsdataitem.h:131
Children not yet created.
Definition: qgsdataitem.h:72
Creating children in separate thread (populating or refreshing)
Definition: qgsdataitem.h:73
QString path() const
Definition: qgsdataitem.h:152
Data item that can be used to report problems (e.g.
Definition: qgsdataitem.h:388
Type type() const
Definition: qgsdataitem.h:140
static QStringList mProviderNames
Definition: qgsdataitem.h:451
A directory: contains subdirectories and layers.
Definition: qgsdataitem.h:333
Base class for all items in the model.
Definition: qgsdataitem.h:43
QStringList supportedCRS()
Returns the supported CRS.
Definition: qgsdataitem.h:291
Capabilities mCapabilities
Definition: qgsdataitem.h:185
virtual void setState(State state)
Set item state.
void setName(const QString &name)
Definition: qgsdataitem.h:151
static QVector< dataItem_t * > mDataItemPtr
Definition: qgsdataitem.h:450
QString mPath
Definition: qgsdataitem.h:196
QString mUri
Definition: qgsdataitem.h:301
virtual Q_DECL_DEPRECATED Capability capabilities()
Definition: qgsdataitem.h:127
QString mVsiPrefix
Definition: qgsdataitem.h:438
static Q_DECL_DEPRECATED QVector< QLibrary * > mLibraries
Definition: qgsdataitem.h:370
virtual QString layerName() const
Definition: qgsdataitem.h:314
Class for storing a coordinate reference system (CRS)
QgsDataItem * mParent
Definition: qgsdataitem.h:186
bool deferredDelete()
The item is scheduled to be deleted.
Definition: qgsdataitem.h:182
void setToolTip(QString msg)
Definition: qgsdataitem.h:163
virtual QVector< QgsDataItem * > createChildren()
Create children.
bool mPopulated
Definition: qgsdataitem.h:190
Item that represents a layer that can be opened with one of the providers.
Definition: qgsdataitem.h:253
QgsDataItem * dataItem_t(QString, QgsDataItem *)
Definition: qgsdataitem.h:38
QVector< QgsDataItem * > mChildren
Definition: qgsdataitem.h:187
QString toolTip() const
Definition: qgsdataitem.h:164
QStringList supportedFormats()
Returns the supported formats.
Definition: qgsdataitem.h:296
virtual QList< QAction * > actions()
Definition: qgsdataitem.h:105
void setPath(const QString &path)
Definition: qgsdataitem.h:153
virtual bool acceptDrop()
Definition: qgsdataitem.h:108
LayerType mLayerType
Definition: qgsdataitem.h:302
void setIconName(const QString &iconName)
Definition: qgsdataitem.h:161