Back to index

salome-gui  6.5.0
TreeItem.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 <QStringList>
00024 #include "TreeItem.hxx"
00025 
00026 TreeItem::TreeItem(const QString &nameId,
00027                    const QVector<QVariant> &columnValues,
00028                    TreeItem *parent)
00029 {
00030   this->initialize(nameId, columnValues, parent);
00031 }
00032 
00036 void TreeItem::initialize(const QString &nameId,
00037                           const QVector<QVariant> &columnValues,
00038                           TreeItem *parent) {
00039   _itemNameId = nameId;
00040   _itemData   = columnValues;
00041   _parentItem = parent;
00042 
00043   // An item is associated to the model of its parent. It can't be
00044   //done automatically in the case where the item has no parent. Then
00045   //the function associatedToModel has to be called explicitely from
00046   //within the user code (for exemple at construction of the model,
00047   //when creating the root item).
00048   if ( parent != NULL ) {
00049     this->associateToModel(parent->associatedModel());
00050   }  
00051 }
00052 
00053 TreeItem::~TreeItem()
00054 {
00055   qDeleteAll(_childItems);
00056   qDeleteAll(_childItemsMapById);
00057   qDeleteAll(_childItemsMapByLabel);
00058 }
00059 
00068 void TreeItem::associateToModel(TreeModel * model) {
00069   _associatedModel = model;
00070 }
00071 
00072 TreeModel * TreeItem::associatedModel() {
00073   return _associatedModel;
00074 }
00075 
00079 QString TreeItem::nameId() {
00080   return _itemNameId;
00081 }
00082 
00092 void TreeItem::appendChild(DataObject * dataObject,
00093                            const QStringList &relativePath) {
00094   // Definition of the nameId
00095   QString nameId = QString(dataObject->getNameId().c_str());
00096 
00097   // Definition of columns data values
00098   QVector<QVariant> columnValues;
00099   columnValues << QString(dataObject->getLabel().c_str());
00100   columnValues << "No value"; // We could use the dataObject properties
00101 
00102   // Append the item at the specified location with the specified values:
00103   this->appendChild(nameId, columnValues, relativePath);
00104 }
00105 
00106 void TreeItem::appendChild(const QString &nameId,
00107                            const QVector<QVariant> &columnValues,
00108                            const QStringList &relativePath) {
00109 
00110   if ( relativePath.isEmpty() ) {
00111     // It is a direct child => just create and append to this.
00112     TreeItem * child = new TreeItem(nameId, columnValues, this);
00113     this->appendChild(child);
00114     return;
00115   }
00116 
00117   // The child is embedded in a sub-folder (to be created if doesn't
00118   // exist).
00119   // We first check if the sub-folder already exist:
00120   TreeItem * folder = this->childByLabel(relativePath[0]);
00121   if ( folder == NULL ) {
00122     // The folder does not exist. It must be created before going any
00123     // further. By convention we choose the folder name as
00124     // identifier.
00125     QString folderNameId = relativePath[0];
00126     QVector<QVariant> folderColumnValues;
00127     folderColumnValues << relativePath[0] << "No value";
00128     folder = new TreeItem(folderNameId, folderColumnValues, this);
00129     this->appendChild(folder);
00130   }
00131     
00132   // We create the relative path of the next iteration (delete the
00133   // first folder path).
00134   QStringList nextRelativePath;
00135   for (int i = 1; i < relativePath.size(); ++i)
00136     nextRelativePath << relativePath[i];
00137     
00138   folder->appendChild(nameId, columnValues, nextRelativePath);
00139 }
00140 
00145 void TreeItem::appendChild(TreeItem *item)
00146 {
00147   TreeModel * model = this->associatedModel();
00148 
00149   int position = this->childCount();
00150   model->beginInsertRows(this->modelIndex(), position, position);
00151   _childItems.append(item);
00152   _childItemsMapById[item->nameId()] = item;
00153   _childItemsMapByLabel[item->data(0).toString()] = item;
00154   model->endInsertRows();
00155 }
00156 
00161 TreeItem *TreeItem::child(int row)
00162 {
00163   return _childItems.value(row);
00164 }
00165 
00166 TreeItem *TreeItem::childById(const QString &nameId)
00167 {
00168   QMap <QString, TreeItem*>::iterator it;
00169   it = _childItemsMapById.find ( nameId );
00170 
00171   if ( it != _childItemsMapById.end() ) {
00172     return it.value();
00173   }
00174   return NULL;
00175 }
00176 
00177 TreeItem *TreeItem::childByLabel(const QString &label)
00178 {
00179   QMap <QString, TreeItem*>::iterator it;
00180   it = _childItemsMapByLabel.find ( label );
00181 
00182   if ( it != _childItemsMapByLabel.end() ) {
00183     return it.value();
00184   }
00185   return NULL;
00186 }
00187 
00188 
00189 
00190 int TreeItem::childCount() const
00191 {
00192   return _childItems.count();
00193 }
00194 
00199 int TreeItem::rowIndex() const
00200 {
00201   if (_parentItem)
00202     return _parentItem->_childItems.indexOf(const_cast<TreeItem*>(this));
00203 
00204   return 0;
00205 }
00206 
00207 int TreeItem::columnCount() const
00208 {
00209   return _itemData.count();
00210 }
00211 
00212 QVariant TreeItem::data(int column) const
00213 {
00214   return _itemData.value(column);
00215 }
00216 
00217 TreeItem *TreeItem::parent()
00218 {
00219   return _parentItem;
00220 }
00221 
00222 bool TreeItem::setData(int column, const QVariant &value)
00223 {
00224   if (column < 0 || column >= _itemData.size())
00225     return false;
00226 
00227   _itemData[column] = value;
00228   return true;
00229 }
00230 
00231 QModelIndex TreeItem::modelIndex(int column)
00232 {
00233   TreeModel * model = this->associatedModel();
00234   if (_parentItem && (_parentItem != model->getRootItem()))
00235     return model->index(rowIndex(),
00236                         column,
00237                         _parentItem->modelIndex());
00238   else
00239     return model->index(rowIndex(),
00240                         column,
00241                         QModelIndex());
00242 }