BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
ScriptPanel.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/SampleDesigner/ScriptPanel.cpp
6 //! @brief Implements class ScriptPanel
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 
22 #include "Sample/Multilayer/MultiLayer.h"
23 #include "Sim/Export/ExportToPython.h"
24 
25 #include <QScrollBar>
26 #include <QStackedWidget>
27 #include <QTextEdit>
28 #include <QVBoxLayout>
29 
30 namespace {
31 
32 const int accumulateUpdatesDuringMsec = 20.;
33 }
34 
35 ScriptPanel::ScriptPanel(QWidget* parent)
36  : QWidget(parent)
37  , m_textEdit(new QTextEdit)
38  , m_highlighter(nullptr)
39  , m_updateTimer(new UpdateTimer(accumulateUpdatesDuringMsec, this))
40  , m_cautionSign(new CautionSign(m_textEdit))
41  , m_currentMultiLayerItem(nullptr)
42 {
43  setWindowTitle("Python Script");
44  setObjectName("ScriptPanel");
45 
46  m_textEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
47 
48  auto* mainLayout = new QVBoxLayout(this);
49  mainLayout->setContentsMargins(0, 0, 0, 0);
50  mainLayout->addWidget(m_textEdit);
51 
52  m_textEdit->setReadOnly(true);
53  QFont textFont("Monospace");
54  m_textEdit->setFont(textFont);
56 
58  Qt::UniqueConnection);
59 }
60 
62 {
63  m_currentMultiLayerItem = sampleItem;
64  updateEditor();
65 }
66 
68 {
69  if (isVisible())
71 }
72 
74 {
75  if (!m_highlighter) {
77  m_textEdit->setLineWrapMode(QTextEdit::NoWrap);
78  }
79 
80  const int oldScrollbarValue = m_textEdit->verticalScrollBar()->value();
81 
82  const QString codeSnippet = generateCodeSnippet();
83  if (!codeSnippet.isEmpty())
84  m_textEdit->setText(codeSnippet);
85  else
86  m_textEdit->clear();
87 
88  m_textEdit->verticalScrollBar()->setValue(oldScrollbarValue);
89 }
90 
91 void ScriptPanel::showEvent(QShowEvent*)
92 {
94 }
95 
96 void ScriptPanel::hideEvent(QHideEvent*)
97 {
99 }
100 
102 {
103  m_cautionSign->clear();
104 
105  if (m_currentMultiLayerItem == nullptr)
106  return {};
107 
108  QString result;
109  try {
111  result.append(QString::fromStdString(Py::Export::sampleCode(*sample)));
112  } catch (const std::exception& ex) {
113  QString message =
114  QString("Generation of Python Script failed. Code is not complete.\n\n"
115  "It can happen if sample requires further assembling or some of sample "
116  "parameters are not valid. See details below.\n\n%1")
117  .arg(QString::fromStdString(ex.what()));
118 
120  }
121 
122  return result;
123 }
Defines class CautionSign.
Defines class DesignerHelper.
Defines class MultiLayerItem.
Defines class PythonSyntaxHighlighter.
Defines namespace GUI::Transform::ToCore.
Defines class SampleDesigner.
Defines class UpdateTimer.
The CautionSign controls appearance of CautionSignWidget on top of parent widget.
Definition: CautionSign.h:25
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
static int getPythonEditorFontSize()
Implementation of highlighting for Python code.
UpdateTimer * m_updateTimer
Definition: ScriptPanel.h:53
ScriptPanel(QWidget *parent)
Definition: ScriptPanel.cpp:35
void updateEditor()
Update the editor with the script content.
Definition: ScriptPanel.cpp:73
void showEvent(QShowEvent *) override
Definition: ScriptPanel.cpp:91
QTextEdit * m_textEdit
Definition: ScriptPanel.h:51
QString generateCodeSnippet()
generates string representing code snippet for all multi layers in the model
MultiLayerItem * m_currentMultiLayerItem
Definition: ScriptPanel.h:55
void onSampleModified()
Definition: ScriptPanel.cpp:67
void hideEvent(QHideEvent *) override
Definition: ScriptPanel.cpp:96
CautionSign * m_cautionSign
Definition: ScriptPanel.h:54
void setCurrentSample(MultiLayerItem *sampleItem)
Definition: ScriptPanel.cpp:61
PythonSyntaxHighlighter * m_highlighter
Definition: ScriptPanel.h:52
The UpdateTimer class accumulates update requests during certain period of time, and at the end of th...
Definition: UpdateTimer.h:27
void scheduleUpdate()
Definition: UpdateTimer.cpp:42
void timeToUpdate()
void reset()
Definition: UpdateTimer.cpp:30
std::unique_ptr< MultiLayer > itemToSample(const MultiLayerItem &item)