34 return serviceRef ? context->GetService<T>(serviceRef) :
nullptr;
39 return QColor(color.GetRed() * 255, color.GetGreen() * 255, color.GetBlue() * 255);
44 return data.isValid() ?
reinterpret_cast<mitk::BaseProperty *
>(data.value<
void *>()) :
nullptr;
51 mitkColor.SetRed(color.red() / 255.0f);
52 mitkColor.SetGreen(color.green() / 255.0f);
53 mitkColor.SetBlue(color.blue() / 255.0f);
64 return pair.second.GetPointer() == m_Property;
72 : QAbstractItemModel(parent),
74 m_FilterProperties(false),
75 m_PropertyAliases(nullptr),
76 m_PropertyFilters(nullptr)
78 this->CreateRootItem();
83 this->SetNewPropertyList(
nullptr);
89 return static_cast<QmitkPropertyItem *>(parent.internalPointer())->GetColumnCount();
91 return m_RootItem->GetColumnCount();
94 void QmitkPropertyItemModel::CreateRootItem()
96 QList<QVariant> rootData;
97 rootData <<
"Property" 102 this->beginResetModel();
103 this->endResetModel();
108 if (!index.isValid())
112 index.column() == 1 ?
GetBaseProperty(static_cast<QmitkPropertyItem *>(index.internalPointer())->GetData(1)) :
nullptr;
114 if (role == Qt::DisplayRole)
116 if (index.column() == 0)
120 else if (index.column() == 1 &&
property !=
nullptr)
122 if (
auto colorProperty = dynamic_cast<mitk::ColorProperty *>(property))
123 return MitkToQt(colorProperty->GetValue());
124 else if (dynamic_cast<mitk::BoolProperty *>(property) ==
nullptr)
125 return QString::fromStdString(property->GetValueAsString());
128 else if (index.column() == 1 &&
property !=
nullptr)
130 if (role == Qt::CheckStateRole)
132 if (
auto boolProperty = dynamic_cast<mitk::BoolProperty *>(property))
133 return boolProperty->GetValue() ? Qt::Checked : Qt::Unchecked;
135 else if (role == Qt::EditRole)
137 if (dynamic_cast<mitk::StringProperty *>(property) !=
nullptr)
139 return QString::fromStdString(property->GetValueAsString());
141 else if (
auto intProperty = dynamic_cast<mitk::IntProperty *>(property))
143 return intProperty->GetValue();
145 else if (
auto floatProperty = dynamic_cast<mitk::FloatProperty *>(property))
147 return floatProperty->GetValue();
149 else if (
auto doubleProperty = dynamic_cast<mitk::DoubleProperty *>(property))
151 return doubleProperty->GetValue();
153 else if (
auto enumProperty = dynamic_cast<mitk::EnumerationProperty *>(property))
158 values << QString::fromStdString(it->second);
162 else if (
auto colorProperty = dynamic_cast<mitk::ColorProperty *>(property))
164 return MitkToQt(colorProperty->GetValue());
169 return QVariant::fromValue<void *>(property);
178 if (property ==
nullptr)
179 return QModelIndex();
182 return QModelIndex();
184 auto propertyMap = m_PropertyList.
Lock()->GetMap();
185 auto it = std::find_if(propertyMap->begin(), propertyMap->end(), PropertyEqualTo(property));
187 if (it == propertyMap->end())
188 return QModelIndex();
190 QString name = QString::fromStdString(it->first);
192 if (!name.contains(
'.'))
194 QModelIndexList item = this->match(
index(0, 0), Qt::DisplayRole, name, 1, Qt::MatchExactly);
201 QStringList names = name.split(
'.');
202 QModelIndexList items =
203 this->match(
index(0, 0), Qt::DisplayRole, names.last(), -1, Qt::MatchRecursive | Qt::MatchExactly);
205 for (
auto item : items)
207 QModelIndex candidate = item;
209 for (
int i = names.length() - 1; i != 0; --i)
211 QModelIndex
parent = item.parent();
213 if (parent.parent() == QModelIndex())
215 if (parent.data() != names.first())
221 if (parent.data() != names[i - 1])
229 return QModelIndex();
234 Qt::ItemFlags
flags = QAbstractItemModel::flags(index);
236 if (index.column() == 1)
238 if (index.data(Qt::EditRole).isValid())
239 flags |= Qt::ItemIsEditable;
241 if (index.data(Qt::CheckStateRole).isValid())
242 flags |= Qt::ItemIsUserCheckable;
250 return m_PropertyList.
Lock().GetPointer();
255 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
256 return m_RootItem->GetData(section);
263 if (!this->hasIndex(row, column, parent))
264 return QModelIndex();
267 parent.isValid() ?
static_cast<QmitkPropertyItem *
>(parent.internalPointer()) : m_RootItem.get();
271 return childItem !=
nullptr ? this->createIndex(row, column, childItem) : QModelIndex();
276 bool updateAliases = m_ShowAliases != (m_PropertyAliases !=
nullptr);
277 bool updateFilters = m_FilterProperties != (m_PropertyFilters !=
nullptr);
279 bool resetPropertyList =
false;
283 m_PropertyAliases = m_ShowAliases ? GetPropertyService<mitk::IPropertyAliases>() :
nullptr;
285 resetPropertyList = !m_PropertyList.
IsExpired();
290 m_PropertyFilters = m_FilterProperties ? GetPropertyService<mitk::IPropertyFilters>() :
nullptr;
292 if (!resetPropertyList)
293 resetPropertyList = !m_PropertyList.
IsExpired();
296 if (resetPropertyList)
297 this->SetNewPropertyList(m_PropertyList.
Lock());
300 void QmitkPropertyItemModel::OnPropertyListModified()
302 this->SetNewPropertyList(m_PropertyList.
Lock());
305 void QmitkPropertyItemModel::OnPropertyListDeleted()
307 this->CreateRootItem();
310 void QmitkPropertyItemModel::OnPropertyModified(
const itk::Object *property,
const itk::EventObject &)
312 QModelIndex
index = this->FindProperty(static_cast<const mitk::BaseProperty *>(property));
314 if (index != QModelIndex())
315 emit dataChanged(index, index);
320 if (!child.isValid())
321 return QModelIndex();
325 if (parentItem == m_RootItem.get())
326 return QModelIndex();
328 return this->createIndex(parentItem->
GetRow(), 0, parentItem);
333 if (parent.column() > 0)
337 parent.isValid() ?
static_cast<QmitkPropertyItem *
>(parent.internalPointer()) : m_RootItem.get();
344 if (!index.isValid() || index.column() != 1 || (role != Qt::EditRole && role != Qt::CheckStateRole))
349 if (property ==
nullptr)
354 boolProperty->SetValue(value.toInt() == Qt::Checked ? true :
false);
358 stringProperty->SetValue(value.toString().toStdString());
360 else if (
mitk::IntProperty *intProperty = dynamic_cast<mitk::IntProperty *>(property))
362 intProperty->SetValue(value.toInt());
364 else if (
mitk::FloatProperty *floatProperty = dynamic_cast<mitk::FloatProperty *>(property))
366 floatProperty->SetValue(value.toFloat());
370 doubleProperty->SetValue(value.toDouble());
374 std::string selection = value.toString().toStdString();
376 if (selection != enumProperty->GetValueAsString() && enumProperty->IsValidEnumerationValue(selection))
377 enumProperty->SetValue(selection);
379 else if (
mitk::ColorProperty *colorProperty = dynamic_cast<mitk::ColorProperty *>(property))
381 colorProperty->SetValue(
QtToMitk(value.value<QColor>()));
384 auto propertyList = m_PropertyList.
Lock();
386 propertyList->Modified();
397 this->beginResetModel();
401 auto propertyList = m_PropertyList.
Lock();
403 propertyList->RemoveObserver(m_PropertyListDeletedTag);
404 propertyList->RemoveObserver(m_PropertyListModifiedTag);
406 const PropertyMap *propertyMap = m_PropertyList.
Lock()->GetMap();
408 for (PropertyMap::const_iterator propertyIt = propertyMap->begin(); propertyIt != propertyMap->end(); ++propertyIt)
410 std::map<std::string, unsigned long>::const_iterator tagIt = m_PropertyModifiedTags.find(propertyIt->first);
412 if (tagIt != m_PropertyModifiedTags.end())
413 propertyIt->second->RemoveObserver(tagIt->second);
415 tagIt = m_PropertyDeletedTags.find(propertyIt->first);
417 if (tagIt != m_PropertyDeletedTags.end())
418 propertyIt->second->RemoveObserver(tagIt->second);
421 m_PropertyModifiedTags.clear();
422 m_PropertyDeletedTags.clear();
425 m_PropertyList = newPropertyList;
429 auto onPropertyListModified = itk::SimpleMemberCommand<QmitkPropertyItemModel>::New();
430 onPropertyListModified->SetCallbackFunction(
this, &QmitkPropertyItemModel::OnPropertyListModified);
431 m_PropertyListModifiedTag = m_PropertyList.
Lock()->AddObserver(itk::ModifiedEvent(), onPropertyListModified);
433 auto onPropertyListDeleted = itk::SimpleMemberCommand<QmitkPropertyItemModel>::New();
434 onPropertyListDeleted->SetCallbackFunction(
this, &QmitkPropertyItemModel::OnPropertyListDeleted);
435 m_PropertyListDeletedTag = m_PropertyList.
Lock()->AddObserver(itk::DeleteEvent(), onPropertyListDeleted);
437 auto modifiedCommand = itk::MemberCommand<QmitkPropertyItemModel>::New();
438 modifiedCommand->SetCallbackFunction(
this, &QmitkPropertyItemModel::OnPropertyModified);
440 const PropertyMap *propertyMap = m_PropertyList.
Lock()->GetMap();
442 for (PropertyMap::const_iterator it = propertyMap->begin(); it != propertyMap->end(); ++it)
443 m_PropertyModifiedTags.insert(
444 std::make_pair(it->first, it->second->AddObserver(itk::ModifiedEvent(), modifiedCommand)));
447 this->CreateRootItem();
449 if (m_PropertyList !=
nullptr && !m_PropertyList.
Lock()->IsEmpty())
452 bool filterProperties =
false;
454 if (m_PropertyFilters !=
nullptr &&
455 (m_PropertyFilters->
HasFilter() || m_PropertyFilters->
HasFilter(m_ClassName.toStdString())))
457 filteredProperties = m_PropertyFilters->
ApplyFilter(*m_PropertyList.
Lock()->GetMap(), m_ClassName.toStdString());
458 filterProperties =
true;
462 !filterProperties ? m_PropertyList.
Lock()->GetMap() : &filteredProperties;
464 mitk::PropertyList::PropertyMap::const_iterator end = propertyMap->end();
466 for (mitk::PropertyList::PropertyMap::const_iterator iter = propertyMap->begin(); iter != end; ++iter)
468 std::vector<std::string> aliases;
470 if (m_PropertyAliases !=
nullptr)
472 aliases = m_PropertyAliases->
GetAliases(iter->first, m_ClassName.toStdString());
474 if (aliases.empty() && !m_ClassName.isEmpty())
475 aliases = m_PropertyAliases->
GetAliases(iter->first);
480 QList<QVariant>
data;
481 data << QString::fromStdString(iter->first)
482 << QVariant::fromValue((reinterpret_cast<void *>(iter->second.GetPointer())));
488 std::vector<std::string>::const_iterator end = aliases.end();
489 for (std::vector<std::string>::const_iterator aliasIter = aliases.begin(); aliasIter != end; ++aliasIter)
491 QList<QVariant>
data;
492 data << QString::fromStdString(*aliasIter)
493 << QVariant::fromValue((reinterpret_cast<void *>(iter->second.GetPointer())));
501 this->endResetModel();
506 if (m_PropertyList != propertyList)
508 m_ClassName = className;
509 this->SetNewPropertyList(propertyList);
515 this->SetNewPropertyList(m_PropertyList.
Lock());
mitk::PropertyList * GetPropertyList() const
itk::SmartPointer< T > Lock() const
int rowCount(const QModelIndex &parent=QModelIndex()) const override
std::pair< std::string, BaseProperty::Pointer > PropertyMapElementType
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
QmitkPropertyItem * GetChild(int row) const
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
~QmitkPropertyItemModel() override
virtual bool HasFilter(const std::string &className="") const =0
Check if a specific data node class name has a property filter.
virtual std::map< std::string, BaseProperty::Pointer > ApplyFilter(const std::map< std::string, BaseProperty::Pointer > &propertyMap, const std::string &className="") const =0
Apply property filter to property list.
Key-value list holding instances of BaseProperty.
QModelIndex parent(const QModelIndex &child) const override
static mitk::BaseProperty * GetBaseProperty(const QVariant &data)
virtual std::vector< std::string > GetAliases(const std::string &propertyName, const std::string &className="")=0
Get aliases for a specific property.
The ColorProperty class RGB color property.
Abstract base class for properties.
std::map< std::string, BaseProperty::Pointer > PropertyMap
static RenderingManager * GetInstance()
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
bool IsExpired() const noexcept
EnumIdsContainerType::const_iterator EnumConstIterator
int columnCount(const QModelIndex &parent=QModelIndex()) const override
itk::RGBPixel< float > Color
Color Standard RGB color typedef (float)
QmitkPropertyItemModel(QObject *parent=nullptr)
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
static QColor MitkToQt(const mitk::Color &color)
void SetPropertyList(mitk::PropertyList *propertyList, const QString &className="")
int GetChildCount() const
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
static mitk::Color QtToMitk(const QColor &color)
Qt::ItemFlags flags(const QModelIndex &index) const override
void RequestUpdateAll(RequestType type=REQUEST_UPDATE_ALL)
void OnPreferencesChanged()