QGIS API Documentation  2.99.0-Master (37c43df)
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 <QFileSystemWatcher>
21 #include <QFutureWatcher>
22 #include <QIcon>
23 #include <QLibrary>
24 #include <QMovie>
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 class QgsDataProvider;
36 class QgsDataItem;
37 
38 typedef QgsDataItem * dataItem_t( QString, QgsDataItem* );
39 
43 class CORE_EXPORT QgsAnimatedIcon : public QObject
44 {
45  Q_OBJECT
46  public:
47 
50  QgsAnimatedIcon( const QString & iconPath = QString::null );
51 
52  QString iconPath() const;
53  void setIconPath( const QString & iconPath );
54  QIcon icon() const { return mIcon; }
55 
57  void connectFrameChanged( const QObject * receiver, const char * method );
59  void disconnectFrameChanged( const QObject * receiver, const char * method );
60 
61  public slots:
62  void onFrameChanged();
63 
64  signals:
66  void frameChanged();
67 
68  private:
69  void resetMovie();
70  int mCount; // number of listeners
71  QMovie * mMovie;
72  QIcon mIcon;
73 };
74 
79 class CORE_EXPORT QgsDataItem : public QObject
80 {
81  Q_OBJECT
82  Q_ENUMS( Type )
83  Q_ENUMS( State )
84  public:
85  enum Type
86  {
92  Project
93  };
94 
96  QgsDataItem( QgsDataItem::Type type, QgsDataItem* parent, const QString& name, const QString& path );
97  virtual ~QgsDataItem();
98 
99  bool hasChildren();
100 
101  int rowCount();
102 
105  virtual QVector<QgsDataItem*> createChildren();
106 
107  enum State
108  {
111  Populated
112  };
113 
115  State state() const;
116 
121  virtual void setState( State state );
122 
128  virtual void addChildItem( QgsDataItem *child, bool refresh = false );
129 
134  virtual void deleteChildItem( QgsDataItem * child );
135 
140  virtual QgsDataItem *removeChildItem( QgsDataItem * child );
141 
144  virtual bool equal( const QgsDataItem *other );
145 
146  virtual QWidget *paramWidget() { return nullptr; }
147 
151  virtual QList<QAction*> actions() { return QList<QAction*>(); }
152 
157  virtual bool acceptDrop() { return false; }
158 
163  virtual bool handleDrop( const QMimeData * /*data*/, Qt::DropAction /*action*/ ) { return false; }
164 
171  virtual bool hasDragEnabled() const { return false; }
172 
179 
181  {
182  NoCapabilities = 0,
183  SetCrs = 1 << 0,
184  Fertile = 1 << 1,
185  Fast = 1 << 2
186  };
187  Q_DECLARE_FLAGS( Capabilities, Capability )
188 
189 
193  virtual bool setCrs( const QgsCoordinateReferenceSystem& crs ) { Q_UNUSED( crs ); return false; }
194 
195  // ### QGIS 3 - rename to capabilities()
196  virtual Capabilities capabilities2() const { return mCapabilities; }
197 
201  virtual void setCapabilities( Capabilities capabilities ) { mCapabilities = capabilities; }
202 
203  // static methods
204 
205  // Find child index in vector of items using '==' operator
206  static int findItem( QVector<QgsDataItem*> items, QgsDataItem * item );
207 
208  // members
209 
210  Type type() const { return mType; }
211 
214  QgsDataItem* parent() const { return mParent; }
215 
218  void setParent( QgsDataItem* parent );
219  QVector<QgsDataItem*> children() const { return mChildren; }
220  virtual QIcon icon();
221  QString name() const { return mName; }
222  void setName( const QString &name ) { mName = name; }
223  QString path() const { return mPath; }
224  void setPath( const QString &path ) { mPath = path; }
226  static QString pathComponent( const QString &component );
227 
228  // Because QIcon (QPixmap) must not be used in outside the GUI thread, it is
229  // not possible to set mIcon in constructor. Either use mIconName/setIconName()
230  // or implement icon().
231  void setIcon( const QIcon& icon ) { mIcon = icon; }
232  void setIconName( const QString & iconName ) { mIconName = iconName; }
233 
234  void setToolTip( const QString& msg ) { mToolTip = msg; }
235  QString toolTip() const { return mToolTip; }
236 
237  // deleteLater() items anc clear the vector
238  static void deleteLater( QVector<QgsDataItem*> &items );
239 
241  void moveToThread( QThread * targetThread );
242 
243  protected:
244  virtual void populate( const QVector<QgsDataItem*>& children );
245 
249  virtual void refresh( const QVector<QgsDataItem *> &children );
250 
257  bool deferredDelete() { return mDeferredDelete; }
258 
260  Capabilities mCapabilities;
262  QVector<QgsDataItem*> mChildren; // easier to have it always
264  QString mName;
265  // Path is slash ('/') separated chain of item identifiers which are usually item names, but may be differen if it is
266  // necessary to distinguish paths of two providers to the same source (e.g GRASS location and standard directory have the same
267  // name but different paths). Identifiers in path must not contain '/' characters.
268  // The path is used to identify item in tree.
269  QString mPath;
270  QString mToolTip;
271  QString mIconName;
272  QIcon mIcon;
273  QMap<QString, QIcon> mIconMap;
274 
275  public slots:
276 
284  virtual void deleteLater();
285 
286  // Populate children using children vector created by createChildren()
287  // @param foreground run createChildren in foreground
288  virtual void populate( bool foreground = false );
289 
291  virtual void depopulate();
292 
293  virtual void refresh();
294 
295  void emitDataChanged();
296  virtual void childrenCreated();
297 
298  signals:
299  void beginInsertItems( QgsDataItem* parent, int first, int last );
300  void endInsertItems();
301  void beginRemoveItems( QgsDataItem* parent, int first, int last );
302  void endRemoveItems();
303  void dataChanged( QgsDataItem * item );
304  void stateChanged( QgsDataItem * item, QgsDataItem::State oldState );
305 
306  private:
307  static QVector<QgsDataItem*> runCreateChildren( QgsDataItem* item );
308 
309  // Set to true if object has to be deleted when possible (nothing running in threads)
310  bool mDeferredDelete;
311  QFutureWatcher< QVector <QgsDataItem*> > *mFutureWatcher;
312  // number of items currently in loading (populating) state
313  static QgsAnimatedIcon * mPopulatingIcon;
314 };
315 
316 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsDataItem::Capabilities )
317 
318 
321 class CORE_EXPORT QgsLayerItem : public QgsDataItem
322 {
323  Q_OBJECT
324  public:
326  {
336  Plugin
337  };
338 
339  QgsLayerItem( QgsDataItem* parent, const QString& name, const QString& path, const QString& uri, LayerType layerType, const QString& providerKey );
340 
341  // --- reimplemented from QgsDataItem ---
342 
343  virtual bool equal( const QgsDataItem *other ) override;
344 
345  virtual bool hasDragEnabled() const override { return true; }
346 
347  virtual QgsMimeDataUtils::Uri mimeUri() const override;
348 
349  // --- New virtual methods for layer item derived classes ---
350 
352  QgsMapLayer::LayerType mapLayerType() const;
353 
355  QString uri() const { return mUri; }
356 
358  QString providerKey() const { return mProviderKey; }
359 
363  QStringList supportedCrs() const { return mSupportedCRS; }
364 
368  QStringList supportedFormats() const { return mSupportFormats; }
369 
373  virtual QString comments() const { return QString(); }
374 
375  protected:
376 
378  QString mProviderKey;
380  QString mUri;
384  QStringList mSupportedCRS;
386  QStringList mSupportFormats;
387 
388  public:
389  static const QIcon &iconPoint();
390  static const QIcon &iconLine();
391  static const QIcon &iconPolygon();
392  static const QIcon &iconTable();
393  static const QIcon &iconRaster();
394  static const QIcon &iconDefault();
395 
397  virtual QString layerName() const { return name(); }
398 };
399 
400 
404 class CORE_EXPORT QgsDataCollectionItem : public QgsDataItem
405 {
406  Q_OBJECT
407  public:
408  QgsDataCollectionItem( QgsDataItem* parent, const QString& name, const QString& path = QString::null );
410 
411  void addChild( QgsDataItem *item ) { mChildren.append( item ); }
412 
413  static const QIcon &iconDir(); // shared icon: open/closed directory
414  static const QIcon &iconDataCollection(); // default icon for data collection
415 };
416 
420 class CORE_EXPORT QgsDirectoryItem : public QgsDataCollectionItem
421 {
422  Q_OBJECT
423  public:
424  enum Column
425  {
433  };
434 
435  QgsDirectoryItem( QgsDataItem* parent, const QString& name, const QString& path );
436 
442  QgsDirectoryItem( QgsDataItem* parent, const QString& name, const QString& dirPath, const QString& path );
443  ~QgsDirectoryItem();
444 
445  virtual void setState( State state ) override;
446 
447  QVector<QgsDataItem*> createChildren() override;
448 
449  QString dirPath() const { return mDirPath; }
450  virtual bool equal( const QgsDataItem *other ) override;
451  virtual QIcon icon() override;
452  virtual QWidget *paramWidget() override;
453 
455  static bool hiddenPath( const QString &path );
456 
457  public slots:
458  virtual void childrenCreated() override;
459  void directoryChanged();
460 
461  protected:
462  void init();
463  QString mDirPath;
464 
465  private:
466  QFileSystemWatcher * mFileSystemWatcher;
467  bool mRefreshLater;
468 };
469 
473 class CORE_EXPORT QgsProjectItem : public QgsDataItem
474 {
475  Q_OBJECT
476  public:
477 
484  QgsProjectItem( QgsDataItem* parent, const QString& name, const QString& path );
485  ~QgsProjectItem();
486 
487  virtual bool hasDragEnabled() const override { return true; }
488 
489 };
490 
494 class CORE_EXPORT QgsErrorItem : public QgsDataItem
495 {
496  Q_OBJECT
497  public:
498 
499  QgsErrorItem( QgsDataItem* parent, const QString& error, const QString& path );
500  ~QgsErrorItem();
501 
502 };
503 
504 
505 // ---------
506 
510 class CORE_EXPORT QgsDirectoryParamWidget : public QTreeWidget
511 {
512  Q_OBJECT
513 
514  public:
515  QgsDirectoryParamWidget( const QString& path, QWidget* parent = nullptr );
516 
517  protected:
518  void mousePressEvent( QMouseEvent* event ) override;
519 
520  public slots:
521  void showHideColumn();
522 };
523 
528 class CORE_EXPORT QgsFavoritesItem : public QgsDataCollectionItem
529 {
530  Q_OBJECT
531  public:
532 
537  QgsFavoritesItem( QgsDataItem* parent, const QString& name, const QString& path = QString() );
538 
539  ~QgsFavoritesItem();
540 
541  QVector<QgsDataItem*> createChildren() override;
542 
547  void addDirectory( const QString& directory );
548 
553  void removeDirectory( QgsDirectoryItem *item );
554 
556  static const QIcon &iconFavorites();
557 
558  private:
559  QVector<QgsDataItem*> createChildren( const QString& favDir );
560 };
561 
565 class CORE_EXPORT QgsZipItem : public QgsDataCollectionItem
566 {
567  Q_OBJECT
568 
569  protected:
570  QString mFilePath;
571  QString mVsiPrefix;
572  QStringList mZipFileList;
573 
574  public:
575  QgsZipItem( QgsDataItem* parent, const QString& name, const QString& path );
576  QgsZipItem( QgsDataItem* parent, const QString& name, const QString& filePath, const QString& path );
577  ~QgsZipItem();
578 
579  QVector<QgsDataItem*> createChildren() override;
580  const QStringList & getZipFileList();
581 
583  static QVector<dataItem_t *> mDataItemPtr;
584  static QStringList mProviderNames;
585 
586  static QString vsiPrefix( const QString& uri ) { return qgsVsiPrefix( uri ); }
587 
591  static QgsDataItem* itemFromPath( QgsDataItem* parent, const QString &path, const QString &name );
592 
597  static QgsDataItem* itemFromPath( QgsDataItem* parent, const QString& filePath, const QString& name, const QString& path );
598 
599  static const QIcon &iconZip();
600 
601  private:
602  void init();
603 };
604 
605 #endif // QGSDATAITEM_H
606 
607 
A Collection: logical collection of layers or subcollections, e.g.
Definition: qgsdataitem.h:404
QStringList mSupportFormats
The list of supported formats.
Definition: qgsdataitem.h:386
QString path() const
Definition: qgsdataitem.h:223
virtual void childrenCreated()
virtual QString layerName() const
Definition: qgsdataitem.h:397
virtual bool hasDragEnabled() const override
Returns true if the item may be dragged.
Definition: qgsdataitem.h:487
QString providerKey() const
Returns provider key.
Definition: qgsdataitem.h:358
QString name() const
Definition: qgsdataitem.h:221
QString toolTip() const
Definition: qgsdataitem.h:235
QVector< QgsDataItem * > children() const
Definition: qgsdataitem.h:219
QString mProviderKey
The provider key.
Definition: qgsdataitem.h:378
virtual bool handleDrop(const QMimeData *, Qt::DropAction)
Attempts to process the mime data dropped on this item.
Definition: qgsdataitem.h:163
virtual QIcon icon()
void addChild(QgsDataItem *item)
Definition: qgsdataitem.h:411
QString mIconName
Definition: qgsdataitem.h:271
State mState
Definition: qgsdataitem.h:263
Type type() const
Definition: qgsdataitem.h:210
void setIcon(const QIcon &icon)
Definition: qgsdataitem.h:231
Abstract base class for spatial data provider implementations.
void setToolTip(const QString &msg)
Definition: qgsdataitem.h:234
QString iconPath(const QString &iconFile)
virtual QWidget * paramWidget()
Definition: qgsdataitem.h:146
QString mToolTip
Definition: qgsdataitem.h:270
A zip file: contains layers, using GDAL/OGR VSIFILE mechanism.
Definition: qgsdataitem.h:565
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:384
QString mFilePath
Definition: qgsdataitem.h:570
QgsDataItem * parent() const
Get item parent.
Definition: qgsdataitem.h:214
QString mName
Definition: qgsdataitem.h:264
LayerType
Types of layers that can be added to a map.
Definition: qgsmaplayer.h:58
QStringList mZipFileList
Definition: qgsdataitem.h:572
virtual void setCapabilities(Capabilities capabilities)
Sets the capabilities for the data item.
Definition: qgsdataitem.h:201
Children not yet created.
Definition: qgsdataitem.h:109
Creating children in separate thread (populating or refreshing)
Definition: qgsdataitem.h:110
Data item that can be used to report problems (e.g.
Definition: qgsdataitem.h:494
virtual bool hasDragEnabled() const
Returns true if the item may be dragged.
Definition: qgsdataitem.h:171
virtual QgsMimeDataUtils::Uri mimeUri() const
Return mime URI for the data item.
Definition: qgsdataitem.h:178
static QStringList mProviderNames
Definition: qgsdataitem.h:584
A directory: contains subdirectories and layers.
Definition: qgsdataitem.h:420
Base class for all items in the model.
Definition: qgsdataitem.h:79
Capabilities mCapabilities
Definition: qgsdataitem.h:260
virtual void setState(State state)
Set item state.
void setName(const QString &name)
Definition: qgsdataitem.h:222
static QVector< dataItem_t * > mDataItemPtr
Definition: qgsdataitem.h:583
QString mPath
Definition: qgsdataitem.h:269
QString mUri
The URI.
Definition: qgsdataitem.h:380
Contains various Favorites directories.
Definition: qgsdataitem.h:528
QString mVsiPrefix
Definition: qgsdataitem.h:571
QStringList supportedFormats() const
Returns the supported formats.
Definition: qgsdataitem.h:368
QString qgsVsiPrefix(const QString &path)
Definition: qgis.cpp:213
This class represents a coordinate reference system (CRS).
Animated icon is keeping an animation running if there are listeners connected to frameChanged...
Definition: qgsdataitem.h:43
QString uri() const
Returns layer uri or empty string if layer cannot be created.
Definition: qgsdataitem.h:355
QgsDataItem * mParent
Definition: qgsdataitem.h:261
bool deferredDelete()
The item is scheduled to be deleted.
Definition: qgsdataitem.h:257
QString dirPath() const
Definition: qgsdataitem.h:449
QIcon icon() const
Definition: qgsdataitem.h:54
virtual QVector< QgsDataItem * > createChildren()
Create children.
Item that represents a layer that can be opened with one of the providers.
Definition: qgsdataitem.h:321
QgsDataItem * dataItem_t(QString, QgsDataItem *)
Definition: qgsdataitem.h:38
QVector< QgsDataItem * > mChildren
Definition: qgsdataitem.h:262
virtual QList< QAction * > actions()
Returns the list of actions available for this item.
Definition: qgsdataitem.h:151
virtual bool hasDragEnabled() const override
Returns true if the item may be dragged.
Definition: qgsdataitem.h:345
Data item that can be used to represent QGIS projects.
Definition: qgsdataitem.h:473
void setPath(const QString &path)
Definition: qgsdataitem.h:224
virtual bool acceptDrop()
Returns whether the item accepts drag and dropped layers - e.g.
Definition: qgsdataitem.h:157
LayerType mLayerType
The layer type.
Definition: qgsdataitem.h:382
virtual QString comments() const
Returns comments of the layer.
Definition: qgsdataitem.h:373
Represents a favorite item.
Definition: qgsdataitem.h:91
QStringList supportedCrs() const
Returns the supported CRS.
Definition: qgsdataitem.h:363
void setIconName(const QString &iconName)
Definition: qgsdataitem.h:232
static QString vsiPrefix(const QString &uri)
Definition: qgsdataitem.h:586
QMap< QString, QIcon > mIconMap
Definition: qgsdataitem.h:273
virtual Capabilities capabilities2() const
Definition: qgsdataitem.h:196