QGIS API Documentation  2.17.0-Master (dfeb663)
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  };
175  Q_DECLARE_FLAGS( Capabilities, Capability )
176 
177  // This will _write_ selected crs in data source
178  virtual bool setCrs( QgsCoordinateReferenceSystem crs )
179  { Q_UNUSED( crs ); return false; }
180 
182  Q_DECL_DEPRECATED virtual Capability capabilities() { return NoCapabilities; }
183 
184  virtual Capabilities capabilities2() const { return mCapabilities; }
185 
186  virtual void setCapabilities( const Capabilities& capabilities ) { mCapabilities = capabilities; }
187 
188  // static methods
189 
190  // Find child index in vector of items using '==' operator
191  static int findItem( QVector<QgsDataItem*> items, QgsDataItem * item );
192 
193  // members
194 
195  Type type() const { return mType; }
196 
199  QgsDataItem* parent() const { return mParent; }
202  void setParent( QgsDataItem* parent );
203  QVector<QgsDataItem*> children() const { return mChildren; }
204  virtual QIcon icon();
205  QString name() const { return mName; }
206  void setName( const QString &name ) { mName = name; }
207  QString path() const { return mPath; }
208  void setPath( const QString &path ) { mPath = path; }
210  static QString pathComponent( const QString &component );
211 
212  // Because QIcon (QPixmap) must not be used in outside the GUI thread, it is
213  // not possible to set mIcon in constructor. Either use mIconName/setIconName()
214  // or implement icon().
215  void setIcon( const QIcon& icon ) { mIcon = icon; }
216  void setIconName( const QString & iconName ) { mIconName = iconName; }
217 
218  void setToolTip( const QString& msg ) { mToolTip = msg; }
219  QString toolTip() const { return mToolTip; }
220 
221  // deleteLater() items anc clear the vector
222  static void deleteLater( QVector<QgsDataItem*> &items );
223 
225  void moveToThread( QThread * targetThread );
226 
227  protected:
228  virtual void populate( const QVector<QgsDataItem*>& children );
229  virtual void refresh( QVector<QgsDataItem*> children );
236  bool deferredDelete() { return mDeferredDelete; }
237 
239  Capabilities mCapabilities;
241  QVector<QgsDataItem*> mChildren; // easier to have it always
246  // Path is slash ('/') separated chain of item identifiers which are usually item names, but may be differen if it is
247  // necessary to distinguish paths of two providers to the same source (e.g GRASS location and standard directory have the same
248  // name but different paths). Identifiers in path must not contain '/' characters.
249  // The path is used to identify item in tree.
255 
256  public slots:
264  virtual void deleteLater();
265 
266  // Populate children using children vector created by createChildren()
267  // @param foreground run createChildren in foreground
268  virtual void populate( bool foreground = false );
269 
271  virtual void depopulate();
272 
273  virtual void refresh();
274 
275  void emitBeginInsertItems( QgsDataItem* parent, int first, int last );
276  void emitEndInsertItems();
277  void emitBeginRemoveItems( QgsDataItem* parent, int first, int last );
278  void emitEndRemoveItems();
279  void emitDataChanged( QgsDataItem* item );
280  void emitDataChanged();
281  void emitStateChanged( QgsDataItem* item, QgsDataItem::State oldState );
282  virtual void childrenCreated();
283 
284  signals:
285  void beginInsertItems( QgsDataItem* parent, int first, int last );
286  void endInsertItems();
287  void beginRemoveItems( QgsDataItem* parent, int first, int last );
288  void endRemoveItems();
289  void dataChanged( QgsDataItem * item );
290  void stateChanged( QgsDataItem * item, QgsDataItem::State oldState );
291 
292  private:
293  static QVector<QgsDataItem*> runCreateChildren( QgsDataItem* item );
294 
295  // Set to true if object has to be deleted when possible (nothing running in threads)
296  bool mDeferredDelete;
297  QFutureWatcher< QVector <QgsDataItem*> > *mFutureWatcher;
298  // number of items currently in loading (populating) state
299  static QgsAnimatedIcon * mPopulatingIcon;
300 };
301 
302 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsDataItem::Capabilities )
303 
304 
307 class CORE_EXPORT QgsLayerItem : public QgsDataItem
308 {
309  Q_OBJECT
310  public:
312  {
322  Plugin
323  };
324 
325  QgsLayerItem( QgsDataItem* parent, const QString& name, const QString& path, const QString& uri, LayerType layerType, const QString& providerKey );
326 
327  // --- reimplemented from QgsDataItem ---
328 
329  virtual bool equal( const QgsDataItem *other ) override;
330 
331  // --- New virtual methods for layer item derived classes ---
332 
334  QgsMapLayer::LayerType mapLayerType();
335 
337  QString uri() { return mUri; }
338 
340  QString providerKey() { return mProviderKey; }
341 
345  QStringList supportedCRS() { return mSupportedCRS; }
346 
350  QStringList supportedFormats() { return mSupportFormats; }
351 
355  virtual QString comments() const { return QString(); }
356 
357  protected:
358 
369 
370  public:
371  static const QIcon &iconPoint();
372  static const QIcon &iconLine();
373  static const QIcon &iconPolygon();
374  static const QIcon &iconTable();
375  static const QIcon &iconRaster();
376  static const QIcon &iconDefault();
377 
379  virtual QString layerName() const { return name(); }
380 };
381 
382 
386 class CORE_EXPORT QgsDataCollectionItem : public QgsDataItem
387 {
388  Q_OBJECT
389  public:
390  QgsDataCollectionItem( QgsDataItem* parent, const QString& name, const QString& path = QString::null );
392 
393  void addChild( QgsDataItem *item ) { mChildren.append( item ); }
394 
395  static const QIcon &iconDir(); // shared icon: open/closed directory
396  static const QIcon &iconDataCollection(); // default icon for data collection
397 };
398 
402 class CORE_EXPORT QgsDirectoryItem : public QgsDataCollectionItem
403 {
404  Q_OBJECT
405  public:
406  enum Column
407  {
415  };
416 
417  QgsDirectoryItem( QgsDataItem* parent, const QString& name, const QString& path );
418 
424  QgsDirectoryItem( QgsDataItem* parent, const QString& name, const QString& dirPath, const QString& path );
425  ~QgsDirectoryItem();
426 
427  virtual void setState( State state ) override;
428 
430 
431  QString dirPath() const { return mDirPath; }
432  virtual bool equal( const QgsDataItem *other ) override;
433  virtual QIcon icon() override;
434  virtual QWidget *paramWidget() override;
435 
436  /* static QVector<QgsDataProvider*> mProviders; */
439  Q_DECL_DEPRECATED static QVector<QLibrary*> mLibraries;
440 
442  static bool hiddenPath( QString path );
443 
444  public slots:
445  virtual void childrenCreated() override;
446  void directoryChanged();
447 
448  protected:
449  void init();
451 
452  private:
453  QFileSystemWatcher * mFileSystemWatcher;
454  bool mRefreshLater;
455 };
456 
460 class CORE_EXPORT QgsProjectItem : public QgsDataItem
461 {
462  Q_OBJECT
463  public:
464 
471  QgsProjectItem( QgsDataItem* parent, const QString& name, const QString& path );
472  ~QgsProjectItem();
473 
474 };
475 
479 class CORE_EXPORT QgsErrorItem : public QgsDataItem
480 {
481  Q_OBJECT
482  public:
483 
484  QgsErrorItem( QgsDataItem* parent, const QString& error, const QString& path );
485  ~QgsErrorItem();
486 
487 };
488 
489 
490 // ---------
494 class CORE_EXPORT QgsDirectoryParamWidget : public QTreeWidget
495 {
496  Q_OBJECT
497 
498  public:
499  QgsDirectoryParamWidget( const QString& path, QWidget* parent = nullptr );
500 
501  protected:
502  void mousePressEvent( QMouseEvent* event ) override;
503 
504  public slots:
505  void showHideColumn();
506 };
507 
511 class CORE_EXPORT QgsFavouritesItem : public QgsDataCollectionItem
512 {
513  Q_OBJECT
514  public:
515  QgsFavouritesItem( QgsDataItem* parent, const QString& name, const QString& path = QString() );
517 
519 
520  void addDirectory( const QString& favIcon );
521  void removeDirectory( QgsDirectoryItem *item );
522 
523  static const QIcon &iconFavourites();
524 
525  private:
527 };
528 
532 class CORE_EXPORT QgsZipItem : public QgsDataCollectionItem
533 {
534  Q_OBJECT
535 
536  protected:
540 
541  public:
542  QgsZipItem( QgsDataItem* parent, const QString& name, const QString& path );
543  QgsZipItem( QgsDataItem* parent, const QString& name, const QString& filePath, const QString& path );
544  ~QgsZipItem();
545 
547  const QStringList & getZipFileList();
548 
552 
553  static QString vsiPrefix( const QString& uri ) { return qgsVsiPrefix( uri ); }
554 
555  static QgsDataItem* itemFromPath( QgsDataItem* parent, QString path, QString name );
557  static QgsDataItem* itemFromPath( QgsDataItem* parent, const QString& filePath, const QString& name, const QString& path );
558 
559  static const QIcon &iconZip();
560 
561  private:
562  void init();
563 };
564 
565 #endif // QGSDATAITEM_H
566 
567 
A Collection: logical collection of layers or subcollections, e.g.
Definition: qgsdataitem.h:386
QStringList mSupportFormats
The list of supported formats.
Definition: qgsdataitem.h:368
QString path() const
Definition: qgsdataitem.h:207
Contains various Favourites directories.
Definition: qgsdataitem.h:511
virtual void childrenCreated()
virtual QString layerName() const
Definition: qgsdataitem.h:379
QString name() const
Definition: qgsdataitem.h:205
QString toolTip() const
Definition: qgsdataitem.h:219
QString providerKey()
Returns provider key.
Definition: qgsdataitem.h:340
QVector< QgsDataItem * > children() const
Definition: qgsdataitem.h:203
QString mProviderKey
The provider key.
Definition: qgsdataitem.h:360
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:393
QString mIconName
Definition: qgsdataitem.h:252
State mState
Definition: qgsdataitem.h:242
Type type() const
Definition: qgsdataitem.h:195
void setIcon(const QIcon &icon)
Definition: qgsdataitem.h:215
Abstract base class for spatial data provider implementations.
void setToolTip(const QString &msg)
Definition: qgsdataitem.h:218
QString iconPath(const QString &iconFile)
QString uri()
Returns layer uri or empty string if layer cannot be created.
Definition: qgsdataitem.h:337
virtual QWidget * paramWidget()
Definition: qgsdataitem.h:149
QString mToolTip
Definition: qgsdataitem.h:251
A zip file: contains layers, using GDAL/OGR VSIFILE mechanism.
Definition: qgsdataitem.h:532
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:366
const char * name() const
QString mFilePath
Definition: qgsdataitem.h:537
QgsDataItem * parent() const
Get item parent.
Definition: qgsdataitem.h:199
QString mName
Definition: qgsdataitem.h:245
LayerType
Layers enum defining the types of layers that can be added to a map.
Definition: qgsmaplayer.h:57
QStringList mZipFileList
Definition: qgsdataitem.h:539
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:479
void deleteLater()
static QStringList mProviderNames
Definition: qgsdataitem.h:551
A directory: contains subdirectories and layers.
Definition: qgsdataitem.h:402
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:345
Capabilities mCapabilities
Definition: qgsdataitem.h:239
virtual void setState(State state)
Set item state.
void setName(const QString &name)
Definition: qgsdataitem.h:206
static QVector< dataItem_t * > mDataItemPtr
Definition: qgsdataitem.h:550
QString mPath
Definition: qgsdataitem.h:250
QString mUri
The URI.
Definition: qgsdataitem.h:362
Q_DECL_DEPRECATED bool isPopulated()
Definition: qgsdataitem.h:124
virtual Q_DECL_DEPRECATED Capability capabilities()
Definition: qgsdataitem.h:182
QString mVsiPrefix
Definition: qgsdataitem.h:538
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:240
bool deferredDelete()
The item is scheduled to be deleted.
Definition: qgsdataitem.h:236
QString dirPath() const
Definition: qgsdataitem.h:431
QIcon icon() const
Definition: qgsdataitem.h:54
virtual QVector< QgsDataItem * > createChildren()
Create children.
bool mPopulated
Definition: qgsdataitem.h:244
Item that represents a layer that can be opened with one of the providers.
Definition: qgsdataitem.h:307
virtual void mousePressEvent(QMouseEvent *event)
QgsDataItem * dataItem_t(QString, QgsDataItem *)
Definition: qgsdataitem.h:38
static Q_DECL_DEPRECATED QVector< QLibrary * > mLibraries
Definition: qgsdataitem.h:439
QVector< QgsDataItem * > mChildren
Definition: qgsdataitem.h:241
QStringList supportedFormats()
Returns the supported formats.
Definition: qgsdataitem.h:350
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:460
void setPath(const QString &path)
Definition: qgsdataitem.h:208
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:364
virtual QString comments() const
Returns comments of the layer.
Definition: qgsdataitem.h:355
virtual void setCapabilities(const Capabilities &capabilities)
Definition: qgsdataitem.h:186
void setIconName(const QString &iconName)
Definition: qgsdataitem.h:216
static QString vsiPrefix(const QString &uri)
Definition: qgsdataitem.h:553
QMap< QString, QIcon > mIconMap
Definition: qgsdataitem.h:254
virtual Capabilities capabilities2() const
Definition: qgsdataitem.h:184