Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
QmitkPropertyItem.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include "QmitkPropertyItem.h"
18 #include <QStringList>
19 
20 QmitkPropertyItem::QmitkPropertyItem(const QList<QVariant> &data) : m_Data(data), m_Parent(nullptr)
21 {
22 }
23 
25 {
26  qDeleteAll(m_Children);
27 }
28 
30 {
31  if (child == nullptr)
32  return;
33 
34  // If property name doesn't contain period(s), append property directly, but...
35  if (!child->GetData(0).toString().contains('.'))
36  {
37  // ... if node already exists without property, just attach it.
38  bool nodeAlreadyExists = false;
39 
40  for (int i = 0; i < m_Children.count(); ++i)
41  {
42  if (m_Children[i]->GetData(0).toString() == child->GetData(0).toString())
43  {
44  if (m_Children[i]->GetData(1).isNull() && child->GetData(1).isValid())
45  m_Children[i]->m_Data[1] = child->m_Data[1];
46 
47  nodeAlreadyExists = true;
48  break;
49  }
50  }
51 
52  if (!nodeAlreadyExists)
53  {
54  m_Children.append(child);
55  child->m_Parent = this;
56  }
57  }
58  else
59  {
60  // Property name contains period(s). Split accordingly.
61  QStringList names = child->GetData(0).toString().split('.');
62 
63  // Traverse subtree and insert nodes if not already present.
64  QmitkPropertyItem *currentParent = this;
65 
66  for (int i = 0; i < names.count(); ++i)
67  {
68  if (i != names.count() - 1)
69  {
70  QmitkPropertyItem *currentChild = nullptr;
71 
72  // Search for current node.
73  for (int j = 0; j < currentParent->m_Children.count(); ++j)
74  {
75  if (currentParent->m_Children[j]->GetData(0).toString() == names[i])
76  {
77  currentChild = currentParent->m_Children[j];
78  break;
79  }
80  }
81 
82  // Node doesn't exist. Create and append it.
83  if (currentChild == nullptr)
84  {
85  QList<QVariant> data;
86  data << names[i] << QVariant();
87 
88  currentChild = new QmitkPropertyItem(data);
89  currentParent->AppendChild(currentChild);
90  }
91 
92  currentParent = currentChild;
93  }
94  else
95  {
96  // Subtree already present, append property as leaf node.
97  QList<QVariant> data;
98  data << names[i] << child->m_Data[1];
99 
100  currentParent->AppendChild(new QmitkPropertyItem(data));
101 
102  delete child;
103  child = nullptr;
104  }
105  }
106  }
107 }
108 
110 {
111  return m_Children.value(row);
112 }
113 
115 {
116  return m_Children.count();
117 }
118 
120 {
121  return m_Data.count();
122 }
123 
124 QVariant QmitkPropertyItem::GetData(int column) const
125 {
126  return m_Data.value(column);
127 }
128 
130 {
131  return m_Parent;
132 }
133 
135 {
136  if (m_Parent != nullptr)
137  return m_Parent->m_Children.indexOf(this);
138 
139  return 0;
140 }
int GetColumnCount() const
void AppendChild(QmitkPropertyItem *child)
int GetChildCount() const
QmitkPropertyItem(const QList< QVariant > &data)
QVariant GetData(int column) const
QmitkPropertyItem * GetChild(int row) const
QmitkPropertyItem * GetParent() const