QGIS API Documentation  2.99.0-Master (cd0ba91)
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  virtual ~QgsDataItem();
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  QString name() const { return mName; }
252  void setName( const QString &name ) { mName = name; }
253  QString path() const { return mPath; }
254  void setPath( const QString &path ) { mPath = path; }
256  static QString pathComponent( const QString &component );
257 
258  // Because QIcon (QPixmap) must not be used in outside the GUI thread, it is
259  // not possible to set mIcon in constructor. Either use mIconName/setIconName()
260  // or implement icon().
261  void setIcon( const QIcon &icon ) { mIcon = icon; }
262  void setIconName( const QString &iconName ) { mIconName = iconName; }
263 
264  void setToolTip( const QString &msg ) { mToolTip = msg; }
265  QString toolTip() const { return mToolTip; }
266 
267  // deleteLater() items anc clear the vector
268  static void deleteLater( QVector<QgsDataItem *> &items );
269 
271  void moveToThread( QThread *targetThread );
272 
273  protected:
274  virtual void populate( const QVector<QgsDataItem *> &children );
275 
279  virtual void refresh( const QVector<QgsDataItem *> &children );
280 
288  bool deferredDelete() { return mDeferredDelete; }
289 
291  Capabilities mCapabilities;
292  QgsDataItem *mParent = nullptr;
293  QVector<QgsDataItem *> mChildren; // easier to have it always
295  QString mName;
296  // Path is slash ('/') separated chain of item identifiers which are usually item names, but may be differen if it is
297  // necessary to distinguish paths of two providers to the same source (e.g GRASS location and standard directory have the same
298  // name but different paths). Identifiers in path must not contain '/' characters.
299  // The path is used to identify item in tree.
300  QString mPath;
301  QString mToolTip;
302  QString mIconName;
303  QIcon mIcon;
304  QMap<QString, QIcon> mIconMap;
305 
306  public slots:
307 
316  virtual void deleteLater();
317 
318  // Populate children using children vector created by createChildren()
319  // \param foreground run createChildren in foreground
320  virtual void populate( bool foreground = false );
321 
323  virtual void depopulate();
324 
325  virtual void refresh();
326 
328  virtual void refreshConnections();
329 
330  virtual void childrenCreated();
331 
332  signals:
333  void beginInsertItems( QgsDataItem *parent, int first, int last );
334  void endInsertItems();
335  void beginRemoveItems( QgsDataItem *parent, int first, int last );
336  void endRemoveItems();
337  void dataChanged( QgsDataItem *item );
338  void stateChanged( QgsDataItem *item, QgsDataItem::State oldState );
339 
346  void connectionsChanged();
347 
348  protected slots:
349 
355  void updateIcon();
356 
357  private:
358  static QVector<QgsDataItem *> runCreateChildren( QgsDataItem *item );
359 
360  // Set to true if object has to be deleted when possible (nothing running in threads)
361  bool mDeferredDelete;
362  QFutureWatcher< QVector <QgsDataItem *> > *mFutureWatcher;
363  // number of items currently in loading (populating) state
364  static QgsAnimatedIcon *sPopulatingIcon;
365 };
366 
367 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsDataItem::Capabilities )
368 
369 
373 class CORE_EXPORT QgsLayerItem : public QgsDataItem
374 {
375  Q_OBJECT
376 
377  public:
379  {
389  Plugin
390  };
391 
392  Q_ENUM( LayerType );
393 
394  QgsLayerItem( QgsDataItem *parent, const QString &name, const QString &path, const QString &uri, LayerType layerType, const QString &providerKey );
395 
396  // --- reimplemented from QgsDataItem ---
397 
398  virtual bool equal( const QgsDataItem *other ) override;
399 
400  virtual bool hasDragEnabled() const override { return true; }
401 
402  virtual QgsMimeDataUtils::Uri mimeUri() const override;
403 
404  // --- New virtual methods for layer item derived classes ---
405 
407  QgsMapLayer::LayerType mapLayerType() const;
408 
410  QString uri() const { return mUri; }
411 
413  QString providerKey() const { return mProviderKey; }
414 
419  QStringList supportedCrs() const { return mSupportedCRS; }
420 
425  QStringList supportedFormats() const { return mSupportFormats; }
426 
431  virtual QString comments() const { return QString(); }
432 
437  static QString layerTypeAsString( const LayerType &layerType );
438 
443  static QString iconName( const LayerType &layerType );
444 
445  protected:
446 
448  QString mProviderKey;
450  QString mUri;
454  QStringList mSupportedCRS;
456  QStringList mSupportFormats;
457 
458  public:
459  static QIcon iconPoint();
460  static QIcon iconLine();
461  static QIcon iconPolygon();
462  static QIcon iconTable();
463  static QIcon iconRaster();
464  static QIcon iconDefault();
465 
467  virtual QString layerName() const { return name(); }
468 };
469 
470 
475 class CORE_EXPORT QgsDataCollectionItem : public QgsDataItem
476 {
477  Q_OBJECT
478  public:
479  QgsDataCollectionItem( QgsDataItem *parent, const QString &name, const QString &path = QString() );
481 
482  void addChild( QgsDataItem *item SIP_TRANSFER ) { mChildren.append( item ); }
483 
484  static QIcon iconDir(); // shared icon: open/closed directory
485  static QIcon iconDataCollection(); // default icon for data collection
486 };
487 
492 class CORE_EXPORT QgsDirectoryItem : public QgsDataCollectionItem
493 {
494  Q_OBJECT
495  public:
496  enum Column
497  {
505  };
506 
507  QgsDirectoryItem( QgsDataItem *parent, const QString &name, const QString &path );
508 
515  QgsDirectoryItem( QgsDataItem *parent, const QString &name, const QString &dirPath, const QString &path );
516 
517  virtual void setState( State state ) override;
518 
519  QVector<QgsDataItem *> createChildren() override;
520 
521  QString dirPath() const { return mDirPath; }
522  virtual bool equal( const QgsDataItem *other ) override;
523  virtual QIcon icon() override;
524  virtual QWidget *paramWidget() override SIP_FACTORY;
525 
527  static bool hiddenPath( const QString &path );
528 
529  QList<QAction *> actions( QWidget *parent ) override;
530 
531 
532  public slots:
533  virtual void childrenCreated() override;
534  void directoryChanged();
535 
536  protected:
537  void init();
538  QString mDirPath;
539 
540  private:
541  QFileSystemWatcher *mFileSystemWatcher = nullptr;
542  bool mRefreshLater;
543  QDateTime mLastScan;
544 };
545 
550 class CORE_EXPORT QgsProjectItem : public QgsDataItem
551 {
552  Q_OBJECT
553  public:
554 
561  QgsProjectItem( QgsDataItem *parent, const QString &name, const QString &path );
562 
563  virtual bool hasDragEnabled() const override { return true; }
564 
565 };
566 
571 class CORE_EXPORT QgsErrorItem : public QgsDataItem
572 {
573  Q_OBJECT
574  public:
575 
576  QgsErrorItem( QgsDataItem *parent, const QString &error, const QString &path );
577 
578 };
579 
580 
581 // ---------
582 
587 class CORE_EXPORT QgsDirectoryParamWidget : public QTreeWidget
588 {
589  Q_OBJECT
590 
591  public:
592  QgsDirectoryParamWidget( const QString &path, QWidget *parent SIP_TRANSFERTHIS = nullptr );
593 
594  protected:
595  void mousePressEvent( QMouseEvent *event ) override;
596 
597  public slots:
598  void showHideColumn();
599 };
600 
606 class CORE_EXPORT QgsFavoritesItem : public QgsDataCollectionItem
607 {
608  Q_OBJECT
609  public:
610 
615  QgsFavoritesItem( QgsDataItem *parent, const QString &name, const QString &path = QString() );
616 
617  QVector<QgsDataItem *> createChildren() override;
618 
623  void addDirectory( const QString &directory );
624 
629  void removeDirectory( QgsDirectoryItem *item );
630 
632  static QIcon iconFavorites();
633 
634  private:
635  QVector<QgsDataItem *> createChildren( const QString &favDir );
636 };
637 
642 class CORE_EXPORT QgsZipItem : public QgsDataCollectionItem
643 {
644  Q_OBJECT
645 
646  protected:
647  QString mFilePath;
648  QString mVsiPrefix;
649  QStringList mZipFileList;
650 
651  public:
652  QgsZipItem( QgsDataItem *parent, const QString &name, const QString &path );
653  QgsZipItem( QgsDataItem *parent, const QString &name, const QString &filePath, const QString &path );
654 
655  QVector<QgsDataItem *> createChildren() override;
656  QStringList getZipFileList();
657 
659  static QVector<dataItem_t *> sDataItemPtr SIP_SKIP;
660  static QStringList sProviderNames;
661 
662  static QString vsiPrefix( const QString &uri ) { return qgsVsiPrefix( uri ); }
663 
667  static QgsDataItem *itemFromPath( QgsDataItem *parent, const QString &path, const QString &name ) SIP_FACTORY;
668 
673  static QgsDataItem *itemFromPath( QgsDataItem *parent, const QString &filePath, const QString &name, const QString &path ) SIP_FACTORY SIP_PYNAME( itemFromFilePath );
674 
675  static QIcon iconZip();
676 
677  private:
678  void init();
679 };
680 
681 #endif // QGSDATAITEM_H
682 
683 
A Collection: logical collection of layers or subcollections, e.g.
Definition: qgsdataitem.h:475
QStringList mSupportFormats
The list of supported formats.
Definition: qgsdataitem.h:456
QString path() const
Definition: qgsdataitem.h:253
virtual void childrenCreated()
virtual QString layerName() const
Definition: qgsdataitem.h:467
virtual bool hasDragEnabled() const override
Returns true if the item may be dragged.
Definition: qgsdataitem.h:563
QString providerKey() const
Returns provider key.
Definition: qgsdataitem.h:413
QString name() const
Definition: qgsdataitem.h:251
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
QString toolTip() const
Definition: qgsdataitem.h:265
QString mProviderKey
The provider key.
Definition: qgsdataitem.h:448
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:482
QString mIconName
Definition: qgsdataitem.h:302
State mState
Definition: qgsdataitem.h:294
Type type() const
Definition: qgsdataitem.h:238
void setIcon(const QIcon &icon)
Definition: qgsdataitem.h:261
Abstract base class for spatial data provider implementations.
void setToolTip(const QString &msg)
Definition: qgsdataitem.h:264
#define SIP_TRANSFERBACK
Definition: qgis_sip.h:41
virtual QWidget * paramWidget()
Definition: qgsdataitem.h:147
QString mToolTip
Definition: qgsdataitem.h:301
A zip file: contains layers, using GDAL/OGR VSIFILE mechanism.
Definition: qgsdataitem.h:642
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:454
QString mFilePath
Definition: qgsdataitem.h:647
QgsDataItem * parent() const
Get item parent.
Definition: qgsdataitem.h:243
QString mName
Definition: qgsdataitem.h:295
#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:649
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:571
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
A directory: contains subdirectories and layers.
Definition: qgsdataitem.h:492
static QStringList sProviderNames
Definition: qgsdataitem.h:660
Base class for all items in the model.
Definition: qgsdataitem.h:49
Capabilities mCapabilities
Definition: qgsdataitem.h:291
virtual void setState(State state)
Set item state.
void setName(const QString &name)
Definition: qgsdataitem.h:252
QVector< QgsDataItem * > children() const
Definition: qgsdataitem.h:249
QString mPath
Definition: qgsdataitem.h:300
QString mUri
The URI.
Definition: qgsdataitem.h:450
Contains various Favorites directories.
Definition: qgsdataitem.h:606
QString mVsiPrefix
Definition: qgsdataitem.h:648
QStringList supportedFormats() const
Returns the supported formats.
Definition: qgsdataitem.h:425
QString qgsVsiPrefix(const QString &path)
Definition: qgis.cpp:215
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:410
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:288
QString dirPath() const
Definition: qgsdataitem.h:521
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:373
QgsDataItem * dataItem_t(QString, QgsDataItem *)
Definition: qgsdataitem.h:42
virtual bool hasDragEnabled() const override
Returns true if the item may be dragged.
Definition: qgsdataitem.h:400
Data item that can be used to represent QGIS projects.
Definition: qgsdataitem.h:550
void setPath(const QString &path)
Definition: qgsdataitem.h:254
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:452
QVector< QgsDataItem * > mChildren
Definition: qgsdataitem.h:293
virtual QString comments() const
Returns comments of the layer.
Definition: qgsdataitem.h:431
Represents a favorite item.
Definition: qgsdataitem.h:82
QStringList supportedCrs() const
Returns the supported CRS.
Definition: qgsdataitem.h:419
void setIconName(const QString &iconName)
Definition: qgsdataitem.h:262
static QString vsiPrefix(const QString &uri)
Definition: qgsdataitem.h:662
QMap< QString, QIcon > mIconMap
Definition: qgsdataitem.h:304
virtual Capabilities capabilities2() const
Definition: qgsdataitem.h:224
#define SIP_PYNAME(name)
Definition: qgis_sip.h:74