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 <QIcon>
21 #include <QLibrary>
22 #include <QObject>
23 #include <QPixmap>
24 #include <QString>
25 #include <QVector>
26 #include <QTreeWidget>
27 
28 #include "qgsapplication.h"
29 #include "qgsmaplayer.h"
31 
32 class QgsDataProvider;
33 class QgsDataItem;
34 
35 typedef QgsDataItem * dataItem_t( QString, QgsDataItem* );
36 
37 
39 class CORE_EXPORT QgsDataItem : public QObject
40 {
41  Q_OBJECT
42  public:
43  enum Type
44  {
49  Favourites
50  };
51 
52  QgsDataItem( QgsDataItem::Type type, QgsDataItem* parent, QString name, QString path );
53  virtual ~QgsDataItem();
54 
55  bool hasChildren();
56 
57  int rowCount();
58 
59  virtual void refresh();
60  virtual void refresh( QVector<QgsDataItem*> children );
61 
62  // Create vector of children
63  virtual QVector<QgsDataItem*> createChildren();
64 
65  // Populate children using children vector created by createChildren()
66  virtual void populate();
67  virtual void populate( QVector<QgsDataItem*> children );
68 
70  virtual void depopulate();
71 
72  bool isPopulated() { return mPopulated; }
73 
75  void setPopulated() { mPopulated = true; }
76 
77  // Insert new child using alphabetical order based on mName, emits necessary signal to model before and after, sets parent and connects signals
78  // refresh - refresh populated item, emit signals to model
79  virtual void addChildItem( QgsDataItem *child, bool refresh = false );
80 
81  // remove and delete child item, signals to browser are emitted
82  virtual void deleteChildItem( QgsDataItem * child );
83 
84  // remove child item but don't delete it, signals to browser are emitted
85  // returns pointer to the removed item or null if no such item was found
86  virtual QgsDataItem *removeChildItem( QgsDataItem * child );
87 
88  virtual bool equal( const QgsDataItem *other );
89 
90  virtual QWidget *paramWidget() { return 0; }
91 
92  // list of actions provided by this item - usually used for popup menu on right-click
93  virtual QList<QAction*> actions() { return QList<QAction*>(); }
94 
95  // whether accepts drag&drop'd layers - e.g. for import
96  virtual bool acceptDrop() { return false; }
97 
98  // try to process the data dropped on this item
99  virtual bool handleDrop( const QMimeData * /*data*/, Qt::DropAction /*action*/ ) { return false; }
100 
102  {
103  NoCapabilities = 0,
104  SetCrs = 1 << 0,
105  Fertile = 1 << 1,
106  Fast = 1 << 2
107  };
108  Q_DECLARE_FLAGS( Capabilities, Capability )
109 
110  // This will _write_ selected crs in data source
111  virtual bool setCrs( QgsCoordinateReferenceSystem crs )
112  { Q_UNUSED( crs ); return false; }
113 
115  Q_DECL_DEPRECATED virtual Capability capabilities() { return NoCapabilities; }
116 
117  virtual Capabilities capabilities2() const { return mCapabilities; }
118 
119  virtual void setCapabilities( Capabilities capabilities ) { mCapabilities = capabilities; }
120 
121  // static methods
122 
123  // Find child index in vector of items using '==' operator
124  static int findItem( QVector<QgsDataItem*> items, QgsDataItem * item );
125 
126  // members
127 
128  Type type() const { return mType; }
129  QgsDataItem* parent() const { return mParent; }
130  void setParent( QgsDataItem* parent ) { mParent = parent; }
131  QVector<QgsDataItem*> children() const { return mChildren; }
132  virtual QIcon icon();
133  QString name() const { return mName; }
134  QString path() const { return mPath; }
135  void setPath( const QString &path ) { mPath = path; }
136 
137  // Because QIcon (QPixmap) must not be used in outside the GUI thread, it is
138  // not possible to set mIcon in constructor. Either use mIconName/setIconName()
139  // or implement icon().
140  void setIcon( QIcon icon ) { mIcon = icon; }
141  void setIconName( const QString & iconName ) { mIconName = iconName; }
142 
143  void setToolTip( QString msg ) { mToolTip = msg; }
144  QString toolTip() const { return mToolTip; }
145 
146  protected:
147 
149  Capabilities mCapabilities;
151  QVector<QgsDataItem*> mChildren; // easier to have it always
153  QString mName;
154  // Path is slash ('/') separated chain of item identifiers which are usually item names, but may be differen if it is
155  // necessary to distinguish paths of two providers to the same source (e.g GRASS location and standard directory have the same
156  // name but different paths). Identifiers in path must not contain '/' characters.
157  // The path is used to identify item in tree.
158  QString mPath;
159  QString mToolTip;
160  QString mIconName;
161  QIcon mIcon;
162  static QMap<QString, QIcon> mIconMap;
163 
164  public slots:
165  void emitBeginInsertItems( QgsDataItem* parent, int first, int last );
166  void emitEndInsertItems();
167  void emitBeginRemoveItems( QgsDataItem* parent, int first, int last );
168  void emitEndRemoveItems();
169 
170  signals:
171  void beginInsertItems( QgsDataItem* parent, int first, int last );
172  void endInsertItems();
173  void beginRemoveItems( QgsDataItem* parent, int first, int last );
174  void endRemoveItems();
175 };
176 
177 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsDataItem::Capabilities )
178 
179 
180 class CORE_EXPORT QgsLayerItem : public QgsDataItem
181 {
182  Q_OBJECT
183  public:
185  {
194  Table
195  };
196 
197  QgsLayerItem( QgsDataItem* parent, QString name, QString path, QString uri, LayerType layerType, QString providerKey );
198 
199  // --- reimplemented from QgsDataItem ---
200 
201  virtual bool equal( const QgsDataItem *other );
202 
203  // --- New virtual methods for layer item derived classes ---
204 
205  // Returns QgsMapLayer::LayerType
206  QgsMapLayer::LayerType mapLayerType();
207 
208  // Returns layer uri or empty string if layer cannot be created
209  QString uri() { return mUri; }
210 
211  // Returns provider key
212  QString providerKey() { return mProviderKey; }
213 
214  protected:
215 
216  QString mProviderKey;
217  QString mUri;
219 
220  public:
221  static const QIcon &iconPoint();
222  static const QIcon &iconLine();
223  static const QIcon &iconPolygon();
224  static const QIcon &iconTable();
225  static const QIcon &iconRaster();
226  static const QIcon &iconDefault();
227 
228  virtual QString layerName() const { return name(); }
229 };
230 
231 
233 class CORE_EXPORT QgsDataCollectionItem : public QgsDataItem
234 {
235  Q_OBJECT
236  public:
237  QgsDataCollectionItem( QgsDataItem* parent, QString name, QString path = QString::null );
239 
240  void addChild( QgsDataItem *item ) { mChildren.append( item ); }
241 
242  static const QIcon &iconDir(); // shared icon: open/closed directory
243  static const QIcon &iconDataCollection(); // default icon for data collection
244 };
245 
247 class CORE_EXPORT QgsDirectoryItem : public QgsDataCollectionItem
248 {
249  Q_OBJECT
250  public:
251  enum Column
252  {
260  };
261 
262  QgsDirectoryItem( QgsDataItem* parent, QString name, QString path );
263 
269  QgsDirectoryItem( QgsDataItem* parent, QString name, QString dirPath, QString path );
270  ~QgsDirectoryItem();
271 
272  QVector<QgsDataItem*> createChildren();
273 
274  QString dirPath() const { return mDirPath; }
275  virtual bool equal( const QgsDataItem *other );
276  virtual QIcon icon();
277  virtual QWidget *paramWidget();
278 
279  /* static QVector<QgsDataProvider*> mProviders; */
281  static QVector<QLibrary*> mLibraries;
282 
283  protected:
284  void init();
285  QString mDirPath;
286 };
287 
291 class CORE_EXPORT QgsErrorItem : public QgsDataItem
292 {
293  Q_OBJECT
294  public:
295 
296  QgsErrorItem( QgsDataItem* parent, QString error, QString path );
297  ~QgsErrorItem();
298 
299 };
300 
301 
302 // ---------
303 
304 class CORE_EXPORT QgsDirectoryParamWidget : public QTreeWidget
305 {
306  Q_OBJECT
307 
308  public:
309  QgsDirectoryParamWidget( QString path, QWidget* parent = NULL );
310 
311  protected:
312  void mousePressEvent( QMouseEvent* event );
313 
314  public slots:
315  void showHideColumn();
316 };
317 
319 class CORE_EXPORT QgsFavouritesItem : public QgsDataCollectionItem
320 {
321  Q_OBJECT
322  public:
323  QgsFavouritesItem( QgsDataItem* parent, QString name, QString path = QString() );
325 
326  QVector<QgsDataItem*> createChildren();
327 
328  void addDirectory( QString favIcon );
329  void removeDirectory( QgsDirectoryItem *item );
330 
331  static const QIcon &iconFavourites();
332 };
333 
335 class CORE_EXPORT QgsZipItem : public QgsDataCollectionItem
336 {
337  Q_OBJECT
338 
339  protected:
340  QString mDirPath;
341  QString mVsiPrefix;
342  QStringList mZipFileList;
343 
344  public:
345  QgsZipItem( QgsDataItem* parent, QString name, QString path );
346  QgsZipItem( QgsDataItem* parent, QString name, QString dirPath, QString path );
347  ~QgsZipItem();
348 
349  QVector<QgsDataItem*> createChildren();
350  const QStringList & getZipFileList();
351 
353  static QVector<dataItem_t *> mDataItemPtr;
354  static QStringList mProviderNames;
355 
356  static QString vsiPrefix( QString uri ) { return qgsVsiPrefix( uri ); }
357 
358  static QgsDataItem* itemFromPath( QgsDataItem* parent, QString path, QString name );
359  static QgsDataItem* itemFromPath( QgsDataItem* parent, QString dirPath, QString name, QString path );
360 
361  static const QIcon &iconZip();
362 
363  private:
364  void init();
365 };
366 
367 #endif // QGSDATAITEM_H
368 
A Collection: logical collection of layers or subcollections, e.g.
Definition: qgsdataitem.h:233
Contains various Favourites directories.
Definition: qgsdataitem.h:319
static QMap< QString, QIcon > mIconMap
Definition: qgsdataitem.h:162
QVector< QgsDataItem * > children() const
Definition: qgsdataitem.h:131
QString name() const
Definition: qgsdataitem.h:133
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:117
QgsDataItem * parent() const
Definition: qgsdataitem.h:129
QString providerKey()
Definition: qgsdataitem.h:212
QString mProviderKey
Definition: qgsdataitem.h:216
virtual bool handleDrop(const QMimeData *, Qt::DropAction)
Definition: qgsdataitem.h:99
virtual QIcon icon()
void addChild(QgsDataItem *item)
Definition: qgsdataitem.h:240
QString qgsVsiPrefix(QString path)
Definition: qgis.cpp:242
QString mIconName
Definition: qgsdataitem.h:160
Abstract base class for spatial data provider implementations.
void setPopulated()
Set as populated without populating.
Definition: qgsdataitem.h:75
void setIcon(QIcon icon)
Definition: qgsdataitem.h:140
static QIcon icon(QString icon)
QString uri()
Definition: qgsdataitem.h:209
virtual QWidget * paramWidget()
Definition: qgsdataitem.h:90
QString mToolTip
Definition: qgsdataitem.h:159
A zip file: contains layers, using GDAL/OGR VSIFILE mechanism.
Definition: qgsdataitem.h:335
virtual bool equal(const QgsDataItem *other)
bool isPopulated()
Definition: qgsdataitem.h:72
QString dirPath() const
Definition: qgsdataitem.h:274
static QString vsiPrefix(QString uri)
Definition: qgsdataitem.h:356
QString mName
Definition: qgsdataitem.h:153
QStringList mZipFileList
Definition: qgsdataitem.h:342
virtual void setCapabilities(Capabilities capabilities)
Definition: qgsdataitem.h:119
QString path() const
Definition: qgsdataitem.h:134
Data item that can be used to report problems (e.g.
Definition: qgsdataitem.h:291
Type type() const
Definition: qgsdataitem.h:128
static QStringList mProviderNames
Definition: qgsdataitem.h:354
A directory: contains subdirectories and layers.
Definition: qgsdataitem.h:247
base class for all items in the model
Definition: qgsdataitem.h:39
Capabilities mCapabilities
Definition: qgsdataitem.h:149
static QVector< dataItem_t * > mDataItemPtr
Definition: qgsdataitem.h:353
QString mPath
Definition: qgsdataitem.h:158
QString mUri
Definition: qgsdataitem.h:217
virtual Q_DECL_DEPRECATED Capability capabilities()
Definition: qgsdataitem.h:115
QString mVsiPrefix
Definition: qgsdataitem.h:341
static QVector< QLibrary * > mLibraries
Definition: qgsdataitem.h:281
virtual QString layerName() const
Definition: qgsdataitem.h:228
void setParent(QgsDataItem *parent)
Definition: qgsdataitem.h:130
Class for storing a coordinate reference system (CRS)
QgsDataItem * mParent
Definition: qgsdataitem.h:150
void setToolTip(QString msg)
Definition: qgsdataitem.h:143
virtual QVector< QgsDataItem * > createChildren()
bool mPopulated
Definition: qgsdataitem.h:152
Item that represents a layer that can be opened with one of the providers.
Definition: qgsdataitem.h:180
QgsDataItem * dataItem_t(QString, QgsDataItem *)
Definition: qgsdataitem.h:35
QVector< QgsDataItem * > mChildren
Definition: qgsdataitem.h:151
QString toolTip() const
Definition: qgsdataitem.h:144
virtual QList< QAction * > actions()
Definition: qgsdataitem.h:93
void setPath(const QString &path)
Definition: qgsdataitem.h:135
QString mDirPath
Definition: qgsdataitem.h:340
virtual bool acceptDrop()
Definition: qgsdataitem.h:96
LayerType mLayerType
Definition: qgsdataitem.h:218
void setIconName(const QString &iconName)
Definition: qgsdataitem.h:141