1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // Author: Guillaume Boulant (EDF/R&D)
23 #include "TreeItem.hxx"
24 #include "TreeModel.hxx"
26 TreeModel::TreeModel(const QStringList &headers, QObject *parent)
27 : QAbstractItemModel(parent)
29 QVector<QVariant> rootData;
30 foreach (QString header, headers)
33 // _MEM_ We have to specify a string identifier for each item so
34 // that it could be easily retrieved by its parent. In the case of
35 // the root item, the value of the identifier doesn't matter => we
36 // choose an arbitrary value
37 //QString rootNameId = "rootItem";
38 _rootItem = new TreeItem("rootItem", rootData);
39 _rootItem->associateToModel(this);
42 TreeModel::~TreeModel()
47 TreeItem * TreeModel::getRootItem() {
52 // =================================================================
53 // This part of the implementation is the standard interface required
54 // for providing an operational TreeModel. These methods are used by
55 // the TreeView for display purpose. We just have to implement how we
56 // want the items to be displayed in the view.
57 // =================================================================
59 int TreeModel::columnCount(const QModelIndex & /* parent */) const
61 return _rootItem->columnCount();
65 * This function is used by the tree model to inform the tree view of
66 * what data used for rendering of the item in the different possible
67 * role (edition, ...).
69 QVariant TreeModel::data(const QModelIndex &index, int role) const
74 if (role != Qt::DisplayRole && role != Qt::EditRole)
77 TreeItem *item = getItem(index);
79 return item->data(index.column());
82 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
87 return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
91 * This retrieves the item asociated to the specified index.
93 TreeItem *TreeModel::getItem(const QModelIndex &index) const
95 // The item associated to an index can be retrieved using the
96 // internalPointer() function on the index object.
97 if (index.isValid()) {
98 TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
99 if (item) return item;
104 QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
107 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
108 return _rootItem->data(section);
114 * This retrieves the index of the item located at (row,column) place
115 * relative to the parent specified by its index.
117 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const
119 if (parent.isValid() && parent.column() != 0)
120 return QModelIndex();
122 TreeItem *parentItem = getItem(parent);
124 TreeItem *childItem = parentItem->child(row);
126 return createIndex(row, column, childItem);
128 return QModelIndex();
131 QModelIndex TreeModel::parent(const QModelIndex &index) const
133 if (!index.isValid())
134 return QModelIndex();
136 TreeItem *childItem = getItem(index);
137 TreeItem *parentItem = childItem->parent();
139 if (parentItem == _rootItem)
140 return QModelIndex();
142 return createIndex(parentItem->rowIndex(), 0, parentItem);
145 int TreeModel::rowCount(const QModelIndex &parent) const
147 TreeItem *parentItem = getItem(parent);
149 return parentItem->childCount();
152 bool TreeModel::setData(const QModelIndex &index, const QVariant &value,
155 if (role != Qt::EditRole)
158 TreeItem *item = getItem(index);
159 bool result = item->setData(index.column(), value);
162 emit dataChanged(index, index);
167 bool TreeModel::setHeaderData(int section, Qt::Orientation orientation,
168 const QVariant &value, int role)
170 if (role != Qt::EditRole || orientation != Qt::Horizontal)
173 bool result = _rootItem->setData(section, value);
176 emit headerDataChanged(orientation, section, section);
183 // =================================================================
184 // This part is a specific behavior to get a TreeModel that can
185 // organize itself the tree hierarchy using data provided in a
186 // filesystem-like format:
188 // data="a/b/c" ==> creation/filling of the hierarchy a->b->c
189 // The "folder" categories are unique whereas the leaves may exists
190 // in multiple instances.
191 // =================================================================
195 * The addData functions run a recurcive filling of the tree model, starting
196 * form the rootItem and descending in the tree using the recurcive
197 * function TreeItem::addData.
200 bool TreeModel::addData(DataObject * dataObject) {
201 QStringList path = QString(dataObject->getPath().c_str()).split(DataObject::pathsep.c_str());
202 return addData(dataObject, path);
204 bool TreeModel::addData(DataObject * dataObject, const QStringList &path) {
205 TreeItem * rootItem = this->getItem();
206 rootItem->appendChild(dataObject, path);
210 bool TreeModel::removeData(DataObject * dataObject) {
211 QStringList path = QString(dataObject->getPath().c_str()).split(DataObject::pathsep.c_str());
212 TreeItem * rootItem = this->getItem();
213 rootItem->removeChild(dataObject, path);