Tulip  4.6.0
Better Visualization Through Research
library/tulip-gui/include/tulip/cxx/GraphPropertiesModel.cxx
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 
00020 #include <tulip/Graph.h>
00021 
00022 namespace tlp {
00023 
00024 template<typename PROPTYPE>
00025 void tlp::GraphPropertiesModel<PROPTYPE>::rebuildCache() {
00026   _properties.clear();
00027 
00028   if (_graph == NULL)
00029     return;
00030 
00031   std::string propName;
00032   forEach(propName,_graph->getInheritedProperties()) {
00033 #ifdef NDEBUG
00034 
00035     if (propName == "viewMetaGraph")
00036       continue;
00037 
00038 #endif
00039     PROPTYPE* prop = dynamic_cast<PROPTYPE*>(_graph->getProperty(propName));
00040 
00041     if (prop != NULL) {
00042       _properties+=prop;
00043     }
00044   }
00045   forEach(propName,_graph->getLocalProperties()) {
00046 #ifdef NDEBUG
00047 
00048     if (propName == "viewMetaGraph")
00049       continue;
00050 
00051 #endif
00052     PROPTYPE* prop = dynamic_cast<PROPTYPE*>(_graph->getProperty(propName));
00053 
00054     if (prop != NULL) {
00055       _properties+=prop;
00056     }
00057   }
00058 }
00059 
00060 template<typename PROPTYPE>
00061 GraphPropertiesModel<PROPTYPE>::GraphPropertiesModel(tlp::Graph* graph, bool checkable, QObject *parent): tlp::TulipModel(parent), _graph(graph), _placeholder(QString::null), _checkable(checkable), _removingRows(false), forcingRedraw(false) {
00062   if (_graph != NULL) {
00063     _graph->addListener(this);
00064     rebuildCache();
00065   }
00066 }
00067 
00068 template<typename PROPTYPE>
00069 GraphPropertiesModel<PROPTYPE>::GraphPropertiesModel(QString placeholder, tlp::Graph* graph, bool checkable, QObject *parent): tlp::TulipModel(parent), _graph(graph), _placeholder(placeholder), _checkable(checkable), _removingRows(false), forcingRedraw(false) {
00070   if (_graph != NULL) {
00071     _graph->addListener(this);
00072     rebuildCache();
00073   }
00074 }
00075 
00076 template<typename PROPTYPE>
00077 QModelIndex GraphPropertiesModel<PROPTYPE>::index(int row, int column,const QModelIndex &parent) const {
00078   if (_graph == NULL || !hasIndex(row,column,parent))
00079     return QModelIndex();
00080 
00081   int vectorIndex = row;
00082 
00083   if (!_placeholder.isNull()) {
00084     if (row == 0)
00085       return createIndex(row,column);
00086 
00087     vectorIndex--;
00088   }
00089 
00090   return createIndex(row,column,_properties[vectorIndex]);
00091 }
00092 
00093 template<typename PROPTYPE>
00094 QModelIndex GraphPropertiesModel<PROPTYPE>::parent(const QModelIndex &) const {
00095   return QModelIndex();
00096 }
00097 
00098 template<typename PROPTYPE>
00099 int GraphPropertiesModel<PROPTYPE>::rowCount(const QModelIndex &parent) const {
00100   if (parent.isValid() || _graph == NULL || forcingRedraw)
00101     return 0;
00102 
00103   int result = _properties.size();
00104 
00105   if (!_placeholder.isNull())
00106     result++;
00107 
00108   return result;
00109 }
00110 
00111 template<typename PROPTYPE>
00112 int GraphPropertiesModel<PROPTYPE>::columnCount(const QModelIndex &) const {
00113   return 3;
00114 }
00115 
00116 template<typename PROPTYPE>
00117 QVariant GraphPropertiesModel<PROPTYPE>::data(const QModelIndex &index, int role) const {
00118   if (_graph == NULL || (index.internalPointer() == NULL && index.row() != 0))
00119     return QVariant();
00120 
00121   PropertyInterface* pi = (PropertyInterface*)(index.internalPointer());
00122 
00123   if (role == Qt::DisplayRole || role == Qt::ToolTipRole) {
00124     if (!_placeholder.isNull() && index.row() == 0)
00125       return _placeholder;
00126 
00127     if (pi == NULL)
00128       return QString();
00129 
00130     if (index.column() == 0)
00131       return QString::fromUtf8(pi->getName().c_str());
00132     else if (index.column() == 1)
00133       return pi->getTypename().c_str();
00134     else if (index.column() == 2)
00135       return (_graph->existLocalProperty(pi->getName()) ? trUtf8("Local") : tr("Inherited from graph ") + QString::number(pi->getGraph()->getId()) + " (" + QString::fromUtf8(pi->getGraph()->getName().c_str()) + ')');
00136   }
00137 
00138   else if (role == Qt::DecorationRole && index.column() == 0 && pi != NULL && !_graph->existLocalProperty(pi->getName()))
00139     return QIcon(":/tulip/gui/ui/inherited_properties.png");
00140 
00141   else if (role == Qt::FontRole) {
00142     QFont f;
00143 
00144     if (!_placeholder.isNull() && index.row() == 0)
00145       f.setItalic(true);
00146 
00147     return f;
00148   }
00149   else if (role == PropertyRole) {
00150     return QVariant::fromValue<PropertyInterface*>(pi);
00151   }
00152   else if (_checkable && role == Qt::CheckStateRole && index.column() == 0) {
00153     return (_checkedProperties.contains((PROPTYPE*) pi) ? Qt::Checked : Qt::Unchecked);
00154   }
00155 
00156   return QVariant();
00157 }
00158 
00159 template<typename PROPTYPE>
00160 int GraphPropertiesModel<PROPTYPE>::rowOf(PROPTYPE* pi) const {
00161   int result = _properties.indexOf(pi);
00162 
00163   if (!_placeholder.isNull())
00164     ++result;
00165 
00166   return result;
00167 }
00168 
00169 template<typename PROPTYPE>
00170 int GraphPropertiesModel<PROPTYPE>::rowOf(const QString& pName) const {
00171   for(int i = 0; i < _properties.size(); ++i) {
00172     if (pName == QString::fromUtf8(_properties[i]->getName().c_str()))
00173       return i;
00174   }
00175 
00176   return -1;
00177 }
00178 
00179 template<typename PROPTYPE>
00180 QVariant tlp::GraphPropertiesModel<PROPTYPE>::headerData(int section, Qt::Orientation orientation, int role) const {
00181   if (orientation == Qt::Horizontal) {
00182     if (role == Qt::DisplayRole) {
00183       if (section == 0)
00184         return trUtf8("Name");
00185       else if (section == 1)
00186         return trUtf8("Type");
00187       else if (section == 2)
00188         return trUtf8("Scope");
00189     }
00190   }
00191 
00192   return TulipModel::headerData(section,orientation,role);
00193 }
00194 
00195 template<typename PROPTYPE>
00196 bool tlp::GraphPropertiesModel<PROPTYPE>::setData(const QModelIndex &index, const QVariant &value, int role) {
00197   if (_graph == NULL)
00198     return false;
00199 
00200   if (_checkable && role == Qt::CheckStateRole && index.column() == 0) {
00201     if (value.value<int>() == (int)Qt::Checked)
00202       _checkedProperties.insert((PROPTYPE*)index.internalPointer());
00203     else
00204       _checkedProperties.remove((PROPTYPE*)index.internalPointer());
00205 
00206     emit checkStateChanged(index,(Qt::CheckState)(value.value<int>()));
00207     return true;
00208   }
00209 
00210   return false;
00211 }
00212 
00213 }
 All Classes Files Functions Variables Enumerations Enumerator Properties