All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LayerTreeModel.h
Go to the documentation of this file.
1 /* Copyright (C) 2008-2013 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 terralib/qt/widgets/layer/explorer/LayerTreeModel.h
22 
23  \brief This class defines the model used in the Qt Model/View architecture for the tree of layers.
24 */
25 
26 #ifndef __TERRALIB_QT_WIDGETS_LAYER_EXLORER_INTERNAL_LAYERTREEMODEL_H
27 #define __TERRALIB_QT_WIDGETS_LAYER_EXLORER_INTERNAL_LAYERTREEMODEL_H
28 
29 // TerraLib
30 #include "../../../../maptools/AbstractLayer.h"
31 #include "../../Config.h"
32 
33 // STL
34 #include <vector>
35 
36 //Qt
37 #include <QtCore/QAbstractItemModel>
38 
39 namespace te
40 {
41  namespace qt
42  {
43  namespace widgets
44  {
45  class AbstractTreeItem;
46 
47  class TEQTWIDGETSEXPORT LayerTreeModel : public QAbstractItemModel
48  {
49  Q_OBJECT
50 
51  public:
52 
53  /*!
54  \brief It constructs a layer tree model with the given parent.
55 
56  \param parent The parent object of the layer tree model.
57  */
58  LayerTreeModel(QObject* parent = 0);
59 
60  /*!
61  \brief It constructs a layer tree model with the given parent.
62 
63  \param layers The layers that will be associated to the layer items of the layer tree model.
64  \param parent The parent object of the layer tree model.
65  */
66  LayerTreeModel(const std::list<te::map::AbstractLayerPtr>& layers, QObject* parent = 0);
67 
68  /*! Destructor */
69  ~LayerTreeModel();
70 
71  /*!
72  \brief It resets the model and associates the new top level items to the given top level layers.
73 
74  \param layers The list of top level layers that will be associated to the new top level items.
75  */
76  void set(const std::list<te::map::AbstractLayerPtr>& layers);
77 
78  /*!
79  \brief It gets the top level layers of the model.
80 
81  \param layers The top level layers of the model.
82  */
83  const std::vector<te::map::AbstractLayerPtr>& getTopLayers() const;
84 
85  /*!
86  \brief It gets the top level layer items of the model.
87 
88  \param layers The top level layer items of the model.
89  */
90  const std::vector<te::qt::widgets::AbstractTreeItem*>& getTopLayerItems() const;
91 
92  /*!
93  \brief It fetches more data for the given parent.
94 
95  \param parent The object used to fetch more data.
96 
97  \return It returns true if there is more data available for parent.
98  */
99  bool canFetchMore(const QModelIndex& parent) const;
100 
101  /*!
102  \brief It fetches any available data for the items with the parent specified by the parent index.
103 
104  \param parent The object used to fetch available data.
105  */
106  void fetchMore(const QModelIndex& parent);
107 
108  /*!
109  \brief It returns the number of columns for the children of the given parent.
110 
111  \param parent The parent index.
112 
113  \return The number of columns for the children of the given parent.
114  */
115  int columnCount(const QModelIndex& parent = QModelIndex()) const;
116 
117  /*!
118  \brief It returns the number of rows of the given parent.
119 
120  \param parent The parent index.
121 
122  \return The number of rows of the given parent.
123  */
124  int rowCount(const QModelIndex & parent = QModelIndex()) const;
125 
126  /*!
127  \brief It returns the index of the item in the model specified by the given row, column and parent index.
128 
129  It will call createIndex() to generate model indexes that other components
130  can use to refer to items in the model.
131 
132  \param row The item row.
133  \param column The item column.
134  \param parent The parent index of the item.
135 
136  \return The item index.
137  */
138  QModelIndex index(int row, int column, const QModelIndex & parent = QModelIndex()) const;
139 
140  /*!
141  \brief It returns the item parent of the given index, or QModelIndex(), if it has no parent.
142 
143  \param index The item index.
144 
145  \return The parent index of the item with the given index, or QModelIndex(), if it has no parent.
146  */
147  QModelIndex parent(const QModelIndex & index) const;
148 
149  /*!
150  \brief It returns the item flags for the given index.
151 
152  \param index The item index.
153 
154  \return The item flags for the given index.
155  */
156  Qt::ItemFlags flags(const QModelIndex& index) const;
157 
158  /*!
159  \brief It returns the data stored under the given role for the item referred to by the index.
160 
161  \param index The item index.
162  \param role The role that is used by the view to indicate to the model which type of data it needs.
163 
164  \return The data stored under the given role for the item referred to by the index.
165  */
166  QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
167 
168  /*!
169  \brief It sets the role data for the item at index to value.
170 
171  \param index The item index.
172  \param value The value to be assigned to the item.
173  \param role The role used.
174 
175  \return It returns true if successful; otherwise, it returns false.
176 
177  \note The dataChanged() signal is emitted if the data was successfully set.
178  */
179  bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole);
180 
181  /*!
182  \brief It checks if the given index has children.
183 
184  \param parent The item index.
185 
186  \return It returns true if the given index has children.
187  */
188  bool hasChildren(const QModelIndex& parent = QModelIndex()) const;
189 
190  /*!
191  \brief It returns a list of MIME types that can be used to describe a list of model indexes.
192 
193  \return The list of MIME types that can be used to describe a list of model indexes.
194  */
195  QStringList mimeTypes() const;
196 
197  /*!
198  \brief It returns the drop actions supported by this model.
199 
200  \return The drop actions supported by this model.
201  */
202  Qt::DropActions supportedDropActions() const;
203 
204  /*!
205  \brief It returns an object that contains serialized items of data corresponding to the list of indexes specified.
206  The formats used to describe the encoded data is obtained from the mimeTypes() function.
207 
208  \param indexes The list of indexes that was dragged.
209 
210  \return An object containing serialized items of data corresponding to the list of indexes specified.
211  If the list of indexes is empty, or there are no supported MIME types,
212  0 is returned rather than a serialized empty list.
213  */
214  QMimeData* mimeData(const QModelIndexList& indexes) const;
215 
216  /*!
217  \brief It handles the data supplied by a drag and drop operation that ended with the given action.
218 
219  \param data The object containing serialized items of data.
220  \param action The action associated by the drag and drop operation.
221  \param row The item row in the model where the operation ended.
222  \param column The item column in the model where the operation ended.
223  \param parent The item parent in the model where the operation ended.
224 
225  \return It returns true if the data and action can be handled by the model; otherwise, it returns false.
226  */
227  bool dropMimeData(const QMimeData* data, Qt::DropAction action,
228  int row, int column, const QModelIndex& parent);
229 
230  /*!
231  \brief It inserts a determined number of rows given by the "count" parameter,
232  starting with the given row under the parent "parent" from the model.
233 
234  \param row The initial row.
235  \param count The number of rows to be inserted.
236  \param parent The parent of the rows to be inserted.
237 
238  \return True, if the rows were successfully inserted; otherwise, it returns false.
239  */
240  bool insertRows(int row, int count, const QModelIndex& parent = QModelIndex());
241 
242  /*!
243  \brief It removes a determined number of rows given by the "count" parameter,
244  starting with the given row under parent "parent" from the model.
245 
246  \param row The initial row.
247  \param count The number of rows to be removed.
248  \param parent The parent of the rows to be removed.
249 
250  \return It returns true if the rows were successfully removed; otherwise, it returns false.
251  */
252  bool removeRows(int row, int count, const QModelIndex& parent = QModelIndex());
253 
254  /*!
255  \brief It sets the model items as checkable or not.
256 
257  \param checkable Flag that indicates if the model items are checkable.
258  */
259  void setCheckable(const bool checkable);
260 
261  /*!
262  \brief It verifies if the model items are checkable or not.
263 
264  \return It returns true if the model items are checkable.
265  */
266  bool isCheckable() const;
267 
268  /*!
269  \brief It gets the index that is associated to the given item.
270 
271  \param item The item whose index will be returned.
272 
273  \return The index that is associated to the item.
274  */
275  QModelIndex getIndex(AbstractTreeItem* item);
276 
277  /*!
278  \brief It gets the layer item that is associated to the given layer.
279 
280  \param layer The layer whose the corresponding layer item will be returned.
281 
282  \return The layer item associated to the layer.
283  */
285 
286  /*!
287  \brief It adds a item layer to the list of item layers of the parent layer item.
288  If the parent layer is not specified, the item layer is added as a top layer item.
289 
290  \param layer The layer that will be associated to the item layer to be added.
291  \param parentItem The parent item where the layer item will be added.
292  */
293  void add(const te::map::AbstractLayerPtr& layer, AbstractTreeItem* parentItem = 0);
294 
295  /*!
296  \brief It removes a item from the model.
297 
298  \param item The item to be removed from the model.
299 
300  \return True, if the item was successfully removed; otherwise, it returns false.
301  */
302  bool remove(AbstractTreeItem* item);
303 
304  signals:
305 
306  void visibilityChanged(const te::map::AbstractLayerPtr& layer);
307  //void expandItem(te::qt::widgets::AbstractTreeItem* item);
308  void layerOrderChanged();
309 
310  protected:
311 
312  /*!
313  \brief It removes the given layers from the list of children of their parents.
314 
315  \param layers The layers that will be removed from the list of children of their parents.
316  */
317  void removeLayerFromParentChildrenList(std::vector<te::map::AbstractLayerPtr>& layers);
318 
319  /*!
320  \brief It emits the dataChanged signal for the descendants indexes of the given index.
321 
322  \param parent The item index whose descendants will have the dataChanged signal emitted.
323  */
324  void emitDataChangedForDescendants(const QModelIndex& parent);
325 
326  /*!
327  \brief It emits the dataChanged signal for the indexes that are ancestors of the given index.
328 
329  \param index The item index whose ancestors will have the dataChanged signal emitted.
330  */
331  void emitDataChangedForAncestors(const QModelIndex& index);
332 
333  private:
334 
335  std::vector<AbstractTreeItem*> m_items; //!< The list of top level items.
336  std::vector<te::map::AbstractLayerPtr> m_layers; //!< The list of top level layers.
337  std::vector<te::map::AbstractLayerPtr> m_insertingLayers; //!< The list of layers to be inserted.
338  bool m_checkable; //!< It informs if the model is checkable.
339  };
340 
341  } // end namespace widgets
342  } // end namespace qt
343 } // end namespace te
344 
345 #endif // __TERRALIB_QT_WIDGETS_LAYER_EXLORER_INTERNAL_LAYERTREEMODEL_H
std::vector< te::map::AbstractLayerPtr > m_insertingLayers
The list of layers to be inserted.
#define TEQTWIDGETSEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:101
std::vector< te::map::AbstractLayerPtr > m_layers
The list of top level layers.
The class that represents an item in a LayerTreeModel.
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
bool m_checkable
It informs if the model is checkable.
std::vector< AbstractTreeItem * > m_items
The list of top level items.