BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
PySampleWidget Class Reference

Displays Python script representing a MultiLayer at the bottom of SampleView. More...

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

Public Slots

void onDataChanged (const QModelIndex &, const QModelIndex &)
 
void onModifiedRow (const QModelIndex &, int, int)
 
void setEditorConnected (bool isConnected)
 
void updateEditor ()
 Update the editor with the script content. More...
 

Public Member Functions

 PySampleWidget (QWidget *parent=nullptr)
 
void setInstrumentModel (InstrumentModel *instrumentModel)
 
void setSampleModel (SampleModel *sampleModel)
 

Protected Member Functions

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

Private Member Functions

QString generateCodeSnippet ()
 generates string representing code snippet for all multi layers in the model More...
 

Private Attributes

PythonSyntaxHighlighterm_highlighter
 
InstrumentModelm_instrumentModel
 
SampleModelm_sampleModel
 
QTextEdit * m_textEdit
 
UpdateTimerm_updateTimer
 
WarningSignm_warningSign
 

Detailed Description

Displays Python script representing a MultiLayer at the bottom of SampleView.

Definition at line 32 of file PySampleWidget.h.

Constructor & Destructor Documentation

◆ PySampleWidget()

PySampleWidget::PySampleWidget ( QWidget *  parent = nullptr)

Definition at line 34 of file PySampleWidget.cpp.

35  : QWidget(parent)
36  , m_textEdit(new QTextEdit)
37  , m_sampleModel(nullptr)
38  , m_instrumentModel(nullptr)
39  , m_highlighter(nullptr)
40  , m_updateTimer(new UpdateTimer(accumulate_updates_during_msec, this))
42 {
43  m_textEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
44 
45  auto mainLayout = new QVBoxLayout;
46  mainLayout->setContentsMargins(0, 0, 0, 0);
47  mainLayout->addWidget(m_textEdit);
48 
49  setLayout(mainLayout);
50 
51  m_textEdit->setReadOnly(true);
52  QFont textFont("Monospace");
53  m_textEdit->setFont(textFont);
55 }
static int getPythonEditorFontSize()
WarningSign * m_warningSign
QTextEdit * m_textEdit
InstrumentModel * m_instrumentModel
PythonSyntaxHighlighter * m_highlighter
SampleModel * m_sampleModel
UpdateTimer * m_updateTimer
The UpdateTimer class accumulates update requests during certain period of time, and at the end of th...
Definition: UpdateTimer.h:27
The WarningSign controls appearance of WarningSignWidget on top of parent widget.
Definition: WarningSign.h:25

References DesignerHelper::getPythonEditorFontSize(), and m_textEdit.

Here is the call graph for this function:

Member Function Documentation

◆ generateCodeSnippet()

QString PySampleWidget::generateCodeSnippet ( )
private

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

Definition at line 145 of file PySampleWidget.cpp.

146 {
147  m_warningSign->clear();
148  QString result;
149 
150  for (const MultiLayerItem* sampleItem : m_sampleModel->topItems<MultiLayerItem>()) {
151  try {
152  auto multilayer = DomainObjectBuilder::buildMultiLayer(*sampleItem);
153  if (!result.isEmpty())
154  result.append("\n");
155  result.append(QString::fromStdString(ExportToPython::sampleCode(*multilayer)));
156  } catch (const std::exception& ex) {
157  QString message =
158  QString("Generation of Python Script failed. Code is not complete.\n\n"
159  "It can happen if sample requires further assembling or some of sample "
160  "parameters are not valid. See details below.\n\n%1")
161  .arg(QString::fromStdString(ex.what()));
162 
164  }
165  }
166 
167  return result;
168 }
QVector< T * > topItems() const
Definition: SessionModel.h:147
void clear()
Clears warning message;.
Definition: WarningSign.cpp:40
void setWarningMessage(const QString &warningMessage)
Shows warning sign on the screen.
Definition: WarningSign.cpp:58
std::unique_ptr< MultiLayer > buildMultiLayer(const SessionItem &multilayer_item)
std::string sampleCode(const MultiLayer &multilayer)

References DomainObjectBuilder::buildMultiLayer(), WarningSign::clear(), m_sampleModel, m_warningSign, ExportToPython::sampleCode(), WarningSign::setWarningMessage(), and SessionModel::topItems().

Referenced by updateEditor().

Here is the call graph for this function:

◆ hideEvent()

void PySampleWidget::hideEvent ( QHideEvent *  )
protected

Definition at line 138 of file PySampleWidget.cpp.

139 {
140  setEditorConnected(isVisible());
141 }
void setEditorConnected(bool isConnected)

References setEditorConnected().

Here is the call graph for this function:

◆ onDataChanged

void PySampleWidget::onDataChanged ( const QModelIndex &  index,
const QModelIndex &   
)
slot

Definition at line 76 of file PySampleWidget.cpp.

77 {
78  auto item = m_sampleModel->itemForIndex(index);
79  if (!item)
80  return;
81 
84 }
SessionItem * itemForIndex(const QModelIndex &index) const
void scheduleUpdate()
Definition: UpdateTimer.cpp:42
bool IsPositionRelated(const SessionItem &item)
Returns true if current item is related to the position on sample editor canvas.

References SessionItemUtils::IsPositionRelated(), SessionModel::itemForIndex(), m_sampleModel, m_updateTimer, and UpdateTimer::scheduleUpdate().

Referenced by setEditorConnected().

Here is the call graph for this function:

◆ onModifiedRow

void PySampleWidget::onModifiedRow ( const QModelIndex &  ,
int  ,
int   
)
slot

Definition at line 71 of file PySampleWidget.cpp.

72 {
74 }

References m_updateTimer, and UpdateTimer::scheduleUpdate().

Referenced by setEditorConnected().

Here is the call graph for this function:

◆ setEditorConnected

void PySampleWidget::setEditorConnected ( bool  isConnected)
slot

Definition at line 106 of file PySampleWidget.cpp.

107 {
108  if (isConnected) {
109  connect(m_sampleModel, &SampleModel::rowsInserted, this, &PySampleWidget::onModifiedRow,
110  Qt::UniqueConnection);
111  connect(m_sampleModel, &SampleModel::rowsRemoved, this, &PySampleWidget::onModifiedRow,
112  Qt::UniqueConnection);
113  connect(m_sampleModel, &SampleModel::dataChanged, this, &PySampleWidget::onDataChanged,
114  Qt::UniqueConnection);
115  connect(m_sampleModel, &SampleModel::modelReset, this, &PySampleWidget::updateEditor,
116  Qt::UniqueConnection);
117 
119  Qt::UniqueConnection);
120 
122 
123  } else {
124  disconnect(m_sampleModel, &SampleModel::rowsInserted, this, &PySampleWidget::onModifiedRow);
125  disconnect(m_sampleModel, &SampleModel::rowsRemoved, this, &PySampleWidget::onModifiedRow);
126  disconnect(m_sampleModel, &SampleModel::dataChanged, this, &PySampleWidget::onDataChanged);
127  disconnect(m_sampleModel, &SampleModel::modelReset, this, &PySampleWidget::updateEditor);
128 
130  }
131 }
void onDataChanged(const QModelIndex &, const QModelIndex &)
void onModifiedRow(const QModelIndex &, int, int)
void updateEditor()
Update the editor with the script content.
void timeToUpdate()

References m_sampleModel, m_updateTimer, onDataChanged(), onModifiedRow(), UpdateTimer::scheduleUpdate(), UpdateTimer::timeToUpdate(), and updateEditor().

Referenced by hideEvent(), setSampleModel(), and showEvent().

Here is the call graph for this function:

◆ setInstrumentModel()

void PySampleWidget::setInstrumentModel ( InstrumentModel instrumentModel)

Definition at line 66 of file PySampleWidget.cpp.

67 {
68  m_instrumentModel = instrumentModel;
69 }

References m_instrumentModel.

Referenced by ScriptPanel::setInstrumentModel().

◆ setSampleModel()

void PySampleWidget::setSampleModel ( SampleModel sampleModel)

Definition at line 57 of file PySampleWidget.cpp.

58 {
59  if (sampleModel != m_sampleModel) {
60  if (m_sampleModel)
61  setEditorConnected(false);
62  m_sampleModel = sampleModel;
63  }
64 }

References m_sampleModel, and setEditorConnected().

Referenced by ScriptPanel::setSampleModel().

Here is the call graph for this function:

◆ showEvent()

void PySampleWidget::showEvent ( QShowEvent *  )
protected

Definition at line 133 of file PySampleWidget.cpp.

134 {
135  setEditorConnected(isVisible());
136 }

References setEditorConnected().

Here is the call graph for this function:

◆ updateEditor

void PySampleWidget::updateEditor ( )
slot

Update the editor with the script content.

Definition at line 87 of file PySampleWidget.cpp.

88 {
89  if (!m_highlighter) {
91  m_textEdit->setLineWrapMode(QTextEdit::NoWrap);
92  }
93 
94  const int old_scrollbar_value = m_textEdit->verticalScrollBar()->value();
95 
96  QString code_snippet = generateCodeSnippet();
97 
98  if (!code_snippet.isEmpty())
99  m_textEdit->setText(code_snippet);
100  else
101  m_textEdit->clear();
102 
103  m_textEdit->verticalScrollBar()->setValue(old_scrollbar_value);
104 }
QString generateCodeSnippet()
generates string representing code snippet for all multi layers in the model
Implementation of highlighting for Python code.

References generateCodeSnippet(), m_highlighter, and m_textEdit.

Referenced by setEditorConnected().

Here is the call graph for this function:

Member Data Documentation

◆ m_highlighter

PythonSyntaxHighlighter* PySampleWidget::m_highlighter
private

Definition at line 58 of file PySampleWidget.h.

Referenced by updateEditor().

◆ m_instrumentModel

InstrumentModel* PySampleWidget::m_instrumentModel
private

Definition at line 57 of file PySampleWidget.h.

Referenced by setInstrumentModel().

◆ m_sampleModel

SampleModel* PySampleWidget::m_sampleModel
private

◆ m_textEdit

QTextEdit* PySampleWidget::m_textEdit
private

Definition at line 55 of file PySampleWidget.h.

Referenced by PySampleWidget(), and updateEditor().

◆ m_updateTimer

UpdateTimer* PySampleWidget::m_updateTimer
private

Definition at line 59 of file PySampleWidget.h.

Referenced by onDataChanged(), onModifiedRow(), and setEditorConnected().

◆ m_warningSign

WarningSign* PySampleWidget::m_warningSign
private

Definition at line 60 of file PySampleWidget.h.

Referenced by generateCodeSnippet().


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