QGIS API Documentation  2.99.0-Master (7d4f81d)
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 
287  virtual void refreshConnections();
288 
289  virtual void childrenCreated();
290 
291  signals:
292  void beginInsertItems( QgsDataItem *parent, int first, int last );
293  void endInsertItems();
294  void beginRemoveItems( QgsDataItem *parent, int first, int last );
295  void endRemoveItems();
296  void dataChanged( QgsDataItem *item );
297  void stateChanged( QgsDataItem *item, QgsDataItem::State oldState );
302  void connectionsChanged();
303 
304  protected slots:
305 
311  void updateIcon();
312 
313  private:
314  static QVector<QgsDataItem *> runCreateChildren( QgsDataItem *item );
315 
316  // Set to true if object has to be deleted when possible (nothing running in threads)
317  bool mDeferredDelete;
318  QFutureWatcher< QVector <QgsDataItem *> > *mFutureWatcher;
319  // number of items currently in loading (populating) state
320  static QgsAnimatedIcon *sPopulatingIcon;
321 };
322 
323 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsDataItem::Capabilities )
324 
325 
328 class CORE_EXPORT QgsLayerItem : public QgsDataItem
329 {
330  Q_OBJECT
331  Q_ENUMS( LayerType )
332  public:
334  {
344  Plugin
345  };
346 
347  Q_ENUMS( LayerType )
348 
349  QgsLayerItem( QgsDataItem *parent, const QString &name, const QString &path, const QString &uri, LayerType layerType, const QString &providerKey );
350 
351  // --- reimplemented from QgsDataItem ---
352 
353  virtual bool equal( const QgsDataItem *other ) override;
354 
355  virtual bool hasDragEnabled() const override { return true; }
356 
357  virtual QgsMimeDataUtils::Uri mimeUri() const override;
358 
359  // --- New virtual methods for layer item derived classes ---
360 
362  QgsMapLayer::LayerType mapLayerType() const;
363 
365  QString uri() const { return mUri; }
366 
368  QString providerKey() const { return mProviderKey; }
369 
373  QStringList supportedCrs() const { return mSupportedCRS; }
374 
378  QStringList supportedFormats() const { return mSupportFormats; }
379 
383  virtual QString comments() const { return QString(); }
384 
388  static QString layerTypeAsString( const LayerType &layerType );
389 
390  protected:
391 
393  QString mProviderKey;
395  QString mUri;
399  QStringList mSupportedCRS;
401  QStringList mSupportFormats;
402 
403  public:
404  static QIcon iconPoint();
405  static QIcon iconLine();
406  static QIcon iconPolygon();
407  static QIcon iconTable();
408  static QIcon iconRaster();
409  static QIcon iconDefault();
410 
412  virtual QString layerName() const { return name(); }
413 };
414 
415 
419 class CORE_EXPORT QgsDataCollectionItem : public QgsDataItem
420 {
421  Q_OBJECT
422  public:
423  QgsDataCollectionItem( QgsDataItem *parent, const QString &name, const QString &path = QString() );
425 
426  void addChild( QgsDataItem *item SIP_TRANSFER ) { mChildren.append( item ); }
427 
428  static QIcon iconDir(); // shared icon: open/closed directory
429  static QIcon iconDataCollection(); // default icon for data collection
430 };
431 
435 class CORE_EXPORT QgsDirectoryItem : public QgsDataCollectionItem
436 {
437  Q_OBJECT
438  public:
439  enum Column
440  {
448  };
449 
450  QgsDirectoryItem( QgsDataItem *parent, const QString &name, const QString &path );
451 
457  QgsDirectoryItem( QgsDataItem *parent, const QString &name, const QString &dirPath, const QString &path );
458 
459  virtual void setState( State state ) override;
460 
461  QVector<QgsDataItem *> createChildren() override;
462 
463  QString dirPath() const { return mDirPath; }
464  virtual bool equal( const QgsDataItem *other ) override;
465  virtual QIcon icon() override;
466  virtual QWidget *paramWidget() override SIP_FACTORY;
467 
469  static bool hiddenPath( const QString &path );
470 
471 
472  public slots:
473  virtual void childrenCreated() override;
474  void directoryChanged();
475 
476  protected:
477  void init();
478  QString mDirPath;
479 
480  private:
481  QFileSystemWatcher *mFileSystemWatcher = nullptr;
482  bool mRefreshLater;
483 };
484 
488 class CORE_EXPORT QgsProjectItem : public QgsDataItem
489 {
490  Q_OBJECT
491  public:
492 
499  QgsProjectItem( QgsDataItem *parent, const QString &name, const QString &path );
500 
501  virtual bool hasDragEnabled() const override { return true; }
502 
503 };
504 
508 class CORE_EXPORT QgsErrorItem : public QgsDataItem
509 {
510  Q_OBJECT
511  public:
512 
513  QgsErrorItem( QgsDataItem *parent, const QString &error, const QString &path );
514 
515 };
516 
517 
518 // ---------
519 
523 class CORE_EXPORT QgsDirectoryParamWidget : public QTreeWidget
524 {
525  Q_OBJECT
526 
527  public:
528  QgsDirectoryParamWidget( const QString &path, QWidget *parent SIP_TRANSFERTHIS = nullptr );
529 
530  protected:
531  void mousePressEvent( QMouseEvent *event ) override;
532 
533  public slots:
534  void showHideColumn();
535 };
536 
541 class CORE_EXPORT QgsFavoritesItem : public QgsDataCollectionItem
542 {
543  Q_OBJECT
544  public:
545 
550  QgsFavoritesItem( QgsDataItem *parent, const QString &name, const QString &path = QString() );
551 
552  QVector<QgsDataItem *> createChildren() override;
553 
558  void addDirectory( const QString &directory );
559 
564  void removeDirectory( QgsDirectoryItem *item );
565 
567  static QIcon iconFavorites();
568 
569  private:
570  QVector<QgsDataItem *> createChildren( const QString &favDir );
571 };
572 
576 class CORE_EXPORT QgsZipItem : public QgsDataCollectionItem
577 {
578  Q_OBJECT
579 
580  protected:
581  QString mFilePath;
582  QString mVsiPrefix;
583  QStringList mZipFileList;
584 
585  public:
586  QgsZipItem( QgsDataItem *parent, const QString &name, const QString &path );
587  QgsZipItem( QgsDataItem *parent, const QString &name, const QString &filePath, const QString &path );
588 
589  QVector<QgsDataItem *> createChildren() override;
590  QStringList getZipFileList();
591 
593  static QVector<dataItem_t *> sDataItemPtr SIP_SKIP;
594  static QStringList sProviderNames;
595 
596  static QString vsiPrefix( const QString &uri ) { return qgsVsiPrefix( uri ); }
597 
601  static QgsDataItem *itemFromPath( QgsDataItem *parent, const QString &path, const QString &name ) SIP_FACTORY;
602 
607  static QgsDataItem *itemFromPath( QgsDataItem *parent, const QString &filePath, const QString &name, const QString &path ) SIP_FACTORY SIP_PYNAME( itemFromFilePath );
608 
609  static QIcon iconZip();
610 
611  private:
612  void init();
613 };
614 
615 #endif // QGSDATAITEM_H
616 
617 
A Collection: logical collection of layers or subcollections, e.g.
Definition: qgsdataitem.h:419
QStringList mSupportFormats
The list of supported formats.
Definition: qgsdataitem.h:401
QString path() const
Definition: qgsdataitem.h:214
virtual void childrenCreated()
virtual QString layerName() const
Definition: qgsdataitem.h:412
virtual bool hasDragEnabled() const override
Returns true if the item may be dragged.
Definition: qgsdataitem.h:501
QString providerKey() const
Returns provider key.
Definition: qgsdataitem.h:368
QString name() const
Definition: qgsdataitem.h:212
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
QString toolTip() const
Definition: qgsdataitem.h:226
QString mProviderKey
The provider key.
Definition: qgsdataitem.h:393
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:426
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:41
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:576
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:399
QString mFilePath
Definition: qgsdataitem.h:581
QgsDataItem * parent() const
Get item parent.
Definition: qgsdataitem.h:205
QString mName
Definition: qgsdataitem.h:255
#define SIP_SKIP
Definition: qgis_sip.h:119
LayerType
Types of layers that can be added to a map.
Definition: qgsmaplayer.h:92
QStringList mZipFileList
Definition: qgsdataitem.h:583
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:36
Creating children in separate thread (populating or refreshing)
Definition: qgsdataitem.h:100
#define SIP_END
Definition: qgis_sip.h:175
Data item that can be used to report problems (e.g.
Definition: qgsdataitem.h:508
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:69
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:435
static QStringList sProviderNames
Definition: qgsdataitem.h:594
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:395
Contains various Favorites directories.
Definition: qgsdataitem.h:541
QString mVsiPrefix
Definition: qgsdataitem.h:582
QStringList supportedFormats() const
Returns the supported formats.
Definition: qgsdataitem.h:378
QString qgsVsiPrefix(const QString &path)
Definition: qgis.cpp:215
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:365
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:165
bool deferredDelete()
The item is scheduled to be deleted.
Definition: qgsdataitem.h:248
QString dirPath() const
Definition: qgsdataitem.h:463
virtual QVector< QgsDataItem * > createChildren()
Create children.
Item that represents a layer that can be opened with one of the providers.
Definition: qgsdataitem.h:328
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:355
Data item that can be used to represent QGIS projects.
Definition: qgsdataitem.h:488
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:397
QVector< QgsDataItem * > mChildren
Definition: qgsdataitem.h:253
virtual QString comments() const
Returns comments of the layer.
Definition: qgsdataitem.h:383
Represents a favorite item.
Definition: qgsdataitem.h:81
QStringList supportedCrs() const
Returns the supported CRS.
Definition: qgsdataitem.h:373
void setIconName(const QString &iconName)
Definition: qgsdataitem.h:223
static QString vsiPrefix(const QString &uri)
Definition: qgsdataitem.h:596
QMap< QString, QIcon > mIconMap
Definition: qgsdataitem.h:264
virtual Capabilities capabilities2() const
Definition: qgsdataitem.h:187
#define SIP_PYNAME(name)
Definition: qgis_sip.h:74