QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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