QGIS API Documentation  3.2.0-Bonn (bc43194)
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 
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  };
215  Q_DECLARE_FLAGS( Capabilities, Capability )
216 
217 
221  virtual bool setCrs( const QgsCoordinateReferenceSystem &crs ) { Q_UNUSED( crs ); return false; }
222 
223  // ### QGIS 3 - rename to capabilities()
224  virtual Capabilities capabilities2() const { return mCapabilities; }
225 
229  virtual void setCapabilities( Capabilities capabilities ) { mCapabilities = capabilities; }
230 
231  // static methods
232 
233  // Find child index in vector of items using '==' operator
234  static int findItem( QVector<QgsDataItem *> items, QgsDataItem *item );
235 
236  // members
237 
238  Type type() const { return mType; }
239 
243  QgsDataItem *parent() const { return mParent; }
244 
248  void setParent( QgsDataItem *parent );
249  QVector<QgsDataItem *> children() const { return mChildren; }
250  virtual QIcon icon();
251 
257  QString name() const { return mName; }
258 
264  void setName( const QString &name );
265 
266  QString path() const { return mPath; }
267  void setPath( const QString &path ) { mPath = path; }
269  static QString pathComponent( const QString &component );
270 
281  virtual QVariant sortKey() const;
282 
288  void setSortKey( const QVariant &key );
289 
290 
291  // Because QIcon (QPixmap) must not be used in outside the GUI thread, it is
292  // not possible to set mIcon in constructor. Either use mIconName/setIconName()
293  // or implement icon().
294  void setIcon( const QIcon &icon ) { mIcon = icon; }
295  void setIconName( const QString &iconName ) { mIconName = iconName; }
296 
297  void setToolTip( const QString &msg ) { mToolTip = msg; }
298  QString toolTip() const { return mToolTip; }
299 
300  // deleteLater() items and clear the vector
301  static void deleteLater( QVector<QgsDataItem *> &items );
302 
304  void moveToThread( QThread *targetThread );
305 
306  protected:
307  virtual void populate( const QVector<QgsDataItem *> &children );
308 
312  virtual void refresh( const QVector<QgsDataItem *> &children );
313 
321  bool deferredDelete() { return mDeferredDelete; }
322 
324  Capabilities mCapabilities;
325  QgsDataItem *mParent = nullptr;
326  QVector<QgsDataItem *> mChildren; // easier to have it always
328  QString mName;
329  // Path is slash ('/') separated chain of item identifiers which are usually item names, but may be differen if it is
330  // necessary to distinguish paths of two providers to the same source (e.g GRASS location and standard directory have the same
331  // name but different paths). Identifiers in path must not contain '/' characters.
332  // The path is used to identify item in tree.
333  QString mPath;
334  QString mToolTip;
335  QString mIconName;
336  QIcon mIcon;
337  QMap<QString, QIcon> mIconMap;
338 
340  QVariant mSortKey;
341 
342  public slots:
343 
352  virtual void deleteLater();
353 
354  // Populate children using children vector created by createChildren()
355  // \param foreground run createChildren in foreground
356  virtual void populate( bool foreground = false );
357 
359  virtual void depopulate();
360 
361  virtual void refresh();
362 
364  virtual void refreshConnections();
365 
366  virtual void childrenCreated();
367 
368  signals:
369  void beginInsertItems( QgsDataItem *parent, int first, int last );
370  void endInsertItems();
371  void beginRemoveItems( QgsDataItem *parent, int first, int last );
372  void endRemoveItems();
373  void dataChanged( QgsDataItem *item );
374  void stateChanged( QgsDataItem *item, QgsDataItem::State oldState );
375 
382  void connectionsChanged();
383 
384  protected slots:
385 
391  void updateIcon();
392 
393  private:
394  static QVector<QgsDataItem *> runCreateChildren( QgsDataItem *item );
395 
396  // Set to true if object has to be deleted when possible (nothing running in threads)
397  bool mDeferredDelete;
398  QFutureWatcher< QVector <QgsDataItem *> > *mFutureWatcher;
399  // number of items currently in loading (populating) state
400  static QgsAnimatedIcon *sPopulatingIcon;
401 };
402 
403 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsDataItem::Capabilities )
404 
405 
409 class CORE_EXPORT QgsLayerItem : public QgsDataItem
410 {
411  Q_OBJECT
412 
413  public:
415  {
426  Mesh
427  };
428 
429  Q_ENUM( LayerType )
430 
431  QgsLayerItem( QgsDataItem *parent, const QString &name, const QString &path, const QString &uri, LayerType layerType, const QString &providerKey );
432 
433  // --- reimplemented from QgsDataItem ---
434 
435  bool equal( const QgsDataItem *other ) override;
436 
437  bool hasDragEnabled() const override { return true; }
438 
439  QgsMimeDataUtils::Uri mimeUri() const override;
440 
441  // --- New virtual methods for layer item derived classes ---
442 
444  QgsMapLayer::LayerType mapLayerType() const;
445 
447  QString uri() const { return mUri; }
448 
450  QString providerKey() const { return mProviderKey; }
451 
456  QStringList supportedCrs() const { return mSupportedCRS; }
457 
462  QStringList supportedFormats() const { return mSupportFormats; }
463 
468  virtual QString comments() const { return QString(); }
469 
474  static QString layerTypeAsString( LayerType layerType );
475 
480  static QString iconName( LayerType layerType );
481 
482  protected:
483 
485  QString mProviderKey;
487  QString mUri;
491  QStringList mSupportedCRS;
493  QStringList mSupportFormats;
494 
495  public:
496  static QIcon iconPoint();
497  static QIcon iconLine();
498  static QIcon iconPolygon();
499  static QIcon iconTable();
500  static QIcon iconRaster();
501  static QIcon iconDefault();
503  static QIcon iconMesh();
504 
506  virtual QString layerName() const { return name(); }
507 };
508 
509 
514 class CORE_EXPORT QgsDataCollectionItem : public QgsDataItem
515 {
516  Q_OBJECT
517  public:
518  QgsDataCollectionItem( QgsDataItem *parent, const QString &name, const QString &path = QString() );
519  ~QgsDataCollectionItem() override;
520 
521  void addChild( QgsDataItem *item SIP_TRANSFER ) { mChildren.append( item ); }
522 
523  static QIcon iconDir(); // shared icon: open/closed directory
524  static QIcon iconDataCollection(); // default icon for data collection
525 };
526 
531 class CORE_EXPORT QgsDirectoryItem : public QgsDataCollectionItem
532 {
533  Q_OBJECT
534  public:
535  enum Column
536  {
544  };
545 
546  QgsDirectoryItem( QgsDataItem *parent, const QString &name, const QString &path );
547 
554  QgsDirectoryItem( QgsDataItem *parent, const QString &name, const QString &dirPath, const QString &path );
555 
556  void setState( State state ) override;
557 
558  QVector<QgsDataItem *> createChildren() override;
559 
560  QString dirPath() const { return mDirPath; }
561  bool equal( const QgsDataItem *other ) override;
562  QIcon icon() override;
563  QWidget *paramWidget() override SIP_FACTORY;
564 
566  static bool hiddenPath( const QString &path );
567 
568  QList<QAction *> actions( QWidget *parent ) override;
569 
570 
571  public slots:
572  void childrenCreated() override;
573  void directoryChanged();
574 
575  protected:
576  void init();
577  QString mDirPath;
578 
579  private:
580  QFileSystemWatcher *mFileSystemWatcher = nullptr;
581  bool mRefreshLater;
582  QDateTime mLastScan;
583 };
584 
589 class CORE_EXPORT QgsProjectItem : public QgsDataItem
590 {
591  Q_OBJECT
592  public:
593 
600  QgsProjectItem( QgsDataItem *parent, const QString &name, const QString &path );
601 
602  bool hasDragEnabled() const override { return true; }
603 
604  QgsMimeDataUtils::Uri mimeUri() const override;
605 
606 };
607 
612 class CORE_EXPORT QgsErrorItem : public QgsDataItem
613 {
614  Q_OBJECT
615  public:
616 
617  QgsErrorItem( QgsDataItem *parent, const QString &error, const QString &path );
618 
619 };
620 
621 
622 // ---------
623 
628 class CORE_EXPORT QgsDirectoryParamWidget : public QTreeWidget
629 {
630  Q_OBJECT
631 
632  public:
633  QgsDirectoryParamWidget( const QString &path, QWidget *parent SIP_TRANSFERTHIS = nullptr );
634 
635  protected:
636  void mousePressEvent( QMouseEvent *event ) override;
637 
638  public slots:
639  void showHideColumn();
640 };
641 
647 class CORE_EXPORT QgsFavoritesItem : public QgsDataCollectionItem
648 {
649  Q_OBJECT
650  public:
651 
656  QgsFavoritesItem( QgsDataItem *parent, const QString &name, const QString &path = QString() );
657 
658  QVector<QgsDataItem *> createChildren() override;
659 
668  void addDirectory( const QString &directory, const QString &name = QString() );
669 
674  void removeDirectory( QgsDirectoryItem *item );
675 
679  void renameFavorite( const QString &path, const QString &name );
680 
682  static QIcon iconFavorites();
683 
684  QVariant sortKey() const override;
685 
686  private:
687  QVector<QgsDataItem *> createChildren( const QString &favDir, const QString &name );
688 };
689 
694 class CORE_EXPORT QgsZipItem : public QgsDataCollectionItem
695 {
696  Q_OBJECT
697 
698  protected:
699  QString mFilePath;
700  QString mVsiPrefix;
701  QStringList mZipFileList;
702 
703  public:
704  QgsZipItem( QgsDataItem *parent, const QString &name, const QString &path );
705  QgsZipItem( QgsDataItem *parent, const QString &name, const QString &filePath, const QString &path );
706 
707  QVector<QgsDataItem *> createChildren() override;
708  QStringList getZipFileList();
709 
711  static QVector<dataItem_t *> sDataItemPtr SIP_SKIP;
712  static QStringList sProviderNames;
713 
714  static QString vsiPrefix( const QString &uri ) { return qgsVsiPrefix( uri ); }
715 
719  static QgsDataItem *itemFromPath( QgsDataItem *parent, const QString &path, const QString &name ) SIP_FACTORY;
720 
725  static QgsDataItem *itemFromPath( QgsDataItem *parent, const QString &filePath, const QString &name, const QString &path ) SIP_FACTORY SIP_PYNAME( itemFromFilePath );
726 
727  static QIcon iconZip();
728 
729  private:
730  void init();
731 };
732 
733 
735 #ifndef SIP_RUN
736 
742 class CORE_EXPORT QgsProjectHomeItem : public QgsDirectoryItem
743 {
744  Q_OBJECT
745 
746  public:
747 
748  QgsProjectHomeItem( QgsDataItem *parent, const QString &name, const QString &dirPath, const QString &path );
749 
750  QIcon icon() override;
751  QVariant sortKey() const override;
752 
753  QList<QAction *> actions( QWidget *parent ) override;
754 
755 
756 };
757 
763 class CORE_EXPORT QgsFavoriteItem : public QgsDirectoryItem
764 {
765  Q_OBJECT
766 
767  public:
768 
769  QgsFavoriteItem( QgsFavoritesItem *parent, const QString &name, const QString &dirPath, const QString &path );
770 
774  void rename( const QString &name );
775 
776  private:
777 
778  QgsFavoritesItem *mFavorites = nullptr;
779 };
780 
781 #endif
782 
784 #endif // QGSDATAITEM_H
785 
786 
A Collection: logical collection of layers or subcollections, e.g.
Definition: qgsdataitem.h:514
virtual QVariant sortKey() const
Returns the sorting key for the item.
QStringList mSupportFormats
The list of supported formats.
Definition: qgsdataitem.h:493
QString path() const
Definition: qgsdataitem.h:266
virtual void childrenCreated()
virtual QString layerName() const
Definition: qgsdataitem.h:506
QString providerKey() const
Returns provider key.
Definition: qgsdataitem.h:450
QString name() const
Returns the name of the item (the displayed text for the item).
Definition: qgsdataitem.h:257
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
QString toolTip() const
Definition: qgsdataitem.h:298
QVariant mSortKey
Custom sort key. If invalid, name() will be used for sorting instead.
Definition: qgsdataitem.h:340
QString mProviderKey
The provider key.
Definition: qgsdataitem.h:485
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:521
QString mIconName
Definition: qgsdataitem.h:335
State mState
Definition: qgsdataitem.h:327
Type type() const
Definition: qgsdataitem.h:238
void setIcon(const QIcon &icon)
Definition: qgsdataitem.h:294
Abstract base class for spatial data provider implementations.
void setToolTip(const QString &msg)
Definition: qgsdataitem.h:297
#define SIP_TRANSFERBACK
Definition: qgis_sip.h:41
virtual QWidget * paramWidget()
Definition: qgsdataitem.h:147
QString mToolTip
Definition: qgsdataitem.h:334
A zip file: contains layers, using GDAL/OGR VSIFILE mechanism.
Definition: qgsdataitem.h:694
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:491
QString mFilePath
Definition: qgsdataitem.h:699
QgsDataItem * parent() const
Gets item parent.
Definition: qgsdataitem.h:243
QString mName
Definition: qgsdataitem.h:328
#define SIP_SKIP
Definition: qgis_sip.h:119
LayerType
Types of layers that can be added to a map.
Definition: qgsmaplayer.h:102
QStringList mZipFileList
Definition: qgsdataitem.h:701
virtual void setCapabilities(Capabilities capabilities)
Sets the capabilities for the data item.
Definition: qgsdataitem.h:229
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:612
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
Custom item type.
Definition: qgsdataitem.h:84
QIcon icon() override
A directory: contains subdirectories and layers.
Definition: qgsdataitem.h:531
static QStringList sProviderNames
Definition: qgsdataitem.h:712
Base class for all items in the model.
Definition: qgsdataitem.h:49
Capabilities mCapabilities
Definition: qgsdataitem.h:324
virtual void setState(State state)
Set item state.
QVector< QgsDataItem * > children() const
Definition: qgsdataitem.h:249
QString mPath
Definition: qgsdataitem.h:333
QString mUri
The URI.
Definition: qgsdataitem.h:487
bool hasDragEnabled() const override
Returns true if the item may be dragged.
Definition: qgsdataitem.h:437
Contains various Favorites directories.
Definition: qgsdataitem.h:647
QString mVsiPrefix
Definition: qgsdataitem.h:700
QStringList supportedFormats() const
Returns the supported formats.
Definition: qgsdataitem.h:462
QString qgsVsiPrefix(const QString &path)
Definition: qgis.cpp:219
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:447
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:321
QString dirPath() const
Definition: qgsdataitem.h:560
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:409
QgsDataItem * dataItem_t(QString, QgsDataItem *)
Definition: qgsdataitem.h:42
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:589
void setPath(const QString &path)
Definition: qgsdataitem.h:267
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:489
QVector< QgsDataItem * > mChildren
Definition: qgsdataitem.h:326
virtual QString comments() const
Returns comments of the layer.
Definition: qgsdataitem.h:468
bool hasDragEnabled() const override
Returns true if the item may be dragged.
Definition: qgsdataitem.h:602
Represents a favorite item.
Definition: qgsdataitem.h:82
QStringList supportedCrs() const
Returns the supported CRS.
Definition: qgsdataitem.h:456
void setIconName(const QString &iconName)
Definition: qgsdataitem.h:295
static QString vsiPrefix(const QString &uri)
Definition: qgsdataitem.h:714
QMap< QString, QIcon > mIconMap
Definition: qgsdataitem.h:337
Added in 2.10.
Definition: qgsdataitem.h:425
virtual Capabilities capabilities2() const
Definition: qgsdataitem.h:224
#define SIP_PYNAME(name)
Definition: qgis_sip.h:74