23 #include "Sim/Export/ExportToPython.h"
24 #include "Sim/Simulation/ScatteringSimulation.h"
25 #include <QFileDialog>
26 #include <QMessageBox>
27 #include <QPushButton>
28 #include <QStandardPaths>
31 #include <QTextStream>
33 #include <QVBoxLayout>
38 , m_textEdit(new QTextEdit)
41 setObjectName(
"PythonScriptWidget");
42 setWindowTitle(
"Python Script");
43 setMinimumSize(128, 128);
45 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
48 toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
50 auto* exportAction =
new QAction(
this);
51 exportAction->setIcon(QIcon(
":/images/content-save-outline.svg"));
52 exportAction->setText(
"Save to file");
53 exportAction->setToolTip(
"Opens dialog to save given script into the file");
56 toolbar->addAction(exportAction);
59 QFont textFont(
"Monospace");
62 m_textEdit->setLineWrapMode(QTextEdit::NoWrap);
64 Q_UNUSED(highlighter);
66 auto* mainLayout =
new QVBoxLayout;
67 mainLayout->setContentsMargins(0, 0, 0, 0);
68 mainLayout->setMargin(0);
69 mainLayout->setSpacing(0);
70 mainLayout->addWidget(toolbar);
73 setLayout(mainLayout);
75 setAttribute(Qt::WA_DeleteOnClose,
true);
89 const QString& outputDir)
95 const auto simulation =
98 QString code = QString::fromStdString(Py::Export::simulationPlotCode(*simulation));
101 }
catch (
const std::exception& ex) {
104 "Generation of Python Script failed. Code is not complete.\n\n"
105 "It can happen if sample requires further assembling or some of sample parameters "
106 "are not valid. See details below.\n\n%1")
107 .arg(QString::fromStdString(ex.what()));
116 if (dirname.isEmpty())
117 dirname = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
119 QString filters(
"Python scripts (*.py)");
120 QString defaultFilter(
"Python scripts (*.py)");
121 QString defaultName = dirname +
"/untitled";
123 QString fileName = QFileDialog::getSaveFileName(
124 nullptr,
"Save file", defaultName, filters, &defaultFilter,
126 : QFileDialog::DontUseNativeDialog);
128 if (fileName.isEmpty())
131 if (!fileName.endsWith(
".py"))
134 QFile file(fileName);
135 if (!file.open(QIODevice::WriteOnly)) {
139 QTextStream out(&file);
ApplicationSettings * appSettings
global pointer to the instance
Defines class ApplicationSettings.
Defines class CautionSign.
Defines class DesignerHelper.
Defines class PythonSyntaxHighlighter.
Defines namespace GUI::Transform::ToCore.
Defines GUI::StyleUtils namespace.
void loadWindowSizeAndPos(QWidget *w)
void saveWindowSizeAndPos(const QWidget *w)
bool useNativeFileDialog() const
The CautionSign controls appearance of CautionSignWidget on top of parent widget.
void clear()
Clears caution message;.
void setCautionMessage(const QString &cautionMessage)
Shows caution sign on the screen. If clear of previous caution sign had happened just few msec ago,...
static int getPythonEditorFontSize()
Abstract base class for instrument-specific item classes.
Implementation of highlighting for Python code.
The SimulationOptionsItem class holds simulation status (run policy, number of threads,...
void setResizable(QDialog *dialog)
Make modal dialog resizable.
void warning(QWidget *parent, const QString &title, const QString &text, const QString &detailedText)