QGIS API Documentation  2.99.0-Master (b95d432)
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 
32 #include "qgsmaplayer.h"
34 #include "qgsmimedatautils.h"
35 
36 
37 class QgsDataProvider;
38 class QgsDataItem;
39 class QgsAnimatedIcon;
40 
41 typedef QgsDataItem *dataItem_t( QString, QgsDataItem * ) SIP_SKIP;
42 
47 class CORE_EXPORT QgsDataItem : public QObject
48 {
49 
50 #ifdef SIP_RUN
52  if ( qobject_cast<QgsLayerItem *>( sipCpp ) )
53  sipType = sipType_QgsLayerItem;
54  else if ( qobject_cast<QgsErrorItem *>( sipCpp ) )
55  sipType = sipType_QgsErrorItem;
56  else if ( qobject_cast<QgsDirectoryItem *>( sipCpp ) )
57  sipType = sipType_QgsDirectoryItem;
58  else if ( qobject_cast<QgsFavoritesItem *>( sipCpp ) )
59  sipType = sipType_QgsFavoritesItem;
60  else if ( qobject_cast<QgsZipItem *>( sipCpp ) )
61  sipType = sipType_QgsZipItem;
62  else if ( qobject_cast<QgsDataCollectionItem *>( sipCpp ) )
63  sipType = sipType_QgsDataCollectionItem;
64  else if ( qobject_cast<QgsProjectItem *>( sipCpp ) )
65  sipType = sipType_QgsProjectItem;
66  else
67  sipType = 0;
68  SIP_END
69 #endif
70 
71  Q_OBJECT
72  Q_ENUMS( Type )
73  Q_ENUMS( State )
74  public:
75  enum Type
76  {
82  Project
83  };
84 
86  QgsDataItem( QgsDataItem::Type type, QgsDataItem *parent SIP_TRANSFERTHIS, const QString &name, const QString &path );
87  virtual ~QgsDataItem();
88 
89  bool hasChildren();
90 
91  int rowCount();
92 
95  virtual QVector<QgsDataItem *> createChildren() SIP_FACTORY;
96 
97  enum State
98  {
101  Populated
102  };
103 
105  State state() const;
106 
111  virtual void setState( State state );
112 
118  virtual void addChildItem( QgsDataItem *child SIP_TRANSFER, bool refresh = false );
119 
124  virtual void deleteChildItem( QgsDataItem *child );
125 
130  virtual QgsDataItem *removeChildItem( QgsDataItem *child ) SIP_TRANSFERBACK;
131 
134  virtual bool equal( const QgsDataItem *other );
135 
136  virtual QWidget *paramWidget() SIP_FACTORY { return nullptr; }
137 
141  virtual QList<QAction *> actions() { return QList<QAction *>(); }
142 
147  virtual bool acceptDrop() { return false; }
148 
153  virtual bool handleDrop( const QMimeData * /*data*/, Qt::DropAction /*action*/ ) { return false; }
154 
161  virtual bool hasDragEnabled() const { return false; }
162 
169 
171  {
172  NoCapabilities = 0,
173  SetCrs = 1 << 0,
174  Fertile = 1 << 1,
175  Fast = 1 << 2,
176  Collapse = 1 << 3
177  };
178  Q_DECLARE_FLAGS( Capabilities, Capability )
179 
180 
184  virtual bool setCrs( const QgsCoordinateReferenceSystem &crs ) { Q_UNUSED( crs ); return false; }
185 
186  // ### QGIS 3 - rename to capabilities()
187  virtual Capabilities capabilities2() const { return mCapabilities; }
188 
192  virtual void setCapabilities( Capabilities capabilities ) { mCapabilities = capabilities; }
193 
194  // static methods
195 
196  // Find child index in vector of items using '==' operator
197  static int findItem( QVector<QgsDataItem *> items, QgsDataItem *item );
198 
199  // members
200 
201  Type type() const { return mType; }
202 
205  QgsDataItem *parent() const { return mParent; }
206 
209  void setParent( QgsDataItem *parent );
210  QVector<QgsDataItem *> children() const { return mChildren; }
211  virtual QIcon icon();
212  QString name() const { return mName; }
213  void setName( const QString &name ) { mName = name; }
214  QString path() const { return mPath; }
215  void setPath( const QString &path ) { mPath = path; }
217  static QString pathComponent( const QString &component );
218 
219  // Because QIcon (QPixmap) must not be used in outside the GUI thread, it is
220  // not possible to set mIcon in constructor. Either use mIconName/setIconName()
221  // or implement icon().
222  void setIcon( const QIcon &icon ) { mIcon = icon; }
223  void setIconName( const QString &iconName ) { mIconName = iconName; }
224 
225  void setToolTip( const QString &msg ) { mToolTip = msg; }
226  QString toolTip() const { return mToolTip; }
227 
228  // deleteLater() items anc clear the vector
229  static void deleteLater( QVector<QgsDataItem *> &items );
230 
232  void moveToThread( QThread *targetThread );
233 
234  protected:
235  virtual void populate( const QVector<QgsDataItem *> &children );
236 
240  virtual void refresh( const QVector<QgsDataItem *> &children );
241 
248  bool deferredDelete() { return mDeferredDelete; }
249 
251  Capabilities mCapabilities;
252  QgsDataItem *mParent = nullptr;
253  QVector<QgsDataItem *> mChildren; // easier to have it always
255  QString mName;
256  // Path is slash ('/') separated chain of item identifiers which are usually item names, but may be differen if it is
257  // necessary to distinguish paths of two providers to the same source (e.g GRASS location and standard directory have the same
258  // name but different paths). Identifiers in path must not contain '/' characters.
259  // The path is used to identify item in tree.
260  QString mPath;
261  QString mToolTip;
262  QString mIconName;
263  QIcon mIcon;
264  QMap<QString, QIcon> mIconMap;
265 
266  public slots:
267 
275  virtual void deleteLater();
276 
277  // Populate children using children vector created by createChildren()
278  // \param foreground run createChildren in foreground
279  virtual void populate( bool foreground = false );
280 
282  virtual void depopulate();
283 
284  virtual void refresh();
285 
287  virtual void refreshConnections();
288 
289  virtual void childrenCreated();
290 
291  signals:
292  void beginInsertItems( QgsDataItem *parent, int first, int last );
293  void endInsertItems();
294  void beginRemoveItems( QgsDataItem *parent, int first, int last );
295  void endRemoveItems();
296  void dataChanged( QgsDataItem *item );
297  void stateChanged( QgsDataItem *item, QgsDataItem::State oldState );
302  void connectionsChanged();
303 
304  protected slots:
305 
311  void updateIcon();
312 
313  private:
314  static QVector<QgsDataItem *> runCreateChildren( QgsDataItem *item );
315 
316  // Set to true if object has to be deleted when possible (nothing running in threads)
317  bool mDeferredDelete;
318  QFutureWatcher< QVector <QgsDataItem *> > *mFutureWatcher;
319  // number of items currently in loading (populating) state
320  static QgsAnimatedIcon *sPopulatingIcon;
321 };
322 
323 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsDataItem::Capabilities )
324 
325 
328 class CORE_EXPORT QgsLayerItem : public QgsDataItem
329 {
330  Q_OBJECT
331  public:
333  {
343  Plugin
344  };
345 
346  QgsLayerItem( QgsDataItem *parent, const QString &name, const QString &path, const QString &uri, LayerType layerType, const QString &providerKey );
347 
348  // --- reimplemented from QgsDataItem ---
349 
350  virtual bool equal( const QgsDataItem *other ) override;
351 
352  virtual bool hasDragEnabled() const override { return true; }
353 
354  virtual QgsMimeDataUtils::Uri mimeUri() const override;
355 
356  // --- New virtual methods for layer item derived classes ---
357 
359  QgsMapLayer::LayerType mapLayerType() const;
360 
362  QString uri() const { return mUri; }
363 
365  QString providerKey() const { return mProviderKey; }
366 
370  QStringList supportedCrs() const { return mSupportedCRS; }
371 
375  QStringList supportedFormats() const { return mSupportFormats; }
376 
380  virtual QString comments() const { return QString(); }
381 
382  protected:
383 
385  QString mProviderKey;
387  QString mUri;
391  QStringList mSupportedCRS;
393  QStringList mSupportFormats;
394 
395  public:
396  static QIcon iconPoint();
397  static QIcon iconLine();
398  static QIcon iconPolygon();
399  static QIcon iconTable();
400  static QIcon iconRaster();
401  static QIcon iconDefault();
402 
404  virtual QString layerName() const { return name(); }
405 };
406 
407 
411 class CORE_EXPORT QgsDataCollectionItem : public QgsDataItem
412 {
413  Q_OBJECT
414  public:
415  QgsDataCollectionItem( QgsDataItem *parent, const QString &name, const QString &path = QString() );
417 
418  void addChild( QgsDataItem *item SIP_TRANSFER ) { mChildren.append( item ); }
419 
420  static QIcon iconDir(); // shared icon: open/closed directory
421  static QIcon iconDataCollection(); // default icon for data collection
422 };
423 
427 class CORE_EXPORT QgsDirectoryItem : public QgsDataCollectionItem
428 {
429  Q_OBJECT
430  public:
431  enum Column
432  {
440  };
441 
442  QgsDirectoryItem( QgsDataItem *parent, const QString &name, const QString &path );
443 
449  QgsDirectoryItem( QgsDataItem *parent, const QString &name, const QString &dirPath, const QString &path );
450 
451  virtual void setState( State state ) override;
452 
453  QVector<QgsDataItem *> createChildren() override;
454 
455  QString dirPath() const { return mDirPath; }
456  virtual bool equal( const QgsDataItem *other ) override;
457  virtual QIcon icon() override;
458  virtual QWidget *paramWidget() override SIP_FACTORY;
459 
461  static bool hiddenPath( const QString &path );
462 
463  public slots:
464  virtual void childrenCreated() override;
465  void directoryChanged();
466 
467  protected:
468  void init();
469  QString mDirPath;
470 
471  private:
472  QFileSystemWatcher *mFileSystemWatcher = nullptr;
473  bool mRefreshLater;
474 };
475 
479 class CORE_EXPORT QgsProjectItem : public QgsDataItem
480 {
481  Q_OBJECT
482  public:
483 
490  QgsProjectItem( QgsDataItem *parent, const QString &name, const QString &path );
491 
492  virtual bool hasDragEnabled() const override { return true; }
493 
494 };
495 
499 class CORE_EXPORT QgsErrorItem : public QgsDataItem
500 {
501  Q_OBJECT
502  public:
503 
504  QgsErrorItem( QgsDataItem *parent, const QString &error, const QString &path );
505 
506 };
507 
508 
509 // ---------
510 
514 class CORE_EXPORT QgsDirectoryParamWidget : public QTreeWidget
515 {
516  Q_OBJECT
517 
518  public:
519  QgsDirectoryParamWidget( const QString &path, QWidget *parent SIP_TRANSFERTHIS = nullptr );
520 
521  protected:
522  void mousePressEvent( QMouseEvent *event ) override;
523 
524  public slots:
525  void showHideColumn();
526 };
527 
532 class CORE_EXPORT QgsFavoritesItem : public QgsDataCollectionItem
533 {
534  Q_OBJECT
535  public:
536 
541  QgsFavoritesItem( QgsDataItem *parent, const QString &name, const QString &path = QString() );
542 
543  QVector<QgsDataItem *> createChildren() override;
544 
549  void addDirectory( const QString &directory );
550 
555  void removeDirectory( QgsDirectoryItem *item );
556 
558  static QIcon iconFavorites();
559 
560  private:
561  QVector<QgsDataItem *> createChildren( const QString &favDir );
562 };
563 
567 class CORE_EXPORT QgsZipItem : public QgsDataCollectionItem
568 {
569  Q_OBJECT
570 
571  protected:
572  QString mFilePath;
573  QString mVsiPrefix;
574  QStringList mZipFileList;
575 
576  public:
577  QgsZipItem( QgsDataItem *parent, const QString &name, const QString &path );
578  QgsZipItem( QgsDataItem *parent, const QString &name, const QString &filePath, const QString &path );
579 
580  QVector<QgsDataItem *> createChildren() override;
581  QStringList getZipFileList();
582 
584  static QVector<dataItem_t *> sDataItemPtr SIP_SKIP;
585  static QStringList sProviderNames;
586 
587  static QString vsiPrefix( const QString &uri ) { return qgsVsiPrefix( uri ); }
588 
592  static QgsDataItem *itemFromPath( QgsDataItem *parent, const QString &path, const QString &name ) SIP_FACTORY;
593 
598  static QgsDataItem *itemFromPath( QgsDataItem *parent, const QString &filePath, const QString &name, const QString &path ) SIP_FACTORY SIP_PYNAME( itemFromFilePath );
599 
600  static QIcon iconZip();
601 
602  private:
603  void init();
604 };
605 
606 #endif // QGSDATAITEM_H
607 
608 
A Collection: logical collection of layers or subcollections, e.g.
Definition: qgsdataitem.h:411
QStringList mSupportFormats
The list of supported formats.
Definition: qgsdataitem.h:393
QString path() const
Definition: qgsdataitem.h:214
virtual void childrenCreated()
virtual QString layerName() const
Definition: qgsdataitem.h:404
virtual bool hasDragEnabled() const override
Returns true if the item may be dragged.
Definition: qgsdataitem.h:492
QString providerKey() const
Returns provider key.
Definition: qgsdataitem.h:365
QString name() const
Definition: qgsdataitem.h:212
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
QString toolTip() const
Definition: qgsdataitem.h:226
QString mProviderKey
The provider key.
Definition: qgsdataitem.h:385
virtual bool handleDrop(const QMimeData *, Qt::DropAction)
Attempts to process the mime data dropped on this item.
Definition: qgsdataitem.h:153
virtual QIcon icon()
void addChild(QgsDataItem *item)
Definition: qgsdataitem.h:418
QString mIconName
Definition: qgsdataitem.h:262
State mState
Definition: qgsdataitem.h:254
Type type() const
Definition: qgsdataitem.h:201
void setIcon(const QIcon &icon)
Definition: qgsdataitem.h:222
Abstract base class for spatial data provider implementations.
void setToolTip(const QString &msg)
Definition: qgsdataitem.h:225
#define SIP_TRANSFERBACK
Definition: qgis_sip.h:41
virtual QWidget * paramWidget()
Definition: qgsdataitem.h:136
QString mToolTip
Definition: qgsdataitem.h:261
A zip file: contains layers, using GDAL/OGR VSIFILE mechanism.
Definition: qgsdataitem.h:567
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:391
QString mFilePath
Definition: qgsdataitem.h:572
QgsDataItem * parent() const
Get item parent.
Definition: qgsdataitem.h:205
QString mName
Definition: qgsdataitem.h:255
#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:574
virtual void setCapabilities(Capabilities capabilities)
Sets the capabilities for the data item.
Definition: qgsdataitem.h:192
Children not yet created.
Definition: qgsdataitem.h:99
#define SIP_TRANSFER
Definition: qgis_sip.h:36
Creating children in separate thread (populating or refreshing)
Definition: qgsdataitem.h:100
#define SIP_END
Definition: qgis_sip.h:175
Data item that can be used to report problems (e.g.
Definition: qgsdataitem.h:499
virtual bool hasDragEnabled() const
Returns true if the item may be dragged.
Definition: qgsdataitem.h:161
virtual QgsMimeDataUtils::Uri mimeUri() const
Return mime URI for the data item.
Definition: qgsdataitem.h:168
#define SIP_FACTORY
Definition: qgis_sip.h:69
virtual QList< QAction * > actions()
Returns the list of actions available for this item.
Definition: qgsdataitem.h:141
A directory: contains subdirectories and layers.
Definition: qgsdataitem.h:427
static QStringList sProviderNames
Definition: qgsdataitem.h:585
Base class for all items in the model.
Definition: qgsdataitem.h:47
Capabilities mCapabilities
Definition: qgsdataitem.h:251
virtual void setState(State state)
Set item state.
void setName(const QString &name)
Definition: qgsdataitem.h:213
QVector< QgsDataItem * > children() const
Definition: qgsdataitem.h:210
QString mPath
Definition: qgsdataitem.h:260
QString mUri
The URI.
Definition: qgsdataitem.h:387
Contains various Favorites directories.
Definition: qgsdataitem.h:532
QString mVsiPrefix
Definition: qgsdataitem.h:573
QStringList supportedFormats() const
Returns the supported formats.
Definition: qgsdataitem.h:375
QString qgsVsiPrefix(const QString &path)
Definition: qgis.cpp:209
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:362
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:165
bool deferredDelete()
The item is scheduled to be deleted.
Definition: qgsdataitem.h:248
QString dirPath() const
Definition: qgsdataitem.h:455
virtual QVector< QgsDataItem * > createChildren()
Create children.
Item that represents a layer that can be opened with one of the providers.
Definition: qgsdataitem.h:328
QgsDataItem * dataItem_t(QString, QgsDataItem *)
Definition: qgsdataitem.h:41
virtual bool hasDragEnabled() const override
Returns true if the item may be dragged.
Definition: qgsdataitem.h:352
Data item that can be used to represent QGIS projects.
Definition: qgsdataitem.h:479
void setPath(const QString &path)
Definition: qgsdataitem.h:215
virtual bool acceptDrop()
Returns whether the item accepts drag and dropped layers - e.g.
Definition: qgsdataitem.h:147
LayerType mLayerType
The layer type.
Definition: qgsdataitem.h:389
QVector< QgsDataItem * > mChildren
Definition: qgsdataitem.h:253
virtual QString comments() const
Returns comments of the layer.
Definition: qgsdataitem.h:380
Represents a favorite item.
Definition: qgsdataitem.h:81
QStringList supportedCrs() const
Returns the supported CRS.
Definition: qgsdataitem.h:370
void setIconName(const QString &iconName)
Definition: qgsdataitem.h:223
static QString vsiPrefix(const QString &uri)
Definition: qgsdataitem.h:587
QMap< QString, QIcon > mIconMap
Definition: qgsdataitem.h:264
virtual Capabilities capabilities2() const
Definition: qgsdataitem.h:187
#define SIP_PYNAME(name)
Definition: qgis_sip.h:74