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