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

Description

Widget to define a simulation. Contains:

  • Elements to select instrument, sample and real data.
  • Elements to set options like number of threads.
  • Buttons to run simulation or to export as a Python script

Definition at line 38 of file SimulationView.h.

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

Public Member Functions

 SimulationView (QWidget *parent, ProjectDocument *document)
 
void exportPythonScript ()
 
void simulate ()
 

Protected Member Functions

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

Private Member Functions

QVector< InstrumentItem * > collectedItems () const
 
SimulationOptionsItemoptionsItem () const
 
void readOptionsFromUI ()
 Read the entries in the UI and store in OptionsItem. More...
 
QVector< RealDataItem * > realDataItems () const
 
QVector< MultiLayerItem * > sampleItems () const
 
const InstrumentItemselectedInstrument () const
 Returns selected InstrumentItem taking into account that there might be several instruments with the same name. More...
 
const RealDataItemselectedRealData () const
 Returns selected real data item taking into account that there might be several items with the same name. More...
 
const MultiLayerItemselectedSample () const
 Returns selected sample (MultiLayerItem) taking into account that there might be several samples with the same name. More...
 
void updateEnabling ()
 Update enabling of elements depending on other elements. More...
 
void updateFunctionalityNarrowing ()
 Show/hide UI elements according to settings in current project. More...
 
void updateSelection (QComboBox *comboBox, QStringList itemList, bool allowNone=false)
 Updates selection combo with string list while preserving previous selection. If allowNone == true, additional "None" item will be added to the combo. More...
 
QString validateSimulationSetup (bool validateRealData) const
 Checks whether selection is correct for running a simulation. Returns empty string if valid, otherwise the error text. More...
 
void writeOptionsToUI ()
 Write entries in OptionsItem to UI. More...
 

Private Attributes

ProjectDocumentm_document
 
Ui::SimulationView * m_ui
 

Constructor & Destructor Documentation

◆ SimulationView()

SimulationView::SimulationView ( QWidget *  parent,
ProjectDocument document 
)

Definition at line 34 of file SimulationView.cpp.

