Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
QmitkFitParameterWidget.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 "QmitkFitParameterModel.h"
15 
16 #include <QClipboard>
17 #include <QFileDialog>
18 #include <QMessageBox>
19 
21 {
22  this->m_Controls.setupUi(this);
23 
25  m_Controls.tableFitParameter->setModel(m_InternalModel);
26 
27  connect(m_Controls.btnCopyResultsToClipboard, SIGNAL(clicked()), this, SLOT(OnClipboardResultsButtonClicked()));
28  connect(m_Controls.btnSaveToFile, SIGNAL(clicked()), this, SLOT(OnExportClicked()));
29 }
30 
33 getFits() const
34 {
35  return m_InternalModel->getFits();
36 };
37 
41 {
43 };
44 
45 const mitk::PointSet*
48 {
50 };
51 
52 void
54 setFits(const FitVectorType& fits)
55 {
56  m_InternalModel->setFits(fits);
57 };
58 
59 void
62 {
64 };
65 
66 void
69 {
71 };
72 
74 {
75 }
76 
77 std::string SanatizeString(std::string str)
78 {
79  std::replace(std::begin(str), std::end(str), ',', ' ');
80  std::replace(std::begin(str), std::end(str), '\n', ' ');
81  std::replace(std::begin(str), std::end(str), '\r', ' ');
82  return str;
83 }
84 
86 {
87  std::ostringstream stream;
88  stream.imbue(std::locale("C"));
89 
90  //head line
91  const auto colCount = this->m_InternalModel->columnCount();
92  for (int col = 0; col < colCount; ++col)
93  {
94  if (col != 0)
95  {
96  stream << ",";
97  }
98  stream << SanatizeString(m_InternalModel->headerData(col, Qt::Horizontal, Qt::DisplayRole).toString().toStdString());
99  }
100  stream << std::endl;
101 
102  //content
103  const auto rowCount = this->m_InternalModel->rowCount();
104  for (int row = 0; row < rowCount; ++row)
105  {
106  QModelIndex index = this->m_InternalModel->index(row, 0);
107 
108  const auto childCount = this->m_InternalModel->rowCount(index);
109 
110  if (childCount == 0)
111  {
112  for (int col = 0; col < colCount; ++col)
113  {
114  if (col != 0)
115  {
116  stream << ",";
117  }
118  stream << SanatizeString(m_InternalModel->data(index.siblingAtColumn(col), Qt::DisplayRole).toString().toStdString());
119  }
120  stream << std::endl;
121  }
122  else
123  {
124  mitkThrow() << "Missing implementation for multiple fits.";
125  //TODO FIT REFACTOR
126  }
127  }
128 
129  return stream.str();
130 }
131 
133 {
134  QApplication::clipboard()->setText(QString::fromStdString(this->streamModelToString()), QClipboard::Clipboard);
135 
136 }
137 
139 {
140  QString fileName = QFileDialog::getSaveFileName(nullptr, tr("Save fit parameter to csv file"));
141 
142  if (fileName.isEmpty())
143  {
144  QMessageBox::critical(nullptr, tr("No file selected!"),
145  tr("Cannot export pixel dump. Please selected a file."));
146  }
147  else
148  {
149  std::ofstream file;
150 
151  std::ios_base::openmode iOpenFlag = std::ios_base::out | std::ios_base::trunc;
152  file.open(fileName.toStdString().c_str(), iOpenFlag);
153 
154  if (!file.is_open())
155  {
156  QMessageBox::critical(nullptr, tr("Cannot create/open selected file!"),
157  tr("Cannot open or create the selected file. Export will be aborted. Selected file name: ") +
158  fileName);
159  return;
160  }
161 
162  file << this->streamModelToString();
163 
164  file.close();
165  }
166 
167 }
const FitVectorType & getFits() const
void setFits(const FitVectorType &fits)
void setCurrentPosition(const mitk::Point3D &currentPos)
void OnClipboardResultsButtonClicked() const
Saves the results table to clipboard.
int columnCount(const QModelIndex &parent=QModelIndex()) const override
int rowCount(const QModelIndex &parent=QModelIndex()) const override
std::string streamModelToString() const
const mitk::PointSet * getPositionBookmarks() const
const FitVectorType & getFits() const
void setFits(const FitVectorType &fits)
QmitkFitParameterModel * m_InternalModel
std::string SanatizeString(std::string str)
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
const mitk::PointSet * getPositionBookmarks() const
Data structure which stores a set of points. Superclass of mitk::Mesh.
Definition: mitkPointSet.h:75
#define mitkThrow()
void setCurrentPosition(const mitk::Point3D &currentPos)
mitk::Point3D getCurrentPosition() const
QmitkFitParameterWidget(QWidget *parent=nullptr)
void setPositionBookmarks(const mitk::PointSet *bookmarks)
std::vector< mitk::modelFit::ModelFitInfo::ConstPointer > FitVectorType
static const char * replace[]
This is a dictionary to replace long names of classes, modules, etc. to shorter versions in the conso...
mitk::Point3D getCurrentPosition() const
Ui::QmitkFitParameterWidget m_Controls
QVariant data(const QModelIndex &index, int role) const override
void setPositionBookmarks(const mitk::PointSet *bookmarks)