QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 "qgsapplication.h"
32 #include "qgsmaplayer.h"
34 
35 class QgsDataProvider;
36 class QgsDataItem;
37 
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 
124  Q_DECL_DEPRECATED bool isPopulated() { return state() == Populated; }
125 
131  virtual void addChildItem( QgsDataItem *child, bool refresh = false );
132 
137  virtual void deleteChildItem( QgsDataItem * child );
138 
143  virtual QgsDataItem *removeChildItem( QgsDataItem * child );
144 
147  virtual bool equal( const QgsDataItem *other );
148 
149  virtual QWidget *paramWidget() { return nullptr; }
150 
154  virtual QList<QAction*> actions() { return QList<QAction*>(); }
155 
160  virtual bool acceptDrop() { return false; }
161 
166  virtual bool handleDrop( const QMimeData * /*data*/, Qt::DropAction /*action*/ ) { return false; }
167 
169  {
170  NoCapabilities = 0,
171  SetCrs = 1 << 0,
172  Fertile = 1 << 1,
173  Fast = 1 << 2,
174  Collapse = 1 << 3
175  };
176  Q_DECLARE_FLAGS( Capabilities, Capability )
177 
178  // This will _write_ selected crs in data source
179  virtual bool setCrs( QgsCoordinateReferenceSystem crs )
180  { Q_UNUSED( crs ); return false; }
181 
183  Q_DECL_DEPRECATED virtual Capability capabilities() { return NoCapabilities; }
184 
185  virtual Capabilities capabilities2() const { return mCapabilities; }
186 
187  virtual void setCapabilities( const Capabilities& capabilities ) { mCapabilities = capabilities; }
188 
189  // static methods
190 
191  // Find child index in vector of items using '==' operator
192  static int findItem( QVector<QgsDataItem*> items, QgsDataItem * item );
193 
194  // members
195 
196  Type type() const { return mType; }
197 
200  QgsDataItem* parent() const { return mParent; }
203  void setParent( QgsDataItem* parent );
204  QVector<QgsDataItem*> children() const { return mChildren; }
205  virtual QIcon icon();
206  QString name() const { return mName; }
207  void setName( const QString &name ) { mName = name; }
208  QString path() const { return mPath; }
209  void setPath( const QString &path ) { mPath = path; }
211  static QString pathComponent( const QString &component );
212 
213  // Because QIcon (QPixmap) must not be used in outside the GUI thread, it is
214  // not possible to set mIcon in constructor. Either use mIconName/setIconName()
215  // or implement icon().
216  void setIcon( const QIcon& icon ) { mIcon = icon; }
217  void setIconName( const QString & iconName ) { mIconName = iconName; }
218 
219  void setToolTip( const QString& msg ) { mToolTip = msg; }
220  QString toolTip() const { return mToolTip; }
221 
222  // deleteLater() items anc clear the vector
223  static void deleteLater( QVector<QgsDataItem*> &items );
224 
226  void moveToThread( QThread * targetThread );
227 
228  protected:
229  virtual void populate( const QVector<QgsDataItem*>& children );
230  virtual void refresh( QVector<QgsDataItem*> children );
237  bool deferredDelete() { return mDeferredDelete; }
238 
240  Capabilities mCapabilities;
242  QVector<QgsDataItem*> mChildren; // easier to have it always
247  // Path is slash ('/') separated chain of item identifiers which are usually item names, but may be differen if it is
248  // necessary to distinguish paths of two providers to the same source (e.g GRASS location and standard directory have the same
249  // name but different paths). Identifiers in path must not contain '/' characters.
250  // The path is used to identify item in tree.
256 
257  public slots:
265  virtual void deleteLater();
266 
267  // Populate children using children vector created by createChildren()
268  // @param foreground run createChildren in foreground
269  virtual void populate( bool foreground = false );
270 
272  virtual void depopulate();
273 
274  virtual void refresh();
275 
276  void emitBeginInsertItems( QgsDataItem* parent, int first, int last );
277  void emitEndInsertItems();
278  void emitBeginRemoveItems( QgsDataItem* parent, int first, int last );
279  void emitEndRemoveItems();
280  void emitDataChanged( QgsDataItem* item );
281  void emitDataChanged();
282  void emitStateChanged( QgsDataItem* item, QgsDataItem::State oldState );
283  virtual void childrenCreated();
284 
285  signals:
286  void beginInsertItems( QgsDataItem* parent, int first, int last );
287  void endInsertItems();
288  void beginRemoveItems( QgsDataItem* parent, int first, int last );
289  void endRemoveItems();
290  void dataChanged( QgsDataItem * item );
291  void stateChanged( QgsDataItem * item, QgsDataItem::State oldState );
292 
293  private:
294  static QVector<QgsDataItem*> runCreateChildren( QgsDataItem* item );
295 
296  // Set to true if object has to be deleted when possible (nothing running in threads)
297  bool mDeferredDelete;
298  QFutureWatcher< QVector <QgsDataItem*> > *mFutureWatcher;
299  // number of items currently in loading (populating) state
300  static QgsAnimatedIcon * mPopulatingIcon;
301 };
302 
303 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsDataItem::Capabilities )
304 
305 
308 class CORE_EXPORT QgsLayerItem : public QgsDataItem
309 {
310  Q_OBJECT
311  public:
313  {
323  Plugin
324  };
325 
326  QgsLayerItem( QgsDataItem* parent, const QString& name, const QString& path, const QString& uri, LayerType layerType, const QString& providerKey );
327 
328  // --- reimplemented from QgsDataItem ---
329 
330  virtual bool equal( const QgsDataItem *other ) override;
331 
332  // --- New virtual methods for layer item derived classes ---
333 
335  QgsMapLayer::LayerType mapLayerType();
336 
338  QString uri() { return mUri; }
339 
341  QString providerKey() { return mProviderKey; }
342 
346  QStringList supportedCRS() { return mSupportedCRS; }
347 
351  QStringList supportedFormats() { return mSupportFormats; }
352 
356  virtual QString comments() const { return QString(); }
357 
358  protected:
359 
370 
371  public:
372  static const QIcon &iconPoint();
373  static const QIcon &iconLine();
374  static const QIcon &iconPolygon();
375  static const QIcon &iconTable();
376  static const QIcon &iconRaster();
377  static const QIcon &iconDefault();
378 
380  virtual QString layerName() const { return name(); }
381 };
382 
383 
387 class CORE_EXPORT QgsDataCollectionItem : public QgsDataItem
388 {
389  Q_OBJECT
390  public:
391  QgsDataCollectionItem( QgsDataItem* parent, const QString& name, const QString& path = QString::null );
393 
394  void addChild( QgsDataItem *item ) { mChildren.append( item ); }
395 
396  static const QIcon &iconDir(); // shared icon: open/closed directory
397  static const QIcon &iconDataCollection(); // default icon for data collection
398 };
399 
403 class CORE_EXPORT QgsDirectoryItem : public QgsDataCollectionItem
404 {
405  Q_OBJECT
406  public:
407  enum Column
408  {
416  };
417 
418  QgsDirectoryItem( QgsDataItem* parent, const QString& name, const QString& path );
419 
425  QgsDirectoryItem( QgsDataItem* parent, const QString& name, const QString& dirPath, const QString& path );
426  ~QgsDirectoryItem();
427 
428  virtual void setState( State state ) override;
429 
431 
432  QString dirPath() const { return mDirPath; }
433  virtual bool equal( const QgsDataItem *other ) override;
434  virtual QIcon icon() override;
435  virtual QWidget *paramWidget() override;
436 
437  /* static QVector<QgsDataProvider*> mProviders; */
440  Q_DECL_DEPRECATED static QVector<QLibrary*> mLibraries;
441 
443  static bool hiddenPath( QString path );
444 
445  public slots:
446  virtual void childrenCreated() override;
447  void directoryChanged();
448 
449  protected:
450  void init();
452 
453  private:
454  QFileSystemWatcher * mFileSystemWatcher;
455  bool mRefreshLater;
456 };
457 
461 class CORE_EXPORT QgsProjectItem : public QgsDataItem
462 {
463  Q_OBJECT
464  public:
465 
472  QgsProjectItem( QgsDataItem* parent, const QString& name, const QString& path );
473  ~QgsProjectItem();
474 
475 };
476 
480 class CORE_EXPORT QgsErrorItem : public QgsDataItem
481 {
482  Q_OBJECT
483  public:
484 
485  QgsErrorItem( QgsDataItem* parent, const QString& error, const QString& path );
486  ~QgsErrorItem();
487 
488 };
489 
490 
491 // ---------
495 class CORE_EXPORT QgsDirectoryParamWidget : public QTreeWidget
496 {
497  Q_OBJECT
498 
499  public:
500  QgsDirectoryParamWidget( const QString& path, QWidget* parent = nullptr );
501 
502  protected:
503  void mousePressEvent( QMouseEvent* event ) override;
504 
505  public slots:
506  void showHideColumn();
507 };
508 
512 class CORE_EXPORT QgsFavouritesItem : public QgsDataCollectionItem
513 {
514  Q_OBJECT
515  public:
516  QgsFavouritesItem( QgsDataItem* parent, const QString& name, const QString& path = QString() );
518 
520 
521  void addDirectory( const QString& favIcon );
522  void removeDirectory( QgsDirectoryItem *item );
523 
524  static const QIcon &iconFavourites();
525 
526  private:
528 };
529 
533 class CORE_EXPORT QgsZipItem : public QgsDataCollectionItem
534 {
535  Q_OBJECT
536 
537  protected:
541 
542  public:
543  QgsZipItem( QgsDataItem* parent, const QString& name, const QString& path );
544  QgsZipItem( QgsDataItem* parent, const QString& name, const QString& filePath, const QString& path );
545  ~QgsZipItem();
546 
548  const QStringList & getZipFileList();
549 
553 
554  static QString vsiPrefix( const QString& uri ) { return qgsVsiPrefix( uri ); }
555 
556  static QgsDataItem* itemFromPath( QgsDataItem* parent, QString path, QString name );
558  static QgsDataItem* itemFromPath( QgsDataItem* parent, const QString& filePath, const QString& name, const QString& path );
559 
560  static const QIcon &iconZip();
561 
562  private:
563  void init();
564 };
565 
566 #endif // QGSDATAITEM_H
567 
568 
A Collection: logical collection of layers or subcollections, e.g.
Definition: qgsdataitem.h:387
QStringList mSupportFormats
The list of supported formats.
Definition: qgsdataitem.h:369
QString path() const
Definition: qgsdataitem.h:208
Contains various Favourites directories.
Definition: qgsdataitem.h:512
virtual void childrenCreated()
virtual QString layerName() const
Definition: qgsdataitem.h:380
QString name() const
Definition: qgsdataitem.h:206
QString toolTip() const
Definition: qgsdataitem.h:220
QString providerKey()
Returns provider key.
Definition: qgsdataitem.h:341
QVector< QgsDataItem * > children() const
Definition: qgsdataitem.h:204
QString mProviderKey
The provider key.
Definition: qgsdataitem.h:361
virtual bool handleDrop(const QMimeData *, Qt::DropAction)
Attempts to process the mime data dropped on this item.
Definition: qgsdataitem.h:166
void moveToThread(QThread *targetThread)
virtual QIcon icon()
void addChild(QgsDataItem *item)
Definition: qgsdataitem.h:394
QString mIconName
Definition: qgsdataitem.h:253
State mState
Definition: qgsdataitem.h:243
Type type() const
Definition: qgsdataitem.h:196
void setIcon(const QIcon &icon)
Definition: qgsdataitem.h:216
Abstract base class for spatial data provider implementations.
void setToolTip(const QString &msg)
Definition: qgsdataitem.h:219
QString iconPath(const QString &iconFile)
QString uri()
Returns layer uri or empty string if layer cannot be created.
Definition: qgsdataitem.h:338
virtual QWidget * paramWidget()
Definition: qgsdataitem.h:149
QString mToolTip
Definition: qgsdataitem.h:252
A zip file: contains layers, using GDAL/OGR VSIFILE mechanism.
Definition: qgsdataitem.h:533
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:367
const char * name() const
QString mFilePath
Definition: qgsdataitem.h:538
QgsDataItem * parent() const
Get item parent.
Definition: qgsdataitem.h:200
QString mName
Definition: qgsdataitem.h:246
LayerType
Layers enum defining the types of layers that can be added to a map.
Definition: qgsmaplayer.h:57
QStringList mZipFileList
Definition: qgsdataitem.h:540
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:480
void deleteLater()
static QStringList mProviderNames
Definition: qgsdataitem.h:552
A directory: contains subdirectories and layers.
Definition: qgsdataitem.h:403
Base class for all items in the model.
Definition: qgsdataitem.h:79
void setParent(QObject *parent)
QStringList supportedCRS()
Returns the supported CRS.
Definition: qgsdataitem.h:346
Capabilities mCapabilities
Definition: qgsdataitem.h:240
virtual void setState(State state)
Set item state.
void setName(const QString &name)
Definition: qgsdataitem.h:207
static QVector< dataItem_t * > mDataItemPtr
Definition: qgsdataitem.h:551
QString mPath
Definition: qgsdataitem.h:251
QString mUri
The URI.
Definition: qgsdataitem.h:363
Q_DECL_DEPRECATED bool isPopulated()
Definition: qgsdataitem.h:124
virtual Q_DECL_DEPRECATED Capability capabilities()
Definition: qgsdataitem.h:183
QString mVsiPrefix
Definition: qgsdataitem.h:539
QString qgsVsiPrefix(const QString &path)
Definition: qgis.cpp:342
Class for storing a coordinate reference system (CRS)
Animated icon is keeping an animation running if there are listeners connected to frameChanged...
Definition: qgsdataitem.h:43
QgsDataItem * mParent
Definition: qgsdataitem.h:241
bool deferredDelete()
The item is scheduled to be deleted.
Definition: qgsdataitem.h:237
QString dirPath() const
Definition: qgsdataitem.h:432
QIcon icon() const
Definition: qgsdataitem.h:54
virtual QVector< QgsDataItem * > createChildren()
Create children.
bool mPopulated
Definition: qgsdataitem.h:245
Item that represents a layer that can be opened with one of the providers.
Definition: qgsdataitem.h:308
virtual void mousePressEvent(QMouseEvent *event)
QgsDataItem * dataItem_t(QString, QgsDataItem *)
Definition: qgsdataitem.h:38
static Q_DECL_DEPRECATED QVector< QLibrary * > mLibraries
Definition: qgsdataitem.h:440
QVector< QgsDataItem * > mChildren
Definition: qgsdataitem.h:242
QStringList supportedFormats()
Returns the supported formats.
Definition: qgsdataitem.h:351
virtual QList< QAction * > actions()
Returns the list of actions available for this item.
Definition: qgsdataitem.h:154
Data item that can be used to represent QGIS projects.
Definition: qgsdataitem.h:461
void setPath(const QString &path)
Definition: qgsdataitem.h:209
virtual bool acceptDrop()
Returns whether the item accepts drag and dropped layers - e.g.
Definition: qgsdataitem.h:160
LayerType mLayerType
The layer type.
Definition: qgsdataitem.h:365
virtual QString comments() const
Returns comments of the layer.
Definition: qgsdataitem.h:356
virtual void setCapabilities(const Capabilities &capabilities)
Definition: qgsdataitem.h:187
void setIconName(const QString &iconName)
Definition: qgsdataitem.h:217
static QString vsiPrefix(const QString &uri)
Definition: qgsdataitem.h:554
QMap< QString, QIcon > mIconMap
Definition: qgsdataitem.h:255
virtual Capabilities capabilities2() const
Definition: qgsdataitem.h:185