All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
LayerTreeModel.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 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 #if (QT_VERSION >= 0x050000)
205  Qt::DropActions supportedDragActions() const;
206 #endif
207 
208  /*!
209  \brief It returns an object that contains serialized items of data corresponding to the list of indexes specified.
210  The formats used to describe the encoded data is obtained from the mimeTypes() function.
211 
212  \param indexes The list of indexes that was dragged.
213 
214  \return An object containing serialized items of data corresponding to the list of indexes specified.
215  If the list of indexes is empty, or there are no supported MIME types,
216  0 is returned rather than a serialized empty list.
217  */
218  QMimeData* mimeData(const QModelIndexList& indexes) const;
219 
220  /*!
221  \brief It handles the data supplied by a drag and drop operation that ended with the given action.
222 
223  \param data The object containing serialized items of data.
224  \param action The action associated by the drag and drop operation.
225  \param row The item row in the model where the operation ended.
226  \param column The item column in the model where the operation ended.
227  \param parent The item parent in the model where the operation ended.
228 
229  \return It returns true if the data and action can be handled by the model; otherwise, it returns false.
230  */
231  bool dropMimeData(const QMimeData* data, Qt::DropAction action,
232  int row, int column, const QModelIndex& parent);
233 
234  /*!
235  \brief It inserts a determined number of rows given by the "count" parameter,
236  starting with the given row under the parent "parent" from the model.
237 
238  \param row The initial row.
239  \param count The number of rows to be inserted.
240  \param parent The parent of the rows to be inserted.
241 
242  \return True, if the rows were successfully inserted; otherwise, it returns false.
243  */
244  bool insertRows(int row, int count, const QModelIndex& parent = QModelIndex());
245 
246  /*!
247  \brief It removes a determined number of rows given by the "count" parameter,
248  starting with the given row under parent "parent" from the model.
249 
250  \param row The initial row.
251  \param count The number of rows to be removed.
252  \param parent The parent of the rows to be removed.
253 
254  \return It returns true if the rows were successfully removed; otherwise, it returns false.
255  */
256  bool removeRows(int row, int count, const QModelIndex& parent = QModelIndex());
257 
258  /*!
259  \brief It sets the model items as checkable or not.
260 
261  \param checkable Flag that indicates if the model items are checkable.
262  */
263  void setCheckable(const bool checkable);
264 
265  /*!
266  \brief It verifies if the model items are checkable or not.
267 
268  \return It returns true if the model items are checkable.
269  */
270  bool isCheckable() const;
271 
272  /*!
273  \brief It gets the index that is associated to the given item.
274 
275  \param item The item whose index will be returned.
276 
277  \return The index that is associated to the item.
278  */
279  QModelIndex getIndex(AbstractTreeItem* item);
280 
281  /*!
282  \brief It gets the layer item that is associated to the given layer.
283 
284  \param layer The layer whose the corresponding layer item will be returned.
285 
286  \return The layer item associated to the layer.
287  */
289 
290  /*!
291  \brief It adds a item layer to the list of item layers of the parent layer item.
292  If the parent layer is not specified, the item layer is added as a top layer item.
293 
294  \param layer The layer that will be associated to the item layer to be added.
295  \param parentItem The parent item where the layer item will be added.
296  */
297  void add(const te::map::AbstractLayerPtr& layer, AbstractTreeItem* parentItem = 0);
298 
299  /*!
300  \brief It removes a item from the model.
301 
302  \param item The item to be removed from the model.
303 
304  \return True, if the item was successfully removed; otherwise, it returns false.
305  */
306  bool remove(AbstractTreeItem* item);
307 
308  signals:
309 
310  void visibilityChanged(const te::map::AbstractLayerPtr& layer);
311  //void expandItem(te::qt::widgets::AbstractTreeItem* item);
312  void layerOrderChanged();
313 
314  protected:
315 
316  /*!
317  \brief It removes the given layers from the list of children of their parents.
318 
319  \param layers The layers that will be removed from the list of children of their parents.
320  */
321  void removeLayerFromParentChildrenList(std::vector<te::map::AbstractLayerPtr>& layers);
322 
323  /*!
324  \brief It emits the dataChanged signal for the descendants indexes of the given index.
325 
326  \param parent The item index whose descendants will have the dataChanged signal emitted.
327  */
328  void emitDataChangedForDescendants(const QModelIndex& parent);
329 
330  /*!
331  \brief It emits the dataChanged signal for the indexes that are ancestors of the given index.
332 
333  \param index The item index whose ancestors will have the dataChanged signal emitted.
334  */
335  void emitDataChangedForAncestors(const QModelIndex& index);
336 
337  private:
338 
339  std::vector<AbstractTreeItem*> m_items; //!< The list of top level items.
340  std::vector<te::map::AbstractLayerPtr> m_layers; //!< The list of top level layers.
341  std::vector<te::map::AbstractLayerPtr> m_insertingLayers; //!< The list of layers to be inserted.
342  bool m_checkable; //!< It informs if the model is checkable.
343  };
344 
345  } // end namespace widgets
346  } // end namespace qt
347 } // end namespace te
348 
349 #endif // __TERRALIB_QT_WIDGETS_LAYER_EXLORER_INTERNAL_LAYERTREEMODEL_H
std::vector< te::map::AbstractLayerPtr > m_layers
The list of top level layers.
std::vector< te::map::AbstractLayerPtr > m_insertingLayers
The list of layers to be inserted.
The class that represents an item in a LayerTreeModel.
bool m_checkable
It informs if the model is checkable.
#define TEQTWIDGETSEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:66
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
std::vector< AbstractTreeItem * > m_items
The list of top level items.