QGIS API Documentation  2.99.0-Master (23ddace)
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 "qgis.h"
21 #include "qgis_core.h"
22 #include <QFileSystemWatcher>
23 #include <QFutureWatcher>
24 #include <QIcon>
25 #include <QLibrary>
26 #include <QObject>
27 #include <QPixmap>
28 #include <QString>
29 #include <QTreeWidget>
30 #include <QVector>
31 
32 #include "qgsmaplayer.h"
34 #include "qgsmimedatautils.h"
35 
36 
37 class QgsDataProvider;
38 class QgsDataItem;
39 class QgsAnimatedIcon;
40 
41 typedef QgsDataItem *dataItem_t( QString, QgsDataItem * ) SIP_SKIP;
42 
47 class CORE_EXPORT QgsDataItem : public QObject
48 {
49 
50 #ifdef SIP_RUN
52  if ( qobject_cast<QgsLayerItem *>( sipCpp ) )
53  sipType = sipType_QgsLayerItem;
54  else if ( qobject_cast<QgsErrorItem *>( sipCpp ) )
55  sipType = sipType_QgsErrorItem;
56  else if ( qobject_cast<QgsDirectoryItem *>( sipCpp ) )
57  sipType = sipType_QgsDirectoryItem;
58  else if ( qobject_cast<QgsFavoritesItem *>( sipCpp ) )
59  sipType = sipType_QgsFavoritesItem;
60  else if ( qobject_cast<QgsZipItem *>( sipCpp ) )
61  sipType = sipType_QgsZipItem;
62  else if ( qobject_cast<QgsDataCollectionItem *>( sipCpp ) )
63  sipType = sipType_QgsDataCollectionItem;
64  else if ( qobject_cast<QgsProjectItem *>( sipCpp ) )
65  sipType = sipType_QgsProjectItem;
66  else
67  sipType = 0;
68  SIP_END
69 #endif
70 
71  Q_OBJECT
72  Q_ENUMS( Type )
73  Q_ENUMS( State )
74  public:
75  enum Type
76  {
82  Project
83  };
84 
86  QgsDataItem( QgsDataItem::Type type, QgsDataItem *parent SIP_TRANSFERTHIS, const QString &name, const QString &path );
87  virtual ~QgsDataItem();
88 
89  bool hasChildren();
90 
91  int rowCount();
92 
95  virtual QVector<QgsDataItem *> createChildren() SIP_FACTORY;
96 
97  enum State
98  {
101  Populated
102  };
103 
105  State state() const;
106 
111  virtual void setState( State state );
112 
118  virtual void addChildItem( QgsDataItem *child SIP_TRANSFER, bool refresh = false );
119 
124  virtual void deleteChildItem( QgsDataItem *child );
125 
130  virtual QgsDataItem *removeChildItem( QgsDataItem *child ) SIP_TRANSFERBACK;
131 
134  virtual bool equal( const QgsDataItem *other );
135 
136  virtual QWidget *paramWidget() SIP_FACTORY { return nullptr; }
137 
141  virtual QList<QAction *> actions() { return QList<QAction *>(); }
142 
147  virtual bool acceptDrop() { return false; }
148 
153  virtual bool handleDrop( const QMimeData * /*data*/, Qt::DropAction /*action*/ ) { return false; }
154 
161  virtual bool hasDragEnabled() const { return false; }
162 
169 
171  {
172  NoCapabilities = 0,
173  SetCrs = 1 << 0,
174  Fertile = 1 << 1,
175  Fast = 1 << 2,
176  Collapse = 1 << 3
177  };
178  Q_DECLARE_FLAGS( Capabilities, Capability )
179 
180 
184  virtual bool setCrs( const QgsCoordinateReferenceSystem &crs ) { Q_UNUSED( crs ); return false; }
185 
186  // ### QGIS 3 - rename to capabilities()
187  virtual Capabilities capabilities2() const { return mCapabilities; }
188 
192  virtual void setCapabilities( Capabilities capabilities ) { mCapabilities = capabilities; }
193 
194  // static methods
195 
196  // Find child index in vector of items using '==' operator
197  static int findItem( QVector<QgsDataItem *> items, QgsDataItem *item );
198 
199  // members
200 
201  Type type() const { return mType; }
202 
205  QgsDataItem *parent() const { return mParent; }
206 
209  void setParent( QgsDataItem *parent );
210  QVector<QgsDataItem *> children() const { return mChildren; }
211  virtual QIcon icon();
212  QString name() const { return mName; }
213  void setName( const QString &name ) { mName = name; }
214  QString path() const { return mPath; }
215  void setPath( const QString &path ) { mPath = path; }
217  static QString pathComponent( const QString &component );
218 
219  // Because QIcon (QPixmap) must not be used in outside the GUI thread, it is
220  // not possible to set mIcon in constructor. Either use mIconName/setIconName()
221  // or implement icon().
222  void setIcon( const QIcon &icon ) { mIcon = icon; }
223  void setIconName( const QString &iconName ) { mIconName = iconName; }
224 
225  void setToolTip( const QString &msg ) { mToolTip = msg; }
226  QString toolTip() const { return mToolTip; }
227 
228  // deleteLater() items anc clear the vector
229  static void deleteLater( QVector<QgsDataItem *> &items );
230 
232  void moveToThread( QThread *targetThread );
233 
234  protected:
235  virtual void populate( const QVector<QgsDataItem *> &children );
236 
240  virtual void refresh( const QVector<QgsDataItem *> &children );
241 
248  bool deferredDelete() { return mDeferredDelete; }
249 
251  Capabilities mCapabilities;
252  QgsDataItem *mParent = nullptr;
253  QVector<QgsDataItem *> mChildren; // easier to have it always
255  QString mName;
256  // Path is slash ('/') separated chain of item identifiers which are usually item names, but may be differen if it is
257  // necessary to distinguish paths of two providers to the same source (e.g GRASS location and standard directory have the same
258  // name but different paths). Identifiers in path must not contain '/' characters.
259  // The path is used to identify item in tree.
260  QString mPath;
261  QString mToolTip;
262  QString mIconName;
263  QIcon mIcon;
264  QMap<QString, QIcon> mIconMap;
265 
266  public slots:
267 
275  virtual void deleteLater();
276 
277  // Populate children using children vector created by createChildren()
278  // \param foreground run createChildren in foreground
279  virtual void populate( bool foreground = false );
280 
282  virtual void depopulate();
283 
284  virtual void refresh();
285 
286  virtual void childrenCreated();
287 
288  signals:
289  void beginInsertItems( QgsDataItem *parent, int first, int last );
290  void endInsertItems();
291  void beginRemoveItems( QgsDataItem *parent, int first, int last );
292  void endRemoveItems();
293  void dataChanged( QgsDataItem *item );
294  void stateChanged( QgsDataItem *item, QgsDataItem::State oldState );
295 
296  protected slots:
297 
303  void updateIcon();
304 
305  private:
306  static QVector<QgsDataItem *> runCreateChildren( QgsDataItem *item );
307 
308  // Set to true if object has to be deleted when possible (nothing running in threads)
309  bool mDeferredDelete;
310  QFutureWatcher< QVector <QgsDataItem *> > *mFutureWatcher;
311  // number of items currently in loading (populating) state
312  static QgsAnimatedIcon *sPopulatingIcon;
313 };
314 
315 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsDataItem::Capabilities )
316 
317 
320 class CORE_EXPORT QgsLayerItem : public QgsDataItem
321 {
322  Q_OBJECT
323  public:
325  {
335  Plugin
336  };
337 
338  QgsLayerItem( QgsDataItem *parent, const QString &name, const QString &path, const QString &uri, LayerType layerType, const QString &providerKey );
339 
340  // --- reimplemented from QgsDataItem ---
341 
342  virtual bool equal( const QgsDataItem *other ) override;
343 
344  virtual bool hasDragEnabled() const override { return true; }
345 
346  virtual QgsMimeDataUtils::Uri mimeUri() const override;
347 
348  // --- New virtual methods for layer item derived classes ---
349 
351  QgsMapLayer::LayerType mapLayerType() const;
352 
354  QString uri() const { return mUri; }
355 
357  QString providerKey() const { return mProviderKey; }
358 
362  QStringList supportedCrs() const { return mSupportedCRS; }
363 
367  QStringList supportedFormats() const { return mSupportFormats; }
368 
372  virtual QString comments() const { return QString(); }
373 
374  protected:
375 
377  QString mProviderKey;
379  QString mUri;
383  QStringList mSupportedCRS;
385  QStringList mSupportFormats;
386 
387  public:
388  static QIcon iconPoint();
389  static QIcon iconLine();
390  static QIcon iconPolygon();
391  static QIcon iconTable();
392  static QIcon iconRaster();
393  static QIcon iconDefault();
394 
396  virtual QString layerName() const { return name(); }
397 };
398 
399 
403 class CORE_EXPORT QgsDataCollectionItem : public QgsDataItem
404 {
405  Q_OBJECT
406  public:
407  QgsDataCollectionItem( QgsDataItem *parent, const QString &name, const QString &path = QString() );
409 
410  void addChild( QgsDataItem *item SIP_TRANSFER ) { mChildren.append( item ); }
411 
412  static QIcon iconDir(); // shared icon: open/closed directory
413  static QIcon iconDataCollection(); // default icon for data collection
414 };
415 
419 class CORE_EXPORT QgsDirectoryItem : public QgsDataCollectionItem
420 {
421  Q_OBJECT
422  public:
423  enum Column
424  {
432  };
433 
434  QgsDirectoryItem( QgsDataItem *parent, const QString &name, const QString &path );
435 
441  QgsDirectoryItem( QgsDataItem *parent, const QString &name, const QString &dirPath, const QString &path );
442 
443  virtual void setState( State state ) override;
444 
445  QVector<QgsDataItem *> createChildren() override;
446 
447  QString dirPath() const { return mDirPath; }
448  virtual bool equal( const QgsDataItem *other ) override;
449  virtual QIcon icon() override;
450  virtual QWidget *paramWidget() override SIP_FACTORY;
451 
453  static bool hiddenPath( const QString &path );
454 
455  public slots:
456  virtual void childrenCreated() override;
457  void directoryChanged();
458 
459  protected:
460  void init();
461  QString mDirPath;
462 
463  private:
464  QFileSystemWatcher *mFileSystemWatcher = nullptr;
465  bool mRefreshLater;
466 };
467 
471 class CORE_EXPORT QgsProjectItem : public QgsDataItem
472 {
473  Q_OBJECT
474  public:
475 
482  QgsProjectItem( QgsDataItem *parent, const QString &name, const QString &path );
483 
484  virtual bool hasDragEnabled() const override { return true; }
485 
486 };
487 
491 class CORE_EXPORT QgsErrorItem : public QgsDataItem
492 {
493  Q_OBJECT
494  public:
495 
496  QgsErrorItem( QgsDataItem *parent, const QString &error, const QString &path );
497 
498 };
499 
500 
501 // ---------
502 
506 class CORE_EXPORT QgsDirectoryParamWidget : public QTreeWidget
507 {
508  Q_OBJECT
509 
510  public:
511  QgsDirectoryParamWidget( const QString &path, QWidget *parent SIP_TRANSFERTHIS = nullptr );
512 
513  protected:
514  void mousePressEvent( QMouseEvent *event ) override;
515 
516  public slots:
517  void showHideColumn();
518 };
519 
524 class CORE_EXPORT QgsFavoritesItem : public QgsDataCollectionItem
525 {
526  Q_OBJECT
527  public:
528 
533  QgsFavoritesItem( QgsDataItem *parent, const QString &name, const QString &path = QString() );
534 
535  QVector<QgsDataItem *> createChildren() override;
536 
541  void addDirectory( const QString &directory );
542 
547  void removeDirectory( QgsDirectoryItem *item );
548 
550  static QIcon iconFavorites();
551 
552  private:
553  QVector<QgsDataItem *> createChildren( const QString &favDir );
554 };
555 
559 class CORE_EXPORT QgsZipItem : public QgsDataCollectionItem
560 {
561  Q_OBJECT
562 
563  protected:
564  QString mFilePath;
565  QString mVsiPrefix;
566  QStringList mZipFileList;
567 
568  public:
569  QgsZipItem( QgsDataItem *parent, const QString &name, const QString &path );
570  QgsZipItem( QgsDataItem *parent, const QString &name, const QString &filePath, const QString &path );
571 
572  QVector<QgsDataItem *> createChildren() override;
573  QStringList getZipFileList();
574 
576  static QVector<dataItem_t *> sDataItemPtr SIP_SKIP;
577  static QStringList sProviderNames;
578 
579  static QString vsiPrefix( const QString &uri ) { return qgsVsiPrefix( uri ); }
580 
584  static QgsDataItem *itemFromPath( QgsDataItem *parent, const QString &path, const QString &name ) SIP_FACTORY;
585 
590  static QgsDataItem *itemFromPath( QgsDataItem *parent, const QString &filePath, const QString &name, const QString &path ) SIP_FACTORY SIP_PYNAME( itemFromFilePath );
591 
592  static QIcon iconZip();
593 
594  private:
595  void init();
596 };
597 
598 #endif // QGSDATAITEM_H
599 
600 
A Collection: logical collection of layers or subcollections, e.g.
Definition: qgsdataitem.h:403
QStringList mSupportFormats
The list of supported formats.
Definition: qgsdataitem.h:385
QString path() const
Definition: qgsdataitem.h:214
virtual void childrenCreated()
virtual QString layerName() const
Definition: qgsdataitem.h:396
virtual bool hasDragEnabled() const override
Returns true if the item may be dragged.
Definition: qgsdataitem.h:484
QString providerKey() const
Returns provider key.
Definition: qgsdataitem.h:357
QString name() const
Definition: qgsdataitem.h:212
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:34
QString toolTip() const
Definition: qgsdataitem.h:226
QString mProviderKey
The provider key.
Definition: qgsdataitem.h:377
virtual bool handleDrop(const QMimeData *, Qt::DropAction)
Attempts to process the mime data dropped on this item.
Definition: qgsdataitem.h:153
virtual QIcon icon()
void addChild(QgsDataItem *item)
Definition: qgsdataitem.h:410
QString mIconName
Definition: qgsdataitem.h:262
State mState
Definition: qgsdataitem.h:254
Type type() const
Definition: qgsdataitem.h:201
void setIcon(const QIcon &icon)
Definition: qgsdataitem.h:222
Abstract base class for spatial data provider implementations.
void setToolTip(const QString &msg)
Definition: qgsdataitem.h:225
#define SIP_TRANSFERBACK
Definition: qgis_sip.h:29
virtual QWidget * paramWidget()
Definition: qgsdataitem.h:136
QString mToolTip
Definition: qgsdataitem.h:261
A zip file: contains layers, using GDAL/OGR VSIFILE mechanism.
Definition: qgsdataitem.h:559
virtual bool equal(const QgsDataItem *other)
Returns true if this item is equal to another item (by testing item type and path).
QStringList mSupportedCRS
The list of supported CRS.
Definition: qgsdataitem.h:383
QString mFilePath
Definition: qgsdataitem.h:564
QgsDataItem * parent() const
Get item parent.
Definition: qgsdataitem.h:205
QString mName
Definition: qgsdataitem.h:255
#define SIP_SKIP
Definition: qgis_sip.h:107
LayerType
Types of layers that can be added to a map.
Definition: qgsmaplayer.h:92
QStringList mZipFileList
Definition: qgsdataitem.h:566
virtual void setCapabilities(Capabilities capabilities)
Sets the capabilities for the data item.
Definition: qgsdataitem.h:192
Children not yet created.
Definition: qgsdataitem.h:99
#define SIP_TRANSFER
Definition: qgis_sip.h:24
Creating children in separate thread (populating or refreshing)
Definition: qgsdataitem.h:100
#define SIP_END
Definition: qgis_sip.h:163
Data item that can be used to report problems (e.g.
Definition: qgsdataitem.h:491
virtual bool hasDragEnabled() const
Returns true if the item may be dragged.
Definition: qgsdataitem.h:161
virtual QgsMimeDataUtils::Uri mimeUri() const
Return mime URI for the data item.
Definition: qgsdataitem.h:168
#define SIP_FACTORY
Definition: qgis_sip.h:57
virtual QList< QAction * > actions()
Returns the list of actions available for this item.
Definition: qgsdataitem.h:141
A directory: contains subdirectories and layers.
Definition: qgsdataitem.h:419
static QStringList sProviderNames
Definition: qgsdataitem.h:577
Base class for all items in the model.
Definition: qgsdataitem.h:47
Capabilities mCapabilities
Definition: qgsdataitem.h:251
virtual void setState(State state)
Set item state.
void setName(const QString &name)
Definition: qgsdataitem.h:213
QVector< QgsDataItem * > children() const
Definition: qgsdataitem.h:210
QString mPath
Definition: qgsdataitem.h:260
QString mUri
The URI.
Definition: qgsdataitem.h:379
Contains various Favorites directories.
Definition: qgsdataitem.h:524
QString mVsiPrefix
Definition: qgsdataitem.h:565
QStringList supportedFormats() const
Returns the supported formats.
Definition: qgsdataitem.h:367
QString qgsVsiPrefix(const QString &path)
Definition: qgis.cpp:209
This class represents a coordinate reference system (CRS).
Animated icon is keeping an animation running if there are listeners connected to frameChanged...
QString uri() const
Returns layer uri or empty string if layer cannot be created.
Definition: qgsdataitem.h:354
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:153
bool deferredDelete()
The item is scheduled to be deleted.
Definition: qgsdataitem.h:248
QString dirPath() const
Definition: qgsdataitem.h:447
virtual QVector< QgsDataItem * > createChildren()
Create children.
Item that represents a layer that can be opened with one of the providers.
Definition: qgsdataitem.h:320
QgsDataItem * dataItem_t(QString, QgsDataItem *)
Definition: qgsdataitem.h:41
virtual bool hasDragEnabled() const override
Returns true if the item may be dragged.
Definition: qgsdataitem.h:344
Data item that can be used to represent QGIS projects.
Definition: qgsdataitem.h:471
void setPath(const QString &path)
Definition: qgsdataitem.h:215
virtual bool acceptDrop()
Returns whether the item accepts drag and dropped layers - e.g.
Definition: qgsdataitem.h:147
LayerType mLayerType
The layer type.
Definition: qgsdataitem.h:381
QVector< QgsDataItem * > mChildren
Definition: qgsdataitem.h:253
virtual QString comments() const
Returns comments of the layer.
Definition: qgsdataitem.h:372
Represents a favorite item.
Definition: qgsdataitem.h:81
QStringList supportedCrs() const
Returns the supported CRS.
Definition: qgsdataitem.h:362
void setIconName(const QString &iconName)
Definition: qgsdataitem.h:223
static QString vsiPrefix(const QString &uri)
Definition: qgsdataitem.h:579
QMap< QString, QIcon > mIconMap
Definition: qgsdataitem.h:264
virtual Capabilities capabilities2() const
Definition: qgsdataitem.h:187
#define SIP_PYNAME(name)
Definition: qgis_sip.h:62