1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
// ************************************************************************************************
//
// BornAgain: simulate and fit reflection and scattering
//
//! @file GUI/View/Plotter/SavePlot.cpp
//! @brief Implements function savePlot in namespace GUI::Plot.
//!
//! @homepage http://www.bornagainproject.org
//! @license GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2018
//! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
// ************************************************************************************************
#include "GUI/View/Plotter/SavePlot.h"
#include "Base/Util/Assert.h"
#include "Device/IO/IOFactory.h"
#include "GUI/View/Plotter/ColorMap.h"
#include "GUI/View/Widget/AppConfig.h"
#include "GUI/View/Widget/FileDialog.h"
#include <QFile>
#include <QMessageBox>
#include <QVector>
#include <qcustomplot.h>
#include <utility>
namespace {
const QString png_extension = ".png";
const QString jpg_extension = ".jpg";
const QString pdf_extension = ".pdf";
const QString int_extension = ".int";
const QString tif_extension = ".tif";
const QString txt_extension = ".txt";
class Format {
public:
Format() = default;
Format(const QString& file_extention, QString filter)
: m_file_extention(file_extention)
, m_filter(filter)
{
}
QString m_file_extention;
QString m_filter;
};
const QVector<Format> outFormats = {
Format(png_extension, "png Image (*.png)"),
Format(jpg_extension, "jpg Image (*.jpg)"),
Format(pdf_extension, "pdf File (*.pdf)"),
Format(int_extension, "BornAgain ASCII format (*.int)"),
Format(txt_extension, "Simple ASCII table (*.txt)"),
#ifdef BA_TIFF_SUPPORT
Format(tif_extension, "32-bits TIFF files (*.tif)"),
#endif
};
bool isPngFile(const QString& fname)
{
return fname.endsWith(png_extension, Qt::CaseInsensitive);
}
bool isJpgFile(const QString& fname)
{
return fname.endsWith(jpg_extension, Qt::CaseInsensitive);
}
bool isPdfFile(const QString& fname)
{
return fname.endsWith(pdf_extension, Qt::CaseInsensitive);
}
void saveToFile(const QString& fname, QCustomPlot* plot, const Datafield* output_data)
{
if (isPngFile(fname))
plot->savePng(fname);
else if (isJpgFile(fname))
plot->saveJpg(fname);
else if (isPdfFile(fname))
plot->savePdf(fname, plot->width(), plot->height());
else {
ASSERT(output_data);
IO::writeDatafield(*output_data, fname.toStdString());
}
}
} // namespace
void GUI::Plot::savePlot(QCustomPlot* plot, const Datafield* output_data)
{
static const QString defaultExtension = ".png";
QString fname =
GUI::FileDialog::w1_1f("Save plot", gApp->artifact_export_dir, "*" + defaultExtension);
if (fname.isEmpty())
return;
if (!fname.endsWith(defaultExtension))
fname += defaultExtension;
try {
::saveToFile(fname, plot, output_data);
} catch (const std::exception& ex) {
QMessageBox::warning(nullptr, "Cannot save", "Cannot save picture in file " + fname);
}
}
|