38 #include <QTextStream>
45 bool _PlaceNewNodesOnTop,
47 : QAbstractItemModel(parent),
49 m_PlaceNewNodesOnTop(_PlaceNewNodesOnTop),
51 m_BlockDataStorageEvents(false),
52 m_AllowHierarchyChange(false)
79 if (!parent.isValid())
82 parentItem =
static_cast<TreeItem *
>(parent.internalPointer());
86 return createIndex(row, column, childItem);
102 if (DicomPropertiesExists(*dataNode))
104 return Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled |
105 Qt::ItemIsDropEnabled;
107 return Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable |
108 Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
112 return Qt::ItemIsDropEnabled;
123 if (!index.isValid())
124 return QModelIndex();
130 return QModelIndex();
132 return this->createIndex(parentItem->
GetIndex(), 0, parentItem);
138 return static_cast<TreeItem *>(index.internalPointer());
144 return Qt::CopyAction | Qt::MoveAction;
149 return Qt::CopyAction | Qt::MoveAction;
153 const QMimeData *data, Qt::DropAction action,
int row,
int ,
const QModelIndex &parent)
156 if (action == Qt::IgnoreAction)
162 bool returnValue =
false;
164 if (data->hasFormat(
"application/x-qabstractitemmodeldatalist"))
170 if (listOfItemsToDrop.empty())
195 bool isValidDragAndDropOperation(
true);
200 QList<TreeItem *>::iterator diIter;
201 for (diIter = listOfItemsToDrop.begin() + 1; diIter != listOfItemsToDrop.end(); diIter++)
203 if (firstParent != (*diIter)->
GetParent())
205 isValidDragAndDropOperation =
false;
216 isValidDragAndDropOperation = listOfItemsToDrop[0]->
GetParent() == parentItem;
220 isValidDragAndDropOperation = listOfItemsToDrop[0]->
GetParent() == dropItem;
226 QList<TreeItem *>::iterator diIter;
227 for (diIter = listOfItemsToDrop.begin(); diIter != listOfItemsToDrop.end(); diIter++)
231 while (tempItem !=
m_Root)
234 if (tempItem == *diIter)
236 isValidDragAndDropOperation =
false;
242 if (!isValidDragAndDropOperation)
243 return isValidDragAndDropOperation;
245 if (listOfItemsToDrop[0] != dropItem && isValidDragAndDropOperation)
253 QList<TreeItem *>::iterator diIter;
254 for (diIter = listOfItemsToDrop.begin(); diIter != listOfItemsToDrop.end(); diIter++)
266 this->beginRemoveRows(
269 this->endRemoveRows();
288 if ((row == -1 && dropItemModelIndex.row() == -1) || dropItemModelIndex.row() > parentItem->
GetChildCount())
295 this->beginInsertRows(dropItemModelIndex, dropIndex, dropIndex + listOfItemsToDrop.size() - 1);
299 this->beginInsertRows(parentModelIndex, dropIndex, dropIndex + listOfItemsToDrop.size() - 1);
302 for (diIter = listOfItemsToDrop.begin(); diIter != listOfItemsToDrop.end(); diIter++)
330 this->endInsertRows();
336 else if (data->hasFormat(
"application/x-mitk-datanodes"))
340 int numberOfNodesDropped = 0;
344 foreach (node, dataNodeList)
351 if (basedata.IsNotNull())
356 numberOfNodesDropped++;
361 if (numberOfNodesDropped > 0)
372 QStringList types = QAbstractItemModel::mimeTypes();
373 types <<
"application/x-qabstractitemmodeldatalist";
374 types <<
"application/x-mitk-datanodes";
385 QMimeData *ret =
new QMimeData;
387 QString treeItemAddresses(
"");
388 QString dataNodeAddresses(
"");
390 QByteArray baTreeItemPtrs;
391 QByteArray baDataNodePtrs;
393 QDataStream dsTreeItemPtrs(&baTreeItemPtrs, QIODevice::WriteOnly);
394 QDataStream dsDataNodePtrs(&baDataNodePtrs, QIODevice::WriteOnly);
396 for (
int i = 0; i < indexes.size(); i++)
398 TreeItem *treeItem =
static_cast<TreeItem *
>(indexes.at(i).internalPointer());
400 dsTreeItemPtrs << reinterpret_cast<quintptr>(treeItem);
401 dsDataNodePtrs << reinterpret_cast<quintptr>(treeItem->
GetDataNode().GetPointer());
404 unsigned long long treeItemAddress =
reinterpret_cast<unsigned long long>(treeItem);
405 unsigned long long dataNodeAddress =
reinterpret_cast<unsigned long long>(treeItem->
GetDataNode().GetPointer());
406 QTextStream(&treeItemAddresses) << treeItemAddress;
407 QTextStream(&dataNodeAddresses) << dataNodeAddress;
409 if (i != indexes.size() - 1)
411 QTextStream(&treeItemAddresses) <<
",";
412 QTextStream(&dataNodeAddresses) <<
",";
418 ret->setData(
"application/x-qabstractitemmodeldatalist", QByteArray(treeItemAddresses.toLatin1()));
419 ret->setData(
"application/x-mitk-datanodes", QByteArray(dataNodeAddresses.toLatin1()));
434 if (DicomPropertiesExists(*dataNode))
449 nodeName += QFile::encodeName(patientsName->
GetValueAsString().c_str()) +
"\n";
450 nodeName += QFile::encodeName(studyDescription->
GetValueAsString().c_str()) +
"\n";
451 nodeName += QFile::encodeName(seriesDescription->
GetValueAsString().c_str());
455 nodeName += QFile::encodeName(patientsName_deprecated->
GetValueAsString().c_str()) +
"\n";
456 nodeName += QFile::encodeName(studyDescription_deprecated->
GetValueAsString().c_str()) +
"\n";
457 nodeName += QFile::encodeName(seriesDescription_deprecated->
GetValueAsString().c_str());
462 nodeName = QFile::encodeName(dataNode->
GetName().c_str());
464 if (nodeName.isEmpty())
466 nodeName =
"unnamed";
469 if (role == Qt::DisplayRole)
471 else if (role == Qt::ToolTipRole)
473 else if (role == Qt::DecorationRole)
476 return nodeDescriptor->
GetIcon();
478 else if (role == Qt::CheckStateRole)
488 return QVariant::fromValue<mitk::DataNode *>(dataNode);
494 bool QmitkDataStorageTreeModel::DicomPropertiesExists(
const mitk::DataNode &node)
const
496 bool propertiesExists =
false;
507 if (patientsName != NULL && studyDescription != NULL && seriesDescription != NULL)
512 propertiesExists =
true;
517 if (patientsName_deprecated != NULL && studyDescription_deprecated != NULL && seriesDescription_deprecated != NULL)
523 propertiesExists =
true;
527 return propertiesExists;
532 if (orientation == Qt::Horizontal && role == Qt::DisplayRole &&
m_Root)
569 rootDataNode->SetName(
"Data Manager");
571 this->beginResetModel();
572 this->endResetModel();
605 void QmitkDataStorageTreeModel::AddNodeInternal(
const mitk::DataNode *node)
611 TreeItem *parentTreeItem =
m_Root;
617 parentTreeItem =
m_Root->
Find(parentDataNode);
621 parentTreeItem =
m_Root->
Find(parentDataNode);
627 index = this->createIndex(parentTreeItem->GetIndex(), 0, parentTreeItem);
634 beginInsertRows(index, 0, 0);
635 parentTreeItem->InsertChild(
new TreeItem(const_cast<mitk::DataNode *>(node)), 0);
639 beginInsertRows(index, parentTreeItem->GetChildCount(), parentTreeItem->GetChildCount());
640 new TreeItem(const_cast<mitk::DataNode *>(node), parentTreeItem);
655 this->AddNodeInternal(node);
663 void QmitkDataStorageTreeModel::RemoveNodeInternal(
const mitk::DataNode *node)
672 TreeItem *parentTreeItem = treeItem->
GetParent();
676 this->beginRemoveRows(parentIndex, treeItem->GetIndex(), treeItem->GetIndex());
679 std::vector<TreeItem *> children = treeItem->GetChildren();
686 for (std::vector<TreeItem *>::iterator it = children.begin(); it != children.end(); it++)
689 beginInsertRows(parentIndex, parentTreeItem->GetChildCount(), parentTreeItem->GetChildCount());
692 parentTreeItem->AddChild(*it);
706 this->RemoveNodeInternal(node);
718 QModelIndex index = this->createIndex(treeItem->
GetIndex(), 0, treeItem);
721 emit dataChanged(index, index);
731 if (_Sources->Size() > 0)
732 dataNode = _Sources->front();
743 if (role == Qt::EditRole && !value.toString().isEmpty())
749 if (planarFigure != NULL)
752 else if (role == Qt::CheckStateRole)
757 QVariant qcheckstate = index.data(Qt::CheckStateRole);
758 int checkstate = qcheckstate.toInt();
759 bool isVisible = bool(checkstate);
764 emit dataChanged(index, index);
779 std::vector<TreeItem *> vec;
782 int i = vec.size() - 1;
783 for (std::vector<TreeItem *>::const_iterator it = vec.begin(); it != vec.end(); ++it)
786 bool fixedLayer =
false;
788 if (!(dataNode->GetBoolProperty(
"fixedLayer", fixedLayer) && fixedLayer))
789 dataNode->SetIntProperty(
"layer", i);
803 vec.push_back(current);
810 return QModelIndex();
812 return this->createIndex(item->
GetIndex(), 0, item);
817 QList<mitk::DataNode::Pointer> res;
843 return QModelIndex();
850 return QList<TreeItem *>();
857 QList<TreeItem *> result;
861 quintptr treeItemPtr;
863 result.push_back(reinterpret_cast<TreeItem *>(treeItemPtr));
869 : m_Parent(_Parent), m_DataNode(_DataNode)
878 m_Parent->RemoveChild(
this);
883 while (m_Children.size() > 0)
884 delete m_Children.back();
894 if (m_DataNode == _DataNode)
895 item =
const_cast<TreeItem *
>(
this);
898 for (std::vector<TreeItem *>::const_iterator it = m_Children.begin(); it != m_Children.end(); ++it)
902 item = (*it)->
Find(_DataNode);
911 std::vector<TreeItem *>::const_iterator it = std::find(m_Children.begin(), m_Children.end(), item);
912 return it != m_Children.end() ? std::distance(m_Children.begin(), it) : -1;
917 return (m_Children.size() > 0 && index >= 0 && index < (int)m_Children.size()) ? m_Children.at(index) : 0;
922 this->InsertChild(item);
927 std::vector<TreeItem *>::iterator it = std::find(m_Children.begin(), m_Children.end(), item);
928 if (it != m_Children.end())
930 m_Children.erase(it);
937 return m_Children.size();
943 return m_Parent->IndexOfChild(
this);
960 std::vector<TreeItem *>::iterator it = std::find(m_Children.begin(), m_Children.end(), item);
961 if (it == m_Children.end())
963 if (m_Children.size() > 0 && index >= 0 && index < (int)m_Children.size())
965 it = m_Children.begin();
966 std::advance(it, index);
967 m_Children.insert(it, item);
970 m_Children.push_back(item);
994 this->beginResetModel();
995 this->endResetModel();
999 for (mitk::DataStorage::SetOfObjects::const_iterator it = _NodeSet->begin(); it != _NodeSet->end(); it++)
1002 this->AddNodeInternal(*it);
static QMimeData * mimeDataFromModelIndexList(const QModelIndexList &indexes)
void RemoveListener(const AbstractDelegate &delegate) const
virtual void Add(mitk::DataNode *node, const mitk::DataStorage::SetOfObjects *parents=nullptr)=0
Adds a DataNode containing a data object to its internal storage.
virtual bool InitializeViews(const BaseGeometry *geometry, RequestType type=REQUEST_UPDATE_ALL, bool preserveRoughOrientationInWorldSpace=false)
void AddListener(const AbstractDelegate &delegate) const
void SetDataStorage(mitk::DataStorage *_DataStorage)
virtual void SetNodeModified(const mitk::DataNode *node)
int IndexOfChild(const TreeItem *item) const
Data management class that handles 'was created by' relations.
QmitkNodeQmitkNodeDescriptor is Decorator class for the mitk::DataNode which enhances certain mitk::D...
void SetVisibility(bool visible, const mitk::BaseRenderer *renderer=nullptr, const char *propertyKey="visible")
Convenience method for setting visibility properties (instances of BoolProperty)
QModelIndex parent(const QModelIndex &index) const override
int columnCount(const QModelIndex &parent=QModelIndex()) const override
bool m_PlaceNewNodesOnTop
void AdjustLayerProperty()
void SetAllowHierarchyChange(bool allowHierarchyChange)
Set whether to allow hierarchy changes by dragging and dropping.
bool GetName(std::string &nodeName, const mitk::BaseRenderer *renderer=nullptr, const char *propertyKey="name") const
Convenience access method for accessing the name of an object (instance of StringProperty with proper...
itkObjectEvent ObjectDelete
AddEvent is emitted when the object pointed to gets deleted.
std::string MITKCORE_EXPORT GeneratePropertyNameForDICOMTag(unsigned int group, unsigned int element)
void TreeToNodeSet(TreeItem *parent, QList< mitk::DataNode::Pointer > &vec) const
Qt::DropActions supportedDropActions() const override
static QList< mitk::DataNode * > ToDataNodePtrList(const QByteArray &ba)
bool m_BlockDataStorageEvents
Flag to block the data storage events if nodes are added/removed by this class.
BaseData * GetData() const
Get the data object (instance of BaseData, e.g., an Image) managed by this DataNode.
bool m_AllowHierarchyChange
Qt::ItemFlags flags(const QModelIndex &index) const override
static const QString DataNodePtrs
mitk::BaseProperty * GetProperty(const char *propertyKey, const mitk::BaseRenderer *renderer=nullptr) const
Get the property (instance of BaseProperty) with key propertyKey from the PropertyList of the rendere...
void InsertChild(TreeItem *item, int index=-1)
TreeItem * TreeItemFromIndex(const QModelIndex &index) const
void SetParent(TreeItem *_Parent)
Sets the parent on the treeitem.
virtual void AddNode(const mitk::DataNode *node)
itk::SmartPointer< const Self > ConstPointer
void SetPlaceNewNodesOnTop(bool _PlaceNewNodesOnTop)
virtual SetOfObjects::ConstPointer GetSources(const mitk::DataNode *node, const NodePredicateBase *condition=nullptr, bool onlyDirectSources=true) const =0
returns a set of source objects for a given node that meet the given condition(s).
ObjectType * GetPointer() const
bool IsVisible(const mitk::BaseRenderer *renderer, const char *propertyKey="visible", bool defaultIsOn=true) const
Convenience access method for visibility properties (instances of BoolProperty). Return value is the ...
itk::SmartPointer< Self > Pointer
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
virtual SetOfObjects::ConstPointer GetAll() const =0
returns a set of all data objects that are stored in the data storage
Abstract base class for properties.
bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role=Qt::EditRole) override
mitk::DataStorage::Pointer m_DataStorage
QModelIndex IndexFromTreeItem(TreeItem *) const
SetOfObjects::ConstPointer GetSubset(const NodePredicateBase *condition) const
returns a set of data objects that meet the given condition(s)
static RenderingManager * GetInstance()
mitk::WeakPointer< mitk::DataStorage > m_DataStorage
TreeItem(mitk::DataNode *_DataNode, TreeItem *_Parent=0)
virtual QIcon GetIcon() const
virtual std::string GetValueAsString() const
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
~QmitkDataStorageTreeModel()
int rowCount(const QModelIndex &parent=QModelIndex()) const override
DataStorageEvent AddNodeEvent
AddEvent is emitted whenever a new node has been added to the DataStorage.
mitk::DataNode::Pointer GetNode(const QModelIndex &index) const
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
mitk::DataNode::Pointer GetDataNode() const
QmitkNodeDescriptor * GetDescriptor(const mitk::DataNode *_Node) const
void RemoveChild(TreeItem *item)
void SetDataStorageDeleted(const itk::Object *_DataStorage)
DataStorageEvent RemoveNodeEvent
RemoveEvent is emitted directly before a node is removed from the DataStorage.
virtual QList< mitk::DataNode::Pointer > GetNodeSet() const
std::vector< TreeItem * > GetChildren() const
void AddChild(TreeItem *item)
const mitk::DataStorage::Pointer GetDataStorage() const
mitk::NodePredicateBase::Pointer m_Predicate
void SetStringProperty(const char *propertyKey, const char *string, const mitk::BaseRenderer *renderer=nullptr)
Convenience method for setting string properties (instances of StringProperty)
DataStorageEvent ChangedNodeEvent
ChangedEvent is emitted directly after a node was changed.
virtual void RemoveNode(const mitk::DataNode *node)
void TreeToVector(TreeItem *parent, std::vector< TreeItem * > &vec) const
Qt::DropActions supportedDragActions() const override
int GetChildCount() const
QModelIndex GetIndex(const mitk::DataNode *) const
QList< TreeItem * > ToTreeItemPtrList(const QMimeData *mimeData)
QStringList mimeTypes() const override
virtual bool Exists(const mitk::DataNode *node) const =0
Checks if a node exists in the DataStorage.
virtual void Remove(const mitk::DataNode *node)=0
Removes node from the DataStorage.
TreeItem * GetParent() const
TreeItem * GetChild(int index) const
QMimeData * mimeData(const QModelIndexList &indexes) const override
TreeItem * Find(const mitk::DataNode *_DataNode) const
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
mitk::DataNode * GetParentNode(const mitk::DataNode *node) const
void RequestUpdateAll(RequestType type=REQUEST_UPDATE_ALL)
static QmitkNodeDescriptorManager * GetInstance()
Class for nodes of the DataTree.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
QmitkDataStorageTreeModel(mitk::DataStorage *_DataStorage, bool _PlaceNewNodesOnTop=false, QObject *parent=0)
static const QString DataStorageTreeItemPtrs