BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
PyImportAssistant Namespace Reference

Description

Assists in importing Python object to GUI models.

Functions

std::unique_ptr< MultiLayer > importMultiLayer ()
 Show select-file dialog, try to import via python, return created sample. More...
 
MultiLayerItemitemizeSample (const MultiLayer &sample)
 Populates GUI sample with domain sample. More...
 

Function Documentation

◆ importMultiLayer()

std::unique_ptr< MultiLayer > PyImportAssistant::importMultiLayer ( )

Show select-file dialog, try to import via python, return created sample.

If sth. went wrong, a dialog has presented already and nullptr is returned.

Definition at line 172 of file PyImportAssistant.cpp.

173 {
174  auto fileName = fileNameToOpen();
175 
176  if (fileName.isEmpty())
177  return {};
178 
179  QString snippet = readFile(fileName);
180  if (snippet.isEmpty())
181  return {};
182 
183  QString funcName = getPySampleFunctionName(snippet);
184  if (funcName.isEmpty())
185  return {};
186 
187  auto sample = createMultiLayer(snippet, funcName);
188  if (!sample)
189  return {};
190 
191  if (sample->sampleName() == "Unnamed")
192  sample->setSampleName(GUI::Util::Path::baseName(fileName).toStdString());
193 
194  return sample;
195 }
QString baseName(const QString &fileName)
Returns base name of file.
Definition: Path.cpp:133

References GUI::Util::Path::baseName().

Referenced by SampleListModel::createSampleFromPython().

Here is the call graph for this function:

◆ itemizeSample()

MultiLayerItem * PyImportAssistant::itemizeSample ( const MultiLayer &  sample)

Populates GUI sample with domain sample.

Definition at line 197 of file PyImportAssistant.cpp.

198 {
199  try {
200  auto* newItem = GUI::Transform::FromCore::itemizeSample(sample);
201 
202  QString message("Seems that import was successful.\n\n"
203  "Check SampleView for new sample and material editor for new materials.");
204  GUI::View::Helpers::information(GUI::Global::mainWindow, "Python import", message);
205  return newItem;
206  } catch (const std::exception& ex) {
207  QString message("Exception thrown while trying to build GUI models.\n"
208  "GUI models might be in inconsistent state.\n\n");
209  QString details = QString::fromStdString(std::string(ex.what()));
210  DetailedMessageBox(GUI::Global::mainWindow, "GUI::Model::ObjectBuilder failure", message,
211  details)
212  .exec();
213  return nullptr;
214  }
215 }
A dialog similar to standard QMessageBox intended for detailed warning messages. On the contrary to Q...
static QMainWindow * mainWindow
Definition: Globals.h:22
MultiLayerItem * itemizeSample(const MultiLayer &sample, const QString &nodeName)
Builds GUI sample structure from a domain sample structure.
void information(QWidget *parent, const QString &title, const QString &text, const QString &detailedText)
Definition: MessageBox.cpp:22

References GUI::View::Helpers::information(), GUI::Transform::FromCore::itemizeSample(), and GUI::Global::mainWindow.

Referenced by SampleListModel::createSampleFromPython().

Here is the call graph for this function: