diff --git a/Plugins/org.mitk.gui.qt.datamanager/src/internal/QmitkPropertyTreeModel.cpp b/Plugins/org.mitk.gui.qt.datamanager/src/internal/QmitkPropertyTreeModel.cpp index d3d4d19a55..53b7553e29 100644 --- a/Plugins/org.mitk.gui.qt.datamanager/src/internal/QmitkPropertyTreeModel.cpp +++ b/Plugins/org.mitk.gui.qt.datamanager/src/internal/QmitkPropertyTreeModel.cpp @@ -1,312 +1,313 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include "QmitkPropertyTreeItem.h" #include "QmitkPropertyTreeModel.h" #include #include #include #include #include #include QmitkPropertyTreeModel::QmitkPropertyTreeModel(QObject *parent) : QAbstractItemModel(parent), m_Properties(NULL), m_RootItem(NULL) { CreateRootItem(); } void QmitkPropertyTreeModel::SetProperties(mitk::PropertyList *properties) { if (properties == m_Properties) return; beginResetModel(); if (m_RootItem != NULL) { delete m_RootItem; m_RootItem = NULL; m_Properties = NULL; } + CreateRootItem(); + if (properties != NULL && !properties->IsEmpty()) { m_Properties = properties; - CreateRootItem(); std::map::const_iterator end = properties->GetMap()->end(); for (std::map::const_iterator iter = properties->GetMap()->begin(); iter != end; ++iter) { QList data; data << iter->first.c_str() << QVariant::fromValue((reinterpret_cast(iter->second.GetPointer()))); m_RootItem->AppendChild(new QmitkPropertyTreeItem(data)); } } endResetModel(); } void QmitkPropertyTreeModel::CreateRootItem() { if (m_RootItem == NULL) { QList rootData; rootData << "Property" << "Value"; m_RootItem = new QmitkPropertyTreeItem(rootData); } } QmitkPropertyTreeModel::~QmitkPropertyTreeModel() { delete m_RootItem; } int QmitkPropertyTreeModel::columnCount(const QModelIndex &parent) const { if (parent.isValid()) return static_cast(parent.internalPointer())->GetColumnCount(); else return m_RootItem->GetColumnCount(); } QVariant QmitkPropertyTreeModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); if (index.column() == 0 && role == Qt::DisplayRole) return static_cast(index.internalPointer())->GetData(index.column()); if (index.column() == 1 && static_cast(index.internalPointer())->GetData(index.column()).isValid()) { mitk::BaseProperty *property = reinterpret_cast(static_cast(index.internalPointer())->GetData(index.column()).value()); if (mitk::ColorProperty *colorProperty = dynamic_cast(property)) { if (role == Qt::DisplayRole || role == Qt::EditRole) { mitk::Color mitkColor = colorProperty->GetColor(); QColor qtColor(static_cast(mitkColor.GetRed() * 255), static_cast(mitkColor.GetGreen() * 255), static_cast(mitkColor.GetBlue() * 255)); return QVariant::fromValue(qtColor); } } else if (mitk::BoolProperty *boolProperty = dynamic_cast(property)) { if (role == Qt::CheckStateRole) return boolProperty->GetValue() ? Qt::Checked : Qt::Unchecked; } else if (mitk::StringProperty *stringProperty = dynamic_cast(property)) { if (role == Qt::DisplayRole || role == Qt::EditRole) return QString(stringProperty->GetValue()); } else if (mitk::IntProperty *intProperty = dynamic_cast(property)) { if (role == Qt::DisplayRole || role == Qt::EditRole) return intProperty->GetValue(); } else if (mitk::FloatProperty *floatProperty = dynamic_cast(property)) { if (role == Qt::DisplayRole || role == Qt::EditRole) return floatProperty->GetValue(); } else if (mitk::DoubleProperty *doubleProperty = dynamic_cast(property)) { if (role == Qt::DisplayRole || role == Qt::EditRole) return doubleProperty->GetValue(); } else if (mitk::EnumerationProperty *enumProperty = dynamic_cast(property)) { if (role == Qt::DisplayRole) { return QString::fromStdString(enumProperty->GetValueAsString()); } else if (role == Qt::EditRole) { QStringList values; for (mitk::EnumerationProperty::EnumConstIterator iter = enumProperty->Begin(); iter != enumProperty->End(); ++iter) values << QString::fromStdString(iter->second); return QVariant(values); } } else { if (role == Qt::DisplayRole) return QString::fromStdString(property->GetValueAsString()); } } return QVariant(); } Qt::ItemFlags QmitkPropertyTreeModel::flags(const QModelIndex &index) const { Qt::ItemFlags flags = QAbstractItemModel::flags(index); if (index.column() == 1) { if (index.data(Qt::EditRole).isValid()) flags |= Qt::ItemIsEditable; if (index.data(Qt::CheckStateRole).isValid()) flags |= Qt::ItemIsUserCheckable; } return flags; } QVariant QmitkPropertyTreeModel::headerData(int section, Qt::Orientation orientation, int role) const { if (orientation == Qt::Horizontal && role == Qt::DisplayRole) return m_RootItem->GetData(section); return QVariant(); } QModelIndex QmitkPropertyTreeModel::index(int row, int column, const QModelIndex &parent) const { if (!hasIndex(row, column, parent)) return QModelIndex(); QmitkPropertyTreeItem *parentItem = parent.isValid() ? static_cast(parent.internalPointer()) : m_RootItem; QmitkPropertyTreeItem *childItem = parentItem->GetChild(row); if (childItem != NULL) return createIndex(row, column, childItem); else return QModelIndex(); } QModelIndex QmitkPropertyTreeModel::parent(const QModelIndex &child) const { if (!child.isValid()) return QModelIndex(); QmitkPropertyTreeItem *parentItem = static_cast(child.internalPointer())->GetParent(); if (parentItem == m_RootItem) return QModelIndex(); return createIndex(parentItem->GetRow(), 0, parentItem); } int QmitkPropertyTreeModel::rowCount(const QModelIndex &parent) const { if (parent.column() > 0) return 0; QmitkPropertyTreeItem *parentItem = parent.isValid() ? static_cast(parent.internalPointer()) : m_RootItem; return parentItem->GetChildCount(); } bool QmitkPropertyTreeModel::setData(const QModelIndex &index, const QVariant &value, int role) { if (index.isValid() && (role == Qt::EditRole || role == Qt::CheckStateRole)) { if (index.column() == 1) { mitk::BaseProperty *property = reinterpret_cast(static_cast(index.internalPointer())->GetData(index.column()).value()); if (mitk::ColorProperty *colorProperty = dynamic_cast(property)) { QColor qtColor = value.value(); if (!qtColor.isValid()) return false; mitk::Color mitkColor = colorProperty->GetColor(); mitkColor.SetRed(qtColor.red() / 255.0); mitkColor.SetGreen(qtColor.green() / 255.0); mitkColor.SetBlue(qtColor.blue() / 255.0); colorProperty->SetColor(mitkColor); m_Properties->InvokeEvent(itk::ModifiedEvent()); m_Properties->Modified(); mitk::RenderingManager::GetInstance()->RequestUpdateAll(); } else if (mitk::BoolProperty *boolProperty = dynamic_cast(property)) { boolProperty->SetValue(value.toInt() == Qt::Checked ? true : false); m_Properties->InvokeEvent(itk::ModifiedEvent()); m_Properties->Modified(); mitk::RenderingManager::GetInstance()->RequestUpdateAll(); } else if (mitk::StringProperty *stringProperty = dynamic_cast(property)) { stringProperty->SetValue(value.toString().toStdString()); m_Properties->InvokeEvent(itk::ModifiedEvent()); m_Properties->Modified(); mitk::RenderingManager::GetInstance()->RequestUpdateAll(); } else if (mitk::IntProperty *intProperty = dynamic_cast(property)) { int intValue = value.toInt(); if (intValue != intProperty->GetValue()) { intProperty->SetValue(intValue); m_Properties->InvokeEvent(itk::ModifiedEvent()); m_Properties->Modified(); mitk::RenderingManager::GetInstance()->RequestUpdateAll(); } } else if (mitk::FloatProperty *floatProperty = dynamic_cast(property)) { int floatValue = value.toFloat(); if (floatValue != floatProperty->GetValue()) { floatProperty->SetValue(floatValue); m_Properties->InvokeEvent(itk::ModifiedEvent()); m_Properties->Modified(); mitk::RenderingManager::GetInstance()->RequestUpdateAll(); } } else if (mitk::EnumerationProperty *enumProperty = dynamic_cast(property)) { std::string activatedItem = value.toString().toStdString(); if (activatedItem != enumProperty->GetValueAsString()) { enumProperty->SetValue(activatedItem); m_Properties->InvokeEvent(itk::ModifiedEvent()); m_Properties->Modified(); mitk::RenderingManager::GetInstance()->RequestUpdateAll(); } } } emit dataChanged(index, index); return true; } return false; } diff --git a/Plugins/org.mitk.gui.qt.datamanager/src/internal/QmitkPropertyTreeView.cpp b/Plugins/org.mitk.gui.qt.datamanager/src/internal/QmitkPropertyTreeView.cpp index 8162fcf3cc..184574c86d 100644 --- a/Plugins/org.mitk.gui.qt.datamanager/src/internal/QmitkPropertyTreeView.cpp +++ b/Plugins/org.mitk.gui.qt.datamanager/src/internal/QmitkPropertyTreeView.cpp @@ -1,102 +1,108 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include "QmitkPropertyTreeView.h" #include "QmitkPropertyTreeModel.h" #include "QmitkLineEdit.h" #include "QmitkPropertyTreeFilterProxyModel.h" #include #include #include #include const std::string QmitkPropertyTreeView::VIEW_ID = "org.mitk.views.propertytreeview"; QmitkPropertyTreeView::QmitkPropertyTreeView() : m_Filter(NULL), m_Model(NULL), m_ProxyModel(NULL), m_Delegate(NULL), m_TreeView(NULL) { } QmitkPropertyTreeView::~QmitkPropertyTreeView() { if (m_Delegate != NULL) delete m_Delegate; if (m_ProxyModel != NULL) delete m_ProxyModel; if (m_Model != NULL) delete m_Model; } void QmitkPropertyTreeView::CreateQtPartControl(QWidget *parent) { m_Filter = new QmitkLineEdit("Filter", parent); m_Model = new QmitkPropertyTreeModel; m_ProxyModel = new QmitkPropertyTreeFilterProxyModel; m_ProxyModel->setSourceModel(m_Model); m_ProxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive); m_ProxyModel->setDynamicSortFilter(true); m_Delegate = new QmitkPropertyDelegate; connect(m_Filter, SIGNAL(textChanged(const QString &)), this, SLOT(OnFilterChanged(const QString &))); m_TreeView = new QTreeView(parent); m_TreeView->setModel(m_ProxyModel); m_TreeView->setItemDelegateForColumn(1, m_Delegate); m_TreeView->setSortingEnabled(true); m_TreeView->setIndentation(16); m_TreeView->setAlternatingRowColors(true); m_TreeView->setSelectionMode(QAbstractItemView::SingleSelection); m_TreeView->setSelectionBehavior(QAbstractItemView::SelectItems); QVBoxLayout *layout = new QVBoxLayout(parent); layout->addWidget(m_Filter); layout->addWidget(m_TreeView); } void QmitkPropertyTreeView::OnFilterChanged(const QString &filter) { m_ProxyModel->setFilterWildcard(filter); m_TreeView->expandAll(); } void QmitkPropertyTreeView::OnSelectionChanged(berry::IWorkbenchPart::Pointer, const QList &nodes) { - if (nodes.empty() || nodes.front().IsNull()) - return; - - std::ostringstream partName; - partName << "Properties (" << nodes.front()->GetName() << ")"; + std::string partName = "Properties"; - SetPartName(partName.str().c_str()); - - m_Model->SetProperties(nodes.front()->GetPropertyList()); + if (nodes.empty() || nodes.front().IsNull()) + { + SetPartName(partName); + m_Model->SetProperties(NULL); + } + else + { + std::ostringstream extPartName; + extPartName << partName << " (" << nodes.front()->GetName() << ")"; + SetPartName(extPartName.str().c_str()); + + m_Model->SetProperties(nodes.front()->GetPropertyList()); + } } void QmitkPropertyTreeView::SetFocus() { m_Filter->setFocus(); }