Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
QmitkFitPlotDataWidget.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 "QmitkFitPlotDataWidget.h"
14 #include "QmitkFitPlotDataModel.h"
16 
17 #include <QClipboard>
18 #include <QFileDialog>
19 #include <QMessageBox>
20 
22 {
23  this->m_Controls.setupUi(this);
24 
26  m_Controls.tablePlotData->setModel(m_InternalModel);
27 
28  connect(m_Controls.btnCopyResultsToClipboard, SIGNAL(clicked()), this, SLOT(OnClipboardResultsButtonClicked()));
29  connect(m_Controls.btnSaveToFile, SIGNAL(clicked()), this, SLOT(OnExportClicked()));
30 }
31 
34 GetPlotData() const
35 {
36  return m_InternalModel->GetPlotData();
37 };
38 
39 void
42 {
44 };
45 
46 const std::string&
48 GetXName() const
49 {
50  return m_InternalModel->GetXName();
51 };
52 
53 void
55 SetXName(const std::string& xName)
56 {
57  m_InternalModel->SetXName(xName);
58 };
59 
61 {
62 }
63 
65 {
66  std::ostringstream stream;
67  stream.imbue(std::locale("C"));
68 
69  //head line
70  const auto colCount = this->m_InternalModel->columnCount();
71  for (int col = 0; col < colCount; ++col)
72  {
73  if (col != 0)
74  {
75  stream << ",";
76  }
77  stream << SanatizeString(m_InternalModel->headerData(col, Qt::Horizontal, Qt::DisplayRole).toString().toStdString());
78  }
79  stream << std::endl;
80 
81  //content
82  const auto rowCount = this->m_InternalModel->rowCount();
83  for (int row = 0; row < rowCount; ++row)
84  {
85  for (int col = 0; col < colCount; ++col)
86  {
87  QModelIndex index = this->m_InternalModel->index(row, col);
88  if (col != 0)
89  {
90  stream << ",";
91  }
92  stream << SanatizeString(m_InternalModel->data(index, Qt::DisplayRole).toString().toStdString());
93  }
94  stream << std::endl;
95  }
96 
97  return stream.str();
98 }
99 
101 {
102  QApplication::clipboard()->setText(QString::fromStdString(this->StreamModelToString()), QClipboard::Clipboard);
103 
104 }
105 
107 {
108  QString fileName = QFileDialog::getSaveFileName(nullptr, tr("Save plot data to csv file"));
109 
110  if (fileName.isEmpty())
111  {
112  QMessageBox::critical(nullptr, tr("No file selected!"),
113  tr("Cannot export pixel dump. Please selected a file."));
114  }
115  else
116  {
117  std::ofstream file;
118 
119  std::ios_base::openmode iOpenFlag = std::ios_base::out | std::ios_base::trunc;
120  file.open(fileName.toStdString().c_str(), iOpenFlag);
121 
122  if (!file.is_open())
123  {
124  QMessageBox::critical(nullptr, tr("Cannot create/open selected file!"),
125  tr("Cannot open or create the selected file. Export will be aborted. Selected file name: ") +
126  fileName);
127  return;
128  }
129 
130  file << this->StreamModelToString();
131 
132  file.close();
133  }
134 
135 }
void SetPlotData(const mitk::ModelFitPlotData *data)
int rowCount(const QModelIndex &parent=QModelIndex()) const override
QmitkFitPlotDataWidget(QWidget *parent=nullptr)
void OnClipboardResultsButtonClicked() const
Saves the results table to clipboard.
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
std::string StreamModelToString() const
const mitk::ModelFitPlotData * GetPlotData() const
std::string SanatizeString(std::string str)
int columnCount(const QModelIndex &parent=QModelIndex()) const override
const std::string & GetXName() const
const std::string & GetXName() const
void SetPlotData(const mitk::ModelFitPlotData *data)
Ui::QmitkFitPlotDataWidget m_Controls
void SetXName(const std::string &xName)
QVariant data(const QModelIndex &index, int role) const override
QmitkFitPlotDataModel * m_InternalModel
const mitk::ModelFitPlotData * GetPlotData() const
void SetXName(const std::string &xName)