QGIS API Documentation  2.99.0-Master (90ae728)
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 <QMovie>
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 class QgsDataProvider;
37 class QgsDataItem;
38 
39 typedef QgsDataItem * dataItem_t( QString, QgsDataItem* );
40 
44 class CORE_EXPORT QgsAnimatedIcon : public QObject
45 {
46  Q_OBJECT
47  public:
48 
51  QgsAnimatedIcon( const QString & iconPath = QString::null );
52 
53  QString iconPath() const;
54  void setIconPath( const QString & iconPath );
55  QIcon icon() const { return mIcon; }
56 
58  void connectFrameChanged( const QObject * receiver, const char * method );
60  void disconnectFrameChanged( const QObject * receiver, const char * method );
61 
62  public slots:
63  void onFrameChanged();
64 
65  signals:
67  void frameChanged();
68 
69  private:
70  void resetMovie();
71  int mCount; // number of listeners
72  QMovie * mMovie = nullptr;
73  QIcon mIcon;
74 };
75 
80 class CORE_EXPORT QgsDataItem : public QObject
81 {
82  Q_OBJECT
83  Q_ENUMS( Type )
84  Q_ENUMS( State )
85  public:
86  enum Type
87  {
93  Project
94  };
95 
97  QgsDataItem( QgsDataItem::Type type, QgsDataItem* parent, const QString& name, const QString& path );
98  virtual ~QgsDataItem();
99 
100  bool hasChildren();
101 
102  int rowCount();
103 
106  virtual QVector<QgsDataItem*> createChildren();
107 
108  enum State
109  {
112  Populated
113  };
114 
116  State state() const;
117 
122  virtual void setState( State state );
123 
129  virtual void addChildItem( QgsDataItem *child, bool refresh = false );
130 
135  virtual void deleteChildItem( QgsDataItem * child );
136 
141  virtual QgsDataItem *removeChildItem( QgsDataItem * child );
142 
145  virtual bool equal( const QgsDataItem *other );
146 
147  virtual QWidget *paramWidget() { return nullptr; }
148 
152  virtual QList<QAction*> actions() { return QList<QAction*>(); }
153 
158  virtual bool acceptDrop() { return false; }
159 
164  virtual bool handleDrop( const QMimeData * /*data*/, Qt::DropAction /*action*/ ) { return false; }
165 
172  virtual bool hasDragEnabled() const { return false; }
173 
180 
182  {
183  NoCapabilities = 0,
184  SetCrs = 1 << 0,
185  Fertile = 1 << 1,
186  Fast = 1 << 2
187  };
188  Q_DECLARE_FLAGS( Capabilities, Capability )
189 
190 
194  virtual bool setCrs( const QgsCoordinateReferenceSystem& crs ) { Q_UNUSED( crs ); return false; }
195 
196  // ### QGIS 3 - rename to capabilities()
197  virtual Capabilities capabilities2() const { return mCapabilities; }
198 
202  virtual void setCapabilities( Capabilities capabilities ) { mCapabilities = capabilities; }
203 
204  // static methods
205 
206  // Find child index in vector of items using '==' operator
207  static int findItem( QVector<QgsDataItem*> items, QgsDataItem * item );
208 
209  // members
210 
211  Type type() const { return mType; }
212 
215  QgsDataItem* parent() const { return mParent; }
216 
219  void setParent( QgsDataItem* parent );
220  QVector<QgsDataItem*> children() const { return mChildren; }
221  virtual QIcon icon();
222  QString name() const { return mName; }
223  void setName( const QString &name ) { mName = name; }
224  QString path() const { return mPath; }
225  void setPath( const QString &path ) { mPath = path; }
227  static QString pathComponent( const QString &component );
228 
229  // Because QIcon (QPixmap) must not be used in outside the GUI thread, it is
230  // not possible to set mIcon in constructor. Either use mIconName/setIconName()
231  // or implement icon().
232  void setIcon( const QIcon& icon ) { mIcon = icon; }
233  void setIconName( const QString & iconName ) { mIconName = iconName; }
234 
235  void setToolTip( const QString& msg ) { mToolTip = msg; }
236  QString toolTip() const { return mToolTip; }
237 
238  // deleteLater() items anc clear the vector
239  static void deleteLater( QVector<QgsDataItem*> &items );
240 
242  void moveToThread( QThread * targetThread );
243 
244  protected:
245  virtual void populate( const QVector<QgsDataItem*>& children );
246 
250  virtual void refresh( const QVector<QgsDataItem *> &children );
251 
258  bool deferredDelete() { return mDeferredDelete; }
259 
261  Capabilities mCapabilities;
262  QgsDataItem* mParent = nullptr;
263  QVector<QgsDataItem*> mChildren; // easier to have it always
265  QString mName;
266  // Path is slash ('/') separated chain of item identifiers which are usually item names, but may be differen if it is
267  // necessary to distinguish paths of two providers to the same source (e.g GRASS location and standard directory have the same
268  // name but different paths). Identifiers in path must not contain '/' characters.
269  // The path is used to identify item in tree.
270  QString mPath;
271  QString mToolTip;
272  QString mIconName;
273  QIcon mIcon;
274  QMap<QString, QIcon> mIconMap;
275 
276  public slots:
277 
285  virtual void deleteLater();
286 
287  // Populate children using children vector created by createChildren()
288  // @param foreground run createChildren in foreground
289  virtual void populate( bool foreground = false );
290 
292  virtual void depopulate();
293 
294  virtual void refresh();
295 
296  void emitDataChanged();
297  virtual void childrenCreated();
298 
299  signals:
300  void beginInsertItems( QgsDataItem* parent, int first, int last );
301  void endInsertItems();
302  void beginRemoveItems( QgsDataItem* parent, int first, int last );
303  void endRemoveItems();
304  void dataChanged( QgsDataItem * item );
305  void stateChanged( QgsDataItem * item, QgsDataItem::State oldState );
306 
307  private:
308  static QVector<QgsDataItem*> runCreateChildren( QgsDataItem* item );
309 
310  // Set to true if object has to be deleted when possible (nothing running in threads)
311  bool mDeferredDelete;
312  QFutureWatcher< QVector <QgsDataItem*> > *mFutureWatcher;
313  // number of items currently in loading (populating) state
314  static QgsAnimatedIcon * sPopulatingIcon;
315 };
316 
317 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsDataItem::Capabilities )
318 
319 
322 class CORE_EXPORT QgsLayerItem : public QgsDataItem
323 {
324  Q_OBJECT
325  public:
327  {
337  Plugin
338  };
339 
340  QgsLayerItem( QgsDataItem* parent, const QString& name, const QString& path, const QString& uri, LayerType layerType, const QString& providerKey );
341 
342  // --- reimplemented from QgsDataItem ---
343 
344  virtual bool equal( const QgsDataItem *other ) override;
345 
346  virtual bool hasDragEnabled() const override { return true; }
347 
348  virtual QgsMimeDataUtils::Uri mimeUri() const override;
349 
350  // --- New virtual methods for layer item derived classes ---
351 
353  QgsMapLayer::LayerType mapLayerType() const;
354 
356  QString uri() const { return mUri; }
357 
359  QString providerKey() const { return mProviderKey; }
360 
364  QStringList supportedCrs() const { return mSupportedCRS; }
365 
369  QStringList supportedFormats() const { return mSupportFormats; }
370 
374  virtual QString comments() const { return QString(); }
375 
376  protected:
377 
379  QString mProviderKey;
381  QString mUri;
385  QStringList mSupportedCRS;
387  QStringList mSupportFormats;
388 
389  public:
390  static QIcon iconPoint();
391  static QIcon iconLine();
392  static QIcon iconPolygon();
393  static QIcon iconTable();
394  static QIcon iconRaster();
395  static QIcon iconDefault();
396 
398  virtual QString layerName() const { return name(); }
399 };
400 
401 
405 class CORE_EXPORT QgsDataCollectionItem : public QgsDataItem
406 {
407  Q_OBJECT
408  public:
409  QgsDataCollectionItem( QgsDataItem* parent, const QString& name, const QString& path = QString::null );
411 
412  void addChild( QgsDataItem *item ) { mChildren.append( item ); }
413 
414  static QIcon iconDir(); // shared icon: open/closed directory
415  static QIcon iconDataCollection(); // default icon for data collection
416 };
417 
421 class CORE_EXPORT QgsDirectoryItem : public QgsDataCollectionItem
422 {
423  Q_OBJECT
424  public:
425  enum Column
426  {
434  };
435 
436  QgsDirectoryItem( QgsDataItem* parent, const QString& name, const QString& path );
437 
443  QgsDirectoryItem( QgsDataItem* parent, const QString& name, const QString& dirPath, const QString& path );
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 = nullptr;
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 
486  virtual bool hasDragEnabled() const override { return true; }
487 
488 };
489 
493 class CORE_EXPORT QgsErrorItem : public QgsDataItem
494 {
495  Q_OBJECT
496  public:
497 
498  QgsErrorItem( QgsDataItem* parent, const QString& error, const QString& path );
499 
500 };
501 
502 
503 // ---------
504 
508 class CORE_EXPORT QgsDirectoryParamWidget : public QTreeWidget
509 {
510  Q_OBJECT
511 
512  public:
513  QgsDirectoryParamWidget( const QString& path, QWidget* parent = nullptr );
514 
515  protected:
516  void mousePressEvent( QMouseEvent* event ) override;
517 
518  public slots:
519  void showHideColumn();
520 };
521 
526 class CORE_EXPORT QgsFavoritesItem : public QgsDataCollectionItem
527 {
528  Q_OBJECT
529  public:
530 
535  QgsFavoritesItem( QgsDataItem* parent, const QString& name, const QString& path = QString() );
536 
537  QVector<QgsDataItem*> createChildren() override;
538 
543  void addDirectory( const QString& directory );
544 
549  void removeDirectory( QgsDirectoryItem *item );
550 
552  static QIcon iconFavorites();
553 
554  private:
555  QVector<QgsDataItem*> createChildren( const QString& favDir );
556 };
557 
561 class CORE_EXPORT QgsZipItem : public QgsDataCollectionItem
562 {
563  Q_OBJECT
564 
565  protected:
566  QString mFilePath;
567  QString mVsiPrefix;
568  QStringList mZipFileList;
569 
570  public:
571  QgsZipItem( QgsDataItem* parent, const QString& name, const QString& path );
572  QgsZipItem( QgsDataItem* parent, const QString& name, const QString& filePath, const QString& path );
573 
574  QVector<QgsDataItem*> createChildren() override;
575  QStringList getZipFileList();
576 
578  static QVector<dataItem_t *> sDataItemPtr;
579  static QStringList sProviderNames;
580 
581  static QString vsiPrefix( const QString& uri ) { return qgsVsiPrefix( uri ); }
582 
586  static QgsDataItem* itemFromPath( QgsDataItem* parent, const QString &path, const QString &name );
587 
592  static QgsDataItem* itemFromPath( QgsDataItem* parent, const QString& filePath, const QString& name, const QString& path );
593 
594  static QIcon iconZip();
595 
596  private:
597  void init();
598 };
599 
600 #endif // QGSDATAITEM_H
601 
602 
A Collection: logical collection of layers or subcollections, e.g.
Definition: qgsdataitem.h:405
QStringList mSupportFormats
The list of supported formats.
Definition: qgsdataitem.h:387
QString path() const
Definition: qgsdataitem.h:224
virtual void childrenCreated()
virtual QString layerName() const
Definition: qgsdataitem.h:398
virtual bool hasDragEnabled() const override
Returns true if the item may be dragged.
Definition: qgsdataitem.h:486
QString providerKey() const
Returns provider key.
Definition: qgsdataitem.h:359
QString name() const
Definition: qgsdataitem.h:222
QString toolTip() const
Definition: qgsdataitem.h:236
QVector< QgsDataItem * > children() const
Definition: qgsdataitem.h:220
QString mProviderKey
The provider key.
Definition: qgsdataitem.h:379
virtual bool handleDrop(const QMimeData *, Qt::DropAction)
Attempts to process the mime data dropped on this item.
Definition: qgsdataitem.h:164
virtual QIcon icon()
void addChild(QgsDataItem *item)
Definition: qgsdataitem.h:412
QString mIconName
Definition: qgsdataitem.h:272
State mState
Definition: qgsdataitem.h:264
Type type() const
Definition: qgsdataitem.h:211
void setIcon(const QIcon &icon)
Definition: qgsdataitem.h:232
Abstract base class for spatial data provider implementations.
void setToolTip(const QString &msg)
Definition: qgsdataitem.h:235
QString iconPath(const QString &iconFile)
virtual QWidget * paramWidget()
Definition: qgsdataitem.h:147
QString mToolTip
Definition: qgsdataitem.h:271
A zip file: contains layers, using GDAL/OGR VSIFILE mechanism.
Definition: qgsdataitem.h:561
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:385
QString mFilePath
Definition: qgsdataitem.h:566
QgsDataItem * parent() const
Get item parent.
Definition: qgsdataitem.h:215
QString mName
Definition: qgsdataitem.h:265
LayerType
Types of layers that can be added to a map.
Definition: qgsmaplayer.h:62
QStringList mZipFileList
Definition: qgsdataitem.h:568
virtual void setCapabilities(Capabilities capabilities)
Sets the capabilities for the data item.
Definition: qgsdataitem.h:202
Children not yet created.
Definition: qgsdataitem.h:110
Creating children in separate thread (populating or refreshing)
Definition: qgsdataitem.h:111
Data item that can be used to report problems (e.g.
Definition: qgsdataitem.h:493
virtual bool hasDragEnabled() const
Returns true if the item may be dragged.
Definition: qgsdataitem.h:172
virtual QgsMimeDataUtils::Uri mimeUri() const
Return mime URI for the data item.
Definition: qgsdataitem.h:179
A directory: contains subdirectories and layers.
Definition: qgsdataitem.h:421
static QStringList sProviderNames
Definition: qgsdataitem.h:579
Base class for all items in the model.
Definition: qgsdataitem.h:80
Capabilities mCapabilities
Definition: qgsdataitem.h:261
virtual void setState(State state)
Set item state.
void setName(const QString &name)
Definition: qgsdataitem.h:223
QString mPath
Definition: qgsdataitem.h:270
QString mUri
The URI.
Definition: qgsdataitem.h:381
static QVector< dataItem_t * > sDataItemPtr
Definition: qgsdataitem.h:578
Contains various Favorites directories.
Definition: qgsdataitem.h:526
QString mVsiPrefix
Definition: qgsdataitem.h:567
QStringList supportedFormats() const
Returns the supported formats.
Definition: qgsdataitem.h:369
QString qgsVsiPrefix(const QString &path)
Definition: qgis.cpp:210
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:44
QString uri() const
Returns layer uri or empty string if layer cannot be created.
Definition: qgsdataitem.h:356
bool deferredDelete()
The item is scheduled to be deleted.
Definition: qgsdataitem.h:258
QString dirPath() const
Definition: qgsdataitem.h:449
QIcon icon() const
Definition: qgsdataitem.h:55
virtual QVector< QgsDataItem * > createChildren()
Create children.
Item that represents a layer that can be opened with one of the providers.
Definition: qgsdataitem.h:322
QgsDataItem * dataItem_t(QString, QgsDataItem *)
Definition: qgsdataitem.h:39
QVector< QgsDataItem * > mChildren
Definition: qgsdataitem.h:263
virtual QList< QAction * > actions()
Returns the list of actions available for this item.
Definition: qgsdataitem.h:152
virtual bool hasDragEnabled() const override
Returns true if the item may be dragged.
Definition: qgsdataitem.h:346
Data item that can be used to represent QGIS projects.
Definition: qgsdataitem.h:473
void setPath(const QString &path)
Definition: qgsdataitem.h:225
virtual bool acceptDrop()
Returns whether the item accepts drag and dropped layers - e.g.
Definition: qgsdataitem.h:158
LayerType mLayerType
The layer type.
Definition: qgsdataitem.h:383
virtual QString comments() const
Returns comments of the layer.
Definition: qgsdataitem.h:374
Represents a favorite item.
Definition: qgsdataitem.h:92
QStringList supportedCrs() const
Returns the supported CRS.
Definition: qgsdataitem.h:364
void setIconName(const QString &iconName)
Definition: qgsdataitem.h:233
static QString vsiPrefix(const QString &uri)
Definition: qgsdataitem.h:581
QMap< QString, QIcon > mIconMap
Definition: qgsdataitem.h:274
virtual Capabilities capabilities2() const
Definition: qgsdataitem.h:197