27 #include "../../common/STLUtils.h"
28 #include "../../common/Translator.h"
29 #include "../Exception.h"
34 : QAbstractItemModel(parent),
35 m_singleSelection(singleSelection)
37 for(std::list<te::map::AbstractLayerPtr>::const_iterator it = layers.begin(); it != layers.end(); ++it)
54 return !m_items.empty();
76 return item->
data(index.column(), role);
87 throw Exception(
TE_TR(
"Invalid data associated to the layer model!"));
95 return QAbstractItemModel::flags(index) | Qt::ItemIsDropEnabled;
100 throw Exception(
TE_TR(
"Invalid data associated to the layer model!"));
102 return QAbstractItemModel::flags(index) | item->
flags();
107 if(!parent.isValid())
108 return !m_items.empty();
113 throw Exception(
TE_TR(
"Invalid data associated to the layer model!"));
121 return QModelIndex();
123 if(!parent.isValid() &&
static_cast<std::size_t
>(row) >= m_items.size())
124 return QModelIndex();
128 if(parentItem && row >= parentItem->children().count())
129 return QModelIndex();
131 if(!parent.isValid())
136 return createIndex(row, column, item);
140 throw Exception(
TE_TR(
"Invalid data associated to the layer model!"));
142 if(parentItem->children().empty())
143 throw Exception(
TE_TR(
"The layer item in the model must have a child item!"));
145 if(row >= parentItem->children().size())
146 throw Exception(
TE_TR(
"The row for the layer item is out of range!"));
151 throw Exception(
TE_TR(
"The layer item is not an te::qt::widgets::AbstractTreeItem!"));
153 return createIndex(row, column, item);
159 return QModelIndex();
163 if(item == 0 || item->parent() == 0)
164 return QModelIndex();
169 throw Exception(
TE_TR(
"The layer item is not an AbstractTreeItem!"));
173 if(grandParentItem == 0)
176 for(std::size_t i = 0; i != m_items.size(); ++i)
178 if(m_items[i] == parentItem)
179 return createIndex(static_cast<int>(i), index.column(), parentItem);
185 const QObjectList& items = grandParentItem->children();
189 for(QObjectList::const_iterator it = items.begin(); it != items.end(); ++it, ++i)
191 if((*it) == parentItem)
192 return createIndex(i, index.column(), parentItem);
196 throw Exception(
TE_TR(
"Could not determine the layer index in the model!"));
201 if(!parent.isValid())
202 return static_cast<int>(m_items.size());
207 throw Exception(
TE_TR(
"Error: NULL layer item!"));
209 return item->children().count();
222 bool retval = item->
setData(index.column(), value, role);
224 emit dataChanged(index, index);
226 if(role == Qt::CheckStateRole)
228 if(parent(index).isValid())
231 QModelIndex ascendentIndex = parent(index);
233 while(ascendentIndex.isValid())
235 emit dataChanged(ascendentIndex, ascendentIndex);
236 ascendentIndex = parent(ascendentIndex);
240 if(m_singleSelection)
246 for(
size_t i = 0; i < m_items.size(); ++i)
248 if(m_items[i] == item)
259 QModelIndex start_ix = createIndex( 0, 0 );
260 QModelIndex end_ix = createIndex( 0, 1 );
261 emit( dataChanged( start_ix, end_ix ) );
268 if(role == Qt::DisplayRole)
270 if(orientation == Qt::Horizontal)
273 return QVariant(
TE_TR(
"Layers"));
274 else if(section == 1)
275 return QVariant(
TE_TR(
"Only Selected"));
279 return QAbstractItemModel::headerData(section, orientation, role);
284 std::map<te::map::AbstractLayerPtr, std::vector<te::dt::Property*> > selected;
286 for(
size_t i = 0; i < m_items.size(); ++i)
306 for(std::list<te::map::AbstractLayerPtr>::const_iterator it = layers.begin(); it != layers.end(); ++it)
311 m_items.push_back(litem);
315 QModelIndex start_ix = createIndex( 0, 0 );
316 QModelIndex end_ix = createIndex( 0, 1 );
317 emit( dataChanged( start_ix, end_ix ) );
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
It returns the index of the item in the model specified by the given row, column and parent index...
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Returns the data for the given role and section in the header with the specified orientation.
int columnCount(const QModelIndex &parent=QModelIndex()) const
It returns the number of columns for the children of the given parent.
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
It returns the data stored under the given role for the item referred to by the index.
bool canFetchMore(const QModelIndex &parent) const
It fetches more data for parent.
void isSelected(bool selected)
Set the Layer as selected.
int rowCount(const QModelIndex &parent=QModelIndex()) const
It returns the number of rows of the given parent.
#define TE_TR(message)
It marks a string in order to get translated.
LayerTreeModel(const std::list< te::map::AbstractLayerPtr > &layers, bool singleSelection=false, QObject *parent=0)
It constructs an item model with the given parent.
std::vector< te::qt::widgets::AbstractTreeItem * > m_items
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
It sets the role data for the item at index to value.
std::vector< te::dt::Property * > getSelected()
Get the selected Properties of the Layer.
The class that defines the model used in the Qt Model/View architecture.
bool hasChildren(const QModelIndex &parent=QModelIndex()) const
It checks if the given index has children.
Qt::ItemFlags flags(const QModelIndex &index) const
It returns the item flags for the given index.
te::map::AbstractLayerPtr getLayer() const
std::map< te::map::AbstractLayerPtr, std::vector< te::dt::Property * > > getSelected()
It get the Layer selected and the Properties selected of the Layer.
A class that represents a Layer in a LayerTreeModel.
void fetchMore(const QModelIndex &parent)
It fetches any available data for the items with the parent specified by the parent index...
QModelIndex parent(const QModelIndex &index) const
It returns the item parent of the given index, or QModelIndex(), if it has no parent.
void FreeContents(boost::unordered_map< K, V * > &m)
This function can be applied to a map of pointers. It will delete each pointer in the map...
void setLayerList(const std::list< te::map::AbstractLayerPtr > &layers)