QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 #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  };
216  Q_DECLARE_FLAGS( Capabilities, Capability )
217 
218 
224  Q_DECL_DEPRECATED virtual bool setCrs( const QgsCoordinateReferenceSystem &crs ) SIP_DEPRECATED
225  {
226  Q_UNUSED( crs );
227  return false;
228  }
229 
239  virtual bool rename( const QString &name );
240 
241  // ### QGIS 4 - rename to capabilities()
242 
248  virtual Capabilities capabilities2() const { return mCapabilities; }
249 
255  virtual void setCapabilities( Capabilities capabilities ) { mCapabilities = capabilities; }
256 
257  // static methods
258 
259  // Find child index in vector of items using '==' operator
260  static int findItem( QVector<QgsDataItem *> items, QgsDataItem *item );
261 
262  // members
263 
264  Type type() const { return mType; }
265 
269  QgsDataItem *parent() const { return mParent; }
270 
274  void setParent( QgsDataItem *parent );
275  QVector<QgsDataItem *> children() const { return mChildren; }
276  virtual QIcon icon();
277 
283  QString name() const { return mName; }
284 
290  void setName( const QString &name );
291 
292  QString path() const { return mPath; }
293  void setPath( const QString &path ) { mPath = path; }
295  static QString pathComponent( const QString &component );
296 
307  virtual QVariant sortKey() const;
308 
314  void setSortKey( const QVariant &key );
315 
316 
317  // Because QIcon (QPixmap) must not be used in outside the GUI thread, it is
318  // not possible to set mIcon in constructor. Either use mIconName/setIconName()
319  // or implement icon().
320  void setIcon( const QIcon &icon ) { mIcon = icon; }
321  void setIconName( const QString &iconName ) { mIconName = iconName; }
322 
323  void setToolTip( const QString &msg ) { mToolTip = msg; }
324  QString toolTip() const { return mToolTip; }
325 
326  // deleteLater() items and clear the vector
327  static void deleteLater( QVector<QgsDataItem *> &items );
328 
330  void moveToThread( QThread *targetThread );
331 
332  protected:
333  virtual void populate( const QVector<QgsDataItem *> &children );
334 
338  virtual void refresh( const QVector<QgsDataItem *> &children );
339 
347  bool deferredDelete() { return mDeferredDelete; }
348 
350  Capabilities mCapabilities;
351  QgsDataItem *mParent = nullptr;
352  QVector<QgsDataItem *> mChildren; // easier to have it always
354  QString mName;
355  // Path is slash ('/') separated chain of item identifiers which are usually item names, but may be different if it is
356  // necessary to distinguish paths of two providers to the same source (e.g GRASS location and standard directory have the same
357  // name but different paths). Identifiers in path must not contain '/' characters.
358  // The path is used to identify item in tree.
359  QString mPath;
360  QString mToolTip;
361  QString mIconName;
362  QIcon mIcon;
363  QMap<QString, QIcon> mIconMap;
364 
366  QVariant mSortKey;
367 
368  public slots:
369 
378  virtual void deleteLater();
379 
380  // Populate children using children vector created by createChildren()
381  // \param foreground run createChildren in foreground
382  virtual void populate( bool foreground = false );
383 
385  virtual void depopulate();
386 
387  virtual void refresh();
388 
390  virtual void refreshConnections();
391 
392  virtual void childrenCreated();
393 
394  signals:
395  void beginInsertItems( QgsDataItem *parent, int first, int last );
396  void endInsertItems();
397  void beginRemoveItems( QgsDataItem *parent, int first, int last );
398  void endRemoveItems();
399  void dataChanged( QgsDataItem *item );
400  void stateChanged( QgsDataItem *item, QgsDataItem::State oldState );
401 
408  void connectionsChanged();
409 
410  protected slots:
411 
417  void updateIcon();
418 
419  private:
420  static QVector<QgsDataItem *> runCreateChildren( QgsDataItem *item );
421 
422  // Set to true if object has to be deleted when possible (nothing running in threads)
423  bool mDeferredDelete;
424  QFutureWatcher< QVector <QgsDataItem *> > *mFutureWatcher;
425  // number of items currently in loading (populating) state
426  static QgsAnimatedIcon *sPopulatingIcon;
427 };
428 
429 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsDataItem::Capabilities )
430 
431 
435 class CORE_EXPORT QgsLayerItem : public QgsDataItem
436 {
437  Q_OBJECT
438 
439  public:
441  {
452  Mesh
453  };
454 
455  Q_ENUM( LayerType )
456 
457  QgsLayerItem( QgsDataItem *parent, const QString &name, const QString &path, const QString &uri, LayerType layerType, const QString &providerKey );
458 
459  // --- reimplemented from QgsDataItem ---
460 
461  bool equal( const QgsDataItem *other ) override;
462 
463  bool hasDragEnabled() const override { return true; }
464 
465  QgsMimeDataUtils::Uri mimeUri() const override;
466 
467  // --- New virtual methods for layer item derived classes ---
468 
470  QgsMapLayer::LayerType mapLayerType() const;
471 
473  QString uri() const { return mUri; }
474 
476  QString providerKey() const { return mProviderKey; }
477 
482  QStringList supportedCrs() const { return mSupportedCRS; }
483 
488  QStringList supportedFormats() const { return mSupportFormats; }
489 
494  virtual QString comments() const { return QString(); }
495 
500  static QString layerTypeAsString( LayerType layerType );
501 
506  static QString iconName( LayerType layerType );
507 
508  protected:
509 
511  QString mProviderKey;
513  QString mUri;
517  QStringList mSupportedCRS;
519  QStringList mSupportFormats;
520 
521  public:
522  static QIcon iconPoint();
523  static QIcon iconLine();
524  static QIcon iconPolygon();
525  static QIcon iconTable();
526  static QIcon iconRaster();
527  static QIcon iconDefault();
529  static QIcon iconMesh();
530 
532  virtual QString layerName() const { return name(); }
533 };
534 
535 
540 class CORE_EXPORT QgsDataCollectionItem : public QgsDataItem
541 {
542  Q_OBJECT
543  public:
544  QgsDataCollectionItem( QgsDataItem *parent, const QString &name, const QString &path = QString() );
545  ~QgsDataCollectionItem() override;
546 
547  void addChild( QgsDataItem *item SIP_TRANSFER ) { mChildren.append( item ); }
548 
549  static QIcon iconDir(); // shared icon: open/closed directory
550  static QIcon iconDataCollection(); // default icon for data collection
551 
552  protected:
553 
558  static QIcon openDirIcon();
559 
564  static QIcon homeDirIcon();
565 };
566 
571 class CORE_EXPORT QgsDirectoryItem : public QgsDataCollectionItem
572 {
573  Q_OBJECT
574  public:
575  enum Column
576  {
584  };
585 
586  QgsDirectoryItem( QgsDataItem *parent, const QString &name, const QString &path );
587 
594  QgsDirectoryItem( QgsDataItem *parent, const QString &name, const QString &dirPath, const QString &path );
595 
596  void setState( State state ) override;
597 
598  QVector<QgsDataItem *> createChildren() override;
599 
600  QString dirPath() const { return mDirPath; }
601  bool equal( const QgsDataItem *other ) override;
602  QIcon icon() override;
603  QWidget *paramWidget() override SIP_FACTORY;
604 
606  static bool hiddenPath( const QString &path );
607 
608  QList<QAction *> actions( QWidget *parent ) override;
609 
610 
611  public slots:
612  void childrenCreated() override;
613  void directoryChanged();
614 
615  protected:
616  void init();
617  QString mDirPath;
618 
619  private:
620  QFileSystemWatcher *mFileSystemWatcher = nullptr;
621  bool mRefreshLater;
622  QDateTime mLastScan;
623 };
624 
629 class CORE_EXPORT QgsProjectItem : public QgsDataItem
630 {
631  Q_OBJECT
632  public:
633 
640  QgsProjectItem( QgsDataItem *parent, const QString &name, const QString &path );
641 
642  bool hasDragEnabled() const override { return true; }
643 
644  QgsMimeDataUtils::Uri mimeUri() const override;
645 
646 };
647 
652 class CORE_EXPORT QgsErrorItem : public QgsDataItem
653 {
654  Q_OBJECT
655  public:
656 
657  QgsErrorItem( QgsDataItem *parent, const QString &error, const QString &path );
658 
659 };
660 
661 
662 // ---------
663 
668 class CORE_EXPORT QgsDirectoryParamWidget : public QTreeWidget
669 {
670  Q_OBJECT
671 
672  public:
673  QgsDirectoryParamWidget( const QString &path, QWidget *parent SIP_TRANSFERTHIS = nullptr );
674 
675  protected:
676  void mousePressEvent( QMouseEvent *event ) override;
677 
678  public slots:
679  void showHideColumn();
680 };
681 
687 class CORE_EXPORT QgsFavoritesItem : public QgsDataCollectionItem
688 {
689  Q_OBJECT
690  public:
691 
696  QgsFavoritesItem( QgsDataItem *parent, const QString &name, const QString &path = QString() );
697 
698  QVector<QgsDataItem *> createChildren() override;
699 
708  void addDirectory( const QString &directory, const QString &name = QString() );
709 
714  void removeDirectory( QgsDirectoryItem *item );
715 
719  void renameFavorite( const QString &path, const QString &name );
720 
722  static QIcon iconFavorites();
723 
724  QVariant sortKey() const override;
725 
726  private:
727  QVector<QgsDataItem *> createChildren( const QString &favDir, const QString &name );
728 };
729 
734 class CORE_EXPORT QgsZipItem : public QgsDataCollectionItem
735 {
736  Q_OBJECT
737 
738  protected:
739  QString mFilePath;
740  QString mVsiPrefix;
741  QStringList mZipFileList;
742 
743  public:
744  QgsZipItem( QgsDataItem *parent, const QString &name, const QString &path );
745  QgsZipItem( QgsDataItem *parent, const QString &name, const QString &filePath, const QString &path );
746 
747  QVector<QgsDataItem *> createChildren() override;
748  QStringList getZipFileList();
749 
751  static QVector<dataItem_t *> sDataItemPtr SIP_SKIP;
752  static QStringList sProviderNames;
753 
754  static QString vsiPrefix( const QString &uri ) { return qgsVsiPrefix( uri ); }
755 
759  static QgsDataItem *itemFromPath( QgsDataItem *parent, const QString &path, const QString &name ) SIP_FACTORY;
760 
765  static QgsDataItem *itemFromPath( QgsDataItem *parent, const QString &filePath, const QString &name, const QString &path ) SIP_FACTORY SIP_PYNAME( itemFromFilePath );
766 
767  static QIcon iconZip();
768 
769  private:
770  void init();
771 };
772 
773 
775 #ifndef SIP_RUN
776 
782 class CORE_EXPORT QgsProjectHomeItem : public QgsDirectoryItem
783 {
784  Q_OBJECT
785 
786  public:
787 
788  QgsProjectHomeItem( QgsDataItem *parent, const QString &name, const QString &dirPath, const QString &path );
789 
790  QIcon icon() override;
791  QVariant sortKey() const override;
792 
793  QList<QAction *> actions( QWidget *parent ) override;
794 
795 
796 };
797 
803 class CORE_EXPORT QgsFavoriteItem : public QgsDirectoryItem
804 {
805  Q_OBJECT
806 
807  public:
808 
809  QgsFavoriteItem( QgsFavoritesItem *parent, const QString &name, const QString &dirPath, const QString &path );
810 
811  bool rename( const QString &name ) override;
812 
813  private:
814 
815  QgsFavoritesItem *mFavorites = nullptr;
816 };
817 
818 #endif
819 
821 #endif // QGSDATAITEM_H
822 
823 
A Collection: logical collection of layers or subcollections, e.g.
Definition: qgsdataitem.h:540
QStringList mSupportFormats
The list of supported formats.
Definition: qgsdataitem.h:519
virtual void childrenCreated()
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
QString name() const
Returns the name of the item (the displayed text for the item).
Definition: qgsdataitem.h:283
QVariant mSortKey
Custom sort key. If invalid, name() will be used for sorting instead.
Definition: qgsdataitem.h:366
virtual Capabilities capabilities2() const
Returns the capabilities for the data item.
Definition: qgsdataitem.h:248
QgsDataItem * parent() const
Gets item parent.
Definition: qgsdataitem.h:269
QIcon icon() const
Gets the icons representation in the current frame.
QString mProviderKey
The provider key.
Definition: qgsdataitem.h:511
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:547
QString mIconName
Definition: qgsdataitem.h:361
State mState
Definition: qgsdataitem.h:353
void setIcon(const QIcon &icon)
Definition: qgsdataitem.h:320
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:323
const QgsCoordinateReferenceSystem & crs
#define SIP_TRANSFERBACK
Definition: qgis_sip.h:41
QVector< QgsDataItem * > children() const
Definition: qgsdataitem.h:275
virtual QWidget * paramWidget()
Definition: qgsdataitem.h:147
QString mToolTip
Definition: qgsdataitem.h:360
A zip file: contains layers, using GDAL/OGR VSIFILE mechanism.
Definition: qgsdataitem.h:734
virtual bool equal(const QgsDataItem *other)
Returns true if this item is equal to another item (by testing item type and path).
virtual QgsMimeDataUtils::Uri mimeUri() const
Returns mime URI for the data item.
Definition: qgsdataitem.h:205
QStringList mSupportedCRS
The list of supported CRS.
Definition: qgsdataitem.h:517
QString mFilePath
Definition: qgsdataitem.h:739
QString dirPath() const
Definition: qgsdataitem.h:600
QString mName
Definition: qgsdataitem.h:354
#define SIP_SKIP
Definition: qgis_sip.h:119
LayerType
Types of layers that can be added to a map.
Definition: qgsmaplayer.h:105
QStringList mZipFileList
Definition: qgsdataitem.h:741
virtual void setCapabilities(Capabilities capabilities)
Sets the capabilities for the data item.
Definition: qgsdataitem.h:255
virtual bool hasDragEnabled() const
Returns true if the item may be dragged.
Definition: qgsdataitem.h:197
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
QString path() const
Definition: qgsdataitem.h:292
Data item that can be used to report problems (e.g.
Definition: qgsdataitem.h:652
Type type() const
Definition: qgsdataitem.h:264
#define SIP_FACTORY
Definition: qgis_sip.h:69
QString providerKey() const
Returns provider key.
Definition: qgsdataitem.h:476
#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:571
static QStringList sProviderNames
Definition: qgsdataitem.h:752
Base class for all items in the model.
Definition: qgsdataitem.h:49
virtual QVariant sortKey() const
Returns the sorting key for the item.
Capabilities mCapabilities
Definition: qgsdataitem.h:350
virtual void setState(State state)
Set item state.
QString mPath
Definition: qgsdataitem.h:359
QString mUri
The URI.
Definition: qgsdataitem.h:513
QString uri() const
Returns layer uri or empty string if layer cannot be created.
Definition: qgsdataitem.h:473
Contains various Favorites directories.
Definition: qgsdataitem.h:687
QStringList supportedCrs() const
Returns the supported CRS.
Definition: qgsdataitem.h:482
QString mVsiPrefix
Definition: qgsdataitem.h:740
virtual QString comments() const
Returns comments of the layer.
Definition: qgsdataitem.h:494
virtual QString layerName() const
Definition: qgsdataitem.h:532
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...
virtual QList< QAction * > actions(QWidget *parent)
Returns the list of actions available for this item.
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:165
bool deferredDelete()
The item is scheduled to be deleted.
Definition: qgsdataitem.h:347
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:435
QgsDataItem * dataItem_t(QString, QgsDataItem *)
Definition: qgsdataitem.h:42
QString toolTip() const
Definition: qgsdataitem.h:324
QList< QAction * > actions(QWidget *parent) override
Returns the list of actions available for this item.
Data item that can be used to represent QGIS projects.
Definition: qgsdataitem.h:629
void setPath(const QString &path)
Definition: qgsdataitem.h:293
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:515
QStringList supportedFormats() const
Returns the supported formats.
Definition: qgsdataitem.h:488
QVector< QgsDataItem * > mChildren
Definition: qgsdataitem.h:352
bool hasDragEnabled() const override
Returns true if the item may be dragged.
Definition: qgsdataitem.h:642
Represents a favorite item.
Definition: qgsdataitem.h:82
void setIconName(const QString &iconName)
Definition: qgsdataitem.h:321
static QString vsiPrefix(const QString &uri)
Definition: qgsdataitem.h:754
QMap< QString, QIcon > mIconMap
Definition: qgsdataitem.h:363
Added in 2.10.
Definition: qgsdataitem.h:451
#define SIP_PYNAME(name)
Definition: qgis_sip.h:74