Tulip  4.6.0
Better Visualization Through Research
library/tulip-gui/include/tulip/GraphPropertiesModel.h
00001 /*
00002  *
00003  * This file is part of Tulip (www.tulip-software.org)
00004  *
00005  * Authors: David Auber and the Tulip development Team
00006  * from LaBRI, University of Bordeaux
00007  *
00008  * Tulip is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU Lesser General Public License
00010  * as published by the Free Software Foundation, either version 3
00011  * of the License, or (at your option) any later version.
00012  *
00013  * Tulip is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00016  * See the GNU General Public License for more details.
00017  *
00018  */
00019 ///@cond DOXYGEN_HIDDEN
00020 
00021 
00022 #ifndef GRAPHPROPERTIESMODEL_H
00023 #define GRAPHPROPERTIESMODEL_H
00024 
00025 #include <tulip/TulipModel.h>
00026 #include <tulip/Observable.h>
00027 #include <tulip/Graph.h>
00028 
00029 #include <QFont>
00030 #include <QIcon>
00031 #include <QSet>
00032 
00033 namespace tlp {
00034 
00035 template<typename PROPTYPE>
00036 class GraphPropertiesModel : public tlp::TulipModel, public tlp::Observable {
00037   tlp::Graph* _graph;
00038   QString _placeholder;
00039   bool _checkable;
00040   QSet<PROPTYPE*> _checkedProperties;
00041   QVector<PROPTYPE*> _properties;
00042   bool _removingRows;
00043   bool forcingRedraw;
00044 
00045   void rebuildCache();
00046 
00047 public:
00048   explicit GraphPropertiesModel(tlp::Graph* graph, bool checkable=false, QObject *parent = NULL);
00049   explicit GraphPropertiesModel(QString placeholder, tlp::Graph* graph, bool checkable=false, QObject *parent = NULL);
00050   virtual ~GraphPropertiesModel() {}
00051 
00052   tlp::Graph* graph() const {
00053     return _graph;
00054   }
00055 
00056   void setGraph(tlp::Graph* graph) {
00057     if (_graph == graph)
00058       return;
00059 
00060     beginResetModel();
00061 
00062     if (_graph != NULL)
00063       _graph->removeListener(this);
00064 
00065     _graph = graph;
00066 
00067     if (_graph != NULL)
00068       _graph->addListener(this);
00069 
00070     rebuildCache();
00071     endResetModel();
00072   }
00073 
00074   QSet<PROPTYPE*> checkedProperties() const {
00075     return _checkedProperties;
00076   }
00077 
00078   // Methods re-implemented from QAbstractItemModel
00079   QModelIndex index(int row, int column,const QModelIndex &parent = QModelIndex()) const;
00080   QModelIndex parent(const QModelIndex &child) const;
00081   int rowCount(const QModelIndex &parent = QModelIndex()) const;
00082   int columnCount(const QModelIndex &parent = QModelIndex()) const;
00083   QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
00084   QVariant headerData(int section, Qt::Orientation orientation, int role) const;
00085   bool setData(const QModelIndex &quiindex, const QVariant &value, int role);
00086 
00087   // Methods inherited from the observable system
00088   void treatEvent(const tlp::Event& evt) {
00089     if (evt.type() == Event::TLP_DELETE) {
00090       beginResetModel();
00091       _graph = NULL;
00092       _properties.clear();
00093       endResetModel();
00094       return;
00095     }
00096 
00097     const GraphEvent* graphEvent = dynamic_cast<const GraphEvent*>(&evt);
00098 
00099     if (graphEvent == NULL)
00100       return;
00101 
00102     if (graphEvent->getType() == GraphEvent::TLP_BEFORE_DEL_LOCAL_PROPERTY || graphEvent->getType() == GraphEvent::TLP_BEFORE_DEL_INHERITED_PROPERTY) {
00103 
00104       PROPTYPE* prop = dynamic_cast<PROPTYPE*>(_graph->getProperty(graphEvent->getPropertyName()));
00105 
00106       if (prop != NULL) {
00107         int row = rowOf(prop);
00108         beginRemoveRows(QModelIndex(),row,row);
00109         _properties.remove(_properties.indexOf(prop));
00110         _removingRows = true;
00111         _checkedProperties.remove(prop);
00112       }
00113     }
00114     else if (graphEvent->getType() == GraphEvent::TLP_AFTER_DEL_LOCAL_PROPERTY || graphEvent->getType() == GraphEvent::TLP_AFTER_DEL_INHERITED_PROPERTY) {
00115       if (_removingRows) {
00116         endRemoveRows();
00117         _removingRows = false;
00118       }
00119     }
00120     else if (graphEvent->getType() == GraphEvent::TLP_ADD_LOCAL_PROPERTY || graphEvent->getType() == GraphEvent::TLP_ADD_INHERITED_PROPERTY) {
00121       PROPTYPE* prop = dynamic_cast<PROPTYPE*>(_graph->getProperty(graphEvent->getPropertyName()));
00122 
00123       if (prop != NULL) {
00124         rebuildCache();
00125         int row = rowOf(prop);
00126 
00127         if (row >= 0) {
00128           beginInsertRows(QModelIndex(),row,row);
00129           endInsertRows();
00130         }
00131       }
00132     }
00133     else if (graphEvent->getType() == GraphEvent::TLP_AFTER_RENAME_LOCAL_PROPERTY) {
00134       // force any needed sorting
00135       emit layoutAboutToBeChanged();
00136       changePersistentIndex(createIndex(0, 0),
00137                             createIndex(_properties.size() - 1, 0));
00138       emit layoutChanged();
00139     }
00140   }
00141 
00142   int rowOf(PROPTYPE*) const;
00143 
00144   int rowOf(const QString& pName) const;
00145 
00146   Qt::ItemFlags flags(const QModelIndex &index) const {
00147     Qt::ItemFlags result = QAbstractItemModel::flags(index);
00148 
00149     if (index.column() == 0 && _checkable)
00150       result |= Qt::ItemIsUserCheckable;
00151 
00152     return result;
00153   }
00154 };
00155 
00156 }
00157 
00158 #include "cxx/GraphPropertiesModel.cxx"
00159 
00160 
00161 #endif // GRAPHPROPERTIESMODEL_H
00162 ///@endcond
 All Classes Files Functions Variables Enumerations Enumerator Properties