Medical Imaging Interaction Toolkit  2018.4.99-389bf124
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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include "QmitkPropertyItem.h"
14 #include <QStringList>
15 
16 QmitkPropertyItem::QmitkPropertyItem(const QList<QVariant> &data) : m_Data(data), m_Parent(nullptr)
17 {
18 }
19 
21 {
22  qDeleteAll(m_Children);
23 }
24 
26 {
27  if (child == nullptr)
28  return;
29 
30  // If property name doesn't contain period(s), append property directly, but...
31  if (!child->GetData(0).toString().contains('.'))
32  {
33  // ... if node already exists without property, just attach it.
34  bool nodeAlreadyExists = false;
35 
36  for (int i = 0; i < m_Children.count(); ++i)
37  {
38  if (m_Children[i]->GetData(0).toString() == child->GetData(0).toString())
39  {
40  if (m_Children[i]->GetData(1).isNull() && child->GetData(1).isValid())
41  m_Children[i]->m_Data[1] = child->m_Data[1];
42 
43  nodeAlreadyExists = true;
44  break;
45  }
46  }
47 
48  if (!nodeAlreadyExists)
49  {
50  m_Children.append(child);
51  child->m_Parent = this;
52  }
53  }
54  else
55  {
56  // Property name contains period(s). Split accordingly.
57  QStringList names = child->GetData(0).toString().split('.');
58 
59  // Traverse subtree and insert nodes if not already present.
60  QmitkPropertyItem *currentParent = this;
61 
62  for (int i = 0; i < names.count(); ++i)
63  {
64  if (i != names.count() - 1)
65  {
66  QmitkPropertyItem *currentChild = nullptr;
67 
68  // Search for current node.
69  for (int j = 0; j < currentParent->m_Children.count(); ++j)
70  {
71  if (currentParent->m_Children[j]->GetData(0).toString() == names[i])
72  {
73  currentChild = currentParent->m_Children[j];
74  break;
75  }
76  }
77 
78  // Node doesn't exist. Create and append it.
79  if (currentChild == nullptr)
80  {
81  QList<QVariant> data;
82  data << names[i] << QVariant();
83 
84  currentChild = new QmitkPropertyItem(data);
85  currentParent->AppendChild(currentChild);
86  }
87 
88  currentParent = currentChild;
89  }
90  else
91  {
92  // Subtree already present, append property as leaf node.
93  QList<QVariant> data;
94  data << names[i] << child->m_Data[1];
95 
96  currentParent->AppendChild(new QmitkPropertyItem(data));
97 
98  delete child;
99  child = nullptr;
100  }
101  }
102  }
103 }
104 
106 {
107  return m_Children.value(row);
108 }
109 
111 {
112  return m_Children.count();
113 }
114 
116 {
117  return m_Data.count();
118 }
119 
120 QVariant QmitkPropertyItem::GetData(int column) const
121 {
122  return m_Data.value(column);
123 }
124 
126 {
127  return m_Parent;
128 }
129 
131 {
132  if (m_Parent != nullptr)
133  return m_Parent->m_Children.indexOf(this);
134 
135  return 0;
136 }
void AppendChild(QmitkPropertyItem *child)
QmitkPropertyItem * GetChild(int row) const
QmitkPropertyItem * GetParent() const
QmitkPropertyItem(const QList< QVariant > &data)
QVariant GetData(int column) const