QGIS API Documentation  3.0.2-Girona (307d082)
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 
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 
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 anc 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  {
425  Plugin
426  };
427 
428  Q_ENUM( LayerType );
429 
430  QgsLayerItem( QgsDataItem *parent, const QString &name, const QString &path, const QString &uri, LayerType layerType, const QString &providerKey );
431 
432  // --- reimplemented from QgsDataItem ---
433 
434  bool equal( const QgsDataItem *other ) override;
435 
436  bool hasDragEnabled() const override { return true; }
437 
438  QgsMimeDataUtils::Uri mimeUri() const override;
439 
440  // --- New virtual methods for layer item derived classes ---
441 
443  QgsMapLayer::LayerType mapLayerType() const;
444 
446  QString uri() const { return mUri; }
447 
449  QString providerKey() const { return mProviderKey; }
450 
455  QStringList supportedCrs() const { return mSupportedCRS; }
456 
461  QStringList supportedFormats() const { return mSupportFormats; }
462 
467  virtual QString comments() const { return QString(); }
468 
473  static QString layerTypeAsString( LayerType layerType );
474 
479  static QString iconName( LayerType layerType );
480 
481  protected:
482 
484  QString mProviderKey;
486  QString mUri;
490  QStringList mSupportedCRS;
492  QStringList mSupportFormats;
493 
494  public:
495  static QIcon iconPoint();
496  static QIcon iconLine();
497  static QIcon iconPolygon();
498  static QIcon iconTable();
499  static QIcon iconRaster();
500  static QIcon iconDefault();
501 
503  virtual QString layerName() const { return name(); }
504 };
505 
506 
511 class CORE_EXPORT QgsDataCollectionItem : public QgsDataItem
512 {
513  Q_OBJECT
514  public:
515  QgsDataCollectionItem( QgsDataItem *parent, const QString &name, const QString &path = QString() );
516  ~QgsDataCollectionItem() override;
517 
518  void addChild( QgsDataItem *item SIP_TRANSFER ) { mChildren.append( item ); }
519 
520  static QIcon iconDir(); // shared icon: open/closed directory
521  static QIcon iconDataCollection(); // default icon for data collection
522 };
523 
528 class CORE_EXPORT QgsDirectoryItem : public QgsDataCollectionItem
529 {
530  Q_OBJECT
531  public:
532  enum Column
533  {
541  };
542 
543  QgsDirectoryItem( QgsDataItem *parent, const QString &name, const QString &path );
544 
551  QgsDirectoryItem( QgsDataItem *parent, const QString &name, const QString &dirPath, const QString &path );
552 
553  void setState( State state ) override;
554 
555  QVector<QgsDataItem *> createChildren() override;
556 
557  QString dirPath() const { return mDirPath; }
558  bool equal( const QgsDataItem *other ) override;
559  QIcon icon() override;
560  QWidget *paramWidget() override SIP_FACTORY;
561 
563  static bool hiddenPath( const QString &path );
564 
565  QList<QAction *> actions( QWidget *parent ) override;
566 
567 
568  public slots:
569  void childrenCreated() override;
570  void directoryChanged();
571 
572  protected:
573  void init();
574  QString mDirPath;
575 
576  private:
577  QFileSystemWatcher *mFileSystemWatcher = nullptr;
578  bool mRefreshLater;
579  QDateTime mLastScan;
580 };
581 
586 class CORE_EXPORT QgsProjectItem : public QgsDataItem
587 {
588  Q_OBJECT
589  public:
590 
597  QgsProjectItem( QgsDataItem *parent, const QString &name, const QString &path );
598 
599  bool hasDragEnabled() const override { return true; }
600 
601 };
602 
607 class CORE_EXPORT QgsErrorItem : public QgsDataItem
608 {
609  Q_OBJECT
610  public:
611 
612  QgsErrorItem( QgsDataItem *parent, const QString &error, const QString &path );
613 
614 };
615 
616 
617 // ---------
618 
623 class CORE_EXPORT QgsDirectoryParamWidget : public QTreeWidget
624 {
625  Q_OBJECT
626 
627  public:
628  QgsDirectoryParamWidget( const QString &path, QWidget *parent SIP_TRANSFERTHIS = nullptr );
629 
630  protected:
631  void mousePressEvent( QMouseEvent *event ) override;
632 
633  public slots:
634  void showHideColumn();
635 };
636 
642 class CORE_EXPORT QgsFavoritesItem : public QgsDataCollectionItem
643 {
644  Q_OBJECT
645  public:
646 
651  QgsFavoritesItem( QgsDataItem *parent, const QString &name, const QString &path = QString() );
652 
653  QVector<QgsDataItem *> createChildren() override;
654 
663  void addDirectory( const QString &directory, const QString &name = QString() );
664 
669  void removeDirectory( QgsDirectoryItem *item );
670 
674  void renameFavorite( const QString &path, const QString &name );
675 
677  static QIcon iconFavorites();
678 
679  QVariant sortKey() const override;
680 
681  private:
682  QVector<QgsDataItem *> createChildren( const QString &favDir, const QString &name );
683 };
684 
689 class CORE_EXPORT QgsZipItem : public QgsDataCollectionItem
690 {
691  Q_OBJECT
692 
693  protected:
694  QString mFilePath;
695  QString mVsiPrefix;
696  QStringList mZipFileList;
697 
698  public:
699  QgsZipItem( QgsDataItem *parent, const QString &name, const QString &path );
700  QgsZipItem( QgsDataItem *parent, const QString &name, const QString &filePath, const QString &path );
701 
702  QVector<QgsDataItem *> createChildren() override;
703  QStringList getZipFileList();
704 
706  static QVector<dataItem_t *> sDataItemPtr SIP_SKIP;
707  static QStringList sProviderNames;
708 
709  static QString vsiPrefix( const QString &uri ) { return qgsVsiPrefix( uri ); }
710 
714  static QgsDataItem *itemFromPath( QgsDataItem *parent, const QString &path, const QString &name ) SIP_FACTORY;
715 
720  static QgsDataItem *itemFromPath( QgsDataItem *parent, const QString &filePath, const QString &name, const QString &path ) SIP_FACTORY SIP_PYNAME( itemFromFilePath );
721 
722  static QIcon iconZip();
723 
724  private:
725  void init();
726 };
727 
728 
730 #ifndef SIP_RUN
731 
737 class CORE_EXPORT QgsProjectHomeItem : public QgsDirectoryItem
738 {
739  Q_OBJECT
740 
741  public:
742 
743  QgsProjectHomeItem( QgsDataItem *parent, const QString &name, const QString &dirPath, const QString &path );
744 
745  QIcon icon() override;
746  QVariant sortKey() const override;
747 
748 };
749 
755 class CORE_EXPORT QgsFavoriteItem : public QgsDirectoryItem
756 {
757  Q_OBJECT
758 
759  public:
760 
761  QgsFavoriteItem( QgsFavoritesItem *parent, const QString &name, const QString &dirPath, const QString &path );
762 
766  void rename( const QString &name );
767 
768  private:
769 
770  QgsFavoritesItem *mFavorites = nullptr;
771 };
772 
773 #endif
774 
776 #endif // QGSDATAITEM_H
777 
778 
A Collection: logical collection of layers or subcollections, e.g.
Definition: qgsdataitem.h:511
virtual QVariant sortKey() const
Returns the sorting key for the item.
QStringList mSupportFormats
The list of supported formats.
Definition: qgsdataitem.h:492
QString path() const
Definition: qgsdataitem.h:266
virtual void childrenCreated()
virtual QString layerName() const
Definition: qgsdataitem.h:503
QString providerKey() const
Returns provider key.
Definition: qgsdataitem.h:449
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:484
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:518
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:689
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:490
QString mFilePath
Definition: qgsdataitem.h:694
QgsDataItem * parent() const
Get 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:94
QStringList mZipFileList
Definition: qgsdataitem.h:696
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:175
Data item that can be used to report problems (e.g.
Definition: qgsdataitem.h:607
virtual bool hasDragEnabled() const
Returns true if the item may be dragged.
Definition: qgsdataitem.h:197
virtual QgsMimeDataUtils::Uri mimeUri() const
Return 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:528
static QStringList sProviderNames
Definition: qgsdataitem.h:707
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:486
bool hasDragEnabled() const override
Returns true if the item may be dragged.
Definition: qgsdataitem.h:436
Contains various Favorites directories.
Definition: qgsdataitem.h:642
QString mVsiPrefix
Definition: qgsdataitem.h:695
QStringList supportedFormats() const
Returns the supported formats.
Definition: qgsdataitem.h:461
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:446
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:557
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
Data item that can be used to represent QGIS projects.
Definition: qgsdataitem.h:586
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:488
QVector< QgsDataItem * > mChildren
Definition: qgsdataitem.h:326
virtual QString comments() const
Returns comments of the layer.
Definition: qgsdataitem.h:467
bool hasDragEnabled() const override
Returns true if the item may be dragged.
Definition: qgsdataitem.h:599
Represents a favorite item.
Definition: qgsdataitem.h:82
QStringList supportedCrs() const
Returns the supported CRS.
Definition: qgsdataitem.h:455
void setIconName(const QString &iconName)
Definition: qgsdataitem.h:295
static QString vsiPrefix(const QString &uri)
Definition: qgsdataitem.h:709
QMap< QString, QIcon > mIconMap
Definition: qgsdataitem.h:337
virtual Capabilities capabilities2() const
Definition: qgsdataitem.h:224
#define SIP_PYNAME(name)
Definition: qgis_sip.h:74