diff --git a/Modules/ImageStatisticsUI/Qmitk/QmitkStatisticsModelToStringConverter.cpp b/Modules/ImageStatisticsUI/Qmitk/QmitkStatisticsModelToStringConverter.cpp index 168c04cd40..6783735d7f 100644 --- a/Modules/ImageStatisticsUI/Qmitk/QmitkStatisticsModelToStringConverter.cpp +++ b/Modules/ImageStatisticsUI/Qmitk/QmitkStatisticsModelToStringConverter.cpp @@ -1,106 +1,106 @@ /*============================================================================ The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center (DKFZ) All rights reserved. Use of this source code is governed by a 3-clause BSD license that can be found in the LICENSE file. ============================================================================*/ #include "QmitkStatisticsModelToStringConverter.h" #include "mitkExceptionMacro.h" QmitkStatisticsModelToStringConverter::QmitkStatisticsModelToStringConverter() {} void QmitkStatisticsModelToStringConverter::SetTableModel(QAbstractItemModel *model) { m_statisticsModel = model; } void QmitkStatisticsModelToStringConverter::SetRootIndex(QModelIndex rootIndex) { m_rootIndex = rootIndex; } QString QmitkStatisticsModelToStringConverter::GetString() const { if (m_statisticsModel == nullptr) { mitkThrow() << "Cannot convert TableModel to String: TableModel is nullptr"; } QString textData; int columns = m_statisticsModel->columnCount(); if (m_includeHeaderData) { for (int i = 0; i < columns; i++) { if (i > 0) { - textData += m_columnDelimiterWithSpace; + textData += m_columnDelimiter; } textData += m_statisticsModel->headerData(i, Qt::Horizontal).toString(); } - textData += m_lineDelimiter; + textData += m_rowDelimiter; } textData += Iterate(m_rootIndex, m_statisticsModel); return textData; } -void QmitkStatisticsModelToStringConverter::SetRowDelimiter(QChar lineDelimiter) +void QmitkStatisticsModelToStringConverter::SetRowDelimiter(QChar rowDelimiter) { - m_lineDelimiter = lineDelimiter; + m_rowDelimiter = rowDelimiter; } void QmitkStatisticsModelToStringConverter::SetColumnDelimiter(QChar columnDelimiter) { - m_columnDelimiterWithSpace = columnDelimiter + QString(" "); + m_columnDelimiter = columnDelimiter; } void QmitkStatisticsModelToStringConverter::SetIncludeHeaderData(bool includeHeaderData) { m_includeHeaderData = includeHeaderData; } QString QmitkStatisticsModelToStringConverter::Iterate(const QModelIndex &index, const QAbstractItemModel *model, int depth) const { QString content; if (index.isValid()) { content = index.data().toString(); } if (!model->hasChildren(index) || (index.flags() & Qt::ItemNeverHasChildren)) { return content; } else { - content += m_lineDelimiter; + content += m_rowDelimiter; } auto rows = model->rowCount(index); auto cols = model->columnCount(index); for (int i = 0; i < rows; ++i) { if (i > 0) { - content += m_lineDelimiter; + content += m_rowDelimiter; } for (int j = 0; j < cols; ++j) { if (j > 0) { - content += m_columnDelimiterWithSpace; + content += m_columnDelimiter; } content += Iterate(model->index(i, j, index), model, depth + 1); } } return content; } diff --git a/Modules/ImageStatisticsUI/Qmitk/QmitkStatisticsModelToStringConverter.h b/Modules/ImageStatisticsUI/Qmitk/QmitkStatisticsModelToStringConverter.h index 51430815d8..9fb66e10ad 100644 --- a/Modules/ImageStatisticsUI/Qmitk/QmitkStatisticsModelToStringConverter.h +++ b/Modules/ImageStatisticsUI/Qmitk/QmitkStatisticsModelToStringConverter.h @@ -1,64 +1,66 @@ /*============================================================================ The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center (DKFZ) All rights reserved. Use of this source code is governed by a 3-clause BSD license that can be found in the LICENSE file. ============================================================================*/ #ifndef QmitkTableModelToQString_H__INCLUDED #define QmitkTableModelToQString_H__INCLUDED #include #include /** \brief Converts the content of the statistics model to a string \details The content of a table model is converted to a string. Each cell is converted to a string. Default -oder: iteration over rows. The line separation delimiter (default: '\n') and the column separation +order: iteration over rows. The row separation delimiter (default: '\n') and the column separation delimiter (default: ',') can be chosen. It also can be chosen if the headers should be exported to the string. - -By default, the produced string is csv conform */ class MITKIMAGESTATISTICSUI_EXPORT QmitkStatisticsModelToStringConverter { public: QmitkStatisticsModelToStringConverter(); void SetTableModel(QAbstractItemModel *model); void SetRootIndex(QModelIndex rootIndex); /** \brief Returns the string \exception throws exception if model is nullptr */ QString GetString() const; - void SetRowDelimiter(QChar lineDelimiter); + void SetRowDelimiter(QChar rowDelimiter); void SetColumnDelimiter(QChar columnDelimiter); /** \brief If header data (column/row captions) are exported */ void SetIncludeHeaderData(bool includeHeaderData); private: /** \brief Iterates recursively over all cells and returns their content \details based on https://stackoverflow.com/questions/39153835/how-to-loop-over-qabstractitemview-indexes */ QString Iterate(const QModelIndex &index, const QAbstractItemModel *model, int depth = 0) const; QAbstractItemModel *m_statisticsModel = nullptr; QModelIndex m_rootIndex; - QChar m_lineDelimiter = '\n'; + + QChar m_rowDelimiter = '\n'; + QChar m_columnDelimiter = ','; + bool m_includeHeaderData = false; - QString m_columnDelimiterWithSpace = ", "; + }; + #endif // QmitkTableModelToQString_H__INCLUDED