16 #include "Base/Util/Assert.h"
17 #include "Device/IO/IOFactory.h"
20 #include <QFileDialog>
21 #include <QMessageBox>
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";
37 Format(QString file_extention, QString filter);
38 QString m_file_extention;
42 Format::Format(QString file_extention, QString filter)
43 : m_file_extention(std::move(file_extention))
44 , m_filter(std::move(filter))
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)"),
59 bool isPngFile(
const QString& fileName)
61 return fileName.endsWith(png_extension, Qt::CaseInsensitive);
64 bool isJpgFile(
const QString& fileName)
66 return fileName.endsWith(jpg_extension, Qt::CaseInsensitive);
69 bool isPdfFile(
const QString& fileName)
71 return fileName.endsWith(pdf_extension, Qt::CaseInsensitive);
74 void saveToFile(
const QString& fileName, QCustomPlot* plot, Datafield* output_data)
76 if (isPngFile(fileName))
77 plot->savePng(fileName);
79 else if (isJpgFile(fileName))
80 plot->saveJpg(fileName);
82 else if (isPdfFile(fileName))
83 plot->savePdf(fileName, plot->width(), plot->height());
87 IOFactory::writeDatafield(*output_data, fileName.toStdString());
92 QString getFilterString()
95 for (
int i = 0; i < outFormats.size(); ++i) {
96 result.append(outFormats[i].m_filter);
97 if (i != outFormats.size() - 1)
103 bool isValidExtension(
const QString& fileName)
105 for (
int i = 0; i < outFormats.size(); ++i)
106 if (fileName.endsWith(outFormats[i].m_file_extention, Qt::CaseInsensitive))
111 QString getExtensionFromFilterName(
const QString& filterName)
113 for (
int i = 0; i < outFormats.size(); ++i)
114 if (outFormats[i].m_filter == filterName)
115 return outFormats[i].m_file_extention;
120 QString composeFileName(
const QString& fileName,
const QString& filterName)
122 if (fileName.isEmpty() || filterName.isEmpty())
124 if (isValidExtension(fileName))
126 return fileName + getExtensionFromFilterName(filterName);
135 QString selectedFilter(
"*.png");
136 QString defaultName = dirname +
"/untitled";
137 QString fileName = QFileDialog::getSaveFileName(
138 nullptr,
"Save Plot", defaultName, getFilterString(), &selectedFilter,
140 : QFileDialog::DontUseNativeDialog);
142 QString nameToSave = composeFileName(fileName, selectedFilter);
144 if (!nameToSave.isEmpty()) {
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()));
ApplicationSettings * appSettings
global pointer to the instance
Defines class ApplicationSettings.
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)