QGIS API Documentation  2.9.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 
40 
43 class CORE_EXPORT QgsDataItem : public QObject
44 {
45  Q_OBJECT
46  Q_ENUMS( Type )
47  Q_ENUMS( State )
48  public:
49  enum Type
50  {
55  Favourites
56  };
57 
59  QgsDataItem( QgsDataItem::Type type, QgsDataItem* parent, QString name, QString path );
60  virtual ~QgsDataItem();
61 
62  bool hasChildren();
63 
64  int rowCount();
65 
68  virtual QVector<QgsDataItem*> createChildren();
69 
70  enum State
71  {
74  Populated
75  };
76 
77  State state() const;
78 
81  virtual void setState( State state );
82 
84  bool isPopulated() { return state() == Populated; }
85 
86  // Insert new child using alphabetical order based on mName, emits necessary signal to model before and after, sets parent and connects signals
87  // refresh - refresh populated item, emit signals to model
88  virtual void addChildItem( QgsDataItem *child, bool refresh = false );
89 
90  // remove and delete child item, signals to browser are emitted
91  virtual void deleteChildItem( QgsDataItem * child );
92 
93  // remove child item but don't delete it, signals to browser are emitted
94  // returns pointer to the removed item or null if no such item was found
95  virtual QgsDataItem *removeChildItem( QgsDataItem * child );
96 
97  virtual bool equal( const QgsDataItem *other );
98 
99  virtual QWidget *paramWidget() { return 0; }
100 
101  // list of actions provided by this item - usually used for popup menu on right-click
102  virtual QList<QAction*> actions() { return QList<QAction*>(); }
103 
104  // whether accepts drag&drop'd layers - e.g. for import
105  virtual bool acceptDrop() { return false; }
106 
107  // try to process the data dropped on this item
108  virtual bool handleDrop( const QMimeData * /*data*/, Qt::DropAction /*action*/ ) { return false; }
109 
111  {
112  NoCapabilities = 0,
113  SetCrs = 1 << 0,
114  Fertile = 1 << 1,
115  Fast = 1 << 2
116  };
117  Q_DECLARE_FLAGS( Capabilities, Capability )
118 
119  // This will _write_ selected crs in data source
120  virtual bool setCrs( QgsCoordinateReferenceSystem crs )
121  { Q_UNUSED( crs ); return false; }
122 
124  Q_DECL_DEPRECATED virtual Capability capabilities() { return NoCapabilities; }
125 
126  virtual Capabilities capabilities2() const { return mCapabilities; }
127 
128  virtual void setCapabilities( Capabilities capabilities ) { mCapabilities = capabilities; }
129 
130  // static methods
131 
132  // Find child index in vector of items using '==' operator
133  static int findItem( QVector<QgsDataItem*> items, QgsDataItem * item );
134 
135  // members
136 
137  Type type() const { return mType; }
138 
141  QgsDataItem* parent() const { return mParent; }
144  void setParent( QgsDataItem* parent );
145  QVector<QgsDataItem*> children() const { return mChildren; }
146  virtual QIcon icon();
147  QString name() const { return mName; }
148  void setName( const QString &name ) { mName = name; }
149  QString path() const { return mPath; }
150  void setPath( const QString &path ) { mPath = path; }
152  static QString pathComponent( const QString &component );
153 
154  // Because QIcon (QPixmap) must not be used in outside the GUI thread, it is
155  // not possible to set mIcon in constructor. Either use mIconName/setIconName()
156  // or implement icon().
157  void setIcon( QIcon icon ) { mIcon = icon; }
158  void setIconName( const QString & iconName ) { mIconName = iconName; }
159 
160  void setToolTip( QString msg ) { mToolTip = msg; }
161  QString toolTip() const { return mToolTip; }
162 
163  // deleteLater() items anc clear the vector
164  static void deleteLater( QVector<QgsDataItem*> &items );
165 
167  void moveToThread( QThread * targetThread );
168 
169  protected:
170  virtual void populate( QVector<QgsDataItem*> children );
171  virtual void refresh( QVector<QgsDataItem*> children );
172  QIcon populatingIcon() { return mPopulatingIcon; }
179  bool deferredDelete() { return mDeferredDelete; }
180 
182  Capabilities mCapabilities;
184  QVector<QgsDataItem*> mChildren; // easier to have it always
188  QString mName;
189  // Path is slash ('/') separated chain of item identifiers which are usually item names, but may be differen if it is
190  // necessary to distinguish paths of two providers to the same source (e.g GRASS location and standard directory have the same
191  // name but different paths). Identifiers in path must not contain '/' characters.
192  // The path is used to identify item in tree.
193  QString mPath;
194  QString mToolTip;
195  QString mIconName;
196  QIcon mIcon;
197  static QMap<QString, QIcon> mIconMap;
198 
199  public slots:
207  virtual void deleteLater();
208 
209  // Populate children using children vector created by createChildren()
210  virtual void populate();
211 
213  virtual void depopulate();
214 
215  virtual void refresh();
216 
217  void emitBeginInsertItems( QgsDataItem* parent, int first, int last );
218  void emitEndInsertItems();
219  void emitBeginRemoveItems( QgsDataItem* parent, int first, int last );
220  void emitEndRemoveItems();
221  void emitDataChanged( QgsDataItem* item );
222  void emitDataChanged( );
223  void emitStateChanged( QgsDataItem* item, QgsDataItem::State oldState );
224  virtual void childrenCreated();
225  void setPopulatingIcon();
226 
227  signals:
228  void beginInsertItems( QgsDataItem* parent, int first, int last );
229  void endInsertItems();
230  void beginRemoveItems( QgsDataItem* parent, int first, int last );
231  void endRemoveItems();
232  void dataChanged( QgsDataItem * item );
233  void stateChanged( QgsDataItem * item, QgsDataItem::State oldState );
234 
235  private:
236  static QVector<QgsDataItem*> runCreateChildren( QgsDataItem* item );
237 
238  // Set to true if object has to be deleted when possible (nothing running in threads)
239  bool mDeferredDelete;
240  QFutureWatcher< QVector <QgsDataItem*> > *mFutureWatcher;
241  // number of items currently in loading (populating) state
242  static int mPopulatingCount;
243  static QMovie * mPopulatingMovie;
244  static QIcon mPopulatingIcon;
245 };
246 
247 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsDataItem::Capabilities )
248 
249 
250 class CORE_EXPORT QgsLayerItem : public QgsDataItem
251 {
252  Q_OBJECT
253  public:
255  {
264  Table
265  };
266 
267  QgsLayerItem( QgsDataItem* parent, QString name, QString path, QString uri, LayerType layerType, QString providerKey );
268 
269  // --- reimplemented from QgsDataItem ---
270 
271  virtual bool equal( const QgsDataItem *other ) override;
272 
273  // --- New virtual methods for layer item derived classes ---
274 
275  // Returns QgsMapLayer::LayerType
276  QgsMapLayer::LayerType mapLayerType();
277 
278  // Returns layer uri or empty string if layer cannot be created
279  QString uri() { return mUri; }
280 
281  // Returns provider key
282  QString providerKey() { return mProviderKey; }
283 
287  QStringList supportedCRS() { return mSupportedCRS; }
288 
292  QStringList supportedFormats() { return mSupportFormats; }
293 
294  protected:
295 
296  QString mProviderKey;
297  QString mUri;
299  QStringList mSupportedCRS;
300  QStringList mSupportFormats;
301 
302  public:
303  static const QIcon &iconPoint();
304  static const QIcon &iconLine();
305  static const QIcon &iconPolygon();
306  static const QIcon &iconTable();
307  static const QIcon &iconRaster();
308  static const QIcon &iconDefault();
309 
310  virtual QString layerName() const { return name(); }
311 };
312 
313 
315 class CORE_EXPORT QgsDataCollectionItem : public QgsDataItem
316 {
317  Q_OBJECT
318  public:
319  QgsDataCollectionItem( QgsDataItem* parent, QString name, QString path = QString::null );
321 
322  void addChild( QgsDataItem *item ) { mChildren.append( item ); }
323 
324  static const QIcon &iconDir(); // shared icon: open/closed directory
325  static const QIcon &iconDataCollection(); // default icon for data collection
326 };
327 
329 class CORE_EXPORT QgsDirectoryItem : public QgsDataCollectionItem
330 {
331  Q_OBJECT
332  public:
333  enum Column
334  {
342  };
343 
344  QgsDirectoryItem( QgsDataItem* parent, QString name, QString path );
345 
351  QgsDirectoryItem( QgsDataItem* parent, QString name, QString dirPath, QString path );
352  ~QgsDirectoryItem();
353 
354  virtual void setState( State state ) override;
355 
356  QVector<QgsDataItem*> createChildren() override;
357 
358  QString dirPath() const { return mDirPath; }
359  virtual bool equal( const QgsDataItem *other ) override;
360  virtual QIcon icon() override;
361  virtual QWidget *paramWidget() override;
362 
363  /* static QVector<QgsDataProvider*> mProviders; */
365  static QVector<QLibrary*> mLibraries;
366 
367  public slots:
368  virtual void childrenCreated() override;
369  void directoryChanged();
370 
371  protected:
372  void init();
373  QString mDirPath;
374 
375  private:
376  QFileSystemWatcher * mFileSystemWatcher;
377  bool mRefreshLater;
378 };
379 
383 class CORE_EXPORT QgsErrorItem : public QgsDataItem
384 {
385  Q_OBJECT
386  public:
387 
388  QgsErrorItem( QgsDataItem* parent, QString error, QString path );
389  ~QgsErrorItem();
390 
391 };
392 
393 
394 // ---------
395 
396 class CORE_EXPORT QgsDirectoryParamWidget : public QTreeWidget
397 {
398  Q_OBJECT
399 
400  public:
401  QgsDirectoryParamWidget( QString path, QWidget* parent = NULL );
402 
403  protected:
404  void mousePressEvent( QMouseEvent* event ) override;
405 
406  public slots:
407  void showHideColumn();
408 };
409 
411 class CORE_EXPORT QgsFavouritesItem : public QgsDataCollectionItem
412 {
413  Q_OBJECT
414  public:
415  QgsFavouritesItem( QgsDataItem* parent, QString name, QString path = QString() );
417 
418  QVector<QgsDataItem*> createChildren() override;
419 
420  void addDirectory( QString favIcon );
421  void removeDirectory( QgsDirectoryItem *item );
422 
423  static const QIcon &iconFavourites();
424 };
425 
427 class CORE_EXPORT QgsZipItem : public QgsDataCollectionItem
428 {
429  Q_OBJECT
430 
431  protected:
432  QString mFilePath;
433  QString mVsiPrefix;
434  QStringList mZipFileList;
435 
436  public:
437  QgsZipItem( QgsDataItem* parent, QString name, QString path );
438  QgsZipItem( QgsDataItem* parent, QString name, QString filePath, QString path );
439  ~QgsZipItem();
440 
441  QVector<QgsDataItem*> createChildren() override;
442  const QStringList & getZipFileList();
443 
445  static QVector<dataItem_t *> mDataItemPtr;
446  static QStringList mProviderNames;
447 
448  static QString vsiPrefix( QString uri ) { return qgsVsiPrefix( uri ); }
449 
450  static QgsDataItem* itemFromPath( QgsDataItem* parent, QString path, QString name );
451  static QgsDataItem* itemFromPath( QgsDataItem* parent, QString filePath, QString name, QString path );
452 
453  static const QIcon &iconZip();
454 
455  private:
456  void init();
457 };
458 
459 #endif // QGSDATAITEM_H
460 
461 
A Collection: logical collection of layers or subcollections, e.g.
Definition: qgsdataitem.h:315
QStringList mSupportFormats
Definition: qgsdataitem.h:300
Contains various Favourites directories.
Definition: qgsdataitem.h:411
virtual void childrenCreated()
static QMap< QString, QIcon > mIconMap
Definition: qgsdataitem.h:197
QVector< QgsDataItem * > children() const
Definition: qgsdataitem.h:145
QString name() const
Definition: qgsdataitem.h:147
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:126
QgsDataItem * parent() const
Get item parent.
Definition: qgsdataitem.h:141
QIcon populatingIcon()
Definition: qgsdataitem.h:172
QString providerKey()
Definition: qgsdataitem.h:282
QString mProviderKey
Definition: qgsdataitem.h:296
virtual bool handleDrop(const QMimeData *, Qt::DropAction)
Definition: qgsdataitem.h:108
virtual QIcon icon()
void addChild(QgsDataItem *item)
Definition: qgsdataitem.h:322
QString qgsVsiPrefix(QString path)
Definition: qgis.cpp:254
QString mIconName
Definition: qgsdataitem.h:195
State mState
Definition: qgsdataitem.h:185
Abstract base class for spatial data provider implementations.
void setIcon(QIcon icon)
Definition: qgsdataitem.h:157
QString uri()
Definition: qgsdataitem.h:279
virtual QWidget * paramWidget()
Definition: qgsdataitem.h:99
QString mToolTip
Definition: qgsdataitem.h:194
A zip file: contains layers, using GDAL/OGR VSIFILE mechanism.
Definition: qgsdataitem.h:427
virtual bool equal(const QgsDataItem *other)
QStringList mSupportedCRS
Definition: qgsdataitem.h:299
QString mFilePath
Definition: qgsdataitem.h:432
bool isPopulated()
Definition: qgsdataitem.h:84
QString dirPath() const
Definition: qgsdataitem.h:358
static QString vsiPrefix(QString uri)
Definition: qgsdataitem.h:448
QString mName
Definition: qgsdataitem.h:188
QStringList mZipFileList
Definition: qgsdataitem.h:434
virtual void setCapabilities(Capabilities capabilities)
Definition: qgsdataitem.h:128
Children not yet created.
Definition: qgsdataitem.h:72
Creating children in separate thread (populating or refreshing)
Definition: qgsdataitem.h:73
QString path() const
Definition: qgsdataitem.h:149
Data item that can be used to report problems (e.g.
Definition: qgsdataitem.h:383
Type type() const
Definition: qgsdataitem.h:137
static QStringList mProviderNames
Definition: qgsdataitem.h:446
A directory: contains subdirectories and layers.
Definition: qgsdataitem.h:329
Base class for all items in the model.
Definition: qgsdataitem.h:43
QStringList supportedCRS()
Returns the supported CRS.
Definition: qgsdataitem.h:287
Capabilities mCapabilities
Definition: qgsdataitem.h:182
virtual void setState(State state)
Set item state.
void setName(const QString &name)
Definition: qgsdataitem.h:148
static QVector< dataItem_t * > mDataItemPtr
Definition: qgsdataitem.h:445
QString mPath
Definition: qgsdataitem.h:193
QString mUri
Definition: qgsdataitem.h:297
virtual Q_DECL_DEPRECATED Capability capabilities()
Definition: qgsdataitem.h:124
QString mVsiPrefix
Definition: qgsdataitem.h:433
static QVector< QLibrary * > mLibraries
Definition: qgsdataitem.h:365
virtual QString layerName() const
Definition: qgsdataitem.h:310
Class for storing a coordinate reference system (CRS)
QgsDataItem * mParent
Definition: qgsdataitem.h:183
bool deferredDelete()
The item is scheduled to be deleted.
Definition: qgsdataitem.h:179
void setToolTip(QString msg)
Definition: qgsdataitem.h:160
virtual QVector< QgsDataItem * > createChildren()
Create children.
bool mPopulated
Definition: qgsdataitem.h:187
Item that represents a layer that can be opened with one of the providers.
Definition: qgsdataitem.h:250
QgsDataItem * dataItem_t(QString, QgsDataItem *)
Definition: qgsdataitem.h:38
QVector< QgsDataItem * > mChildren
Definition: qgsdataitem.h:184
QString toolTip() const
Definition: qgsdataitem.h:161
QStringList supportedFormats()
Returns the supported formats.
Definition: qgsdataitem.h:292
virtual QList< QAction * > actions()
Definition: qgsdataitem.h:102
void setPath(const QString &path)
Definition: qgsdataitem.h:150
virtual bool acceptDrop()
Definition: qgsdataitem.h:105
LayerType mLayerType
Definition: qgsdataitem.h:298
void setIconName(const QString &iconName)
Definition: qgsdataitem.h:158