QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 <QObject>
26 #include <QPixmap>
27 #include <QString>
28 #include <QTreeWidget>
29 #include <QVector>
30 #include <QDateTime>
31 
32 #include "qgsmaplayer.h"
34 #include "qgsmimedatautils.h"
35 #include "qgswkbtypes.h"
36 
37 class QgsDataProvider;
38 class QgsDataItem;
39 class QgsAnimatedIcon;
40 class QgsBookmarkManager;
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 
154  Q_DECL_DEPRECATED virtual QWidget *paramWidget() SIP_FACTORY SIP_DEPRECATED { return nullptr; }
155 
163  virtual QList<QAction *> actions( QWidget *parent );
164 
173  virtual QList<QMenu *> menus( QWidget *parent );
174 
183  Q_DECL_DEPRECATED virtual bool acceptDrop() SIP_DEPRECATED { return false; }
184 
193  Q_DECL_DEPRECATED virtual bool handleDrop( const QMimeData * /*data*/, Qt::DropAction /*action*/ ) SIP_DEPRECATED { return false; }
194 
201  virtual bool handleDoubleClick();
202 
210  virtual bool hasDragEnabled() const { return false; }
211 
219 
221  {
222  NoCapabilities = 0,
223  SetCrs = 1 << 0,
224  Fertile = 1 << 1,
225  Fast = 1 << 2,
226  Collapse = 1 << 3,
227  Rename = 1 << 4,
228  Delete = 1 << 5,
229  };
230  Q_DECLARE_FLAGS( Capabilities, Capability )
231 
232 
238  Q_DECL_DEPRECATED virtual bool setCrs( const QgsCoordinateReferenceSystem &crs ) SIP_DEPRECATED
239  {
240  Q_UNUSED( crs )
241  return false;
242  }
243 
256  Q_DECL_DEPRECATED virtual bool rename( const QString &name ) SIP_DEPRECATED;
257 
258  // ### QGIS 4 - rename to capabilities()
259 
265  virtual Capabilities capabilities2() const { return mCapabilities; }
266 
272  virtual void setCapabilities( Capabilities capabilities ) { mCapabilities = capabilities; }
273 
274  // static methods
275 
276  // Find child index in vector of items using '==' operator
277  static int findItem( QVector<QgsDataItem *> items, QgsDataItem *item );
278 
279  // members
280 
281  Type type() const { return mType; }
282 
286  QgsDataItem *parent() const { return mParent; }
287 
291  void setParent( QgsDataItem *parent );
292  QVector<QgsDataItem *> children() const { return mChildren; }
293  virtual QIcon icon();
294 
300  QString name() const { return mName; }
301 
307  void setName( const QString &name );
308 
309  QString path() const { return mPath; }
310  void setPath( const QString &path ) { mPath = path; }
312  static QString pathComponent( const QString &component );
313 
324  virtual QVariant sortKey() const;
325 
331  void setSortKey( const QVariant &key );
332 
333 
334  // Because QIcon (QPixmap) must not be used in outside the GUI thread, it is
335  // not possible to set mIcon in constructor. Either use mIconName/setIconName()
336  // or implement icon().
337  void setIcon( const QIcon &icon ) { mIcon = icon; }
338  void setIconName( const QString &iconName ) { mIconName = iconName; }
339 
340  void setToolTip( const QString &msg ) { mToolTip = msg; }
341  QString toolTip() const { return mToolTip; }
342 
343  // deleteLater() items and clear the vector
344  static void deleteLater( QVector<QgsDataItem *> &items );
345 
347  void moveToThread( QThread *targetThread );
348 
349  protected:
350  virtual void populate( const QVector<QgsDataItem *> &children );
351 
355  virtual void refresh( const QVector<QgsDataItem *> &children );
356 
364  bool deferredDelete() { return mDeferredDelete; }
365 
367  Capabilities mCapabilities;
368  QgsDataItem *mParent = nullptr;
369  QVector<QgsDataItem *> mChildren; // easier to have it always
371  QString mName;
372  // Path is slash ('/') separated chain of item identifiers which are usually item names, but may be different if it is
373  // necessary to distinguish paths of two providers to the same source (e.g GRASS location and standard directory have the same
374  // name but different paths). Identifiers in path must not contain '/' characters.
375  // The path is used to identify item in tree.
376  QString mPath;
377  QString mToolTip;
378  QString mIconName;
379  QIcon mIcon;
380  QMap<QString, QIcon> mIconMap;
381 
383  QVariant mSortKey;
384 
385  public slots:
386 
395  virtual void deleteLater();
396 
397  // Populate children using children vector created by createChildren()
398  // \param foreground run createChildren in foreground
399  virtual void populate( bool foreground = false );
400 
402  virtual void depopulate();
403 
404  virtual void refresh();
405 
407  virtual void refreshConnections();
408 
409  virtual void childrenCreated();
410 
411  signals:
412  void beginInsertItems( QgsDataItem *parent, int first, int last );
413  void endInsertItems();
414  void beginRemoveItems( QgsDataItem *parent, int first, int last );
415  void endRemoveItems();
416  void dataChanged( QgsDataItem *item );
417  void stateChanged( QgsDataItem *item, QgsDataItem::State oldState );
418 
425  void connectionsChanged();
426 
427  protected slots:
428 
434  void updateIcon();
435 
436  private:
437  static QVector<QgsDataItem *> runCreateChildren( QgsDataItem *item );
438 
439  // Set to true if object has to be deleted when possible (nothing running in threads)
440  bool mDeferredDelete;
441  QFutureWatcher< QVector <QgsDataItem *> > *mFutureWatcher;
442  // number of items currently in loading (populating) state
443  static QgsAnimatedIcon *sPopulatingIcon;
444 };
445 
446 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsDataItem::Capabilities )
447 
448 
452 class CORE_EXPORT QgsLayerItem : public QgsDataItem
453 {
454  Q_OBJECT
455 
456  public:
458  {
469  Mesh
470  };
471 
472  Q_ENUM( LayerType )
473 
474  QgsLayerItem( QgsDataItem *parent, const QString &name, const QString &path, const QString &uri, LayerType layerType, const QString &providerKey );
475 
476  // --- reimplemented from QgsDataItem ---
477 
478  bool equal( const QgsDataItem *other ) override;
479 
480  bool hasDragEnabled() const override { return true; }
481 
482  QgsMimeDataUtils::Uri mimeUri() const override;
483 
484  // --- New virtual methods for layer item derived classes ---
485 
487  QgsMapLayerType mapLayerType() const;
488 
493  static LayerType typeFromMapLayer( QgsMapLayer *layer );
494 
496  QString uri() const { return mUri; }
497 
499  QString providerKey() const { return mProviderKey; }
500 
505  QStringList supportedCrs() const { return mSupportedCRS; }
506 
511  QStringList supportedFormats() const { return mSupportFormats; }
512 
517  virtual QString comments() const { return QString(); }
518 
523  static QString layerTypeAsString( LayerType layerType );
524 
529  static QString iconName( LayerType layerType );
530 
537  Q_DECL_DEPRECATED virtual bool deleteLayer() SIP_DEPRECATED;
538 
539  protected:
540 
542  QString mProviderKey;
544  QString mUri;
548  QStringList mSupportedCRS;
550  QStringList mSupportFormats;
551 
552  public:
553  static QIcon iconPoint();
554  static QIcon iconLine();
555  static QIcon iconPolygon();
556  static QIcon iconTable();
557  static QIcon iconRaster();
558  static QIcon iconDefault();
560  static QIcon iconMesh();
561 
563  virtual QString layerName() const { return name(); }
564 };
565 
566 
571 class CORE_EXPORT QgsDataCollectionItem : public QgsDataItem
572 {
573  Q_OBJECT
574  public:
575  QgsDataCollectionItem( QgsDataItem *parent, const QString &name, const QString &path = QString() );
576  ~QgsDataCollectionItem() override;
577 
578  void addChild( QgsDataItem *item SIP_TRANSFER ) { mChildren.append( item ); }
579 
584  static QIcon iconDir();
585 
590  static QIcon iconDataCollection();
591 
592  protected:
593 
598  static QIcon openDirIcon();
599 
604  static QIcon homeDirIcon();
605 };
606 
611 class CORE_EXPORT QgsDirectoryItem : public QgsDataCollectionItem
612 {
613  Q_OBJECT
614  public:
615 
616  QgsDirectoryItem( QgsDataItem *parent, const QString &name, const QString &path );
617 
624  QgsDirectoryItem( QgsDataItem *parent, const QString &name, const QString &dirPath, const QString &path );
625 
626  void setState( State state ) override;
627 
628  QVector<QgsDataItem *> createChildren() override;
629 
633  QString dirPath() const { return mDirPath; }
634 
635  bool equal( const QgsDataItem *other ) override;
636  QIcon icon() override;
637  Q_DECL_DEPRECATED QWidget *paramWidget() override SIP_FACTORY SIP_DEPRECATED;
638  bool hasDragEnabled() const override { return true; }
639  QgsMimeDataUtils::Uri mimeUri() const override;
640 
642  static bool hiddenPath( const QString &path );
643 
644  public slots:
645  void childrenCreated() override;
646  void directoryChanged();
647 
648  protected:
649  void init();
650  QString mDirPath;
651 
652  private:
653  QFileSystemWatcher *mFileSystemWatcher = nullptr;
654  bool mRefreshLater;
655  QDateTime mLastScan;
656 };
657 
662 class CORE_EXPORT QgsProjectItem : public QgsDataItem
663 {
664  Q_OBJECT
665  public:
666 
673  QgsProjectItem( QgsDataItem *parent, const QString &name, const QString &path );
674 
675  bool hasDragEnabled() const override { return true; }
676 
677  QgsMimeDataUtils::Uri mimeUri() const override;
678 
679 };
680 
685 class CORE_EXPORT QgsErrorItem : public QgsDataItem
686 {
687  Q_OBJECT
688  public:
689 
690  QgsErrorItem( QgsDataItem *parent, const QString &error, const QString &path );
691 
692 };
693 
694 // ---------
695 
702 class CORE_EXPORT QgsDirectoryParamWidget : public QTreeWidget
703 {
704  Q_OBJECT
705 
706  public:
707  QgsDirectoryParamWidget( const QString &path, QWidget *parent SIP_TRANSFERTHIS = nullptr );
708 
709  protected:
710  void mousePressEvent( QMouseEvent *event ) override;
711 
712  public slots:
713  void showHideColumn();
714 };
715 
721 class CORE_EXPORT QgsFavoritesItem : public QgsDataCollectionItem
722 {
723  Q_OBJECT
724  public:
725 
730  QgsFavoritesItem( QgsDataItem *parent, const QString &name, const QString &path = QString() );
731 
732  QVector<QgsDataItem *> createChildren() override;
733 
742  void addDirectory( const QString &directory, const QString &name = QString() );
743 
748  void removeDirectory( QgsDirectoryItem *item );
749 
753  void renameFavorite( const QString &path, const QString &name );
754 
756  static QIcon iconFavorites();
757 
758  QVariant sortKey() const override;
759 
760  private:
761  QVector<QgsDataItem *> createChildren( const QString &favDir, const QString &name );
762 };
763 
768 class CORE_EXPORT QgsZipItem : public QgsDataCollectionItem
769 {
770  Q_OBJECT
771 
772  protected:
773  QString mFilePath;
774  QString mVsiPrefix;
775  QStringList mZipFileList;
776 
777  public:
778  QgsZipItem( QgsDataItem *parent, const QString &name, const QString &path );
779  QgsZipItem( QgsDataItem *parent, const QString &name, const QString &filePath, const QString &path );
780 
781  QVector<QgsDataItem *> createChildren() override;
782  QStringList getZipFileList();
783 
785  static QVector<dataItem_t *> sDataItemPtr SIP_SKIP;
786  static QStringList sProviderNames;
787 
788  static QString vsiPrefix( const QString &uri ) { return qgsVsiPrefix( uri ); }
789 
793  static QgsDataItem *itemFromPath( QgsDataItem *parent, const QString &path, const QString &name ) SIP_FACTORY;
794 
799  static QgsDataItem *itemFromPath( QgsDataItem *parent, const QString &filePath, const QString &name, const QString &path ) SIP_FACTORY SIP_PYNAME( itemFromFilePath );
800 
801  static QIcon iconZip();
802 
803  private:
804  void init();
805 };
806 
807 
809 #ifndef SIP_RUN
810 
816 class CORE_EXPORT QgsProjectHomeItem : public QgsDirectoryItem
817 {
818  Q_OBJECT
819 
820  public:
821 
822  QgsProjectHomeItem( QgsDataItem *parent, const QString &name, const QString &dirPath, const QString &path );
823 
824  QIcon icon() override;
825  QVariant sortKey() const override;
826 
827 };
828 
834 Q_NOWARN_DEPRECATED_PUSH // rename is deprecated
835 class CORE_EXPORT QgsFavoriteItem : public QgsDirectoryItem
836 {
837  Q_OBJECT
838 
839  public:
840 
841  QgsFavoriteItem( QgsFavoritesItem *parent, const QString &name, const QString &dirPath, const QString &path );
842 
843  bool rename( const QString &name ) override;
844 
845  private:
846 
847  QgsFavoritesItem *mFavorites = nullptr;
848 };
850 
851 #endif
852 
854 #endif // QGSDATAITEM_H
855 
856 
A Collection: logical collection of layers or subcollections, e.g.
Definition: qgsdataitem.h:571
virtual QVariant sortKey() const
Returns the sorting key for the item.
QStringList mSupportFormats
The list of supported formats.
Definition: qgsdataitem.h:550
QString path() const
Definition: qgsdataitem.h:309
Base class for all map layer types.
Definition: qgsmaplayer.h:79
virtual void childrenCreated()
virtual QString layerName() const
Definition: qgsdataitem.h:563
QString providerKey() const
Returns provider key.
Definition: qgsdataitem.h:499
QString name() const
Returns the name of the item (the displayed text for the item).
Definition: qgsdataitem.h:300
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
QString toolTip() const
Definition: qgsdataitem.h:341
QVariant mSortKey
Custom sort key. If invalid, name() will be used for sorting instead.
Definition: qgsdataitem.h:383
QString mProviderKey
The provider key.
Definition: qgsdataitem.h:542
virtual QIcon icon()
void addChild(QgsDataItem *item)
Definition: qgsdataitem.h:578
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:649
QString mIconName
Definition: qgsdataitem.h:378
State mState
Definition: qgsdataitem.h:370
Type type() const
Definition: qgsdataitem.h:281
void setIcon(const QIcon &icon)
Definition: qgsdataitem.h:337
virtual Q_DECL_DEPRECATED bool rename(const QString &name)
Sets a new name for the item, and returns true if the item was successfully renamed.
Manages storage of a set of bookmarks.
Abstract base class for spatial data provider implementations.
void setToolTip(const QString &msg)
Definition: qgsdataitem.h:340
const QgsCoordinateReferenceSystem & crs
#define SIP_TRANSFERBACK
Definition: qgis_sip.h:48
QString mToolTip
Definition: qgsdataitem.h:377
A zip file: contains layers, using GDAL/OGR VSIFILE mechanism.
Definition: qgsdataitem.h:768
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:548
QString mFilePath
Definition: qgsdataitem.h:773
QgsDataItem * parent() const
Gets item parent.
Definition: qgsdataitem.h:286
QString mName
Definition: qgsdataitem.h:371
#define SIP_SKIP
Definition: qgis_sip.h:126
QStringList mZipFileList
Definition: qgsdataitem.h:775
virtual void setCapabilities(Capabilities capabilities)
Sets the capabilities for the data item.
Definition: qgsdataitem.h:272
Children not yet created.
Definition: qgsdataitem.h:104
#define SIP_TRANSFER
Definition: qgis_sip.h:36
virtual Q_DECL_DEPRECATED bool acceptDrop()
Returns whether the item accepts drag and dropped layers - e.g.
Definition: qgsdataitem.h:183
Creating children in separate thread (populating or refreshing)
Definition: qgsdataitem.h:105
#define SIP_END
Definition: qgis_sip.h:189
Data item that can be used to report problems (e.g.
Definition: qgsdataitem.h:685
virtual bool hasDragEnabled() const
Returns true if the item may be dragged.
Definition: qgsdataitem.h:210
virtual QgsMimeDataUtils::Uri mimeUri() const
Returns mime URI for the data item.
Definition: qgsdataitem.h:218
#define SIP_FACTORY
Definition: qgis_sip.h:76
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
Custom item type.
Definition: qgsdataitem.h:84
QIcon icon() override
A directory: contains subdirectories and layers.
Definition: qgsdataitem.h:611
static QStringList sProviderNames
Definition: qgsdataitem.h:786
Base class for all items in the model.
Definition: qgsdataitem.h:49
virtual Q_DECL_DEPRECATED QWidget * paramWidget()
Returns source widget from data item for QgsBrowserPropertiesWidget.
Definition: qgsdataitem.h:154
Capabilities mCapabilities
Definition: qgsdataitem.h:367
virtual void setState(State state)
Set item state.
QVector< QgsDataItem * > children() const
Definition: qgsdataitem.h:292
bool hasDragEnabled() const override
Returns true if the item may be dragged.
Definition: qgsdataitem.h:638
QString mPath
Definition: qgsdataitem.h:376
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:650
QString mUri
The URI.
Definition: qgsdataitem.h:544
TODO: move to qgis_gui for QGIS 4.
Definition: qgsdataitem.h:702
bool hasDragEnabled() const override
Returns true if the item may be dragged.
Definition: qgsdataitem.h:480
Contains various Favorites directories.
Definition: qgsdataitem.h:721
QString mVsiPrefix
Definition: qgsdataitem.h:774
QStringList supportedFormats() const
Returns the supported formats.
Definition: qgsdataitem.h:511
QString qgsVsiPrefix(const QString &path)
Definition: qgis.cpp:227
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:496
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:172
bool deferredDelete()
The item is scheduled to be deleted.
Definition: qgsdataitem.h:364
QString dirPath() const
Returns the full path to the directory the item represents.
Definition: qgsdataitem.h:633
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:452
virtual Q_DECL_DEPRECATED bool handleDrop(const QMimeData *, Qt::DropAction)
Attempts to process the mime data dropped on this item.
Definition: qgsdataitem.h:193
QgsDataItem * dataItem_t(QString, QgsDataItem *)
Definition: qgsdataitem.h:42
QgsMapLayerType
Types of layers that can be added to a map.
Definition: qgsmaplayer.h:66
Data item that can be used to represent QGIS projects.
Definition: qgsdataitem.h:662
void setPath(const QString &path)
Definition: qgsdataitem.h:310
LayerType mLayerType
The layer type.
Definition: qgsdataitem.h:546
QVector< QgsDataItem * > mChildren
Definition: qgsdataitem.h:369
virtual QString comments() const
Returns comments of the layer.
Definition: qgsdataitem.h:517
bool hasDragEnabled() const override
Returns true if the item may be dragged.
Definition: qgsdataitem.h:675
Represents a favorite item.
Definition: qgsdataitem.h:82
QStringList supportedCrs() const
Returns the supported CRS.
Definition: qgsdataitem.h:505
void setIconName(const QString &iconName)
Definition: qgsdataitem.h:338
static QString vsiPrefix(const QString &uri)
Definition: qgsdataitem.h:788
QMap< QString, QIcon > mIconMap
Definition: qgsdataitem.h:380
Added in 2.10.
Definition: qgsdataitem.h:468
virtual Capabilities capabilities2() const
Returns the capabilities for the data item.
Definition: qgsdataitem.h:265
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81