QGIS API Documentation  2.99.0-Master (716ff6c)
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_core.h"
21 #include <QFileSystemWatcher>
22 #include <QFutureWatcher>
23 #include <QIcon>
24 #include <QLibrary>
25 #include <QObject>
26 #include <QPixmap>
27 #include <QString>
28 #include <QTreeWidget>
29 #include <QVector>
30 
31 #include "qgsmaplayer.h"
33 #include "qgsmimedatautils.h"
34 
35 
36 class QgsDataProvider;
37 class QgsDataItem;
38 class QgsAnimatedIcon;
39 
40 typedef QgsDataItem *dataItem_t( QString, QgsDataItem * );
41 
46 class CORE_EXPORT QgsDataItem : public QObject
47 {
48  Q_OBJECT
49  Q_ENUMS( Type )
50  Q_ENUMS( State )
51  public:
52  enum Type
53  {
59  Project
60  };
61 
63  QgsDataItem( QgsDataItem::Type type, QgsDataItem *parent, const QString &name, const QString &path );
64  virtual ~QgsDataItem();
65 
66  bool hasChildren();
67 
68  int rowCount();
69 
72  virtual QVector<QgsDataItem *> createChildren();
73 
74  enum State
75  {
78  Populated
79  };
80 
82  State state() const;
83 
88  virtual void setState( State state );
89 
95  virtual void addChildItem( QgsDataItem *child, bool refresh = false );
96 
101  virtual void deleteChildItem( QgsDataItem *child );
102 
107  virtual QgsDataItem *removeChildItem( QgsDataItem *child );
108 
111  virtual bool equal( const QgsDataItem *other );
112 
113  virtual QWidget *paramWidget() { return nullptr; }
114 
118  virtual QList<QAction *> actions() { return QList<QAction *>(); }
119 
124  virtual bool acceptDrop() { return false; }
125 
130  virtual bool handleDrop( const QMimeData * /*data*/, Qt::DropAction /*action*/ ) { return false; }
131 
138  virtual bool hasDragEnabled() const { return false; }
139 
146 
148  {
149  NoCapabilities = 0,
150  SetCrs = 1 << 0,
151  Fertile = 1 << 1,
152  Fast = 1 << 2
153  };
154  Q_DECLARE_FLAGS( Capabilities, Capability )
155 
156 
160  virtual bool setCrs( const QgsCoordinateReferenceSystem &crs ) { Q_UNUSED( crs ); return false; }
161 
162  // ### QGIS 3 - rename to capabilities()
163  virtual Capabilities capabilities2() const { return mCapabilities; }
164 
168  virtual void setCapabilities( Capabilities capabilities ) { mCapabilities = capabilities; }
169 
170  // static methods
171 
172  // Find child index in vector of items using '==' operator
173  static int findItem( QVector<QgsDataItem *> items, QgsDataItem *item );
174 
175  // members
176 
177  Type type() const { return mType; }
178 
181  QgsDataItem *parent() const { return mParent; }
182 
185  void setParent( QgsDataItem *parent );
186  QVector<QgsDataItem *> children() const { return mChildren; }
187  virtual QIcon icon();
188  QString name() const { return mName; }
189  void setName( const QString &name ) { mName = name; }
190  QString path() const { return mPath; }
191  void setPath( const QString &path ) { mPath = path; }
193  static QString pathComponent( const QString &component );
194 
195  // Because QIcon (QPixmap) must not be used in outside the GUI thread, it is
196  // not possible to set mIcon in constructor. Either use mIconName/setIconName()
197  // or implement icon().
198  void setIcon( const QIcon &icon ) { mIcon = icon; }
199  void setIconName( const QString &iconName ) { mIconName = iconName; }
200 
201  void setToolTip( const QString &msg ) { mToolTip = msg; }
202  QString toolTip() const { return mToolTip; }
203 
204  // deleteLater() items anc clear the vector
205  static void deleteLater( QVector<QgsDataItem *> &items );
206 
208  void moveToThread( QThread *targetThread );
209 
210  protected:
211  virtual void populate( const QVector<QgsDataItem *> &children );
212 
216  virtual void refresh( const QVector<QgsDataItem *> &children );
217 
224  bool deferredDelete() { return mDeferredDelete; }
225 
227  Capabilities mCapabilities;
228  QgsDataItem *mParent = nullptr;
229  QVector<QgsDataItem *> mChildren; // easier to have it always
231  QString mName;
232  // Path is slash ('/') separated chain of item identifiers which are usually item names, but may be differen if it is
233  // necessary to distinguish paths of two providers to the same source (e.g GRASS location and standard directory have the same
234  // name but different paths). Identifiers in path must not contain '/' characters.
235  // The path is used to identify item in tree.
236  QString mPath;
237  QString mToolTip;
238  QString mIconName;
239  QIcon mIcon;
240  QMap<QString, QIcon> mIconMap;
241 
242  public slots:
243 
251  virtual void deleteLater();
252 
253  // Populate children using children vector created by createChildren()
254  // \param foreground run createChildren in foreground
255  virtual void populate( bool foreground = false );
256 
258  virtual void depopulate();
259 
260  virtual void refresh();
261 
262  virtual void childrenCreated();
263 
264  signals:
265  void beginInsertItems( QgsDataItem *parent, int first, int last );
266  void endInsertItems();
267  void beginRemoveItems( QgsDataItem *parent, int first, int last );
268  void endRemoveItems();
269  void dataChanged( QgsDataItem *item );
270  void stateChanged( QgsDataItem *item, QgsDataItem::State oldState );
271 
272  protected slots:
273 
279  void updateIcon();
280 
281  private:
282  static QVector<QgsDataItem *> runCreateChildren( QgsDataItem *item );
283 
284  // Set to true if object has to be deleted when possible (nothing running in threads)
285  bool mDeferredDelete;
286  QFutureWatcher< QVector <QgsDataItem *> > *mFutureWatcher;
287  // number of items currently in loading (populating) state
288  static QgsAnimatedIcon *sPopulatingIcon;
289 };
290 
291 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsDataItem::Capabilities )
292 
293 
296 class CORE_EXPORT QgsLayerItem : public QgsDataItem
297 {
298  Q_OBJECT
299  public:
301  {
311  Plugin
312  };
313 
314  QgsLayerItem( QgsDataItem *parent, const QString &name, const QString &path, const QString &uri, LayerType layerType, const QString &providerKey );
315 
316  // --- reimplemented from QgsDataItem ---
317 
318  virtual bool equal( const QgsDataItem *other ) override;
319 
320  virtual bool hasDragEnabled() const override { return true; }
321 
322  virtual QgsMimeDataUtils::Uri mimeUri() const override;
323 
324  // --- New virtual methods for layer item derived classes ---
325 
327  QgsMapLayer::LayerType mapLayerType() const;
328 
330  QString uri() const { return mUri; }
331 
333  QString providerKey() const { return mProviderKey; }
334 
338  QStringList supportedCrs() const { return mSupportedCRS; }
339 
343  QStringList supportedFormats() const { return mSupportFormats; }
344 
348  virtual QString comments() const { return QString(); }
349 
350  protected:
351 
353  QString mProviderKey;
355  QString mUri;
359  QStringList mSupportedCRS;
361  QStringList mSupportFormats;
362 
363  public:
364  static QIcon iconPoint();
365  static QIcon iconLine();
366  static QIcon iconPolygon();
367  static QIcon iconTable();
368  static QIcon iconRaster();
369  static QIcon iconDefault();
370 
372  virtual QString layerName() const { return name(); }
373 };
374 
375 
379 class CORE_EXPORT QgsDataCollectionItem : public QgsDataItem
380 {
381  Q_OBJECT
382  public:
383  QgsDataCollectionItem( QgsDataItem *parent, const QString &name, const QString &path = QString::null );
385 
386  void addChild( QgsDataItem *item ) { mChildren.append( item ); }
387 
388  static QIcon iconDir(); // shared icon: open/closed directory
389  static QIcon iconDataCollection(); // default icon for data collection
390 };
391 
395 class CORE_EXPORT QgsDirectoryItem : public QgsDataCollectionItem
396 {
397  Q_OBJECT
398  public:
399  enum Column
400  {
408  };
409 
410  QgsDirectoryItem( QgsDataItem *parent, const QString &name, const QString &path );
411 
417  QgsDirectoryItem( QgsDataItem *parent, const QString &name, const QString &dirPath, const QString &path );
418 
419  virtual void setState( State state ) override;
420 
421  QVector<QgsDataItem *> createChildren() override;
422 
423  QString dirPath() const { return mDirPath; }
424  virtual bool equal( const QgsDataItem *other ) override;
425  virtual QIcon icon() override;
426  virtual QWidget *paramWidget() override;
427 
429  static bool hiddenPath( const QString &path );
430 
431  public slots:
432  virtual void childrenCreated() override;
433  void directoryChanged();
434 
435  protected:
436  void init();
437  QString mDirPath;
438 
439  private:
440  QFileSystemWatcher *mFileSystemWatcher = nullptr;
441  bool mRefreshLater;
442 };
443 
447 class CORE_EXPORT QgsProjectItem : public QgsDataItem
448 {
449  Q_OBJECT
450  public:
451 
458  QgsProjectItem( QgsDataItem *parent, const QString &name, const QString &path );
459 
460  virtual bool hasDragEnabled() const override { return true; }
461 
462 };
463 
467 class CORE_EXPORT QgsErrorItem : public QgsDataItem
468 {
469  Q_OBJECT
470  public:
471 
472  QgsErrorItem( QgsDataItem *parent, const QString &error, const QString &path );
473 
474 };
475 
476 
477 // ---------
478 
482 class CORE_EXPORT QgsDirectoryParamWidget : public QTreeWidget
483 {
484  Q_OBJECT
485 
486  public:
487  QgsDirectoryParamWidget( const QString &path, QWidget *parent = nullptr );
488 
489  protected:
490  void mousePressEvent( QMouseEvent *event ) override;
491 
492  public slots:
493  void showHideColumn();
494 };
495 
500 class CORE_EXPORT QgsFavoritesItem : public QgsDataCollectionItem
501 {
502  Q_OBJECT
503  public:
504 
509  QgsFavoritesItem( QgsDataItem *parent, const QString &name, const QString &path = QString() );
510 
511  QVector<QgsDataItem *> createChildren() override;
512 
517  void addDirectory( const QString &directory );
518 
523  void removeDirectory( QgsDirectoryItem *item );
524 
526  static QIcon iconFavorites();
527 
528  private:
529  QVector<QgsDataItem *> createChildren( const QString &favDir );
530 };
531 
535 class CORE_EXPORT QgsZipItem : public QgsDataCollectionItem
536 {
537  Q_OBJECT
538 
539  protected:
540  QString mFilePath;
541  QString mVsiPrefix;
542  QStringList mZipFileList;
543 
544  public:
545  QgsZipItem( QgsDataItem *parent, const QString &name, const QString &path );
546  QgsZipItem( QgsDataItem *parent, const QString &name, const QString &filePath, const QString &path );
547 
548  QVector<QgsDataItem *> createChildren() override;
549  QStringList getZipFileList();
550 
552  static QVector<dataItem_t *> sDataItemPtr;
553  static QStringList sProviderNames;
554 
555  static QString vsiPrefix( const QString &uri ) { return qgsVsiPrefix( uri ); }
556 
560  static QgsDataItem *itemFromPath( QgsDataItem *parent, const QString &path, const QString &name );
561 
566  static QgsDataItem *itemFromPath( QgsDataItem *parent, const QString &filePath, const QString &name, const QString &path );
567 
568  static QIcon iconZip();
569 
570  private:
571  void init();
572 };
573 
574 #endif // QGSDATAITEM_H
575 
576 
A Collection: logical collection of layers or subcollections, e.g.
Definition: qgsdataitem.h:379
QStringList mSupportFormats
The list of supported formats.
Definition: qgsdataitem.h:361
QString path() const
Definition: qgsdataitem.h:190
virtual void childrenCreated()
virtual QString layerName() const
Definition: qgsdataitem.h:372
virtual bool hasDragEnabled() const override
Returns true if the item may be dragged.
Definition: qgsdataitem.h:460
QString providerKey() const
Returns provider key.
Definition: qgsdataitem.h:333
QString name() const
Definition: qgsdataitem.h:188
QString toolTip() const
Definition: qgsdataitem.h:202
QString mProviderKey
The provider key.
Definition: qgsdataitem.h:353
virtual bool handleDrop(const QMimeData *, Qt::DropAction)
Attempts to process the mime data dropped on this item.
Definition: qgsdataitem.h:130
virtual QIcon icon()
void addChild(QgsDataItem *item)
Definition: qgsdataitem.h:386
QString mIconName
Definition: qgsdataitem.h:238
State mState
Definition: qgsdataitem.h:230
Type type() const
Definition: qgsdataitem.h:177
void setIcon(const QIcon &icon)
Definition: qgsdataitem.h:198
Abstract base class for spatial data provider implementations.
void setToolTip(const QString &msg)
Definition: qgsdataitem.h:201
virtual QWidget * paramWidget()
Definition: qgsdataitem.h:113
QString mToolTip
Definition: qgsdataitem.h:237
A zip file: contains layers, using GDAL/OGR VSIFILE mechanism.
Definition: qgsdataitem.h:535
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:359
QString mFilePath
Definition: qgsdataitem.h:540
QgsDataItem * parent() const
Get item parent.
Definition: qgsdataitem.h:181
QString mName
Definition: qgsdataitem.h:231
LayerType
Types of layers that can be added to a map.
Definition: qgsmaplayer.h:90
QStringList mZipFileList
Definition: qgsdataitem.h:542
virtual void setCapabilities(Capabilities capabilities)
Sets the capabilities for the data item.
Definition: qgsdataitem.h:168
Children not yet created.
Definition: qgsdataitem.h:76
Creating children in separate thread (populating or refreshing)
Definition: qgsdataitem.h:77
Data item that can be used to report problems (e.g.
Definition: qgsdataitem.h:467
virtual bool hasDragEnabled() const
Returns true if the item may be dragged.
Definition: qgsdataitem.h:138
virtual QgsMimeDataUtils::Uri mimeUri() const
Return mime URI for the data item.
Definition: qgsdataitem.h:145
virtual QList< QAction * > actions()
Returns the list of actions available for this item.
Definition: qgsdataitem.h:118
A directory: contains subdirectories and layers.
Definition: qgsdataitem.h:395
static QStringList sProviderNames
Definition: qgsdataitem.h:553
Base class for all items in the model.
Definition: qgsdataitem.h:46
Capabilities mCapabilities
Definition: qgsdataitem.h:227
virtual void setState(State state)
Set item state.
void setName(const QString &name)
Definition: qgsdataitem.h:189
QVector< QgsDataItem * > children() const
Definition: qgsdataitem.h:186
QString mPath
Definition: qgsdataitem.h:236
QString mUri
The URI.
Definition: qgsdataitem.h:355
static QVector< dataItem_t * > sDataItemPtr
Definition: qgsdataitem.h:552
Contains various Favorites directories.
Definition: qgsdataitem.h:500
QString mVsiPrefix
Definition: qgsdataitem.h:541
QStringList supportedFormats() const
Returns the supported formats.
Definition: qgsdataitem.h:343
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:330
bool deferredDelete()
The item is scheduled to be deleted.
Definition: qgsdataitem.h:224
QString dirPath() const
Definition: qgsdataitem.h:423
virtual QVector< QgsDataItem * > createChildren()
Create children.
Item that represents a layer that can be opened with one of the providers.
Definition: qgsdataitem.h:296
QgsDataItem * dataItem_t(QString, QgsDataItem *)
Definition: qgsdataitem.h:40
virtual bool hasDragEnabled() const override
Returns true if the item may be dragged.
Definition: qgsdataitem.h:320
Data item that can be used to represent QGIS projects.
Definition: qgsdataitem.h:447
void setPath(const QString &path)
Definition: qgsdataitem.h:191
virtual bool acceptDrop()
Returns whether the item accepts drag and dropped layers - e.g.
Definition: qgsdataitem.h:124
LayerType mLayerType
The layer type.
Definition: qgsdataitem.h:357
QVector< QgsDataItem * > mChildren
Definition: qgsdataitem.h:229
virtual QString comments() const
Returns comments of the layer.
Definition: qgsdataitem.h:348
Represents a favorite item.
Definition: qgsdataitem.h:58
QStringList supportedCrs() const
Returns the supported CRS.
Definition: qgsdataitem.h:338
void setIconName(const QString &iconName)
Definition: qgsdataitem.h:199
static QString vsiPrefix(const QString &uri)
Definition: qgsdataitem.h:555
QMap< QString, QIcon > mIconMap
Definition: qgsdataitem.h:240
virtual Capabilities capabilities2() const
Definition: qgsdataitem.h:163