Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
QmitkStatisticsModelToStringConverter.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 
14 #include "mitkExceptionMacro.h"
15 
17 
19 {
20  m_statisticsModel = model;
21 }
22 
24 {
25  m_rootIndex = rootIndex;
26 }
27 
29 {
30  if (m_statisticsModel == nullptr)
31  {
32  mitkThrow() << "Cannot convert TableModel to String: TableModel is nullptr";
33  }
34 
35  QString textData;
36  int columns = m_statisticsModel->columnCount();
37 
38  if (m_includeHeaderData)
39  {
40  for (int i = 0; i < columns; i++)
41  {
42  if (i > 0)
43  {
44  textData += m_columnDelimiterWithSpace;
45  }
46  textData += m_statisticsModel->headerData(i, Qt::Horizontal).toString();
47  }
48  textData += m_lineDelimiter;
49  }
50  textData += Iterate(m_rootIndex, m_statisticsModel);
51 
52  return textData;
53 }
54 
56 {
57  m_lineDelimiter = lineDelimiter;
58 }
59 
61 {
62  m_columnDelimiterWithSpace = columnDelimiter + QString(" ");
63 }
64 
66 {
67  m_includeHeaderData = includeHeaderData;
68 }
69 
70 QString QmitkStatisticsModelToStringConverter::Iterate(const QModelIndex &index,
71  const QAbstractItemModel *model,
72  int depth) const
73 {
74  QString content;
75  if (index.isValid())
76  {
77  content = index.data().toString();
78  }
79  if (!model->hasChildren(index) || (index.flags() & Qt::ItemNeverHasChildren))
80  {
81  return content;
82  }
83  else
84  {
85  content += m_lineDelimiter;
86  }
87 
88  auto rows = model->rowCount(index);
89  auto cols = model->columnCount(index);
90  for (int i = 0; i < rows; ++i)
91  {
92  if (i > 0)
93  {
94  content += m_lineDelimiter;
95  }
96  for (int j = 0; j < cols; ++j)
97  {
98  if (j > 0)
99  {
100  content += m_columnDelimiterWithSpace;
101  }
102  content += Iterate(model->index(i, j, index), model, depth + 1);
103  }
104  }
105  return content;
106 }
#define mitkThrow()
void SetIncludeHeaderData(bool includeHeaderData)
If header data (column/row captions) are exported.