BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
SavePlotAssistant.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/PlotUtil/SavePlotAssistant.cpp
6 //! @brief Implements class SavePlotAssistant
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
16 #include "Base/Util/Assert.h"
17 #include "Device/IO/IOFactory.h"
20 #include <QFileDialog>
21 #include <QMessageBox>
22 #include <QVector>
23 #include <utility>
24 
25 namespace {
26 
27 const QString png_extension = ".png";
28 const QString jpg_extension = ".jpg";
29 const QString pdf_extension = ".pdf";
30 const QString int_extension = ".int";
31 const QString tif_extension = ".tif";
32 const QString txt_extension = ".txt";
33 
34 class Format {
35 public:
36  Format() = default;
37  Format(QString file_extention, QString filter);
38  QString m_file_extention;
39  QString m_filter;
40 };
41 
42 Format::Format(QString file_extention, QString filter)
43  : m_file_extention(std::move(file_extention))
44  , m_filter(std::move(filter))
45 {
46 }
47 
48 const QVector<Format> outFormats = {
49  Format(png_extension, "png Image (*.png)"),
50  Format(jpg_extension, "jpg Image (*.jpg)"),
51  Format(pdf_extension, "pdf File (*.pdf)"),
52  Format(int_extension, "BornAgain ASCII format (*.int)"),
53  Format(txt_extension, "Simple ASCII table (*.txt)"),
54 #ifdef BA_TIFF_SUPPORT
55  Format(tif_extension, "32-bits TIFF files (*.tif)"),
56 #endif
57 };
58 
59 bool isPngFile(const QString& fileName)
60 {
61  return fileName.endsWith(png_extension, Qt::CaseInsensitive);
62 }
63 
64 bool isJpgFile(const QString& fileName)
65 {
66  return fileName.endsWith(jpg_extension, Qt::CaseInsensitive);
67 }
68 
69 bool isPdfFile(const QString& fileName)
70 {
71  return fileName.endsWith(pdf_extension, Qt::CaseInsensitive);
72 }
73 
74 void saveToFile(const QString& fileName, QCustomPlot* plot, Datafield* output_data)
75 {
76  if (isPngFile(fileName))
77  plot->savePng(fileName);
78 
79  else if (isJpgFile(fileName))
80  plot->saveJpg(fileName);
81 
82  else if (isPdfFile(fileName))
83  plot->savePdf(fileName, plot->width(), plot->height());
84 
85  else {
86  ASSERT(output_data);
87  IOFactory::writeDatafield(*output_data, fileName.toStdString());
88  }
89 }
90 
91 //! Returns string contraining all defined filters in the format suitable for QFileDialog
92 QString getFilterString()
93 {
94  QString result;
95  for (int i = 0; i < outFormats.size(); ++i) {
96  result.append(outFormats[i].m_filter);
97  if (i != outFormats.size() - 1)
98  result.append(";;");
99  }
100  return result;
101 }
102 
103 bool isValidExtension(const QString& fileName)
104 {
105  for (int i = 0; i < outFormats.size(); ++i)
106  if (fileName.endsWith(outFormats[i].m_file_extention, Qt::CaseInsensitive))
107  return true;
108  return false;
109 }
110 
111 QString getExtensionFromFilterName(const QString& filterName)
112 {
113  for (int i = 0; i < outFormats.size(); ++i)
114  if (outFormats[i].m_filter == filterName)
115  return outFormats[i].m_file_extention;
116  return "";
117 }
118 
119 //! Compose file name to save plot from information provided by QFileDialog
120 QString composeFileName(const QString& fileName, const QString& filterName)
121 {
122  if (fileName.isEmpty() || filterName.isEmpty())
123  return "";
124  if (isValidExtension(fileName))
125  return fileName;
126  return fileName + getExtensionFromFilterName(filterName);
127 }
128 
129 } // namespace
130 
131 
132 void GUI::Plot::savePlot(const QString& dirname, QCustomPlot* plot, Datafield* output_data)
133 
134 {
135  QString selectedFilter("*.png");
136  QString defaultName = dirname + "/untitled";
137  QString fileName = QFileDialog::getSaveFileName(
138  nullptr, "Save Plot", defaultName, getFilterString(), &selectedFilter,
139  appSettings->useNativeFileDialog() ? QFileDialog::Options()
140  : QFileDialog::DontUseNativeDialog);
141 
142  QString nameToSave = composeFileName(fileName, selectedFilter);
143 
144  if (!nameToSave.isEmpty()) {
145  try {
146  saveToFile(nameToSave, plot, output_data);
147  } catch (const std::exception& ex) {
148  QString message = "Attempt to save file with the name '";
149  message.append(nameToSave);
150  message.append("' has failed with following error message\n\n");
151  message.append(QString::fromStdString(ex.what()));
152  QMessageBox::warning(nullptr, "Houston, we have a problem.", message);
153  }
154  }
155 }
ApplicationSettings * appSettings
global pointer to the instance
Defines class ApplicationSettings.
Defines class ColorMap.
Defines class SavePlotAssistant.
bool useNativeFileDialog() const
void savePlot(const QString &dirname, QCustomPlot *plot, Datafield *output_data)
void warning(QWidget *parent, const QString &title, const QString &text, const QString &detailedText)
Definition: MessageBox.cpp:37