QGIS API Documentation  2.0.1-Dufour
 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 <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 // TODO: bad place, where to put this? qgsproviderregistry.h?
36 typedef int dataCapabilities_t();
37 typedef QgsDataItem * dataItem_t( QString, QgsDataItem* );
38 
39 
41 class CORE_EXPORT QgsDataItem : public QObject
42 {
43  Q_OBJECT
44  public:
45  enum Type
46  {
51  Favourites
52  };
53 
54  QgsDataItem( QgsDataItem::Type type, QgsDataItem* parent, QString name, QString path );
55  virtual ~QgsDataItem();
56 
57  bool hasChildren();
58 
59  int rowCount();
60 
61  //
62 
63  virtual void refresh();
64 
65  // Create vector of children
66  virtual QVector<QgsDataItem*> createChildren();
67 
68  // Populate children using children vector created by createChildren()
69  virtual void populate();
70  bool isPopulated() { return mPopulated; }
71 
72  // Insert new child using alphabetical order based on mName, emits necessary signal to model before and after, sets parent and connects signals
73  // refresh - refresh populated item, emit signals to model
74  virtual void addChildItem( QgsDataItem * child, bool refresh = false );
75 
76  // remove and delete child item, signals to browser are emitted
77  virtual void deleteChildItem( QgsDataItem * child );
78 
79  // remove child item but don't delete it, signals to browser are emitted
80  // returns pointer to the removed item or null if no such item was found
81  virtual QgsDataItem * removeChildItem( QgsDataItem * child );
82 
83  virtual bool equal( const QgsDataItem *other );
84 
85  virtual QWidget * paramWidget() { return 0; }
86 
87  // list of actions provided by this item - usually used for popup menu on right-click
88  virtual QList<QAction*> actions() { return QList<QAction*>(); }
89 
90  // whether accepts drag&drop'd layers - e.g. for import
91  virtual bool acceptDrop() { return false; }
92 
93  // try to process the data dropped on this item
94  virtual bool handleDrop( const QMimeData * /*data*/, Qt::DropAction /*action*/ ) { return false; }
95 
96  //
97 
99  {
100  NoCapabilities = 0,
101  SetCrs = 1 //Can set CRS on layer or group of layers
102  };
103 
104  // This will _write_ selected crs in data source
105  virtual bool setCrs( QgsCoordinateReferenceSystem crs )
106  { Q_UNUSED( crs ); return false; }
107 
108  virtual Capability capabilities() { return NoCapabilities; }
109 
110  // static methods
111 
112  // Find child index in vector of items using '==' operator
113  static int findItem( QVector<QgsDataItem*> items, QgsDataItem * item );
114 
115  // members
116 
117  Type type() const { return mType; }
118  QgsDataItem* parent() const { return mParent; }
119  void setParent( QgsDataItem* parent ) { mParent = parent; }
120  QVector<QgsDataItem*> children() const { return mChildren; }
121  QIcon icon() const { return mIcon; }
122  QString name() const { return mName; }
123  QString path() const { return mPath; }
124 
125  void setIcon( QIcon icon ) { mIcon = icon; }
126 
127  void setToolTip( QString msg ) { mToolTip = msg; }
128  QString toolTip() const { return mToolTip; }
129 
130  protected:
131 
134  QVector<QgsDataItem*> mChildren; // easier to have it always
136  QString mName;
137  QString mPath; // it is also used to identify item in tree
138  QString mToolTip;
139  QIcon mIcon;
140 
141  public slots:
142  void emitBeginInsertItems( QgsDataItem* parent, int first, int last );
143  void emitEndInsertItems();
144  void emitBeginRemoveItems( QgsDataItem* parent, int first, int last );
145  void emitEndRemoveItems();
146 
147  signals:
148  void beginInsertItems( QgsDataItem* parent, int first, int last );
149  void endInsertItems();
150  void beginRemoveItems( QgsDataItem* parent, int first, int last );
151  void endRemoveItems();
152 };
153 
155 class CORE_EXPORT QgsLayerItem : public QgsDataItem
156 {
157  Q_OBJECT
158  public:
160  {
169  Table
170  };
171 
172  QgsLayerItem( QgsDataItem* parent, QString name, QString path, QString uri, LayerType layerType, QString providerKey );
173 
174  // --- reimplemented from QgsDataItem ---
175 
176  virtual bool equal( const QgsDataItem *other );
177 
178  // --- New virtual methods for layer item derived classes ---
179 
180  // Returns QgsMapLayer::LayerType
181  QgsMapLayer::LayerType mapLayerType();
182 
183  // Returns layer uri or empty string if layer cannot be created
184  QString uri() { return mUri; }
185 
186  // Returns provider key
187  QString providerKey() { return mProviderKey; }
188 
189  protected:
190 
191  QString mProviderKey;
192  QString mUri;
194 
195  public:
196  static const QIcon &iconPoint();
197  static const QIcon &iconLine();
198  static const QIcon &iconPolygon();
199  static const QIcon &iconTable();
200  static const QIcon &iconRaster();
201  static const QIcon &iconDefault();
202 
203  virtual QString layerName() const { return name(); }
204 };
205 
206 
208 class CORE_EXPORT QgsDataCollectionItem : public QgsDataItem
209 {
210  Q_OBJECT
211  public:
212  QgsDataCollectionItem( QgsDataItem* parent, QString name, QString path = QString::null );
214 
215  void setPopulated() { mPopulated = true; }
216  void addChild( QgsDataItem *item ) { mChildren.append( item ); }
217 
218  static const QIcon &iconDir(); // shared icon: open/closed directory
219  static const QIcon &iconDataCollection(); // default icon for data collection
220 };
221 
223 class CORE_EXPORT QgsDirectoryItem : public QgsDataCollectionItem
224 {
225  Q_OBJECT
226  public:
227  enum Column
228  {
236  };
237  QgsDirectoryItem( QgsDataItem* parent, QString name, QString path );
238  ~QgsDirectoryItem();
239 
240  QVector<QgsDataItem*> createChildren();
241 
242  virtual bool equal( const QgsDataItem *other );
243 
244  virtual QWidget *paramWidget();
245 
246  /* static QVector<QgsDataProvider*> mProviders; */
248  static QVector<QLibrary*> mLibraries;
249 };
250 
254 class CORE_EXPORT QgsErrorItem : public QgsDataItem
255 {
256  Q_OBJECT
257  public:
258 
259  QgsErrorItem( QgsDataItem* parent, QString error, QString path );
260  ~QgsErrorItem();
261 
262  //QVector<QgsDataItem*> createChildren();
263  //virtual bool equal( const QgsDataItem *other );
264 };
265 
266 
267 // ---------
268 
269 class CORE_EXPORT QgsDirectoryParamWidget : public QTreeWidget
270 {
271  Q_OBJECT
272 
273  public:
274  QgsDirectoryParamWidget( QString path, QWidget* parent = NULL );
275 
276  protected:
277  void mousePressEvent( QMouseEvent* event );
278 
279  public slots:
280  void showHideColumn();
281 };
282 
284 class CORE_EXPORT QgsFavouritesItem : public QgsDataCollectionItem
285 {
286  Q_OBJECT
287  public:
288  QgsFavouritesItem( QgsDataItem* parent, QString name, QString path = QString() );
290 
291  QVector<QgsDataItem*> createChildren();
292 
293  void addDirectory( QString favIcon );
294  void removeDirectory( QgsDirectoryItem *item );
295 
296  static const QIcon &iconFavourites();
297 };
298 
300 class CORE_EXPORT QgsZipItem : public QgsDataCollectionItem
301 {
302  Q_OBJECT
303 
304  protected:
305  QString mVsiPrefix;
306  QStringList mZipFileList;
307 
308  public:
309  QgsZipItem( QgsDataItem* parent, QString name, QString path );
310  ~QgsZipItem();
311 
312  QVector<QgsDataItem*> createChildren();
313  const QStringList & getZipFileList();
314 
316  static QVector<dataItem_t *> mDataItemPtr;
317  static QStringList mProviderNames;
318 
319  static QString vsiPrefix( QString uri );
320 
321  static QgsDataItem* itemFromPath( QgsDataItem* parent, QString path, QString name );
322 
323  static const QIcon &iconZip();
324 
325 };
326 
327 #endif // QGSDATAITEM_H
328