Back to index

salome-gui  6.5.0
TreeModel.hxx
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 #ifndef TREEMODEL_H
00024 #define TREEMODEL_H
00025 
00026 #include "TreeData.hxx"
00027 
00028 #include <QAbstractItemModel>
00029 #include <QModelIndex>
00030 #include <QVariant>
00031 #include <QStringList>
00032 
00033 #include "DataObject.hxx"
00034 
00035 class TreeItem;
00036 class TreeView;
00037 
00038 class TREEDATA_EXPORT TreeModel : public QAbstractItemModel
00039 {
00040   Q_OBJECT
00041 
00042   // IMPORTANT NOTE:
00043   // In this implementation of QAbstractItemModel, a tree item is
00044   // associated to the tree model it belongs to (it can request its
00045   // model throw a pointer to this model). Then we declare the
00046   // TreeItem as a friend class so that it can request the protected
00047   // methods (for example beginInsertRows and endInsertRows, required
00048   // to manage correctly the addition of an item in the model. An
00049   // item can append a child to itself, so it needs to inform the
00050   // model when it begins and when it ends).
00051   friend class TreeItem;
00052   friend class TreeView;
00053 
00054 public:
00055   TreeModel(const QStringList &headers, QObject *parent = 0);
00056   ~TreeModel();
00057 
00058   //
00059   // =================================================================
00060   // This part of the specification is the standard interface required
00061   // for providing an operational TreeModel. These methods are used by
00062   // the TreeView for display purpose. We just have to implement how
00063   // we want the items to be displayed in the view.
00064   // =================================================================
00065   //
00066   // MEM: note that these methods are not intended to be used
00067   // directly. That's the job of the viewer to know how to use
00068   // them. We just have to give the implementation for customizing the
00069   // appearance of the tree. The implementation generally requests the
00070   // items'data to set the appearance features.
00071   QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
00072   QModelIndex parent(const QModelIndex &index) const;
00073   int rowCount(const QModelIndex &parent = QModelIndex()) const;
00074   int columnCount(const QModelIndex &parent = QModelIndex()) const;
00075   QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
00076   bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole);
00077   QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
00078   bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
00079   Qt::ItemFlags flags(const QModelIndex &index) const;
00080 
00081   //
00082   // =================================================================
00083   // This part is a specific behavior to get a TreeModel that can
00084   // organize itself the tree hierarchy using data provided in a
00085   // filesystem-like format:
00086   // 
00087   // data="a/b/c" ==> creation/filling of the hierarchy a->b->c
00088   // The "folder" categories are unique whereas the leaves may exists
00089   // in multiple instances.
00090   // =================================================================
00091   //
00092   bool addData(DataObject * dataObject);
00093   bool addData(DataObject * dataObject, const QStringList &path);
00094 
00095   // TODO: We should implement the delete and the update fucntions
00096 
00097   // This part contains helper functions for general purposes
00098   TreeItem * getRootItem();
00099 
00100 private:
00101   TreeItem *getItem(const QModelIndex &index = QModelIndex()) const;
00102   TreeItem * _rootItem;
00103 };
00104 
00105 #endif // TREEMODEL_H