LayerItemModel.h
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3 This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5 TerraLib is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9 
10 TerraLib is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14 
15 You should have received a copy of the GNU Lesser General Public License
16 along with TerraLib. See COPYING. If not, write to
17 TerraLib Team at <terralib-team@terralib.org>.
18 */
19 
20 /*!
21  * \file LayerItemModel.h
22  *
23  * \brief Defines a layer item model for Qt5.
24  */
25 #ifndef __TERRALIB_QT_WIDGETS_LAYER_INTERNAL_LAYERITEMMODEL_H
26 #define __TERRALIB_QT_WIDGETS_LAYER_INTERNAL_LAYERITEMMODEL_H
27 
28 #include "../../Config.h"
29 
30 // TerraLib
31 #include "../../../../maptools/AbstractLayer.h"
32 
33 // Qt
34 #include <QAbstractItemModel>
35 #include <QVariant>
36 
37 // STL
38 #include <list>
39 #include <memory>
40 
41 #ifndef Q_DECL_OVERRIDE
42  #define Q_DECL_OVERRIDE
43 #endif
44 
45 
46 // Forward declarations
47 namespace te
48 {
49  namespace qt
50  {
51  namespace widgets
52  {
53  class TreeItem;
54 
55  /*!
56  * \class LayerItemModel
57  *
58  * \brief Defines an abstract model based on TreeItem objects.
59  *
60  * Models are used by the item views of the Qt5 that represents some data structure. Its used for manipulate models
61  * using a view. This is part of the Model / View framework of the Qt5.
62  *
63  * \sa TreeItem, te::map::AbstractLayer
64  *
65  * \ingroup widgets
66  */
67  class TEQTWIDGETSEXPORT LayerItemModel: public QAbstractItemModel
68  {
69  Q_OBJECT
70 
71  public:
72  /*!
73  * \name Instantiation Methods
74  *
75  * Methods related to creation and destruction of the object.
76  */
77  //@{
78 
79  /*!
80  * \brief Constructor.
81  *
82  * \param parent Model parent (Used by Qt5).
83  */
84  explicit LayerItemModel(QObject *parent = 0);
85 
86  /*!
87  * \brief Destructor.
88  */
89  ~LayerItemModel();
90  //@}
91 
92  /*!
93  * \name Update Methods
94  *
95  * Methods used for feed the model.
96  */
97  //@{
98 
99  /*!
100  * \brief Adds a layer to the model at the end of children list.
101  *
102  * \param layer The layer to be added.
103  *
104  * \param idxPath Unused.
105  */
106  void addLayer(te::map::AbstractLayerPtr layer, const std::string& idxPath = "./");
107 
108  /*!
109  * \brief Adds the layer to the model.
110  *
111  * \param layer Layer to be added.
112  *
113  * \param parent Parent of the new LayerItem.
114  *
115  * \param idxPath Unused.
116  */
117  void addLayer(te::map::AbstractLayerPtr layer, const QModelIndex& parent, const std::string& idxPath = "./");
118 
119  /*!
120  * \brief Adds a list of layers to the model at the end of children list.
121  *
122  * \param layers The layer list to be added.
123  *
124  * \param idxPath Unused.
125  */
126  void addLayers(const std::list<te::map::AbstractLayerPtr>& layers, const std::string& idxPath = "./");
127 
128  /*!
129  * \brief Adds a list of layers to the model.
130  *
131  * \param layers The layer list to be added.
132  *
133  * \param parent The parent of the new LayerItems.
134  *
135  * \param idxPath Unused.
136  */
137  void addLayers(const std::list<te::map::AbstractLayerPtr>& layers, const QModelIndex& parent, const std::string& idxPath = "./");
138 
139  /*!
140  * \brief Updates the list of layers in the model.
141  *
142  * \param layers The new set of layers to be set to the model.
143  *
144  * \note Older items are removed from the model.
145  */
146  void setLayers(const std::list<te::map::AbstractLayerPtr>& layers);
147 
148  /*!
149  * \brief Adds the set of items to the \a parent at the position \a pos.
150  *
151  * These methods is used when you needs to reparent some items. It is a common operation on the view,
152  * like a drag and drop operation, for example. The \a items are removed from its parents and added
153  * in the \a parent, as children.
154  *
155  * \param items The items to be reparent.
156  *
157  * \param parent The new parent, where the items must be inserted.
158  *
159  * \param pos The position where the items must be inserted.
160  *
161  * \note If \a pos is greater than the size of the list of children, items are, then, added to end of the
162  * list of children.
163  *
164  * \note If you want to add items to the root, try to pass "0" as value for \a parent argument.
165  */
166  void addItems(const std::vector<TreeItem*> items, TreeItem* parent, const int& pos = 0);
167 
168  /*!
169  * \brief Adds a new folder item to the model.
170  *
171  * \param name Label of the folder.
172  *
173  * \param parent Parent item. If this value is "0" the folder is added to the root of the model.
174  */
175  void addFolder(const std::string& name, TreeItem* parent = 0);
176 
177  /*!
178  * \brief Adds a new folder item to the model.
179  *
180  * \param name Label of the folder.
181  *
182  * \param idx The parent of the new FolderItem.
183  */
184  void addFolder(const std::string& name, const QModelIndex& idx);
185  //@}
186 
187  /*!
188  * \name Getter Methods
189  *
190  * Methods used for get items from the model.
191  */
192  //@{
193 
194  /*!
195  * \brief Get all layers contained in the model. Commonly used for persistent pourposes.
196  *
197  * \return A list with all layers containded in the model.
198  */
199  std::list<te::map::AbstractLayerPtr> getAllLayers();
200 
201  /*!
202  * \brief Returns a list with layers that are visible.
203  *
204  * \return List of visible layers.
205  *
206  * \note Folder layers are not contained in the returned list.
207  */
208  std::list<te::map::AbstractLayerPtr> getVisibleLayers();
209  //@}
210 
211  /*!
212  * \name Reimplementation of QAbstractItemModel
213  *
214  * Methods reimplemented of the QAbstractItemModel class.
215  */
216  //@{
217  QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE;
218 
219  Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE;
220 
221  QVariant headerData(int section, Qt::Orientation orientation,
222  int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
223 
224  QModelIndex index(int row, int column,
225  const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
226 
227  QModelIndex parent(const QModelIndex &index) const Q_DECL_OVERRIDE;
228 
229  int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
230 
231  int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
232 
233  bool setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole) Q_DECL_OVERRIDE;
234 
235  bool removeRows(int row, int count, const QModelIndex & parent = QModelIndex()) Q_DECL_OVERRIDE;
236 
237  bool insertRows(int row, int count, const QModelIndex& parent = QModelIndex()) Q_DECL_OVERRIDE;
238 
239  Qt::DropActions supportedDropActions() const Q_DECL_OVERRIDE;
240 
241  QStringList mimeTypes() const Q_DECL_OVERRIDE;
242 
243  QMimeData* mimeData(const QModelIndexList &indexes) const Q_DECL_OVERRIDE;
244 
245  bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent);
246 
247  bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) Q_DECL_OVERRIDE;
248  //@}
249 
250  /*!
251  * \brief Removes the items in \a lst from the model.
252  *
253  * \param lst List of indexes of the items to be removed.
254  */
255  void removeItems(const QModelIndexList& lst);
256 
257  Q_SIGNALS:
258 
259  /*!
260  * \name Qt signals
261  *
262  * Signals emitted by LayerItemModel
263  */
264  //@{
265 
266  /*!
267  * \brief Signal emited when an item visibility has changed.
268  */
269  void visibilityChanged();
270  //@}
271 
272  protected:
273 
274  std::auto_ptr<TreeItem> m_root; //!< The root item of the model.
275  };
276  }
277  }
278 }
279 
280 #endif // __TERRALIB_QT_WIDGETS_LAYER_INTERNAL_LAYERITEMMODEL_H
#define Q_DECL_OVERRIDE
Defines a hierarchical structure.
Definition: TreeItem.h:72
URI C++ Library.
Defines an abstract model based on TreeItem objects.
std::auto_ptr< TreeItem > m_root
The root item of the model.
#define TEQTWIDGETSEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:63
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr