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

Description

Resides at the bottom of SampleView and displays a Python script.

Definition at line 31 of file ScriptPanel.h.

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

Public Member Functions

 ScriptPanel (QWidget *parent)
 
void onSampleModified ()
 
void setCurrentSample (MultiLayerItem *sampleItem)
 

Protected Member Functions

void hideEvent (QHideEvent *) override
 
void showEvent (QShowEvent *) override
 

Private Member Functions

QString generateCodeSnippet ()
 generates string representing code snippet for all multi layers in the model More...
 
void updateEditor ()
 Update the editor with the script content. More...
 

Private Attributes

CautionSignm_cautionSign
 
MultiLayerItemm_currentMultiLayerItem
 
PythonSyntaxHighlighterm_highlighter
 
QTextEdit * m_textEdit
 
UpdateTimerm_updateTimer
 

Constructor & Destructor Documentation

◆ ScriptPanel()

ScriptPanel::ScriptPanel ( QWidget *  parent)
explicit

Definition at line 35 of file ScriptPanel.cpp.

36  : QWidget(parent)
37  , m_textEdit(new QTextEdit)
38  , m_highlighter(nullptr)
39  , m_updateTimer(new UpdateTimer(accumulateUpdatesDuringMsec, this))
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 }
The CautionSign controls appearance of CautionSignWidget on top of parent widget.
Definition: CautionSign.h:25
static int getPythonEditorFontSize()
UpdateTimer * m_updateTimer
Definition: ScriptPanel.h:53
void updateEditor()
Update the editor with the script content.
Definition: ScriptPanel.cpp:73
QTextEdit * m_textEdit
Definition: ScriptPanel.h:51
MultiLayerItem * m_currentMultiLayerItem
Definition: ScriptPanel.h:55
CautionSign * m_cautionSign
Definition: ScriptPanel.h:54
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 timeToUpdate()

References DesignerHelper::getPythonEditorFontSize(), m_textEdit, m_updateTimer, UpdateTimer::timeToUpdate(), and updateEditor().

Here is the call graph for this function:

Member Function Documentation

◆ generateCodeSnippet()

QString ScriptPanel::generateCodeSnippet ( )
private

generates string representing code snippet for all multi layers in the model

Definition at line 101 of file ScriptPanel.cpp.

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 }
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< MultiLayer > itemToSample(const MultiLayerItem &item)

References CautionSign::clear(), GUI::Transform::ToCore::itemToSample(), m_cautionSign, m_currentMultiLayerItem, and CautionSign::setCautionMessage().

Referenced by updateEditor().

Here is the call graph for this function:

◆ hideEvent()

void ScriptPanel::hideEvent ( QHideEvent *  )
overrideprotected

Definition at line 96 of file ScriptPanel.cpp.

97 {
99 }
void reset()
Definition: UpdateTimer.cpp:30

References m_updateTimer, and UpdateTimer::reset().

Here is the call graph for this function:

◆ onSampleModified()

void ScriptPanel::onSampleModified ( )

Definition at line 67 of file ScriptPanel.cpp.

68 {
69  if (isVisible())
71 }
void scheduleUpdate()
Definition: UpdateTimer.cpp:42

References m_updateTimer, and UpdateTimer::scheduleUpdate().

Referenced by SampleView::SampleView().

Here is the call graph for this function:

◆ setCurrentSample()

void ScriptPanel::setCurrentSample ( MultiLayerItem sampleItem)

Definition at line 61 of file ScriptPanel.cpp.

62 {
63  m_currentMultiLayerItem = sampleItem;
64  updateEditor();
65 }

References m_currentMultiLayerItem, and updateEditor().

Referenced by SampleView::SampleView().

Here is the call graph for this function:

◆ showEvent()

void ScriptPanel::showEvent ( QShowEvent *  )
overrideprotected

Definition at line 91 of file ScriptPanel.cpp.

92 {
94 }

References m_updateTimer, and UpdateTimer::scheduleUpdate().

Here is the call graph for this function:

◆ updateEditor()

void ScriptPanel::updateEditor ( )
private

Update the editor with the script content.

Definition at line 73 of file ScriptPanel.cpp.

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 }
Implementation of highlighting for Python code.
QString generateCodeSnippet()
generates string representing code snippet for all multi layers in the model

References generateCodeSnippet(), m_highlighter, and m_textEdit.

Referenced by ScriptPanel(), and setCurrentSample().

Here is the call graph for this function:

Member Data Documentation

◆ m_cautionSign

CautionSign* ScriptPanel::m_cautionSign
private

Definition at line 54 of file ScriptPanel.h.

Referenced by generateCodeSnippet().

◆ m_currentMultiLayerItem

MultiLayerItem* ScriptPanel::m_currentMultiLayerItem
private

Definition at line 55 of file ScriptPanel.h.

Referenced by generateCodeSnippet(), and setCurrentSample().

◆ m_highlighter

PythonSyntaxHighlighter* ScriptPanel::m_highlighter
private

Definition at line 52 of file ScriptPanel.h.

Referenced by updateEditor().

◆ m_textEdit

QTextEdit* ScriptPanel::m_textEdit
private

Definition at line 51 of file ScriptPanel.h.

Referenced by ScriptPanel(), and updateEditor().

◆ m_updateTimer

UpdateTimer* ScriptPanel::m_updateTimer
private

Definition at line 53 of file ScriptPanel.h.

Referenced by ScriptPanel(), hideEvent(), onSampleModified(), and showEvent().


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