QGIS API Documentation  2.11.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties 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 
39 
41 class CORE_EXPORT 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
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.
232 
233  public slots:
241  virtual void deleteLater();
242 
243  // Populate children using children vector created by createChildren()
244  // @param foreground run createChildren in foreground
245  virtual void populate( bool foreground = false );
246 
248  virtual void depopulate();
249 
250  virtual void refresh();
251 
252  void emitBeginInsertItems( QgsDataItem* parent, int first, int last );
253  void emitEndInsertItems();
254  void emitBeginRemoveItems( QgsDataItem* parent, int first, int last );
255  void emitEndRemoveItems();
256  void emitDataChanged( QgsDataItem* item );
257  void emitDataChanged( );
258  void emitStateChanged( QgsDataItem* item, QgsDataItem::State oldState );
259  virtual void childrenCreated();
260 
261  signals:
262  void beginInsertItems( QgsDataItem* parent, int first, int last );
263  void endInsertItems();
264  void beginRemoveItems( QgsDataItem* parent, int first, int last );
265  void endRemoveItems();
266  void dataChanged( QgsDataItem * item );
267  void stateChanged( QgsDataItem * item, QgsDataItem::State oldState );
268 
269  private:
270  static QVector<QgsDataItem*> runCreateChildren( QgsDataItem* item );
271 
272  // Set to true if object has to be deleted when possible (nothing running in threads)
273  bool mDeferredDelete;
274  QFutureWatcher< QVector <QgsDataItem*> > *mFutureWatcher;
275  // number of items currently in loading (populating) state
276  static QgsAnimatedIcon * mPopulatingIcon;
277 };
278 
279 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsDataItem::Capabilities )
280 
281 
282 class CORE_EXPORT QgsLayerItem : public QgsDataItem
283 {
284  Q_OBJECT
285  public:
287  {
297  Plugin
298  };
299 
300  QgsLayerItem( QgsDataItem* parent, QString name, QString path, QString uri, LayerType layerType, QString providerKey );
301 
302  // --- reimplemented from QgsDataItem ---
303 
304  virtual bool equal( const QgsDataItem *other ) override;
305 
306  // --- New virtual methods for layer item derived classes ---
307 
308  // Returns QgsMapLayer::LayerType
309  QgsMapLayer::LayerType mapLayerType();
310 
311  // Returns layer uri or empty string if layer cannot be created
312  QString uri() { return mUri; }
313 
314  // Returns provider key
315  QString providerKey() { return mProviderKey; }
316 
320  QStringList supportedCRS() { return mSupportedCRS; }
321 
325  QStringList supportedFormats() { return mSupportFormats; }
326 
327  protected:
328 
334 
335  public:
336  static const QIcon &iconPoint();
337  static const QIcon &iconLine();
338  static const QIcon &iconPolygon();
339  static const QIcon &iconTable();
340  static const QIcon &iconRaster();
341  static const QIcon &iconDefault();
342 
343  virtual QString layerName() const { return name(); }
344 };
345 
346 
348 class CORE_EXPORT QgsDataCollectionItem : public QgsDataItem
349 {
350  Q_OBJECT
351  public:
352  QgsDataCollectionItem( QgsDataItem* parent, QString name, QString path = QString::null );
354 
355  void addChild( QgsDataItem *item ) { mChildren.append( item ); }
356 
357  static const QIcon &iconDir(); // shared icon: open/closed directory
358  static const QIcon &iconDataCollection(); // default icon for data collection
359 };
360 
362 class CORE_EXPORT QgsDirectoryItem : public QgsDataCollectionItem
363 {
364  Q_OBJECT
365  public:
366  enum Column
367  {
375  };
376 
377  QgsDirectoryItem( QgsDataItem* parent, QString name, QString path );
378 
384  QgsDirectoryItem( QgsDataItem* parent, QString name, QString dirPath, QString path );
385  ~QgsDirectoryItem();
386 
387  virtual void setState( State state ) override;
388 
390 
391  QString dirPath() const { return mDirPath; }
392  virtual bool equal( const QgsDataItem *other ) override;
393  virtual QIcon icon() override;
394  virtual QWidget *paramWidget() override;
395 
396  /* static QVector<QgsDataProvider*> mProviders; */
399  Q_DECL_DEPRECATED static QVector<QLibrary*> mLibraries;
400 
401  public slots:
402  virtual void childrenCreated() override;
403  void directoryChanged();
404 
405  protected:
406  void init();
408 
409  private:
410  QFileSystemWatcher * mFileSystemWatcher;
411  bool mRefreshLater;
412 };
413 
417 class CORE_EXPORT QgsErrorItem : public QgsDataItem
418 {
419  Q_OBJECT
420  public:
421 
422  QgsErrorItem( QgsDataItem* parent, QString error, QString path );
423  ~QgsErrorItem();
424 
425 };
426 
427 
428 // ---------
429 
430 class CORE_EXPORT QgsDirectoryParamWidget : public QTreeWidget
431 {
432  Q_OBJECT
433 
434  public:
435  QgsDirectoryParamWidget( QString path, QWidget* parent = NULL );
436 
437  protected:
438  void mousePressEvent( QMouseEvent* event ) override;
439 
440  public slots:
441  void showHideColumn();
442 };
443 
445 class CORE_EXPORT QgsFavouritesItem : public QgsDataCollectionItem
446 {
447  Q_OBJECT
448  public:
449  QgsFavouritesItem( QgsDataItem* parent, QString name, QString path = QString() );
451 
453 
454  void addDirectory( QString favIcon );
455  void removeDirectory( QgsDirectoryItem *item );
456 
457  static const QIcon &iconFavourites();
458 };
459 
461 class CORE_EXPORT QgsZipItem : public QgsDataCollectionItem
462 {
463  Q_OBJECT
464 
465  protected:
469 
470  public:
471  QgsZipItem( QgsDataItem* parent, QString name, QString path );
472  QgsZipItem( QgsDataItem* parent, QString name, QString filePath, QString path );
473  ~QgsZipItem();
474 
476  const QStringList & getZipFileList();
477 
481 
482  static QString vsiPrefix( QString uri ) { return qgsVsiPrefix( uri ); }
483 
484  static QgsDataItem* itemFromPath( QgsDataItem* parent, QString path, QString name );
485  static QgsDataItem* itemFromPath( QgsDataItem* parent, QString filePath, QString name, QString path );
486 
487  static const QIcon &iconZip();
488 
489  private:
490  void init();
491 };
492 
493 #endif // QGSDATAITEM_H
494 
495 
A Collection: logical collection of layers or subcollections, e.g.
Definition: qgsdataitem.h:348
QStringList mSupportFormats
Definition: qgsdataitem.h:333
Contains various Favourites directories.
Definition: qgsdataitem.h:445
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
QgsDataItem * parent() const
Get item parent.
Definition: qgsdataitem.h:176
QIcon icon() const
Definition: qgsdataitem.h:52
QString providerKey()
Definition: qgsdataitem.h:315
QString mProviderKey
Definition: qgsdataitem.h:329
virtual bool handleDrop(const QMimeData *, Qt::DropAction)
Definition: qgsdataitem.h:143
void moveToThread(QThread *targetThread)
virtual QIcon icon()
void addChild(QgsDataItem *item)
Definition: qgsdataitem.h:355
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 iconPath(QString iconFile)
QString uri()
Definition: qgsdataitem.h:312
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:461
virtual bool equal(const QgsDataItem *other)
QStringList mSupportedCRS
Definition: qgsdataitem.h:332
const char * name() const
QString mFilePath
Definition: qgsdataitem.h:466
bool isPopulated()
Definition: qgsdataitem.h:119
QString dirPath() const
Definition: qgsdataitem.h:391
static QString vsiPrefix(QString uri)
Definition: qgsdataitem.h:482
QString mName
Definition: qgsdataitem.h:222
QStringList mZipFileList
Definition: qgsdataitem.h:468
virtual void setCapabilities(Capabilities capabilities)
Definition: qgsdataitem.h:163
Children not yet created.
Definition: qgsdataitem.h:104
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:417
Type type() const
Definition: qgsdataitem.h:172
void deleteLater()
static QStringList mProviderNames
Definition: qgsdataitem.h:480
A directory: contains subdirectories and layers.
Definition: qgsdataitem.h:362
Base class for all items in the model.
Definition: qgsdataitem.h:75
void setParent(QObject *parent)
QStringList supportedCRS()
Returns the supported CRS.
Definition: qgsdataitem.h:320
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:479
QString mPath
Definition: qgsdataitem.h:227
QString mUri
Definition: qgsdataitem.h:330
virtual Q_DECL_DEPRECATED Capability capabilities()
Definition: qgsdataitem.h:159
QString mVsiPrefix
Definition: qgsdataitem.h:467
static Q_DECL_DEPRECATED QVector< QLibrary * > mLibraries
Definition: qgsdataitem.h:399
virtual QString layerName() const
Definition: qgsdataitem.h:343
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:282
virtual void mousePressEvent(QMouseEvent *event)
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:325
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:331
void setIconName(const QString &iconName)
Definition: qgsdataitem.h:193