QGIS API Documentation
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 
41 class CORE_EXPORT QgsAnimatedIcon : public QObject
42 {
43  Q_OBJECT
44  public:
45 
48  QgsAnimatedIcon( const QString & iconPath = QString::null );
49 
50  QString iconPath() const;
51  void setIconPath( const QString & iconPath );
52  QIcon icon() const { return mIcon; }
53 
55  void connectFrameChanged( const QObject * receiver, const char * method );
57  void disconnectFrameChanged( const QObject * receiver, const char * method );
58 
59  public slots:
60  void onFrameChanged();
61 
62  signals:
64  void frameChanged();
65 
66  private:
67  void resetMovie();
68  int mCount; // number of listeners
69  QMovie * mMovie;
70  QIcon mIcon;
71 };
72 
75 class CORE_EXPORT QgsDataItem : public QObject
76 {
77  Q_OBJECT
78  Q_ENUMS( Type )
79  Q_ENUMS( State )
80  public:
81  enum Type
82  {
88  Project
89  };
90 
92  QgsDataItem( QgsDataItem::Type type, QgsDataItem* parent, const QString& name, const QString& path );
93  virtual ~QgsDataItem();
94 
95  bool hasChildren();
96 
97  int rowCount();
98 
101  virtual QVector<QgsDataItem*> createChildren();
102 
103  enum State
104  {
107  Populated
108  };
109 
111  State state() const;
112 
117  virtual void setState( State state );
118 
120  Q_DECL_DEPRECATED bool isPopulated() { return state() == Populated; }
121 
127  virtual void addChildItem( QgsDataItem *child, bool refresh = false );
128 
133  virtual void deleteChildItem( QgsDataItem * child );
134 
139  virtual QgsDataItem *removeChildItem( QgsDataItem * child );
140 
143  virtual bool equal( const QgsDataItem *other );
144 
145  virtual QWidget *paramWidget() { return nullptr; }
146 
150  virtual QList<QAction*> actions() { return QList<QAction*>(); }
151 
156  virtual bool acceptDrop() { return false; }
157 
162  virtual bool handleDrop( const QMimeData * /*data*/, Qt::DropAction /*action*/ ) { return false; }
163 
165  {
166  NoCapabilities = 0,
167  SetCrs = 1 << 0,
168  Fertile = 1 << 1,
169  Fast = 1 << 2
170  };
171  Q_DECLARE_FLAGS( Capabilities, Capability )
172 
173  // This will _write_ selected crs in data source
174  virtual bool setCrs( QgsCoordinateReferenceSystem crs )
175  { Q_UNUSED( crs ); return false; }
176 
178  Q_DECL_DEPRECATED virtual Capability capabilities() { return NoCapabilities; }
179 
180  virtual Capabilities capabilities2() const { return mCapabilities; }
181 
182  virtual void setCapabilities( const Capabilities& capabilities ) { mCapabilities = capabilities; }
183 
184  // static methods
185 
186  // Find child index in vector of items using '==' operator
187  static int findItem( QVector<QgsDataItem*> items, QgsDataItem * item );
188 
189  // members
190 
191  Type type() const { return mType; }
192 
195  QgsDataItem* parent() const { return mParent; }
198  void setParent( QgsDataItem* parent );
199  QVector<QgsDataItem*> children() const { return mChildren; }
200  virtual QIcon icon();
201  QString name() const { return mName; }
202  void setName( const QString &name ) { mName = name; }
203  QString path() const { return mPath; }
204  void setPath( const QString &path ) { mPath = path; }
206  static QString pathComponent( const QString &component );
207 
208  // Because QIcon (QPixmap) must not be used in outside the GUI thread, it is
209  // not possible to set mIcon in constructor. Either use mIconName/setIconName()
210  // or implement icon().
211  void setIcon( const QIcon& icon ) { mIcon = icon; }
212  void setIconName( const QString & iconName ) { mIconName = iconName; }
213 
214  void setToolTip( const QString& msg ) { mToolTip = msg; }
215  QString toolTip() const { return mToolTip; }
216 
217  // deleteLater() items anc clear the vector
218  static void deleteLater( QVector<QgsDataItem*> &items );
219 
221  void moveToThread( QThread * targetThread );
222 
223  protected:
224  virtual void populate( const QVector<QgsDataItem*>& children );
225  virtual void refresh( QVector<QgsDataItem*> children );
232  bool deferredDelete() { return mDeferredDelete; }
233 
235  Capabilities mCapabilities;
237  QVector<QgsDataItem*> mChildren; // easier to have it always
242  // Path is slash ('/') separated chain of item identifiers which are usually item names, but may be differen if it is
243  // necessary to distinguish paths of two providers to the same source (e.g GRASS location and standard directory have the same
244  // name but different paths). Identifiers in path must not contain '/' characters.
245  // The path is used to identify item in tree.
251 
252  public slots:
260  virtual void deleteLater();
261 
262  // Populate children using children vector created by createChildren()
263  // @param foreground run createChildren in foreground
264  virtual void populate( bool foreground = false );
265 
267  virtual void depopulate();
268 
269  virtual void refresh();
270 
271  void emitBeginInsertItems( QgsDataItem* parent, int first, int last );
272  void emitEndInsertItems();
273  void emitBeginRemoveItems( QgsDataItem* parent, int first, int last );
274  void emitEndRemoveItems();
275  void emitDataChanged( QgsDataItem* item );
276  void emitDataChanged();
277  void emitStateChanged( QgsDataItem* item, QgsDataItem::State oldState );
278  virtual void childrenCreated();
279 
280  signals:
281  void beginInsertItems( QgsDataItem* parent, int first, int last );
282  void endInsertItems();
283  void beginRemoveItems( QgsDataItem* parent, int first, int last );
284  void endRemoveItems();
285  void dataChanged( QgsDataItem * item );
286  void stateChanged( QgsDataItem * item, QgsDataItem::State oldState );
287 
288  private:
289  static QVector<QgsDataItem*> runCreateChildren( QgsDataItem* item );
290 
291  // Set to true if object has to be deleted when possible (nothing running in threads)
292  bool mDeferredDelete;
293  QFutureWatcher< QVector <QgsDataItem*> > *mFutureWatcher;
294  // number of items currently in loading (populating) state
295  static QgsAnimatedIcon * mPopulatingIcon;
296 };
297 
298 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsDataItem::Capabilities )
299 
300 
301 class CORE_EXPORT QgsLayerItem : public QgsDataItem
302 {
303  Q_OBJECT
304  public:
306  {
316  Plugin
317  };
318 
319  QgsLayerItem( QgsDataItem* parent, const QString& name, const QString& path, const QString& uri, LayerType layerType, const QString& providerKey );
320 
321  // --- reimplemented from QgsDataItem ---
322 
323  virtual bool equal( const QgsDataItem *other ) override;
324 
325  // --- New virtual methods for layer item derived classes ---
326 
328  QgsMapLayer::LayerType mapLayerType();
329 
331  QString uri() { return mUri; }
332 
334  QString providerKey() { return mProviderKey; }
335 
339  QStringList supportedCRS() { return mSupportedCRS; }
340 
344  QStringList supportedFormats() { return mSupportFormats; }
345 
349  virtual QString comments() const { return QString(); }
350 
351  protected:
352 
363 
364  public:
365  static const QIcon &iconPoint();
366  static const QIcon &iconLine();
367  static const QIcon &iconPolygon();
368  static const QIcon &iconTable();
369  static const QIcon &iconRaster();
370  static const QIcon &iconDefault();
371 
373  virtual QString layerName() const { return name(); }
374 };
375 
376 
378 class CORE_EXPORT QgsDataCollectionItem : public QgsDataItem
379 {
380  Q_OBJECT
381  public:
382  QgsDataCollectionItem( QgsDataItem* parent, const QString& name, const QString& path = QString::null );
384 
385  void addChild( QgsDataItem *item ) { mChildren.append( item ); }
386 
387  static const QIcon &iconDir(); // shared icon: open/closed directory
388  static const QIcon &iconDataCollection(); // default icon for data collection
389 };
390 
392 class CORE_EXPORT QgsDirectoryItem : public QgsDataCollectionItem
393 {
394  Q_OBJECT
395  public:
396  enum Column
397  {
405  };
406 
407  QgsDirectoryItem( QgsDataItem* parent, const QString& name, const QString& path );
408 
414  QgsDirectoryItem( QgsDataItem* parent, const QString& name, const QString& dirPath, const QString& path );
415  ~QgsDirectoryItem();
416 
417  virtual void setState( State state ) override;
418 
420 
421  QString dirPath() const { return mDirPath; }
422  virtual bool equal( const QgsDataItem *other ) override;
423  virtual QIcon icon() override;
424  virtual QWidget *paramWidget() override;
425 
426  /* static QVector<QgsDataProvider*> mProviders; */
429  Q_DECL_DEPRECATED static QVector<QLibrary*> mLibraries;
430 
432  static bool hiddenPath( QString path );
433 
434  public slots:
435  virtual void childrenCreated() override;
436  void directoryChanged();
437 
438  protected:
439  void init();
441 
442  private:
443  QFileSystemWatcher * mFileSystemWatcher;
444  bool mRefreshLater;
445 };
446 
450 class CORE_EXPORT QgsProjectItem : public QgsDataItem
451 {
452  Q_OBJECT
453  public:
454 
461  QgsProjectItem( QgsDataItem* parent, const QString& name, const QString& path );
462  ~QgsProjectItem();
463 
464 };
465 
469 class CORE_EXPORT QgsErrorItem : public QgsDataItem
470 {
471  Q_OBJECT
472  public:
473 
474  QgsErrorItem( QgsDataItem* parent, const QString& error, const QString& path );
475  ~QgsErrorItem();
476 
477 };
478 
479 
480 // ---------
481 
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 
497 class CORE_EXPORT QgsFavouritesItem : public QgsDataCollectionItem
498 {
499  Q_OBJECT
500  public:
501  QgsFavouritesItem( QgsDataItem* parent, const QString& name, const QString& path = QString() );
503 
505 
506  void addDirectory( const QString& favIcon );
507  void removeDirectory( QgsDirectoryItem *item );
508 
509  static const QIcon &iconFavourites();
510 
511  private:
513 };
514 
516 class CORE_EXPORT QgsZipItem : public QgsDataCollectionItem
517 {
518  Q_OBJECT
519 
520  protected:
524 
525  public:
526  QgsZipItem( QgsDataItem* parent, const QString& name, const QString& path );
527  QgsZipItem( QgsDataItem* parent, const QString& name, const QString& filePath, const QString& path );
528  ~QgsZipItem();
529 
531  const QStringList & getZipFileList();
532 
536 
537  static QString vsiPrefix( const QString& uri ) { return qgsVsiPrefix( uri ); }
538 
539  static QgsDataItem* itemFromPath( QgsDataItem* parent, QString path, QString name );
541  static QgsDataItem* itemFromPath( QgsDataItem* parent, const QString& filePath, const QString& name, const QString& path );
542 
543  static const QIcon &iconZip();
544 
545  private:
546  void init();
547 };
548 
549 #endif // QGSDATAITEM_H
550 
551 
A Collection: logical collection of layers or subcollections, e.g.
Definition: qgsdataitem.h:378
QStringList mSupportFormats
The list of supported formats.
Definition: qgsdataitem.h:362
Contains various Favourites directories.
Definition: qgsdataitem.h:497
virtual void childrenCreated()
QVector< QgsDataItem * > children() const
Definition: qgsdataitem.h:199
QString name() const
Definition: qgsdataitem.h:201
virtual Capabilities capabilities2() const
Definition: qgsdataitem.h:180
QgsDataItem * parent() const
Get item parent.
Definition: qgsdataitem.h:195
QIcon icon() const
Definition: qgsdataitem.h:52
QString providerKey()
Returns provider key.
Definition: qgsdataitem.h:334
QString mProviderKey
The provider key.
Definition: qgsdataitem.h:354
virtual bool handleDrop(const QMimeData *, Qt::DropAction)
Attempts to process the mime data dropped on this item.
Definition: qgsdataitem.h:162
void moveToThread(QThread *targetThread)
virtual QIcon icon()
void addChild(QgsDataItem *item)
Definition: qgsdataitem.h:385
QString mIconName
Definition: qgsdataitem.h:248
State mState
Definition: qgsdataitem.h:238
void setIcon(const QIcon &icon)
Definition: qgsdataitem.h:211
Abstract base class for spatial data provider implementations.
void setToolTip(const QString &msg)
Definition: qgsdataitem.h:214
QString iconPath(const QString &iconFile)
QString uri()
Returns layer uri or empty string if layer cannot be created.
Definition: qgsdataitem.h:331
virtual QWidget * paramWidget()
Definition: qgsdataitem.h:145
QString mToolTip
Definition: qgsdataitem.h:247
A zip file: contains layers, using GDAL/OGR VSIFILE mechanism.
Definition: qgsdataitem.h:516
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:360
const char * name() const
QString mFilePath
Definition: qgsdataitem.h:521
QString dirPath() const
Definition: qgsdataitem.h:421
QString mName
Definition: qgsdataitem.h:241
LayerType
Layers enum defining the types of layers that can be added to a map.
Definition: qgsmaplayer.h:55
QStringList mZipFileList
Definition: qgsdataitem.h:523
Children not yet created.
Definition: qgsdataitem.h:105
Creating children in separate thread (populating or refreshing)
Definition: qgsdataitem.h:106
QString path() const
Definition: qgsdataitem.h:203
Data item that can be used to report problems (e.g.
Definition: qgsdataitem.h:469
Type type() const
Definition: qgsdataitem.h:191
void deleteLater()
static QStringList mProviderNames
Definition: qgsdataitem.h:535
A directory: contains subdirectories and layers.
Definition: qgsdataitem.h:392
Base class for all items in the model.
Definition: qgsdataitem.h:75
void setParent(QObject *parent)
QStringList supportedCRS()
Returns the supported CRS.
Definition: qgsdataitem.h:339
Capabilities mCapabilities
Definition: qgsdataitem.h:235
virtual void setState(State state)
Set item state.
void setName(const QString &name)
Definition: qgsdataitem.h:202
static QVector< dataItem_t * > mDataItemPtr
Definition: qgsdataitem.h:534
QString mPath
Definition: qgsdataitem.h:246
QString mUri
The URI.
Definition: qgsdataitem.h:356
Q_DECL_DEPRECATED bool isPopulated()
Definition: qgsdataitem.h:120
virtual Q_DECL_DEPRECATED Capability capabilities()
Definition: qgsdataitem.h:178
QString mVsiPrefix
Definition: qgsdataitem.h:522
virtual QString comments() const
Returns comments of the layer.
Definition: qgsdataitem.h:349
virtual QString layerName() const
Definition: qgsdataitem.h:373
QString qgsVsiPrefix(const QString &path)
Definition: qgis.cpp:336
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:41
QgsDataItem * mParent
Definition: qgsdataitem.h:236
bool deferredDelete()
The item is scheduled to be deleted.
Definition: qgsdataitem.h:232
virtual QVector< QgsDataItem * > createChildren()
Create children.
bool mPopulated
Definition: qgsdataitem.h:240
Item that represents a layer that can be opened with one of the providers.
Definition: qgsdataitem.h:301
virtual void mousePressEvent(QMouseEvent *event)
QgsDataItem * dataItem_t(QString, QgsDataItem *)
Definition: qgsdataitem.h:38
static Q_DECL_DEPRECATED QVector< QLibrary * > mLibraries
Definition: qgsdataitem.h:429
QVector< QgsDataItem * > mChildren
Definition: qgsdataitem.h:237
QString toolTip() const
Definition: qgsdataitem.h:215
QStringList supportedFormats()
Returns the supported formats.
Definition: qgsdataitem.h:344
virtual QList< QAction * > actions()
Returns the list of actions available for this item.
Definition: qgsdataitem.h:150
Data item that can be used to represent QGIS projects.
Definition: qgsdataitem.h:450
void setPath(const QString &path)
Definition: qgsdataitem.h:204
virtual bool acceptDrop()
Returns whether the item accepts drag and dropped layers - e.g.
Definition: qgsdataitem.h:156
LayerType mLayerType
The layer type.
Definition: qgsdataitem.h:358
virtual void setCapabilities(const Capabilities &capabilities)
Definition: qgsdataitem.h:182
void setIconName(const QString &iconName)
Definition: qgsdataitem.h:212
static QString vsiPrefix(const QString &uri)
Definition: qgsdataitem.h:537
QMap< QString, QIcon > mIconMap
Definition: qgsdataitem.h:250