QGIS API Documentation  2.7.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; }
140  QgsDataItem* parent() const { return mParent; }
143  void setParent( QgsDataItem* parent );
144  QVector<QgsDataItem*> children() const { return mChildren; }
145  virtual QIcon icon();
146  QString name() const { return mName; }
147  void setName( const QString &name ) { mName = name; }
148  QString path() const { return mPath; }
149  void setPath( const QString &path ) { mPath = path; }
151  static QString pathComponent( const QString &component );
152 
153  // Because QIcon (QPixmap) must not be used in outside the GUI thread, it is
154  // not possible to set mIcon in constructor. Either use mIconName/setIconName()
155  // or implement icon().
156  void setIcon( QIcon icon ) { mIcon = icon; }
157  void setIconName( const QString & iconName ) { mIconName = iconName; }
158 
159  void setToolTip( QString msg ) { mToolTip = msg; }
160  QString toolTip() const { return mToolTip; }
161 
162  // deleteLater() items anc clear the vector
163  static void deleteLater( QVector<QgsDataItem*> &items );
164 
166  void moveToThread( QThread * targetThread );
167 
168  protected:
169  virtual void populate( QVector<QgsDataItem*> children );
170  virtual void refresh( QVector<QgsDataItem*> children );
171  QIcon populatingIcon() { return mPopulatingIcon; }
178  bool deferredDelete() { return mDeferredDelete; }
179 
181  Capabilities mCapabilities;
183  QVector<QgsDataItem*> mChildren; // easier to have it always
187  QString mName;
188  // Path is slash ('/') separated chain of item identifiers which are usually item names, but may be differen if it is
189  // necessary to distinguish paths of two providers to the same source (e.g GRASS location and standard directory have the same
190  // name but different paths). Identifiers in path must not contain '/' characters.
191  // The path is used to identify item in tree.
192  QString mPath;
193  QString mToolTip;
194  QString mIconName;
195  QIcon mIcon;
196  static QMap<QString, QIcon> mIconMap;
197 
198  public slots:
206  virtual void deleteLater();
207 
208  // Populate children using children vector created by createChildren()
209  virtual void populate();
210 
212  virtual void depopulate();
213 
214  virtual void refresh();
215 
216  void emitBeginInsertItems( QgsDataItem* parent, int first, int last );
217  void emitEndInsertItems();
218  void emitBeginRemoveItems( QgsDataItem* parent, int first, int last );
219  void emitEndRemoveItems();
220  void emitDataChanged( QgsDataItem* item );
221  void emitDataChanged( );
222  void emitStateChanged( QgsDataItem* item, QgsDataItem::State oldState );
223  virtual void childrenCreated();
224  void setPopulatingIcon();
225 
226  signals:
227  void beginInsertItems( QgsDataItem* parent, int first, int last );
228  void endInsertItems();
229  void beginRemoveItems( QgsDataItem* parent, int first, int last );
230  void endRemoveItems();
231  void dataChanged( QgsDataItem * item );
232  void stateChanged( QgsDataItem * item, QgsDataItem::State oldState );
233 
234  private:
235  static QVector<QgsDataItem*> runCreateChildren( QgsDataItem* item );
236 
237  // Set to true if object has to be deleted when possible (nothing running in threads)
238  bool mDeferredDelete;
239  QFutureWatcher< QVector <QgsDataItem*> > *mFutureWatcher;
240  // number of items currently in loading (populating) state
241  static int mPopulatingCount;
242  static QMovie * mPopulatingMovie;
243  static QIcon mPopulatingIcon;
244 };
245 
246 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsDataItem::Capabilities )
247 
248 
249 class CORE_EXPORT QgsLayerItem : public QgsDataItem
250 {
251  Q_OBJECT
252  public:
254  {
263  Table
264  };
265 
266  QgsLayerItem( QgsDataItem* parent, QString name, QString path, QString uri, LayerType layerType, QString providerKey );
267 
268  // --- reimplemented from QgsDataItem ---
269 
270  virtual bool equal( const QgsDataItem *other ) override;
271 
272  // --- New virtual methods for layer item derived classes ---
273 
274  // Returns QgsMapLayer::LayerType
275  QgsMapLayer::LayerType mapLayerType();
276 
277  // Returns layer uri or empty string if layer cannot be created
278  QString uri() { return mUri; }
279 
280  // Returns provider key
281  QString providerKey() { return mProviderKey; }
282 
286  QStringList supportedCRS() { return mSupportedCRS; }
287 
291  QStringList supportedFormats() { return mSupportFormats; }
292 
293  protected:
294 
295  QString mProviderKey;
296  QString mUri;
298  QStringList mSupportedCRS;
299  QStringList mSupportFormats;
300 
301  public:
302  static const QIcon &iconPoint();
303  static const QIcon &iconLine();
304  static const QIcon &iconPolygon();
305  static const QIcon &iconTable();
306  static const QIcon &iconRaster();
307  static const QIcon &iconDefault();
308 
309  virtual QString layerName() const { return name(); }
310 };
311 
312 
314 class CORE_EXPORT QgsDataCollectionItem : public QgsDataItem
315 {
316  Q_OBJECT
317  public:
318  QgsDataCollectionItem( QgsDataItem* parent, QString name, QString path = QString::null );
320 
321  void addChild( QgsDataItem *item ) { mChildren.append( item ); }
322 
323  static const QIcon &iconDir(); // shared icon: open/closed directory
324  static const QIcon &iconDataCollection(); // default icon for data collection
325 };
326 
328 class CORE_EXPORT QgsDirectoryItem : public QgsDataCollectionItem
329 {
330  Q_OBJECT
331  public:
332  enum Column
333  {
341  };
342 
343  QgsDirectoryItem( QgsDataItem* parent, QString name, QString path );
344 
350  QgsDirectoryItem( QgsDataItem* parent, QString name, QString dirPath, QString path );
351  ~QgsDirectoryItem();
352 
353  virtual void setState( State state ) override;
354 
355  QVector<QgsDataItem*> createChildren() override;
356 
357  QString dirPath() const { return mDirPath; }
358  virtual bool equal( const QgsDataItem *other ) override;
359  virtual QIcon icon() override;
360  virtual QWidget *paramWidget() override;
361 
362  /* static QVector<QgsDataProvider*> mProviders; */
364  static QVector<QLibrary*> mLibraries;
365 
366  public slots:
367  virtual void childrenCreated() override;
368  void directoryChanged();
369 
370  protected:
371  void init();
372  QString mDirPath;
373 
374  private:
375  QFileSystemWatcher * mFileSystemWatcher;
376  bool mRefreshLater;
377 };
378 
382 class CORE_EXPORT QgsErrorItem : public QgsDataItem
383 {
384  Q_OBJECT
385  public:
386 
387  QgsErrorItem( QgsDataItem* parent, QString error, QString path );
388  ~QgsErrorItem();
389 
390 };
391 
392 
393 // ---------
394 
395 class CORE_EXPORT QgsDirectoryParamWidget : public QTreeWidget
396 {
397  Q_OBJECT
398 
399  public:
400  QgsDirectoryParamWidget( QString path, QWidget* parent = NULL );
401 
402  protected:
403  void mousePressEvent( QMouseEvent* event ) override;
404 
405  public slots:
406  void showHideColumn();
407 };
408 
410 class CORE_EXPORT QgsFavouritesItem : public QgsDataCollectionItem
411 {
412  Q_OBJECT
413  public:
414  QgsFavouritesItem( QgsDataItem* parent, QString name, QString path = QString() );
416 
417  QVector<QgsDataItem*> createChildren() override;
418 
419  void addDirectory( QString favIcon );
420  void removeDirectory( QgsDirectoryItem *item );
421 
422  static const QIcon &iconFavourites();
423 };
424 
426 class CORE_EXPORT QgsZipItem : public QgsDataCollectionItem
427 {
428  Q_OBJECT
429 
430  protected:
431  QString mFilePath;
432  QString mVsiPrefix;
433  QStringList mZipFileList;
434 
435  public:
436  QgsZipItem( QgsDataItem* parent, QString name, QString path );
437  QgsZipItem( QgsDataItem* parent, QString name, QString filePath, QString path );
438  ~QgsZipItem();
439 
440  QVector<QgsDataItem*> createChildren() override;
441  const QStringList & getZipFileList();
442 
444  static QVector<dataItem_t *> mDataItemPtr;
445  static QStringList mProviderNames;
446 
447  static QString vsiPrefix( QString uri ) { return qgsVsiPrefix( uri ); }
448 
449  static QgsDataItem* itemFromPath( QgsDataItem* parent, QString path, QString name );
450  static QgsDataItem* itemFromPath( QgsDataItem* parent, QString filePath, QString name, QString path );
451 
452  static const QIcon &iconZip();
453 
454  private:
455  void init();
456 };
457 
458 #endif // QGSDATAITEM_H
459 
460 
A Collection: logical collection of layers or subcollections, e.g.
Definition: qgsdataitem.h:314
QStringList mSupportFormats
Definition: qgsdataitem.h:299
Contains various Favourites directories.
Definition: qgsdataitem.h:410
virtual void childrenCreated()
static QMap< QString, QIcon > mIconMap
Definition: qgsdataitem.h:196
QVector< QgsDataItem * > children() const
Definition: qgsdataitem.h:144
QString name() const
Definition: qgsdataitem.h:146
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:140
QIcon populatingIcon()
Definition: qgsdataitem.h:171
QString providerKey()
Definition: qgsdataitem.h:281
QString mProviderKey
Definition: qgsdataitem.h:295
virtual bool handleDrop(const QMimeData *, Qt::DropAction)
Definition: qgsdataitem.h:108
virtual QIcon icon()
void addChild(QgsDataItem *item)
Definition: qgsdataitem.h:321
QString qgsVsiPrefix(QString path)
Definition: qgis.cpp:242
QString mIconName
Definition: qgsdataitem.h:194
State mState
Definition: qgsdataitem.h:184
Abstract base class for spatial data provider implementations.
void setIcon(QIcon icon)
Definition: qgsdataitem.h:156
QString uri()
Definition: qgsdataitem.h:278
virtual QWidget * paramWidget()
Definition: qgsdataitem.h:99
QString mToolTip
Definition: qgsdataitem.h:193
A zip file: contains layers, using GDAL/OGR VSIFILE mechanism.
Definition: qgsdataitem.h:426
virtual bool equal(const QgsDataItem *other)
QStringList mSupportedCRS
Definition: qgsdataitem.h:298
QString mFilePath
Definition: qgsdataitem.h:431
bool isPopulated()
Definition: qgsdataitem.h:84
QString dirPath() const
Definition: qgsdataitem.h:357
static QString vsiPrefix(QString uri)
Definition: qgsdataitem.h:447
QString mName
Definition: qgsdataitem.h:187
QStringList mZipFileList
Definition: qgsdataitem.h:433
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:148
Data item that can be used to report problems (e.g.
Definition: qgsdataitem.h:382
Type type() const
Definition: qgsdataitem.h:137
static QStringList mProviderNames
Definition: qgsdataitem.h:445
A directory: contains subdirectories and layers.
Definition: qgsdataitem.h:328
Base class for all items in the model.
Definition: qgsdataitem.h:43
QStringList supportedCRS()
Returns the supported CRS.
Definition: qgsdataitem.h:286
Capabilities mCapabilities
Definition: qgsdataitem.h:181
virtual void setState(State state)
Set item state.
void setName(const QString &name)
Definition: qgsdataitem.h:147
static QVector< dataItem_t * > mDataItemPtr
Definition: qgsdataitem.h:444
QString mPath
Definition: qgsdataitem.h:192
QString mUri
Definition: qgsdataitem.h:296
virtual Q_DECL_DEPRECATED Capability capabilities()
Definition: qgsdataitem.h:124
QString mVsiPrefix
Definition: qgsdataitem.h:432
static QVector< QLibrary * > mLibraries
Definition: qgsdataitem.h:364
virtual QString layerName() const
Definition: qgsdataitem.h:309
Class for storing a coordinate reference system (CRS)
QgsDataItem * mParent
Definition: qgsdataitem.h:182
bool deferredDelete()
The item is scheduled to be deleted.
Definition: qgsdataitem.h:178
void setToolTip(QString msg)
Definition: qgsdataitem.h:159
virtual QVector< QgsDataItem * > createChildren()
Create children.
bool mPopulated
Definition: qgsdataitem.h:186
Item that represents a layer that can be opened with one of the providers.
Definition: qgsdataitem.h:249
QgsDataItem * dataItem_t(QString, QgsDataItem *)
Definition: qgsdataitem.h:38
QVector< QgsDataItem * > mChildren
Definition: qgsdataitem.h:183
QString toolTip() const
Definition: qgsdataitem.h:160
QStringList supportedFormats()
Returns the supported formats.
Definition: qgsdataitem.h:291
virtual QList< QAction * > actions()
Definition: qgsdataitem.h:102
void setPath(const QString &path)
Definition: qgsdataitem.h:149
virtual bool acceptDrop()
Definition: qgsdataitem.h:105
LayerType mLayerType
Definition: qgsdataitem.h:297
void setIconName(const QString &iconName)
Definition: qgsdataitem.h:157