QGIS API Documentation  2.99.0-Master (53aba61)
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 
48 class CORE_EXPORT QgsDataItem : public QObject
49 {
50 
51 #ifdef SIP_RUN
53  if ( qobject_cast<QgsLayerItem *>( sipCpp ) )
54  sipType = sipType_QgsLayerItem;
55  else if ( qobject_cast<QgsErrorItem *>( sipCpp ) )
56  sipType = sipType_QgsErrorItem;
57  else if ( qobject_cast<QgsDirectoryItem *>( sipCpp ) )
58  sipType = sipType_QgsDirectoryItem;
59  else if ( qobject_cast<QgsFavoritesItem *>( sipCpp ) )
60  sipType = sipType_QgsFavoritesItem;
61  else if ( qobject_cast<QgsZipItem *>( sipCpp ) )
62  sipType = sipType_QgsZipItem;
63  else if ( qobject_cast<QgsDataCollectionItem *>( sipCpp ) )
64  sipType = sipType_QgsDataCollectionItem;
65  else if ( qobject_cast<QgsProjectItem *>( sipCpp ) )
66  sipType = sipType_QgsProjectItem;
67  else
68  sipType = 0;
69  SIP_END
70 #endif
71 
72  Q_OBJECT
73 
74  public:
75  enum Type
76  {
84  };
85 
86  Q_ENUM( Type );
87 
89  QgsDataItem( QgsDataItem::Type type, QgsDataItem *parent SIP_TRANSFERTHIS, const QString &name, const QString &path );
90  virtual ~QgsDataItem();
91 
92  bool hasChildren();
93 
94  int rowCount();
95 
98  virtual QVector<QgsDataItem *> createChildren() SIP_FACTORY;
99 
100  enum State
101  {
104  Populated
105  };
106  Q_ENUM( State );
107 
109  State state() const;
110 
115  virtual void setState( State state );
116 
122  virtual void addChildItem( QgsDataItem *child SIP_TRANSFER, bool refresh = false );
123 
128  virtual void deleteChildItem( QgsDataItem *child );
129 
134  virtual QgsDataItem *removeChildItem( QgsDataItem *child ) SIP_TRANSFERBACK;
135 
138  virtual bool equal( const QgsDataItem *other );
139 
140  virtual QWidget *paramWidget() SIP_FACTORY { return nullptr; }
141 
149  virtual QList<QAction *> actions( QWidget *parent );
150 
158  virtual QList<QMenu *> menus( QWidget *parent );
159 
164  virtual bool acceptDrop() { return false; }
165 
170  virtual bool handleDrop( const QMimeData * /*data*/, Qt::DropAction /*action*/ ) { return false; }
171 
178  virtual bool handleDoubleClick();
179 
186  virtual bool hasDragEnabled() const { return false; }
187 
194 
196  {
197  NoCapabilities = 0,
198  SetCrs = 1 << 0,
199  Fertile = 1 << 1,
200  Fast = 1 << 2,
201  Collapse = 1 << 3
202  };
203  Q_DECLARE_FLAGS( Capabilities, Capability )
204 
205 
209  virtual bool setCrs( const QgsCoordinateReferenceSystem &crs ) { Q_UNUSED( crs ); return false; }
210 
211  // ### QGIS 3 - rename to capabilities()
212  virtual Capabilities capabilities2() const { return mCapabilities; }
213 
217  virtual void setCapabilities( Capabilities capabilities ) { mCapabilities = capabilities; }
218 
219  // static methods
220 
221  // Find child index in vector of items using '==' operator
222  static int findItem( QVector<QgsDataItem *> items, QgsDataItem *item );
223 
224  // members
225 
226  Type type() const { return mType; }
227 
230  QgsDataItem *parent() const { return mParent; }
231 
234  void setParent( QgsDataItem *parent );
235  QVector<QgsDataItem *> children() const { return mChildren; }
236  virtual QIcon icon();
237  QString name() const { return mName; }
238  void setName( const QString &name ) { mName = name; }
239  QString path() const { return mPath; }
240  void setPath( const QString &path ) { mPath = path; }
242  static QString pathComponent( const QString &component );
243 
244  // Because QIcon (QPixmap) must not be used in outside the GUI thread, it is
245  // not possible to set mIcon in constructor. Either use mIconName/setIconName()
246  // or implement icon().
247  void setIcon( const QIcon &icon ) { mIcon = icon; }
248  void setIconName( const QString &iconName ) { mIconName = iconName; }
249 
250  void setToolTip( const QString &msg ) { mToolTip = msg; }
251  QString toolTip() const { return mToolTip; }
252 
253  // deleteLater() items anc clear the vector
254  static void deleteLater( QVector<QgsDataItem *> &items );
255 
257  void moveToThread( QThread *targetThread );
258 
259  protected:
260  virtual void populate( const QVector<QgsDataItem *> &children );
261 
265  virtual void refresh( const QVector<QgsDataItem *> &children );
266 
273  bool deferredDelete() { return mDeferredDelete; }
274 
276  Capabilities mCapabilities;
277  QgsDataItem *mParent = nullptr;
278  QVector<QgsDataItem *> mChildren; // easier to have it always
280  QString mName;
281  // Path is slash ('/') separated chain of item identifiers which are usually item names, but may be differen if it is
282  // necessary to distinguish paths of two providers to the same source (e.g GRASS location and standard directory have the same
283  // name but different paths). Identifiers in path must not contain '/' characters.
284  // The path is used to identify item in tree.
285  QString mPath;
286  QString mToolTip;
287  QString mIconName;
288  QIcon mIcon;
289  QMap<QString, QIcon> mIconMap;
290 
291  public slots:
292 
300  virtual void deleteLater();
301 
302  // Populate children using children vector created by createChildren()
303  // \param foreground run createChildren in foreground
304  virtual void populate( bool foreground = false );
305 
307  virtual void depopulate();
308 
309  virtual void refresh();
310 
312  virtual void refreshConnections();
313 
314  virtual void childrenCreated();
315 
316  signals:
317  void beginInsertItems( QgsDataItem *parent, int first, int last );
318  void endInsertItems();
319  void beginRemoveItems( QgsDataItem *parent, int first, int last );
320  void endRemoveItems();
321  void dataChanged( QgsDataItem *item );
322  void stateChanged( QgsDataItem *item, QgsDataItem::State oldState );
327  void connectionsChanged();
328 
329  protected slots:
330 
336  void updateIcon();
337 
338  private:
339  static QVector<QgsDataItem *> runCreateChildren( QgsDataItem *item );
340 
341  // Set to true if object has to be deleted when possible (nothing running in threads)
342  bool mDeferredDelete;
343  QFutureWatcher< QVector <QgsDataItem *> > *mFutureWatcher;
344  // number of items currently in loading (populating) state
345  static QgsAnimatedIcon *sPopulatingIcon;
346 };
347 
348 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsDataItem::Capabilities )
349 
350 
353 class CORE_EXPORT QgsLayerItem : public QgsDataItem
354 {
355  Q_OBJECT
356 
357  public:
359  {
369  Plugin
370  };
371 
372  Q_ENUM( LayerType );
373 
374  QgsLayerItem( QgsDataItem *parent, const QString &name, const QString &path, const QString &uri, LayerType layerType, const QString &providerKey );
375 
376  // --- reimplemented from QgsDataItem ---
377 
378  virtual bool equal( const QgsDataItem *other ) override;
379 
380  virtual bool hasDragEnabled() const override { return true; }
381 
382  virtual QgsMimeDataUtils::Uri mimeUri() const override;
383 
384  // --- New virtual methods for layer item derived classes ---
385 
387  QgsMapLayer::LayerType mapLayerType() const;
388 
390  QString uri() const { return mUri; }
391 
393  QString providerKey() const { return mProviderKey; }
394 
398  QStringList supportedCrs() const { return mSupportedCRS; }
399 
403  QStringList supportedFormats() const { return mSupportFormats; }
404 
408  virtual QString comments() const { return QString(); }
409 
413  static QString layerTypeAsString( const LayerType &layerType );
414 
418  static QString iconName( const LayerType &layerType );
419 
420  protected:
421 
423  QString mProviderKey;
425  QString mUri;
429  QStringList mSupportedCRS;
431  QStringList mSupportFormats;
432 
433  public:
434  static QIcon iconPoint();
435  static QIcon iconLine();
436  static QIcon iconPolygon();
437  static QIcon iconTable();
438  static QIcon iconRaster();
439  static QIcon iconDefault();
440 
442  virtual QString layerName() const { return name(); }
443 };
444 
445 
449 class CORE_EXPORT QgsDataCollectionItem : public QgsDataItem
450 {
451  Q_OBJECT
452  public:
453  QgsDataCollectionItem( QgsDataItem *parent, const QString &name, const QString &path = QString() );
455 
456  void addChild( QgsDataItem *item SIP_TRANSFER ) { mChildren.append( item ); }
457 
458  static QIcon iconDir(); // shared icon: open/closed directory
459  static QIcon iconDataCollection(); // default icon for data collection
460 };
461 
465 class CORE_EXPORT QgsDirectoryItem : public QgsDataCollectionItem
466 {
467  Q_OBJECT
468  public:
469  enum Column
470  {
478  };
479 
480  QgsDirectoryItem( QgsDataItem *parent, const QString &name, const QString &path );
481 
487  QgsDirectoryItem( QgsDataItem *parent, const QString &name, const QString &dirPath, const QString &path );
488 
489  virtual void setState( State state ) override;
490 
491  QVector<QgsDataItem *> createChildren() override;
492 
493  QString dirPath() const { return mDirPath; }
494  virtual bool equal( const QgsDataItem *other ) override;
495  virtual QIcon icon() override;
496  virtual QWidget *paramWidget() override SIP_FACTORY;
497 
499  static bool hiddenPath( const QString &path );
500 
501  QList<QAction *> actions( QWidget *parent ) override;
502 
503 
504  public slots:
505  virtual void childrenCreated() override;
506  void directoryChanged();
507 
508  protected:
509  void init();
510  QString mDirPath;
511 
512  private:
513  QFileSystemWatcher *mFileSystemWatcher = nullptr;
514  bool mRefreshLater;
515  QDateTime mLastScan;
516 };
517 
521 class CORE_EXPORT QgsProjectItem : public QgsDataItem
522 {
523  Q_OBJECT
524  public:
525 
532  QgsProjectItem( QgsDataItem *parent, const QString &name, const QString &path );
533 
534  virtual bool hasDragEnabled() const override { return true; }
535 
536 };
537 
541 class CORE_EXPORT QgsErrorItem : public QgsDataItem
542 {
543  Q_OBJECT
544  public:
545 
546  QgsErrorItem( QgsDataItem *parent, const QString &error, const QString &path );
547 
548 };
549 
550 
551 // ---------
552 
556 class CORE_EXPORT QgsDirectoryParamWidget : public QTreeWidget
557 {
558  Q_OBJECT
559 
560  public:
561  QgsDirectoryParamWidget( const QString &path, QWidget *parent SIP_TRANSFERTHIS = nullptr );
562 
563  protected:
564  void mousePressEvent( QMouseEvent *event ) override;
565 
566  public slots:
567  void showHideColumn();
568 };
569 
574 class CORE_EXPORT QgsFavoritesItem : public QgsDataCollectionItem
575 {
576  Q_OBJECT
577  public:
578 
583  QgsFavoritesItem( QgsDataItem *parent, const QString &name, const QString &path = QString() );
584 
585  QVector<QgsDataItem *> createChildren() override;
586 
591  void addDirectory( const QString &directory );
592 
597  void removeDirectory( QgsDirectoryItem *item );
598 
600  static QIcon iconFavorites();
601 
602  private:
603  QVector<QgsDataItem *> createChildren( const QString &favDir );
604 };
605 
609 class CORE_EXPORT QgsZipItem : public QgsDataCollectionItem
610 {
611  Q_OBJECT
612 
613  protected:
614  QString mFilePath;
615  QString mVsiPrefix;
616  QStringList mZipFileList;
617 
618  public:
619  QgsZipItem( QgsDataItem *parent, const QString &name, const QString &path );
620  QgsZipItem( QgsDataItem *parent, const QString &name, const QString &filePath, const QString &path );
621 
622  QVector<QgsDataItem *> createChildren() override;
623  QStringList getZipFileList();
624 
626  static QVector<dataItem_t *> sDataItemPtr SIP_SKIP;
627  static QStringList sProviderNames;
628 
629  static QString vsiPrefix( const QString &uri ) { return qgsVsiPrefix( uri ); }
630 
634  static QgsDataItem *itemFromPath( QgsDataItem *parent, const QString &path, const QString &name ) SIP_FACTORY;
635 
640  static QgsDataItem *itemFromPath( QgsDataItem *parent, const QString &filePath, const QString &name, const QString &path ) SIP_FACTORY SIP_PYNAME( itemFromFilePath );
641 
642  static QIcon iconZip();
643 
644  private:
645  void init();
646 };
647 
648 #endif // QGSDATAITEM_H
649 
650 
A Collection: logical collection of layers or subcollections, e.g.
Definition: qgsdataitem.h:449
QStringList mSupportFormats
The list of supported formats.
Definition: qgsdataitem.h:431
QString path() const
Definition: qgsdataitem.h:239
virtual void childrenCreated()
virtual QString layerName() const
Definition: qgsdataitem.h:442
virtual bool hasDragEnabled() const override
Returns true if the item may be dragged.
Definition: qgsdataitem.h:534
QString providerKey() const
Returns provider key.
Definition: qgsdataitem.h:393
QString name() const
Definition: qgsdataitem.h:237
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
QString toolTip() const
Definition: qgsdataitem.h:251
QString mProviderKey
The provider key.
Definition: qgsdataitem.h:423
virtual bool handleDrop(const QMimeData *, Qt::DropAction)
Attempts to process the mime data dropped on this item.
Definition: qgsdataitem.h:170
virtual QIcon icon()
void addChild(QgsDataItem *item)
Definition: qgsdataitem.h:456
QString mIconName
Definition: qgsdataitem.h:287
State mState
Definition: qgsdataitem.h:279
Type type() const
Definition: qgsdataitem.h:226
void setIcon(const QIcon &icon)
Definition: qgsdataitem.h:247
Abstract base class for spatial data provider implementations.
void setToolTip(const QString &msg)
Definition: qgsdataitem.h:250
#define SIP_TRANSFERBACK
Definition: qgis_sip.h:41
virtual QWidget * paramWidget()
Definition: qgsdataitem.h:140
QString mToolTip
Definition: qgsdataitem.h:286
A zip file: contains layers, using GDAL/OGR VSIFILE mechanism.
Definition: qgsdataitem.h:609
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:429
QString mFilePath
Definition: qgsdataitem.h:614
QgsDataItem * parent() const
Get item parent.
Definition: qgsdataitem.h:230
QString mName
Definition: qgsdataitem.h:280
#define SIP_SKIP
Definition: qgis_sip.h:119
LayerType
Types of layers that can be added to a map.
Definition: qgsmaplayer.h:92
QStringList mZipFileList
Definition: qgsdataitem.h:616
virtual void setCapabilities(Capabilities capabilities)
Sets the capabilities for the data item.
Definition: qgsdataitem.h:217
Children not yet created.
Definition: qgsdataitem.h:102
#define SIP_TRANSFER
Definition: qgis_sip.h:36
Creating children in separate thread (populating or refreshing)
Definition: qgsdataitem.h:103
#define SIP_END
Definition: qgis_sip.h:175
Data item that can be used to report problems (e.g.
Definition: qgsdataitem.h:541
virtual bool hasDragEnabled() const
Returns true if the item may be dragged.
Definition: qgsdataitem.h:186
virtual QgsMimeDataUtils::Uri mimeUri() const
Return mime URI for the data item.
Definition: qgsdataitem.h:193
#define SIP_FACTORY
Definition: qgis_sip.h:69
Custom item type.
Definition: qgsdataitem.h:83
A directory: contains subdirectories and layers.
Definition: qgsdataitem.h:465
static QStringList sProviderNames
Definition: qgsdataitem.h:627
Base class for all items in the model.
Definition: qgsdataitem.h:48
Capabilities mCapabilities
Definition: qgsdataitem.h:276
virtual void setState(State state)
Set item state.
void setName(const QString &name)
Definition: qgsdataitem.h:238
QVector< QgsDataItem * > children() const
Definition: qgsdataitem.h:235
QString mPath
Definition: qgsdataitem.h:285
QString mUri
The URI.
Definition: qgsdataitem.h:425
Contains various Favorites directories.
Definition: qgsdataitem.h:574
QString mVsiPrefix
Definition: qgsdataitem.h:615
QStringList supportedFormats() const
Returns the supported formats.
Definition: qgsdataitem.h:403
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:390
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:273
QString dirPath() const
Definition: qgsdataitem.h:493
virtual QVector< QgsDataItem * > createChildren()
Create children.
Represents a QGIS project.
Definition: qgsdataitem.h:82
Item that represents a layer that can be opened with one of the providers.
Definition: qgsdataitem.h:353
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:380
Data item that can be used to represent QGIS projects.
Definition: qgsdataitem.h:521
void setPath(const QString &path)
Definition: qgsdataitem.h:240
virtual bool acceptDrop()
Returns whether the item accepts drag and dropped layers - e.g.
Definition: qgsdataitem.h:164
LayerType mLayerType
The layer type.
Definition: qgsdataitem.h:427
QVector< QgsDataItem * > mChildren
Definition: qgsdataitem.h:278
virtual QString comments() const
Returns comments of the layer.
Definition: qgsdataitem.h:408
Represents a favorite item.
Definition: qgsdataitem.h:81
QStringList supportedCrs() const
Returns the supported CRS.
Definition: qgsdataitem.h:398
void setIconName(const QString &iconName)
Definition: qgsdataitem.h:248
static QString vsiPrefix(const QString &uri)
Definition: qgsdataitem.h:629
QMap< QString, QIcon > mIconMap
Definition: qgsdataitem.h:289
virtual Capabilities capabilities2() const
Definition: qgsdataitem.h:212
#define SIP_PYNAME(name)
Definition: qgis_sip.h:74