35  : QWidget(parent)
36  , m_ui(new Ui::SimulationView)
37  , m_document(document)
38 {
39  m_ui->setupUi(this);
40 
41  setAttribute(Qt::WA_StyledBackground, true);
42  setProperty("stylable", true); // for stylesheet addressing
43 
47 
48  auto* buttonGroup = new QButtonGroup(this);
49  buttonGroup->addButton(m_ui->runPolicyImmediatelyRadio);
50  buttonGroup->addButton(m_ui->runPolicyBackgroundRadio);
51 
52  auto* computationButtonGroup = new QButtonGroup(this);
53  computationButtonGroup->addButton(m_ui->analyticalRadio);
54  computationButtonGroup->addButton(m_ui->monteCarloRadio);
55 
56  buttonGroup = new QButtonGroup(this);
57  buttonGroup->addButton(m_ui->ambientLayerRadio);
58  buttonGroup->addButton(m_ui->averageLayerRadio);
59 
60  // -- fill combo for "number of threads"
61  const int nthreads = static_cast<int>(std::thread::hardware_concurrency());
62  m_ui->numberOfThreadsCombo->addItem(QString("Max (%1 threads)").arg(nthreads), nthreads);
63  for (int i = nthreads - 1; i > 1; i--)
64  m_ui->numberOfThreadsCombo->addItem(QString("%1 threads").arg(i), i);
65  m_ui->numberOfThreadsCombo->addItem("1 thread", 1);
66 
68 
69  connect(m_ui->simulateButton, &QPushButton::clicked, this, &SimulationView::simulate);
70  connect(m_ui->exportToPyScriptButton, &QPushButton::clicked, this,
72  connect(computationButtonGroup, QOverload<int>::of(&QButtonGroup::buttonClicked), this,
74 
75  connect(m_ui->runPolicyImmediatelyRadio, &QRadioButton::toggled,
76  [=]() { document->setModified(); });
77 
78  connect(m_ui->analyticalRadio, &QRadioButton::toggled, [=]() { document->setModified(); });
79 
80  connect(m_ui->averageLayerRadio, &QRadioButton::toggled, [=]() { document->setModified(); });
81 
82  connect(m_ui->numberOfThreadsCombo, QOverload<int>::of(&QComboBox::currentIndexChanged),
83  [=]() { document->setModified(); });
84 
85  connect(m_ui->numberOfMonteCarloPoints, QOverload<int>::of(&QSpinBox::valueChanged),
86  [=]() { document->setModified(); });
87 
88  connect(m_ui->includeSpecularCheck, &QCheckBox::toggled, [=]() { document->setModified(); });
89 
90  connect(m_document, &ProjectDocument::modified, this,
92 }
static GroupBoxCollapser * installIntoGroupBox(QGroupBox *groupBox, bool expanded=true)
void modified()
Emitted for any modifications in the document.
Ui::SimulationView * m_ui
void updateFunctionalityNarrowing()
Show/hide UI elements according to settings in current project.
ProjectDocument * m_document
void updateEnabling()
Update enabling of elements depending on other elements.

References exportPythonScript(), GroupBoxCollapser::installIntoGroupBox(), m_document, m_ui, ProjectDocument::modified(), simulate(), updateEnabling(), and updateFunctionalityNarrowing().

Here is the call graph for this function:

Member Function Documentation

◆ collectedItems()

QVector< InstrumentItem * > SimulationView::collectedItems ( ) const
private

Definition at line 238 of file SimulationView.cpp.

239 {
241 }
QVector< InstrumentItem * > collectedItems() const
InstrumentCollection * collectedItems() const

References InstrumentCollection::collectedItems(), ProjectDocument::collectedItems(), and m_document.

Referenced by selectedInstrument().

Here is the call graph for this function:

◆ exportPythonScript()

void SimulationView::exportPythonScript ( )

Definition at line 152 of file SimulationView.cpp.

153 {
155  if (const QString msg = validateSimulationSetup(false); !msg.isEmpty()) {
156  QMessageBox::warning(this, "Can't export to Python",
157  "Can't export to Python with current settings\n\n" + msg);
158  return;
159  }
160  auto* pythonWidget = new PythonScriptWidget(GUI::Global::mainWindow);
161  pythonWidget->show();
162  pythonWidget->raise();
163  pythonWidget->generatePythonScript(selectedSample(), selectedInstrument(), *optionsItem(),
165 }
static ProjectManager * instance()
The PythonScriptWidget displays a python script which represents full simulation. Part of SimulationS...
SimulationOptionsItem * optionsItem() const
const InstrumentItem * selectedInstrument() const
Returns selected InstrumentItem taking into account that there might be several instruments with the ...
const MultiLayerItem * selectedSample() const
Returns selected sample (MultiLayerItem) taking into account that there might be several samples with...
QString validateSimulationSetup(bool validateRealData) const
Checks whether selection is correct for running a simulation. Returns empty string if valid,...
void readOptionsFromUI()
Read the entries in the UI and store in OptionsItem.
static QMainWindow * mainWindow
Definition: Globals.h:22
QString projectDir(const QString &projectFileName)
Returns project directory deduced from project file name.
void warning(QWidget *parent, const QString &title, const QString &text, const QString &detailedText)
Definition: MessageBox.cpp:37

References ProjectManager::instance(), GUI::Global::mainWindow, optionsItem(), GUI::Project::Utils::projectDir(), readOptionsFromUI(), selectedInstrument(), selectedSample(), validateSimulationSetup(), and GUI::View::Helpers::warning().

Referenced by SimulationView().

Here is the call graph for this function:

◆ hideEvent()

void SimulationView::hideEvent ( QHideEvent *  )
overrideprotected

Definition at line 131 of file SimulationView.cpp.

132 {
133  // Storing the options has do be done here because on a showEvent the values are set again from
134  // the SimulationOptionsItem (=> keep values when switching views)
136 }

References readOptionsFromUI().

Here is the call graph for this function:

◆ optionsItem()

SimulationOptionsItem * SimulationView::optionsItem ( ) const
private

Definition at line 248 of file SimulationView.cpp.

249 {
251 }
SimulationOptionsItem * simulationOptionsItem()

References m_document, and ProjectDocument::simulationOptionsItem().

Referenced by exportPythonScript(), readOptionsFromUI(), simulate(), and writeOptionsToUI().

Here is the call graph for this function:

◆ readOptionsFromUI()

void SimulationView::readOptionsFromUI ( )
private

Read the entries in the UI and store in OptionsItem.

Definition at line 167 of file SimulationView.cpp.

168 {
169  optionsItem()->setRunImmediately(m_ui->runPolicyImmediatelyRadio->isChecked());
170  optionsItem()->setNumberOfThreads(m_ui->numberOfThreadsCombo->currentData().toInt());
171  if (m_ui->analyticalRadio->isChecked())
173  else
174  optionsItem()->setUseMonteCarloIntegration(m_ui->numberOfMonteCarloPoints->value());
175  optionsItem()->setUseAverageMaterials(m_ui->averageLayerRadio->isChecked());
176  optionsItem()->setIncludeSpecularPeak(m_ui->includeSpecularCheck->isChecked());
177 }
void setRunImmediately(bool runImmediately)
void setUseAverageMaterials(bool useAverageMaterials)
void setNumberOfThreads(unsigned n)
void setIncludeSpecularPeak(bool includeSpecularPeak)
void setUseMonteCarloIntegration(unsigned numberOfPoints)

References m_ui, optionsItem(), SimulationOptionsItem::setIncludeSpecularPeak(), SimulationOptionsItem::setNumberOfThreads(), SimulationOptionsItem::setRunImmediately(), SimulationOptionsItem::setUseAnalytical(), SimulationOptionsItem::setUseAverageMaterials(), and SimulationOptionsItem::setUseMonteCarloIntegration().

Referenced by exportPythonScript(), hideEvent(), and simulate().

Here is the call graph for this function:

◆ realDataItems()

QVector< RealDataItem * > SimulationView::realDataItems ( ) const
private

Definition at line 243 of file SimulationView.cpp.

244 {
246 }
RealDataModel * realDataModel() const
QVector< RealDataItem * > realDataItems() const

References m_document, RealDataModel::realDataItems(), and ProjectDocument::realDataModel().

Referenced by selectedRealData().

Here is the call graph for this function:

◆ sampleItems()

QVector< MultiLayerItem * > SimulationView::sampleItems ( ) const
private

Definition at line 233 of file SimulationView.cpp.

234 {
235  return m_document->sampleItems()->sampleItems();
236 }
QVector< MultiLayerItem * > sampleItems() const
MultiLayerItems * sampleItems()

References m_document, ProjectDocument::sampleItems(), and MultiLayerItems::sampleItems().

Referenced by selectedSample().

Here is the call graph for this function:

◆ selectedInstrument()

const InstrumentItem * SimulationView::selectedInstrument ( ) const
private

Returns selected InstrumentItem taking into account that there might be several instruments with the same name.

Definition at line 258 of file SimulationView.cpp.

259 {
260  return collectedItems().value(m_ui->instrumentCombo->currentIndex(), nullptr);
261 }
QVector< InstrumentItem * > collectedItems() const

References collectedItems(), and m_ui.

Referenced by exportPythonScript(), simulate(), and validateSimulationSetup().

Here is the call graph for this function:

◆ selectedRealData()

const RealDataItem * SimulationView::selectedRealData ( ) const
private

Returns selected real data item taking into account that there might be several items with the same name.

Definition at line 263 of file SimulationView.cpp.

264 {
265  return realDataItems().value(m_ui->realDataCombo->currentIndex() - 1, nullptr); // -1: "None"
266 }
QVector< RealDataItem * > realDataItems() const

References m_ui, and realDataItems().

Referenced by simulate(), and validateSimulationSetup().

Here is the call graph for this function:

◆ selectedSample()

const MultiLayerItem * SimulationView::selectedSample ( ) const
private

Returns selected sample (MultiLayerItem) taking into account that there might be several samples with the same name.

Definition at line 253 of file SimulationView.cpp.

254 {
255  return sampleItems().value(m_ui->sampleCombo->currentIndex(), nullptr);
256 }
QVector< MultiLayerItem * > sampleItems() const

References m_ui, and sampleItems().

Referenced by exportPythonScript(), simulate(), and validateSimulationSetup().

Here is the call graph for this function:

◆ showEvent()

void SimulationView::showEvent ( QShowEvent *  )
overrideprotected

Definition at line 94 of file SimulationView.cpp.

95 {
97 }
void writeOptionsToUI()
Write entries in OptionsItem to UI.

References writeOptionsToUI().

Here is the call graph for this function:

◆ simulate()

void SimulationView::simulate ( )

Definition at line 138 of file SimulationView.cpp.

139 {
141  if (const QString msg = validateSimulationSetup(true); !msg.isEmpty()) {
142  QMessageBox::warning(this, "Can't run the job",
143  "Can't run the job with current settings\n\n" + msg);
144  return;
145  }
146  JobModel* jobModel = m_document->jobModel();
147  JobItem* jobItem = jobModel->addJob(selectedSample(), selectedInstrument(), selectedRealData(),
148  *optionsItem());
149  jobModel->runJob(jobItem);
150 }
void runJob(JobItem *jobItem)
Definition: JobModel.cpp:142
JobItem * addJob(const MultiLayerItem *sampleItem, const InstrumentItem *instrumentItem, const RealDataItem *realDataItem, const SimulationOptionsItem &optionItem)
Main method to add a job.
Definition: JobModel.cpp:52
JobModel * jobModel() const
const RealDataItem * selectedRealData() const
Returns selected real data item taking into account that there might be several items with the same n...

References JobModel::addJob(), ProjectDocument::jobModel(), m_document, optionsItem(), readOptionsFromUI(), JobModel::runJob(), selectedInstrument(), selectedRealData(), selectedSample(), validateSimulationSetup(), and GUI::View::Helpers::warning().

Referenced by SimulationView(), and MainWindow::onRunSimulationShortcut().

Here is the call graph for this function:

◆ updateEnabling()

void SimulationView::updateEnabling ( )
private

Update enabling of elements depending on other elements.

Definition at line 179 of file SimulationView.cpp.

180 {
181  m_ui->numberOfMonteCarloPoints->setEnabled(m_ui->monteCarloRadio->isChecked());
182 }

References m_ui.

Referenced by SimulationView(), and writeOptionsToUI().

◆ updateFunctionalityNarrowing()

void SimulationView::updateFunctionalityNarrowing ( )
private

Show/hide UI elements according to settings in current project.

Definition at line 227 of file SimulationView.cpp.

228 {
229  m_ui->instrumentCombo->setVisible(!m_document->singleInstrumentMode());
230  m_ui->instrumentLabel->setVisible(!m_document->singleInstrumentMode());
231 }
bool singleInstrumentMode() const

References m_document, m_ui, and ProjectDocument::singleInstrumentMode().

Referenced by SimulationView().

Here is the call graph for this function:

◆ updateSelection()

void SimulationView::updateSelection ( QComboBox *  comboBox,
QStringList  itemList,
bool  allowNone = false 
)
private

Updates selection combo with string list while preserving previous selection. If allowNone == true, additional "None" item will be added to the combo.

Definition at line 184 of file SimulationView.cpp.

185 {
186  const QString previousItem = comboBox->currentText();
187 
188  comboBox->clear();
189  comboBox->setEnabled(!itemList.isEmpty());
190 
191  if (itemList.isEmpty()) {
192  comboBox->addItem("Not yet defined");
193  return;
194  }
195 
196  if (allowNone)
197  itemList.insert(-1, "None");
198  comboBox->addItems(itemList);
199  if (itemList.contains(previousItem))
200  comboBox->setCurrentIndex(itemList.indexOf(previousItem));
201 }

Referenced by writeOptionsToUI().

◆ validateSimulationSetup()

QString SimulationView::validateSimulationSetup ( bool  validateRealData) const
private

Checks whether selection is correct for running a simulation. Returns empty string if valid, otherwise the error text.

Definition at line 203 of file SimulationView.cpp.

204 {
205  QString messages;
206  const auto append = [&](const QString& m) { messages.append("- " + m + "\n"); };
207 
208  if (!selectedSample())
209  append("No sample selected");
210  else {
211  SampleValidator sampleValidator;
212  if (!sampleValidator.isValidMultiLayer(selectedSample()))
213  append(sampleValidator.getValidationMessage());
214  }
215 
216  if (!selectedInstrument())
217  append("No instrument selected");
218 
219  if (validateRealData && selectedRealData() && selectedInstrument()
221  append("The experimental data does not fit in the selected instrument. Try linking "
222  "them in Import Tab.");
223 
224  return messages;
225 }
virtual bool alignedWith(const RealDataItem *item) const
Validates whether MultiLayerItem is suitable for simulation.
bool isValidMultiLayer(const MultiLayerItem *sample)
QString getValidationMessage() const

References InstrumentItem::alignedWith(), SampleValidator::getValidationMessage(), SampleValidator::isValidMultiLayer(), selectedInstrument(), selectedRealData(), and selectedSample().

Referenced by exportPythonScript(), and simulate().

Here is the call graph for this function:

◆ writeOptionsToUI()

void SimulationView::writeOptionsToUI ( )
private

Write entries in OptionsItem to UI.

Definition at line 99 of file SimulationView.cpp.

100 {
101  QSignalBlocker b1(m_ui->runPolicyImmediatelyRadio);
102  QSignalBlocker b2(m_ui->analyticalRadio);
103  QSignalBlocker b3(m_ui->averageLayerRadio);
104  QSignalBlocker b4(m_ui->numberOfThreadsCombo);
105  QSignalBlocker b5(m_ui->numberOfMonteCarloPoints);
106  QSignalBlocker b6(m_ui->includeSpecularCheck);
107 
108  // -- selection group
111  updateSelection(m_ui->realDataCombo, m_document->realDataModel()->realDataNames(), true);
112 
113  // -- options group
114  optionsItem()->runImmediately() ? m_ui->runPolicyImmediatelyRadio->setChecked(true)
115  : m_ui->runPolicyBackgroundRadio->setChecked(true);
116 
117  optionsItem()->useAnalytical() ? m_ui->analyticalRadio->setChecked(true)
118  : m_ui->monteCarloRadio->setChecked(true);
119 
120  optionsItem()->useAverageMaterials() ? m_ui->averageLayerRadio->setChecked(true)
121  : m_ui->ambientLayerRadio->setChecked(true);
122 
123  m_ui->numberOfThreadsCombo->setCurrentIndex(
124  m_ui->numberOfThreadsCombo->findData(optionsItem()->numberOfThreads()));
125  m_ui->numberOfMonteCarloPoints->setValue(optionsItem()->numberOfMonteCarloPoints());
126  m_ui->includeSpecularCheck->setChecked(optionsItem()->includeSpecularPeak());
127 
128  updateEnabling();
129 }
QStringList instrumentNames() const
QStringList sampleNames() const
QStringList realDataNames() const
void updateSelection(QComboBox *comboBox, QStringList itemList, bool allowNone=false)
Updates selection combo with string list while preserving previous selection. If allowNone == true,...

References ProjectDocument::collectedItems(), InstrumentCollection::instrumentNames(), m_document, m_ui, optionsItem(), ProjectDocument::realDataModel(), RealDataModel::realDataNames(), SimulationOptionsItem::runImmediately(), ProjectDocument::sampleItems(), MultiLayerItems::sampleNames(), updateEnabling(), updateSelection(), SimulationOptionsItem::useAnalytical(), and SimulationOptionsItem::useAverageMaterials().

Referenced by showEvent().

Here is the call graph for this function:

Member Data Documentation

◆ m_document

◆ m_ui

Ui::SimulationView* SimulationView::m_ui
private

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