20 #include <tulip/Graph.h>
21 #include <tulip/TlpQtTools.h>
25 template <
typename PROPTYPE>
26 void tlp::GraphPropertiesModel<PROPTYPE>::rebuildCache() {
29 if (_graph ==
nullptr)
32 for (
auto inheritedProp : _graph->getInheritedObjectProperties()) {
35 if (inheritedProp->getName() ==
"viewMetaGraph")
39 PROPTYPE *prop =
dynamic_cast<PROPTYPE *
>(inheritedProp);
41 if (prop !=
nullptr) {
45 for (
auto localProp : _graph->getLocalObjectProperties()) {
48 if (localProp->getName() ==
"viewMetaGraph")
52 PROPTYPE *prop =
dynamic_cast<PROPTYPE *
>(localProp);
54 if (prop !=
nullptr) {
60 template <
typename PROPTYPE>
61 GraphPropertiesModel<PROPTYPE>::GraphPropertiesModel(
tlp::Graph *graph,
bool checkable,
63 :
tlp::TulipModel(parent), _graph(graph), _checkable(checkable), _removingRows(false),
64 forcingRedraw(false) {
65 if (_graph !=
nullptr) {
66 _graph->addListener(
this);
71 template <
typename PROPTYPE>
72 GraphPropertiesModel<PROPTYPE>::GraphPropertiesModel(QString placeholder,
tlp::Graph *graph,
73 bool checkable, QObject *parent)
74 :
tlp::TulipModel(parent), _graph(graph), _placeholder(placeholder), _checkable(checkable),
75 _removingRows(false), forcingRedraw(false) {
76 if (_graph !=
nullptr) {
77 _graph->addListener(
this);
82 template <
typename PROPTYPE>
83 QModelIndex GraphPropertiesModel<PROPTYPE>::index(
int row,
int column,
84 const QModelIndex &parent)
const {
85 if (_graph ==
nullptr || !hasIndex(row, column, parent))
88 int vectorIndex = row;
90 if (!_placeholder.isEmpty()) {
92 return createIndex(row, column);
97 return createIndex(row, column, _properties[vectorIndex]);
100 template <
typename PROPTYPE>
101 QModelIndex GraphPropertiesModel<PROPTYPE>::parent(
const QModelIndex &)
const {
102 return QModelIndex();
105 template <
typename PROPTYPE>
106 int GraphPropertiesModel<PROPTYPE>::rowCount(
const QModelIndex &parent)
const {
107 if (parent.isValid() || _graph ==
nullptr || forcingRedraw)
110 int result = _properties.size();
112 if (!_placeholder.isEmpty())
118 template <
typename PROPTYPE>
119 int GraphPropertiesModel<PROPTYPE>::columnCount(
const QModelIndex &)
const {
123 template <
typename PROPTYPE>
124 QVariant GraphPropertiesModel<PROPTYPE>::data(
const QModelIndex &index,
int role)
const {
125 if (_graph ==
nullptr || (index.internalPointer() ==
nullptr && index.row() != 0))
128 PropertyInterface *pi =
static_cast<PropertyInterface *
>(index.internalPointer());
130 if (role == Qt::DisplayRole || role == Qt::ToolTipRole) {
131 if (!_placeholder.isEmpty() && index.row() == 0)
137 if (index.column() == 0)
139 else if (index.column() == 1)
140 return pi->getTypename().c_str();
141 else if (index.column() == 2)
142 return (_graph->existLocalProperty(pi->getName())
144 : tr(
"Inherited from graph ") + QString::number(pi->getGraph()->getId()) +
" (" +
148 else if (role == Qt::DecorationRole && index.column() == 0 && pi !=
nullptr &&
149 !_graph->existLocalProperty(pi->getName()))
150 return QIcon(
":/tulip/gui/ui/inherited_properties.png");
152 else if (role == Qt::FontRole) {
154 QWidget *p =
dynamic_cast<QWidget *
>(QAbstractItemModel::parent());
158 if (!_placeholder.isEmpty() && index.row() == 0)
162 }
else if (role == PropertyRole) {
163 return QVariant::fromValue<PropertyInterface *>(pi);
164 }
else if (_checkable && role == Qt::CheckStateRole && index.column() == 0) {
165 return (_checkedProperties.contains(
static_cast<PROPTYPE *
>(pi)) ? Qt::Checked : Qt::Unchecked);
171 template <
typename PROPTYPE>
172 int GraphPropertiesModel<PROPTYPE>::rowOf(PROPTYPE *pi)
const {
173 int result = _properties.indexOf(pi);
175 if (result > -1 && !_placeholder.isEmpty())
181 template <
typename PROPTYPE>
182 int GraphPropertiesModel<PROPTYPE>::rowOf(
const QString &pName)
const {
183 for (
int i = 0; i < _properties.size(); ++i) {
191 template <
typename PROPTYPE>
192 QVariant tlp::GraphPropertiesModel<PROPTYPE>::headerData(
int section, Qt::Orientation orientation,
194 if (orientation == Qt::Horizontal) {
195 if (role == Qt::DisplayRole) {
198 else if (section == 1)
200 else if (section == 2)
205 return TulipModel::headerData(section, orientation, role);
208 template <
typename PROPTYPE>
209 bool tlp::GraphPropertiesModel<PROPTYPE>::setData(
const QModelIndex &index,
const QVariant &value,
211 if (_graph ==
nullptr)
214 if (_checkable && role == Qt::CheckStateRole && index.column() == 0) {
215 if (value.value<
int>() ==
int(Qt::Checked))
216 _checkedProperties.insert(
static_cast<PROPTYPE *
>(index.internalPointer()));
218 _checkedProperties.remove(
static_cast<PROPTYPE *
>(index.internalPointer()));
220 emit checkStateChanged(index,
static_cast<Qt::CheckState
>(value.value<
int>()));
QString tlpStringToQString(const std::string &toConvert)
Convert a Tulip UTF-8 encoded std::string to a QString.