QGIS API Documentation  2.9.0-Master
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 
38 typedef QgsDataItem * dataItem_t( QString, QgsDataItem* );
39 
41 class 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  {
87  Favourites
88  };
89 
91  QgsDataItem( QgsDataItem::Type type, QgsDataItem* parent, QString name, QString path );
92  virtual ~QgsDataItem();
93 
94  bool hasChildren();
95 
96  int rowCount();
97 
100  virtual QVector<QgsDataItem*> createChildren();
101 
102  enum State
103  {
106  Populated
107  };
108 
110  State state() const;
111 
116  virtual void setState( State state );
117 
119  bool isPopulated() { return state() == Populated; }
120 
121  // Insert new child using alphabetical order based on mName, emits necessary signal to model before and after, sets parent and connects signals
122  // refresh - refresh populated item, emit signals to model
123  virtual void addChildItem( QgsDataItem *child, bool refresh = false );
124 
125  // remove and delete child item, signals to browser are emitted
126  virtual void deleteChildItem( QgsDataItem * child );
127 
128  // remove child item but don't delete it, signals to browser are emitted
129  // returns pointer to the removed item or null if no such item was found
130  virtual QgsDataItem *removeChildItem( QgsDataItem * child );
131 
132  virtual bool equal( const QgsDataItem *other );
133 
134  virtual QWidget *paramWidget() { return 0; }
135 
136  // list of actions provided by this item - usually used for popup menu on right-click
137  virtual QList<QAction*> actions() { return QList<QAction*>(); }
138 
139  // whether accepts drag&drop'd layers - e.g. for import
140  virtual bool acceptDrop() { return false; }
141 
142  // try to process the data dropped on this item
143  virtual bool handleDrop( const QMimeData * /*data*/, Qt::DropAction /*action*/ ) { return false; }
144 
146  {
147  NoCapabilities = 0,
148  SetCrs = 1 << 0,
149  Fertile = 1 << 1,
150  Fast = 1 << 2
151  };
152  Q_DECLARE_FLAGS( Capabilities, Capability )
153 
154  // This will _write_ selected crs in data source
155  virtual bool setCrs( QgsCoordinateReferenceSystem crs )
156  { Q_UNUSED( crs ); return false; }
157 
159  Q_DECL_DEPRECATED virtual Capability capabilities() { return NoCapabilities; }
160 
161  virtual Capabilities capabilities2() const { return mCapabilities; }
162 
163  virtual void setCapabilities( Capabilities capabilities ) { mCapabilities = capabilities; }
164 
165  // static methods
166 
167  // Find child index in vector of items using '==' operator
168  static int findItem( QVector<QgsDataItem*> items, QgsDataItem * item );
169 
170  // members
171 
172  Type type() const { return mType; }
173 
176  QgsDataItem* parent() const { return mParent; }
179  void setParent( QgsDataItem* parent );
180  QVector<QgsDataItem*> children() const { return mChildren; }
181  virtual QIcon icon();
182  QString name() const { return mName; }
183  void setName( const QString &name ) { mName = name; }
184  QString path() const { return mPath; }
185  void setPath( const QString &path ) { mPath = path; }
187  static QString pathComponent( const QString &component );
188 
189  // Because QIcon (QPixmap) must not be used in outside the GUI thread, it is
190  // not possible to set mIcon in constructor. Either use mIconName/setIconName()
191  // or implement icon().
192  void setIcon( QIcon icon ) { mIcon = icon; }
193  void setIconName( const QString & iconName ) { mIconName = iconName; }
194 
195  void setToolTip( QString msg ) { mToolTip = msg; }
196  QString toolTip() const { return mToolTip; }
197 
198  // deleteLater() items anc clear the vector
199  static void deleteLater( QVector<QgsDataItem*> &items );
200 
202  void moveToThread( QThread * targetThread );
203 
204  protected:
205  virtual void populate( QVector<QgsDataItem*> children );
206  virtual void refresh( QVector<QgsDataItem*> children );
213  bool deferredDelete() { return mDeferredDelete; }
214 
216  Capabilities mCapabilities;
218  QVector<QgsDataItem*> mChildren; // easier to have it always
222  QString mName;
223  // Path is slash ('/') separated chain of item identifiers which are usually item names, but may be differen if it is
224  // necessary to distinguish paths of two providers to the same source (e.g GRASS location and standard directory have the same
225  // name but different paths). Identifiers in path must not contain '/' characters.
226  // The path is used to identify item in tree.
227  QString mPath;
228  QString mToolTip;
229  QString mIconName;
230  QIcon mIcon;
231  static QMap<QString, QIcon> mIconMap;
232 
233  public slots:
241  virtual void deleteLater();
242 
243  // Populate children using children vector created by createChildren()
244  virtual void populate();
245 
247  virtual void depopulate();
248 
249  virtual void refresh();
250 
251  void emitBeginInsertItems( QgsDataItem* parent, int first, int last );
252  void emitEndInsertItems();
253  void emitBeginRemoveItems( QgsDataItem* parent, int first, int last );
254  void emitEndRemoveItems();
255  void emitDataChanged( QgsDataItem* item );
256  void emitDataChanged( );
257  void emitStateChanged( QgsDataItem* item, QgsDataItem::State oldState );
258  virtual void childrenCreated();
259 
260  signals:
261  void beginInsertItems( QgsDataItem* parent, int first, int last );
262  void endInsertItems();
263  void beginRemoveItems( QgsDataItem* parent, int first, int last );
264  void endRemoveItems();
265  void dataChanged( QgsDataItem * item );
266  void stateChanged( QgsDataItem * item, QgsDataItem::State oldState );
267 
268  private:
269  static QVector<QgsDataItem*> runCreateChildren( QgsDataItem* item );
270 
271  // Set to true if object has to be deleted when possible (nothing running in threads)
272  bool mDeferredDelete;
273  QFutureWatcher< QVector <QgsDataItem*> > *mFutureWatcher;
274  // number of items currently in loading (populating) state
275  static QgsAnimatedIcon * mPopulatingIcon;
276 };
277 
278 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsDataItem::Capabilities )
279 
280 
281 class CORE_EXPORT QgsLayerItem : public QgsDataItem
282 {
283  Q_OBJECT
284  public:
286  {
296  Plugin
297  };
298 
299  QgsLayerItem( QgsDataItem* parent, QString name, QString path, QString uri, LayerType layerType, QString providerKey );
300 
301  // --- reimplemented from QgsDataItem ---
302 
303  virtual bool equal( const QgsDataItem *other ) override;
304 
305  // --- New virtual methods for layer item derived classes ---
306 
307  // Returns QgsMapLayer::LayerType
308  QgsMapLayer::LayerType mapLayerType();
309 
310  // Returns layer uri or empty string if layer cannot be created
311  QString uri() { return mUri; }
312 
313  // Returns provider key
314  QString providerKey() { return mProviderKey; }
315 
319  QStringList supportedCRS() { return mSupportedCRS; }
320 
324  QStringList supportedFormats() { return mSupportFormats; }
325 
326  protected:
327 
328  QString mProviderKey;
329  QString mUri;
331  QStringList mSupportedCRS;
332  QStringList mSupportFormats;
333 
334  public:
335  static const QIcon &iconPoint();
336  static const QIcon &iconLine();
337  static const QIcon &iconPolygon();
338  static const QIcon &iconTable();
339  static const QIcon &iconRaster();
340  static const QIcon &iconDefault();
341 
342  virtual QString layerName() const { return name(); }
343 };
344 
345 
347 class CORE_EXPORT QgsDataCollectionItem : public QgsDataItem
348 {
349  Q_OBJECT
350  public:
351  QgsDataCollectionItem( QgsDataItem* parent, QString name, QString path = QString::null );
353 
354  void addChild( QgsDataItem *item ) { mChildren.append( item ); }
355 
356  static const QIcon &iconDir(); // shared icon: open/closed directory
357  static const QIcon &iconDataCollection(); // default icon for data collection
358 };
359 
361 class CORE_EXPORT QgsDirectoryItem : public QgsDataCollectionItem
362 {
363  Q_OBJECT
364  public:
365  enum Column
366  {
374  };
375 
376  QgsDirectoryItem( QgsDataItem* parent, QString name, QString path );
377 
383  QgsDirectoryItem( QgsDataItem* parent, QString name, QString dirPath, QString path );
384  ~QgsDirectoryItem();
385 
386  virtual void setState( State state ) override;
387 
388  QVector<QgsDataItem*> createChildren() override;
389 
390  QString dirPath() const { return mDirPath; }
391  virtual bool equal( const QgsDataItem *other ) override;
392  virtual QIcon icon() override;
393  virtual QWidget *paramWidget() override;
394 
395  /* static QVector<QgsDataProvider*> mProviders; */
398  Q_DECL_DEPRECATED static QVector<QLibrary*> mLibraries;
399 
400  public slots:
401  virtual void childrenCreated() override;
402  void directoryChanged();
403 
404  protected:
405  void init();
406  QString mDirPath;
407 
408  private:
409  QFileSystemWatcher * mFileSystemWatcher;
410  bool mRefreshLater;
411 };
412 
416 class CORE_EXPORT QgsErrorItem : public QgsDataItem
417 {
418  Q_OBJECT
419  public:
420 
421  QgsErrorItem( QgsDataItem* parent, QString error, QString path );
422  ~QgsErrorItem();
423 
424 };
425 
426 
427 // ---------
428 
429 class CORE_EXPORT QgsDirectoryParamWidget : public QTreeWidget
430 {
431  Q_OBJECT
432 
433  public:
434  QgsDirectoryParamWidget( QString path, QWidget* parent = NULL );
435 
436  protected:
437  void mousePressEvent( QMouseEvent* event ) override;
438 
439  public slots:
440  void showHideColumn();
441 };
442 
444 class CORE_EXPORT QgsFavouritesItem : public QgsDataCollectionItem
445 {
446  Q_OBJECT
447  public:
448  QgsFavouritesItem( QgsDataItem* parent, QString name, QString path = QString() );
450 
451  QVector<QgsDataItem*> createChildren() override;
452 
453  void addDirectory( QString favIcon );
454  void removeDirectory( QgsDirectoryItem *item );
455 
456  static const QIcon &iconFavourites();
457 };
458 
460 class CORE_EXPORT QgsZipItem : public QgsDataCollectionItem
461 {
462  Q_OBJECT
463 
464  protected:
465  QString mFilePath;
466  QString mVsiPrefix;
467  QStringList mZipFileList;
468 
469  public:
470  QgsZipItem( QgsDataItem* parent, QString name, QString path );
471  QgsZipItem( QgsDataItem* parent, QString name, QString filePath, QString path );
472  ~QgsZipItem();
473 
474  QVector<QgsDataItem*> createChildren() override;
475  const QStringList & getZipFileList();
476 
478  static QVector<dataItem_t *> mDataItemPtr;
479  static QStringList mProviderNames;
480 
481  static QString vsiPrefix( QString uri ) { return qgsVsiPrefix( uri ); }
482 
483  static QgsDataItem* itemFromPath( QgsDataItem* parent, QString path, QString name );
484  static QgsDataItem* itemFromPath( QgsDataItem* parent, QString filePath, QString name, QString path );
485 
486  static const QIcon &iconZip();
487 
488  private:
489  void init();
490 };
491 
492 #endif // QGSDATAITEM_H
493 
494 
A Collection: logical collection of layers or subcollections, e.g.
Definition: qgsdataitem.h:347
QStringList mSupportFormats
Definition: qgsdataitem.h:332
Contains various Favourites directories.
Definition: qgsdataitem.h:444
virtual void childrenCreated()
static QMap< QString, QIcon > mIconMap
Definition: qgsdataitem.h:231
QVector< QgsDataItem * > children() const
Definition: qgsdataitem.h:180
QString name() const
Definition: qgsdataitem.h:182
LayerType
Layers enum defining the types of layers that can be added to a map.
Definition: qgsmaplayer.h:55
virtual Capabilities capabilities2() const
Definition: qgsdataitem.h:161
void disconnectFrameChanged(const QObject *receiver, const char *method)
Disconnect listener from frameChanged() signal.
Definition: qgsdataitem.cpp:87
QgsDataItem * parent() const
Get item parent.
Definition: qgsdataitem.h:176
QIcon icon() const
Definition: qgsdataitem.h:52
QString providerKey()
Definition: qgsdataitem.h:314
QString mProviderKey
Definition: qgsdataitem.h:328
virtual bool handleDrop(const QMimeData *, Qt::DropAction)
Definition: qgsdataitem.h:143
virtual QIcon icon()
void addChild(QgsDataItem *item)
Definition: qgsdataitem.h:354
QString qgsVsiPrefix(QString path)
Definition: qgis.cpp:269
QString mIconName
Definition: qgsdataitem.h:229
State mState
Definition: qgsdataitem.h:219
Abstract base class for spatial data provider implementations.
void setIcon(QIcon icon)
Definition: qgsdataitem.h:192
QString uri()
Definition: qgsdataitem.h:311
virtual QWidget * paramWidget()
Definition: qgsdataitem.h:134
QString mToolTip
Definition: qgsdataitem.h:228
A zip file: contains layers, using GDAL/OGR VSIFILE mechanism.
Definition: qgsdataitem.h:460
virtual bool equal(const QgsDataItem *other)
QStringList mSupportedCRS
Definition: qgsdataitem.h:331
QString mFilePath
Definition: qgsdataitem.h:465
bool isPopulated()
Definition: qgsdataitem.h:119
void connectFrameChanged(const QObject *receiver, const char *method)
Connect listener to frameChanged() signal.
Definition: qgsdataitem.cpp:77
QString iconPath() const
Definition: qgsdataitem.cpp:61
QString dirPath() const
Definition: qgsdataitem.h:390
static QString vsiPrefix(QString uri)
Definition: qgsdataitem.h:481
QString mName
Definition: qgsdataitem.h:222
QStringList mZipFileList
Definition: qgsdataitem.h:467
virtual void setCapabilities(Capabilities capabilities)
Definition: qgsdataitem.h:163
Children not yet created.
Definition: qgsdataitem.h:104
void onFrameChanged()
Definition: qgsdataitem.cpp:71
Creating children in separate thread (populating or refreshing)
Definition: qgsdataitem.h:105
QString path() const
Definition: qgsdataitem.h:184
Data item that can be used to report problems (e.g.
Definition: qgsdataitem.h:416
Type type() const
Definition: qgsdataitem.h:172
static QStringList mProviderNames
Definition: qgsdataitem.h:479
A directory: contains subdirectories and layers.
Definition: qgsdataitem.h:361
Base class for all items in the model.
Definition: qgsdataitem.h:75
QStringList supportedCRS()
Returns the supported CRS.
Definition: qgsdataitem.h:319
Capabilities mCapabilities
Definition: qgsdataitem.h:216
virtual void setState(State state)
Set item state.
void setName(const QString &name)
Definition: qgsdataitem.h:183
static QVector< dataItem_t * > mDataItemPtr
Definition: qgsdataitem.h:478
QString mPath
Definition: qgsdataitem.h:227
QString mUri
Definition: qgsdataitem.h:329
QgsAnimatedIcon(const QString &iconPath=QString::null)
Constructor.
Definition: qgsdataitem.cpp:46
virtual Q_DECL_DEPRECATED Capability capabilities()
Definition: qgsdataitem.h:159
QString mVsiPrefix
Definition: qgsdataitem.h:466
static Q_DECL_DEPRECATED QVector< QLibrary * > mLibraries
Definition: qgsdataitem.h:398
virtual QString layerName() const
Definition: qgsdataitem.h: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:41
QgsDataItem * mParent
Definition: qgsdataitem.h:217
bool deferredDelete()
The item is scheduled to be deleted.
Definition: qgsdataitem.h:213
void setToolTip(QString msg)
Definition: qgsdataitem.h:195
virtual QVector< QgsDataItem * > createChildren()
Create children.
bool mPopulated
Definition: qgsdataitem.h:221
Item that represents a layer that can be opened with one of the providers.
Definition: qgsdataitem.h:281
QgsDataItem * dataItem_t(QString, QgsDataItem *)
Definition: qgsdataitem.h:38
QVector< QgsDataItem * > mChildren
Definition: qgsdataitem.h:218
QString toolTip() const
Definition: qgsdataitem.h:196
QStringList supportedFormats()
Returns the supported formats.
Definition: qgsdataitem.h:324
virtual QList< QAction * > actions()
Definition: qgsdataitem.h:137
void setPath(const QString &path)
Definition: qgsdataitem.h:185
virtual bool acceptDrop()
Definition: qgsdataitem.h:140
LayerType mLayerType
Definition: qgsdataitem.h:330
void setIconPath(const QString &iconPath)
Definition: qgsdataitem.cpp:66
void setIconName(const QString &iconName)
Definition: qgsdataitem.h:193
void frameChanged()
Emited when icon changed.