BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
PythonScriptWidget Class Reference

Description

The PythonScriptWidget displays a python script which represents full simulation. Part of SimulationSetupWidget.

Definition at line 29 of file PythonScriptWidget.h.

Inheritance diagram for PythonScriptWidget:
[legend]
Collaboration diagram for PythonScriptWidget:
[legend]

Public Member Functions

 PythonScriptWidget (QWidget *parent=nullptr)
 
 ~PythonScriptWidget () override
 
void generatePythonScript (const MultiLayerItem *sampleItem, const InstrumentItem *instrumentItem, const SimulationOptionsItem &optionItem, const QString &outputDir="")
 

Private Slots

void onExportToFileButton ()
 

Private Attributes

CautionSignm_cautionSign
 
QString m_outputDir
 
QTextEdit * m_textEdit
 

Constructor & Destructor Documentation

◆ PythonScriptWidget()

PythonScriptWidget::PythonScriptWidget ( QWidget *  parent = nullptr)

Definition at line 36 of file PythonScriptWidget.cpp.

37  : QDialog(parent)
38  , m_textEdit(new QTextEdit)
40 {
41  setObjectName("PythonScriptWidget");
42  setWindowTitle("Python Script");
43  setMinimumSize(128, 128);
44  resize(512, 400);
45  setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
46 
47  auto* toolbar = new StyledToolbar;
48  toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
49 
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");
54  connect(exportAction, &QAction::triggered, this, &PythonScriptWidget::onExportToFileButton);
55 
56  toolbar->addAction(exportAction);
57 
58  m_textEdit->setReadOnly(true);
59  QFont textFont("Monospace");
60  m_textEdit->setFont(textFont);
62  m_textEdit->setLineWrapMode(QTextEdit::NoWrap);
63  auto* highlighter = new PythonSyntaxHighlighter(m_textEdit->document());
64  Q_UNUSED(highlighter);
65 
66  auto* mainLayout = new QVBoxLayout;
67  mainLayout->setContentsMargins(0, 0, 0, 0);
68  mainLayout->setMargin(0);
69  mainLayout->setSpacing(0);
70  mainLayout->addWidget(toolbar);
71  mainLayout->addWidget(m_textEdit);
72 
73  setLayout(mainLayout);
74 
75  setAttribute(Qt::WA_DeleteOnClose, true);
78 }
ApplicationSettings * appSettings
global pointer to the instance
void loadWindowSizeAndPos(QWidget *w)
The CautionSign controls appearance of CautionSignWidget on top of parent widget.
Definition: CautionSign.h:25
static int getPythonEditorFontSize()
CautionSign * m_cautionSign
Implementation of highlighting for Python code.
The StyledToolbar class represents our standard narrow toolbar with the height 24 pixels.
Definition: StyledToolbar.h:22
void setResizable(QDialog *dialog)
Make modal dialog resizable.
Definition: StyleUtils.cpp:100

References appSettings, DesignerHelper::getPythonEditorFontSize(), ApplicationSettings::loadWindowSizeAndPos(), m_textEdit, onExportToFileButton(), and GUI::Util::Style::setResizable().

Here is the call graph for this function:

◆ ~PythonScriptWidget()

PythonScriptWidget::~PythonScriptWidget ( )
override

Definition at line 81 of file PythonScriptWidget.cpp.

82 {
84 }
void saveWindowSizeAndPos(const QWidget *w)

References appSettings, and ApplicationSettings::saveWindowSizeAndPos().

Here is the call graph for this function:

Member Function Documentation

◆ generatePythonScript()

void PythonScriptWidget::generatePythonScript ( const MultiLayerItem sampleItem,
const InstrumentItem instrumentItem,
const SimulationOptionsItem optionItem,
const QString &  outputDir = "" 
)

Definition at line 86 of file PythonScriptWidget.cpp.

90 {
91  m_outputDir = outputDir;
93 
94  try {
95  const auto simulation =
96  GUI::Transform::ToCore::itemsToSimulation(sampleItem, instrumentItem, optionItem);
97 
98  QString code = QString::fromStdString(Py::Export::simulationPlotCode(*simulation));
99  m_textEdit->clear();
100  m_textEdit->setText(code);
101  } catch (const std::exception& ex) {
102  QString message =
103  QString(
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()));
108 
110  }
111 }
void clear()
Clears caution message;.
Definition: CautionSign.cpp:42
void setCautionMessage(const QString &cautionMessage)
Shows caution sign on the screen. If clear of previous caution sign had happened just few msec ago,...
Definition: CautionSign.cpp:60
std::unique_ptr< ISimulation > itemsToSimulation(const MultiLayerItem *sampleItem, const InstrumentItem *instrumentItem, const SimulationOptionsItem &optionsItem)
Creates domain simulation from sample and instrument items.

References CautionSign::clear(), GUI::Transform::ToCore::itemsToSimulation(), m_cautionSign, m_outputDir, m_textEdit, and CautionSign::setCautionMessage().

Here is the call graph for this function:

◆ onExportToFileButton

void PythonScriptWidget::onExportToFileButton ( )
privateslot

Definition at line 113 of file PythonScriptWidget.cpp.

114 {
115  QString dirname(m_outputDir);
116  if (dirname.isEmpty())
117  dirname = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
118 
119  QString filters("Python scripts (*.py)");
120  QString defaultFilter("Python scripts (*.py)");
121  QString defaultName = dirname + "/untitled";
122 
123  QString fileName = QFileDialog::getSaveFileName(
124  nullptr, "Save file", defaultName, filters, &defaultFilter,
125  appSettings->useNativeFileDialog() ? QFileDialog::Options()
126  : QFileDialog::DontUseNativeDialog);
127 
128  if (fileName.isEmpty())
129  return;
130 
131  if (!fileName.endsWith(".py"))
132  fileName += ".py";
133 
134  QFile file(fileName);
135  if (!file.open(QIODevice::WriteOnly)) {
136  QMessageBox::warning(this, "Save Python file", "File could not be opened for writing!");
137  return;
138  }
139  QTextStream out(&file);
140  out << m_textEdit->toPlainText();
141  file.close();
142  raise();
143 }
bool useNativeFileDialog() const
void warning(QWidget *parent, const QString &title, const QString &text, const QString &detailedText)
Definition: MessageBox.cpp:37

References appSettings, m_outputDir, m_textEdit, ApplicationSettings::useNativeFileDialog(), and GUI::View::Helpers::warning().

Referenced by PythonScriptWidget().

Here is the call graph for this function:

Member Data Documentation

◆ m_cautionSign

CautionSign* PythonScriptWidget::m_cautionSign
private

Definition at line 46 of file PythonScriptWidget.h.

Referenced by generatePythonScript().

◆ m_outputDir

QString PythonScriptWidget::m_outputDir
private

Definition at line 47 of file PythonScriptWidget.h.

Referenced by generatePythonScript(), and onExportToFileButton().

◆ m_textEdit

QTextEdit* PythonScriptWidget::m_textEdit
private

The documentation for this class was generated from the following files: