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