QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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_sip.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 #include <QDateTime>
32 
33 #include "qgsmaplayer.h"
35 #include "qgsmimedatautils.h"
36 #include "qgswkbtypes.h"
37 
38 class QgsDataProvider;
39 class QgsDataItem;
40 class QgsAnimatedIcon;
41 
42 typedef QgsDataItem *dataItem_t( QString, QgsDataItem * ) SIP_SKIP;
43 
49 class CORE_EXPORT QgsDataItem : public QObject
50 {
51 
52 #ifdef SIP_RUN
54  if ( qobject_cast<QgsLayerItem *>( sipCpp ) )
55  sipType = sipType_QgsLayerItem;
56  else if ( qobject_cast<QgsErrorItem *>( sipCpp ) )
57  sipType = sipType_QgsErrorItem;
58  else if ( qobject_cast<QgsDirectoryItem *>( sipCpp ) )
59  sipType = sipType_QgsDirectoryItem;
60  else if ( qobject_cast<QgsFavoritesItem *>( sipCpp ) )
61  sipType = sipType_QgsFavoritesItem;
62  else if ( qobject_cast<QgsZipItem *>( sipCpp ) )
63  sipType = sipType_QgsZipItem;
64  else if ( qobject_cast<QgsDataCollectionItem *>( sipCpp ) )
65  sipType = sipType_QgsDataCollectionItem;
66  else if ( qobject_cast<QgsProjectItem *>( sipCpp ) )
67  sipType = sipType_QgsProjectItem;
68  else
69  sipType = 0;
70  SIP_END
71 #endif
72 
73  Q_OBJECT
74 
75  public:
76  enum Type
77  {
85  };
86 
87  Q_ENUM( Type )
88 
89 
90  QgsDataItem( QgsDataItem::Type type, QgsDataItem *parent SIP_TRANSFERTHIS, const QString &name, const QString &path );
91  ~QgsDataItem() override;
92 
93  bool hasChildren();
94 
95  int rowCount();
96 
100  virtual QVector<QgsDataItem *> createChildren() SIP_FACTORY;
101 
102  enum State
103  {
106  Populated
107  };
108  Q_ENUM( State )
109 
110 
111  State state() const;
112 
118  virtual void setState( State state );
119 
126  virtual void addChildItem( QgsDataItem *child SIP_TRANSFER, bool refresh = false );
127 
133  virtual void deleteChildItem( QgsDataItem *child );
134 
140  virtual QgsDataItem *removeChildItem( QgsDataItem *child ) SIP_TRANSFERBACK;
141 
145  virtual bool equal( const QgsDataItem *other );
146 
147  virtual QWidget *paramWidget() SIP_FACTORY { return nullptr; }
148 
156  virtual QList<QAction *> actions( QWidget *parent );
157 
166  virtual QList<QMenu *> menus( QWidget *parent );
167 
173  virtual bool acceptDrop() { return false; }
174 
180  virtual bool handleDrop( const QMimeData * /*data*/, Qt::DropAction /*action*/ ) { return false; }
181 
188  virtual bool handleDoubleClick();
189 
197  virtual bool hasDragEnabled() const { return false; }
198 
206 
208  {
209  NoCapabilities = 0,
210  SetCrs = 1 << 0,
211  Fertile = 1 << 1,
212  Fast = 1 << 2,
213  Collapse = 1 << 3,
214  Rename = 1 << 4,
215  Delete = 1 << 5,
216  };
217  Q_DECLARE_FLAGS( Capabilities, Capability )
218 
219 
225  Q_DECL_DEPRECATED virtual bool setCrs( const QgsCoordinateReferenceSystem &crs ) SIP_DEPRECATED
226  {
227  Q_UNUSED( crs )
228  return false;
229  }
230 
240  virtual bool rename( const QString &name );
241 
242  // ### QGIS 4 - rename to capabilities()
243 
249  virtual Capabilities capabilities2() const { return mCapabilities; }
250 
256  virtual void setCapabilities( Capabilities capabilities ) { mCapabilities = capabilities; }
257 
258  // static methods
259 
260  // Find child index in vector of items using '==' operator
261  static int findItem( QVector<QgsDataItem *> items, QgsDataItem *item );
262 
263  // members
264 
265  Type type() const { return mType; }
266 
270  QgsDataItem *parent() const { return mParent; }
271 
275  void setParent( QgsDataItem *parent );
276  QVector<QgsDataItem *> children() const { return mChildren; }
277  virtual QIcon icon();
278 
284  QString name() const { return mName; }
285 
291  void setName( const QString &name );
292 
293  QString path() const { return mPath; }
294  void setPath( const QString &path ) { mPath = path; }
296  static QString pathComponent( const QString &component );
297 
308  virtual QVariant sortKey() const;
309 
315  void setSortKey( const QVariant &key );
316 
317 
318  // Because QIcon (QPixmap) must not be used in outside the GUI thread, it is
319  // not possible to set mIcon in constructor. Either use mIconName/setIconName()
320  // or implement icon().
321  void setIcon( const QIcon &icon ) { mIcon = icon; }
322  void setIconName( const QString &iconName ) { mIconName = iconName; }
323 
324  void setToolTip( const QString &msg ) { mToolTip = msg; }
325  QString toolTip() const { return mToolTip; }
326 
327  // deleteLater() items and clear the vector
328  static void deleteLater( QVector<QgsDataItem *> &items );
329 
331  void moveToThread( QThread *targetThread );
332 
333  protected:
334  virtual void populate( const QVector<QgsDataItem *> &children );
335 
339  virtual void refresh( const QVector<QgsDataItem *> &children );
340 
348  bool deferredDelete() { return mDeferredDelete; }
349 
351  Capabilities mCapabilities;
352  QgsDataItem *mParent = nullptr;
353  QVector<QgsDataItem *> mChildren; // easier to have it always
355  QString mName;
356  // Path is slash ('/') separated chain of item identifiers which are usually item names, but may be different if it is
357  // necessary to distinguish paths of two providers to the same source (e.g GRASS location and standard directory have the same
358  // name but different paths). Identifiers in path must not contain '/' characters.
359  // The path is used to identify item in tree.
360  QString mPath;
361  QString mToolTip;
362  QString mIconName;
363  QIcon mIcon;
364  QMap<QString, QIcon> mIconMap;
365 
367  QVariant mSortKey;
368 
369  public slots:
370 
379  virtual void deleteLater();
380 
381  // Populate children using children vector created by createChildren()
382  // \param foreground run createChildren in foreground
383  virtual void populate( bool foreground = false );
384 
386  virtual void depopulate();
387 
388  virtual void refresh();
389 
391  virtual void refreshConnections();
392 
393  virtual void childrenCreated();
394 
395  signals:
396  void beginInsertItems( QgsDataItem *parent, int first, int last );
397  void endInsertItems();
398  void beginRemoveItems( QgsDataItem *parent, int first, int last );
399  void endRemoveItems();
400  void dataChanged( QgsDataItem *item );
401  void stateChanged( QgsDataItem *item, QgsDataItem::State oldState );
402 
409  void connectionsChanged();
410 
411  protected slots:
412 
418  void updateIcon();
419 
420  private:
421  static QVector<QgsDataItem *> runCreateChildren( QgsDataItem *item );
422 
423  // Set to true if object has to be deleted when possible (nothing running in threads)
424  bool mDeferredDelete;
425  QFutureWatcher< QVector <QgsDataItem *> > *mFutureWatcher;
426  // number of items currently in loading (populating) state
427  static QgsAnimatedIcon *sPopulatingIcon;
428 };
429 
430 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsDataItem::Capabilities )
431 
432 
436 class CORE_EXPORT QgsLayerItem : public QgsDataItem
437 {
438  Q_OBJECT
439 
440  public:
442  {
453  Mesh
454  };
455 
456  Q_ENUM( LayerType )
457 
458  QgsLayerItem( QgsDataItem *parent, const QString &name, const QString &path, const QString &uri, LayerType layerType, const QString &providerKey );
459 
460  // --- reimplemented from QgsDataItem ---
461 
462  bool equal( const QgsDataItem *other ) override;
463 
464  bool hasDragEnabled() const override { return true; }
465 
466  QgsMimeDataUtils::Uri mimeUri() const override;
467 
468  // --- New virtual methods for layer item derived classes ---
469 
471  QgsMapLayerType mapLayerType() const;
472 
477  static LayerType typeFromMapLayer( QgsMapLayer *layer );
478 
480  QString uri() const { return mUri; }
481 
483  QString providerKey() const { return mProviderKey; }
484 
489  QStringList supportedCrs() const { return mSupportedCRS; }
490 
495  QStringList supportedFormats() const { return mSupportFormats; }
496 
501  virtual QString comments() const { return QString(); }
502 
507  static QString layerTypeAsString( LayerType layerType );
508 
513  static QString iconName( LayerType layerType );
514 
516  virtual bool deleteLayer();
517 
518  protected:
519 
521  QString mProviderKey;
523  QString mUri;
527  QStringList mSupportedCRS;
529  QStringList mSupportFormats;
530 
531  public:
532  static QIcon iconPoint();
533  static QIcon iconLine();
534  static QIcon iconPolygon();
535  static QIcon iconTable();
536  static QIcon iconRaster();
537  static QIcon iconDefault();
539  static QIcon iconMesh();
540 
542  virtual QString layerName() const { return name(); }
543 };
544 
545 
550 class CORE_EXPORT QgsDataCollectionItem : public QgsDataItem
551 {
552  Q_OBJECT
553  public:
554  QgsDataCollectionItem( QgsDataItem *parent, const QString &name, const QString &path = QString() );
555  ~QgsDataCollectionItem() override;
556 
557  void addChild( QgsDataItem *item SIP_TRANSFER ) { mChildren.append( item ); }
558 
563  static QIcon iconDir();
564 
569  static QIcon iconDataCollection();
570 
571  protected:
572 
577  static QIcon openDirIcon();
578 
583  static QIcon homeDirIcon();
584 };
585 
590 class CORE_EXPORT QgsDirectoryItem : public QgsDataCollectionItem
591 {
592  Q_OBJECT
593  public:
594 
595  QgsDirectoryItem( QgsDataItem *parent, const QString &name, const QString &path );
596 
603  QgsDirectoryItem( QgsDataItem *parent, const QString &name, const QString &dirPath, const QString &path );
604 
605  void setState( State state ) override;
606 
607  QVector<QgsDataItem *> createChildren() override;
608 
612  QString dirPath() const { return mDirPath; }
613 
614  bool equal( const QgsDataItem *other ) override;
615  QIcon icon() override;
616  QWidget *paramWidget() override SIP_FACTORY;
617  bool hasDragEnabled() const override { return true; }
618  QgsMimeDataUtils::Uri mimeUri() const override;
619 
621  static bool hiddenPath( const QString &path );
622 
623  public slots:
624  void childrenCreated() override;
625  void directoryChanged();
626 
627  protected:
628  void init();
629  QString mDirPath;
630 
631  private:
632  QFileSystemWatcher *mFileSystemWatcher = nullptr;
633  bool mRefreshLater;
634  QDateTime mLastScan;
635 };
636 
641 class CORE_EXPORT QgsProjectItem : public QgsDataItem
642 {
643  Q_OBJECT
644  public:
645 
652  QgsProjectItem( QgsDataItem *parent, const QString &name, const QString &path );
653 
654  bool hasDragEnabled() const override { return true; }
655 
656  QgsMimeDataUtils::Uri mimeUri() const override;
657 
658 };
659 
664 class CORE_EXPORT QgsErrorItem : public QgsDataItem
665 {
666  Q_OBJECT
667  public:
668 
669  QgsErrorItem( QgsDataItem *parent, const QString &error, const QString &path );
670 
671 };
672 
673 
674 // ---------
675 
680 class CORE_EXPORT QgsDirectoryParamWidget : public QTreeWidget
681 {
682  Q_OBJECT
683 
684  public:
685  QgsDirectoryParamWidget( const QString &path, QWidget *parent SIP_TRANSFERTHIS = nullptr );
686 
687  protected:
688  void mousePressEvent( QMouseEvent *event ) override;
689 
690  public slots:
691  void showHideColumn();
692 };
693 
699 class CORE_EXPORT QgsFavoritesItem : public QgsDataCollectionItem
700 {
701  Q_OBJECT
702  public:
703 
708  QgsFavoritesItem( QgsDataItem *parent, const QString &name, const QString &path = QString() );
709 
710  QVector<QgsDataItem *> createChildren() override;
711 
720  void addDirectory( const QString &directory, const QString &name = QString() );
721 
726  void removeDirectory( QgsDirectoryItem *item );
727 
731  void renameFavorite( const QString &path, const QString &name );
732 
734  static QIcon iconFavorites();
735 
736  QVariant sortKey() const override;
737 
738  private:
739  QVector<QgsDataItem *> createChildren( const QString &favDir, const QString &name );
740 };
741 
746 class CORE_EXPORT QgsZipItem : public QgsDataCollectionItem
747 {
748  Q_OBJECT
749 
750  protected:
751  QString mFilePath;
752  QString mVsiPrefix;
753  QStringList mZipFileList;
754 
755  public:
756  QgsZipItem( QgsDataItem *parent, const QString &name, const QString &path );
757  QgsZipItem( QgsDataItem *parent, const QString &name, const QString &filePath, const QString &path );
758 
759  QVector<QgsDataItem *> createChildren() override;
760  QStringList getZipFileList();
761 
763  static QVector<dataItem_t *> sDataItemPtr SIP_SKIP;
764  static QStringList sProviderNames;
765 
766  static QString vsiPrefix( const QString &uri ) { return qgsVsiPrefix( uri ); }
767 
771  static QgsDataItem *itemFromPath( QgsDataItem *parent, const QString &path, const QString &name ) SIP_FACTORY;
772 
777  static QgsDataItem *itemFromPath( QgsDataItem *parent, const QString &filePath, const QString &name, const QString &path ) SIP_FACTORY SIP_PYNAME( itemFromFilePath );
778 
779  static QIcon iconZip();
780 
781  private:
782  void init();
783 };
784 
785 
787 #ifndef SIP_RUN
788 
794 class CORE_EXPORT QgsProjectHomeItem : public QgsDirectoryItem
795 {
796  Q_OBJECT
797 
798  public:
799 
800  QgsProjectHomeItem( QgsDataItem *parent, const QString &name, const QString &dirPath, const QString &path );
801 
802  QIcon icon() override;
803  QVariant sortKey() const override;
804 
805 };
806 
812 class CORE_EXPORT QgsFavoriteItem : public QgsDirectoryItem
813 {
814  Q_OBJECT
815 
816  public:
817 
818  QgsFavoriteItem( QgsFavoritesItem *parent, const QString &name, const QString &dirPath, const QString &path );
819 
820  bool rename( const QString &name ) override;
821 
822  private:
823 
824  QgsFavoritesItem *mFavorites = nullptr;
825 };
826 
827 #endif
828 
830 #endif // QGSDATAITEM_H
831 
832 
A Collection: logical collection of layers or subcollections, e.g.
Definition: qgsdataitem.h:550
virtual QVariant sortKey() const
Returns the sorting key for the item.
QStringList mSupportFormats
The list of supported formats.
Definition: qgsdataitem.h:529
QString path() const
Definition: qgsdataitem.h:293
Base class for all map layer types.
Definition: qgsmaplayer.h:78
virtual void childrenCreated()
virtual QString layerName() const
Definition: qgsdataitem.h:542
QString providerKey() const
Returns provider key.
Definition: qgsdataitem.h:483
QString name() const
Returns the name of the item (the displayed text for the item).
Definition: qgsdataitem.h:284
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
QString toolTip() const
Definition: qgsdataitem.h:325
QVariant mSortKey
Custom sort key. If invalid, name() will be used for sorting instead.
Definition: qgsdataitem.h:367
QString mProviderKey
The provider key.
Definition: qgsdataitem.h:521
virtual bool handleDrop(const QMimeData *, Qt::DropAction)
Attempts to process the mime data dropped on this item.
Definition: qgsdataitem.h:180
virtual QIcon icon()
void addChild(QgsDataItem *item)
Definition: qgsdataitem.h:557
QString mIconName
Definition: qgsdataitem.h:362
State mState
Definition: qgsdataitem.h:354
Type type() const
Definition: qgsdataitem.h:265
void setIcon(const QIcon &icon)
Definition: qgsdataitem.h:321
virtual bool rename(const QString &name)
Sets a new name for the item, and returns true if the item was successfully renamed.
Abstract base class for spatial data provider implementations.
void setToolTip(const QString &msg)
Definition: qgsdataitem.h:324
const QgsCoordinateReferenceSystem & crs
#define SIP_TRANSFERBACK
Definition: qgis_sip.h:41
virtual QWidget * paramWidget()
Definition: qgsdataitem.h:147
QString mToolTip
Definition: qgsdataitem.h:361
A zip file: contains layers, using GDAL/OGR VSIFILE mechanism.
Definition: qgsdataitem.h:746
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:527
QString mFilePath
Definition: qgsdataitem.h:751
QgsDataItem * parent() const
Gets item parent.
Definition: qgsdataitem.h:270
QString mName
Definition: qgsdataitem.h:355
#define SIP_SKIP
Definition: qgis_sip.h:119
QStringList mZipFileList
Definition: qgsdataitem.h:753
virtual void setCapabilities(Capabilities capabilities)
Sets the capabilities for the data item.
Definition: qgsdataitem.h:256
Children not yet created.
Definition: qgsdataitem.h:104
#define SIP_TRANSFER
Definition: qgis_sip.h:36
Creating children in separate thread (populating or refreshing)
Definition: qgsdataitem.h:105
#define SIP_END
Definition: qgis_sip.h:182
Data item that can be used to report problems (e.g.
Definition: qgsdataitem.h:664
virtual bool hasDragEnabled() const
Returns true if the item may be dragged.
Definition: qgsdataitem.h:197
virtual QgsMimeDataUtils::Uri mimeUri() const
Returns mime URI for the data item.
Definition: qgsdataitem.h:205
#define SIP_FACTORY
Definition: qgis_sip.h:69
#define SIP_DEPRECATED
Definition: qgis_sip.h:99
Custom item type.
Definition: qgsdataitem.h:84
QIcon icon() override
A directory: contains subdirectories and layers.
Definition: qgsdataitem.h:590
static QStringList sProviderNames
Definition: qgsdataitem.h:764
Base class for all items in the model.
Definition: qgsdataitem.h:49
Capabilities mCapabilities
Definition: qgsdataitem.h:351
virtual void setState(State state)
Set item state.
QVector< QgsDataItem * > children() const
Definition: qgsdataitem.h:276
bool hasDragEnabled() const override
Returns true if the item may be dragged.
Definition: qgsdataitem.h:617
QString mPath
Definition: qgsdataitem.h:360
QString mUri
The URI.
Definition: qgsdataitem.h:523
bool hasDragEnabled() const override
Returns true if the item may be dragged.
Definition: qgsdataitem.h:464
Contains various Favorites directories.
Definition: qgsdataitem.h:699
QString mVsiPrefix
Definition: qgsdataitem.h:752
QStringList supportedFormats() const
Returns the supported formats.
Definition: qgsdataitem.h:495
QString qgsVsiPrefix(const QString &path)
Definition: qgis.cpp:226
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:480
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:165
bool deferredDelete()
The item is scheduled to be deleted.
Definition: qgsdataitem.h:348
QString dirPath() const
Returns the full path to the directory the item represents.
Definition: qgsdataitem.h:612
virtual QVector< QgsDataItem * > createChildren()
Create children.
Represents a QGIS project.
Definition: qgsdataitem.h:83
Item that represents a layer that can be opened with one of the providers.
Definition: qgsdataitem.h:436
QgsDataItem * dataItem_t(QString, QgsDataItem *)
Definition: qgsdataitem.h:42
QgsMapLayerType
Types of layers that can be added to a map.
Definition: qgsmaplayer.h:65
Data item that can be used to represent QGIS projects.
Definition: qgsdataitem.h:641
void setPath(const QString &path)
Definition: qgsdataitem.h:294
virtual bool acceptDrop()
Returns whether the item accepts drag and dropped layers - e.g.
Definition: qgsdataitem.h:173
LayerType mLayerType
The layer type.
Definition: qgsdataitem.h:525
QVector< QgsDataItem * > mChildren
Definition: qgsdataitem.h:353
virtual QString comments() const
Returns comments of the layer.
Definition: qgsdataitem.h:501
bool hasDragEnabled() const override
Returns true if the item may be dragged.
Definition: qgsdataitem.h:654
Represents a favorite item.
Definition: qgsdataitem.h:82
QStringList supportedCrs() const
Returns the supported CRS.
Definition: qgsdataitem.h:489
void setIconName(const QString &iconName)
Definition: qgsdataitem.h:322
static QString vsiPrefix(const QString &uri)
Definition: qgsdataitem.h:766
QMap< QString, QIcon > mIconMap
Definition: qgsdataitem.h:364
Added in 2.10.
Definition: qgsdataitem.h:452
virtual Capabilities capabilities2() const
Returns the capabilities for the data item.
Definition: qgsdataitem.h:249
#define SIP_PYNAME(name)
Definition: qgis_sip.h:74