Back to index

salome-gui  6.5.0
TreeModel.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 // Author: Guillaume Boulant (EDF/R&D)
00021 
00022 
00023 #include <QtGui>
00024 
00025 #include "TreeItem.hxx"
00026 #include "TreeModel.hxx"
00027 
00028 TreeModel::TreeModel(const QStringList &headers, QObject *parent)
00029   : QAbstractItemModel(parent)
00030 {
00031   QVector<QVariant> rootData;
00032   foreach (QString header, headers)
00033     rootData << header;
00034 
00035   // _MEM_ We have to specify a string identifier for each item so
00036   // that it could be easily retrieved by its parent. In the case of
00037   // the root item, the value of the identifier doesn't matter => we
00038   // choose an arbitrary value
00039   //QString rootNameId = "rootItem";
00040   _rootItem = new TreeItem("rootItem", rootData);
00041   _rootItem->associateToModel(this);
00042 }
00043 
00044 TreeModel::~TreeModel()
00045 {
00046   delete _rootItem;
00047 }
00048 
00049 TreeItem * TreeModel::getRootItem() {
00050   return _rootItem;
00051 }
00052 
00053 //
00054 // =================================================================
00055 // This part of the implementation is the standard interface required
00056 // for providing an operational TreeModel. These methods are used by
00057 // the TreeView for display purpose. We just have to implement how we
00058 // want the items to be displayed in the view.
00059 // =================================================================
00060 //
00061 int TreeModel::columnCount(const QModelIndex & /* parent */) const
00062 {
00063   return _rootItem->columnCount();
00064 }
00065 
00071 QVariant TreeModel::data(const QModelIndex &index, int role) const
00072 {
00073   if (!index.isValid())
00074     return QVariant();
00075 
00076   if (role != Qt::DisplayRole && role != Qt::EditRole)
00077     return QVariant();
00078 
00079   TreeItem *item = getItem(index);
00080 
00081   return item->data(index.column());
00082 }
00083 
00084 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
00085 {
00086   if (!index.isValid())
00087     return 0;
00088 
00089   return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
00090 }
00091 
00095 TreeItem *TreeModel::getItem(const QModelIndex &index) const
00096 {
00097   // The item associated to an index can be retrieved using the
00098   // internalPointer() function on the index object.
00099   if (index.isValid()) {
00100     TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
00101     if (item) return item;
00102   }
00103   return _rootItem;
00104 }
00105 
00106 QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
00107                                int role) const
00108 {
00109   if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
00110     return _rootItem->data(section);
00111 
00112   return QVariant();
00113 }
00114 
00119 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const
00120 {
00121   if (parent.isValid() && parent.column() != 0)
00122     return QModelIndex();
00123 
00124   TreeItem *parentItem = getItem(parent);
00125 
00126   TreeItem *childItem = parentItem->child(row);
00127   if (childItem)
00128     return createIndex(row, column, childItem);
00129   else
00130     return QModelIndex();
00131 }
00132 
00133 QModelIndex TreeModel::parent(const QModelIndex &index) const
00134 {
00135   if (!index.isValid())
00136     return QModelIndex();
00137 
00138   TreeItem *childItem = getItem(index);
00139   TreeItem *parentItem = childItem->parent();
00140 
00141   if (parentItem == _rootItem)
00142     return QModelIndex();
00143 
00144   return createIndex(parentItem->rowIndex(), 0, parentItem);
00145 }
00146 
00147 int TreeModel::rowCount(const QModelIndex &parent) const
00148 {
00149   TreeItem *parentItem = getItem(parent);
00150 
00151   return parentItem->childCount();
00152 }
00153 
00154 bool TreeModel::setData(const QModelIndex &index, const QVariant &value,
00155                         int role)
00156 {
00157   if (role != Qt::EditRole)
00158     return false;
00159 
00160   TreeItem *item = getItem(index);
00161   bool result = item->setData(index.column(), value);
00162 
00163   if (result)
00164     emit dataChanged(index, index);
00165 
00166   return result;
00167 }
00168 
00169 bool TreeModel::setHeaderData(int section, Qt::Orientation orientation,
00170                               const QVariant &value, int role)
00171 {
00172   if (role != Qt::EditRole || orientation != Qt::Horizontal)
00173     return false;
00174 
00175   bool result = _rootItem->setData(section, value);
00176 
00177   if (result)
00178     emit headerDataChanged(orientation, section, section);
00179 
00180   return result;
00181 }
00182 
00183 
00184 //
00185 // =================================================================
00186 // This part is a specific behavior to get a TreeModel that can
00187 // organize itself the tree hierarchy using data provided in a
00188 // filesystem-like format:
00189 // 
00190 // data="a/b/c" ==> creation/filling of the hierarchy a->b->c
00191 // The "folder" categories are unique whereas the leaves may exists
00192 // in multiple instances.
00193 // =================================================================
00194 //
00195 
00196 /*
00197  * The addData functions run a recurcive filling of the tree model, starting
00198  * form the rootItem and descending in the tree using the recurcive
00199  * function TreeItem::addData.
00200  */
00201 
00202 bool TreeModel::addData(DataObject * dataObject) {
00203   QStringList path = QString(dataObject->getPath().c_str()).split(DataObject::pathsep.c_str());
00204   return addData(dataObject, path);
00205 }
00206 bool TreeModel::addData(DataObject * dataObject, const QStringList &path) {
00207   TreeItem * rootItem = this->getItem();
00208   rootItem->appendChild(dataObject, path);
00209   return true;
00210 }