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

Description

List model for sample selection (used in the left pane of the layer oriented sample editor)

Definition at line 24 of file SampleListModel.h.

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

Public Member Functions

 SampleListModel (QObject *parent, MultiLayerItems *model)
 
QModelIndex createSample ()
 Create a new sample (sample) and return the index of it. More...
 
QModelIndex createSampleFromExamples (const QString &className, const QString &title, const QString &description)
 Create sample from list of built-in examples. More...
 
QModelIndex createSampleFromPython ()
 Create sample from an imported python code. More...
 
QVariant data (const QModelIndex &index, int role=Qt::DisplayRole) const override
 
Qt::ItemFlags flags (const QModelIndex &index) const override
 
QModelIndex indexForItem (MultiLayerItem *item) const
 
MultiLayerItemitemForIndex (const QModelIndex &index) const
 
void removeSample (MultiLayerItem *item)
 Remove the given sample. nullptr is allowed. More...
 
int rowCount (const QModelIndex &parent=QModelIndex()) const override
 
bool setData (const QModelIndex &index, const QVariant &value, int role) override
 

Private Attributes

MultiLayerItemsm_sampleItems = nullptr
 

Constructor & Destructor Documentation

◆ SampleListModel()

SampleListModel::SampleListModel ( QObject *  parent,
MultiLayerItems model 
)

Definition at line 28 of file SampleListModel.cpp.

29  : QAbstractListModel(parent)
30  , m_sampleItems(model)
31 {
32 }
MultiLayerItems * m_sampleItems

Member Function Documentation

◆ createSample()

QModelIndex SampleListModel::createSample ( )

Create a new sample (sample) and return the index of it.

Definition at line 124 of file SampleListModel.cpp.

125 {
126  const QStringList existingNames = m_sampleItems->sampleNames();
127 
128  const int row = m_sampleItems->sampleItems().size();
129  beginInsertRows(QModelIndex(), row, row);
130  auto* sample_item = m_sampleItems->addMultiLayer();
131  sample_item->setSampleName(GUI::Util::String::suggestName(existingNames, "Sample"));
132  sample_item->addStandardMaterials();
133  endInsertRows();
134  return indexForItem(sample_item);
135 }
void setSampleName(const QString &name)
QStringList sampleNames() const
QVector< MultiLayerItem * > sampleItems() const
MultiLayerItem * addMultiLayer()
Adds a sample and returns the new item.
QModelIndex indexForItem(MultiLayerItem *item) const
QString suggestName(const QStringList &existingNames, const QString &name)
Returns a name suggestion based on the given name.
Definition: String.cpp:26

References MultiLayerItems::addMultiLayer(), indexForItem(), m_sampleItems, MultiLayerItems::sampleItems(), MultiLayerItems::sampleNames(), MultiLayerItem::setSampleName(), and GUI::Util::String::suggestName().

Referenced by SampleListView::createNewSample().

Here is the call graph for this function:

◆ createSampleFromExamples()

QModelIndex SampleListModel::createSampleFromExamples ( const QString &  className,
const QString &  title,
const QString &  description 
)

Create sample from list of built-in examples.

Returns invalid index if anything went wrong.

Definition at line 137 of file SampleListModel.cpp.

140 {
141  try {
142  auto* sample =
143  dynamic_cast<MultiLayerItem*>(GUI::ExamplesFactory::itemizeSample(className));
144  if (!sample)
145  return {};
146  sample->setSampleName(title);
147  sample->setDescription(description);
148 
149  const int row = m_sampleItems->sampleItems().size();
150  beginInsertRows(QModelIndex(), row, row);
151  m_sampleItems->addMultiLayer(sample);
152  endInsertRows();
153  return indexForItem(sample);
154  } catch (const std::exception& ex) {
155  QString message("Exception thrown while trying to build GUI models.\n");
156  QString details = QString::fromStdString(ex.what());
157  DetailedMessageBox(GUI::Global::mainWindow, "Example builder failure", message, details)
158  .exec();
159  return {};
160  }
161 }
A dialog similar to standard QMessageBox intended for detailed warning messages. On the contrary to Q...
MultiLayerItem * itemizeSample(const QString &name)
Create a sample item of the built-in example with the given internal name.
static QMainWindow * mainWindow
Definition: Globals.h:22

References MultiLayerItems::addMultiLayer(), indexForItem(), GUI::ExamplesFactory::itemizeSample(), m_sampleItems, GUI::Global::mainWindow, MultiLayerItems::sampleItems(), and MultiLayerItem::setSampleName().

Referenced by SampleListView::createSampleFromLibrary().

Here is the call graph for this function:

◆ createSampleFromPython()

QModelIndex SampleListModel::createSampleFromPython ( )

Create sample from an imported python code.

Returns invalid index if anything went wrong.

Definition at line 164 of file SampleListModel.cpp.

165 {
166  auto sample = PyImportAssistant::importMultiLayer();
167  if (!sample)
168  return {}; // any messages already shown to user; no dlg necessary anymore
169 
170 
171  auto* sampleItem = PyImportAssistant::itemizeSample(*sample);
172  if (!sampleItem)
173  return {}; // any messages already shown to user; no dlg necessary anymore
174 
175  sampleItem->setDescription("Imported from python code");
176 
177  const int row = m_sampleItems->sampleItems().size();
178  beginInsertRows(QModelIndex(), row, row);
179  m_sampleItems->addMultiLayer(sampleItem);
180  endInsertRows();
181  return indexForItem(sampleItem);
182 }
MultiLayerItem * itemizeSample(const MultiLayer &sample)
Populates GUI sample with domain sample.
std::unique_ptr< MultiLayer > importMultiLayer()
Show select-file dialog, try to import via python, return created sample.

References MultiLayerItems::addMultiLayer(), PyImportAssistant::importMultiLayer(), indexForItem(), PyImportAssistant::itemizeSample(), m_sampleItems, and MultiLayerItems::sampleItems().

Referenced by SampleListView::importSampleFromPython().

Here is the call graph for this function:

◆ data()

QVariant SampleListModel::data ( const QModelIndex &  index,
int  role = Qt::DisplayRole 
) const
override

Definition at line 42 of file SampleListModel.cpp.

43 {
44  auto* const item = itemForIndex(index);
45 
46  if (role == Qt::ToolTipRole)
47  return item->description();
48 
49  if (role == Qt::DisplayRole) {
50  auto descr = item->description();
51  if (!descr.isEmpty()) {
52  descr.prepend("<br><br>");
53  const int maxDescriptionLines = 8;
54  while (descr.count("\n") >= maxDescriptionLines) {
55  descr.truncate(descr.lastIndexOf("\n"));
56  descr += " [...]";
57  }
58  descr.replace("\n", "<br>");
59  }
60  return "<b>" + item->sampleName() + "</b>" + descr;
61  }
62 
63  if (role == Qt::EditRole)
64  return item->sampleName();
65 
66  return QVariant();
67 }
MultiLayerItem * itemForIndex(const QModelIndex &index) const

References itemForIndex().

Here is the call graph for this function:

◆ flags()

Qt::ItemFlags SampleListModel::flags ( const QModelIndex &  index) const
override

Definition at line 69 of file SampleListModel.cpp.

70 {
71  auto f = QAbstractItemModel::flags(index);
72  f |= Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled;
73 
74  return f;
75 }

◆ indexForItem()

QModelIndex SampleListModel::indexForItem ( MultiLayerItem item) const

Definition at line 105 of file SampleListModel.cpp.

106 {
107  if (auto row = m_sampleItems->sampleItems().indexOf(item); row >= 0)
108  return index(row, 0);
109 
110  return QModelIndex();
111 }

References m_sampleItems, and MultiLayerItems::sampleItems().

Referenced by createSample(), createSampleFromExamples(), createSampleFromPython(), removeSample(), and SampleListView::setCurrentSample().

Here is the call graph for this function:

◆ itemForIndex()

MultiLayerItem * SampleListModel::itemForIndex ( const QModelIndex &  index) const

Definition at line 97 of file SampleListModel.cpp.

98 {
99  if (!index.isValid())
100  return nullptr;
101 
102  return m_sampleItems->sampleItems()[index.row()];
103 }

References m_sampleItems, and MultiLayerItems::sampleItems().

Referenced by SampleListView::currentSample(), data(), SampleListView::getOverlayActions(), SampleListView::onCurrentChanged(), setData(), and SampleListView::showContextMenu().

Here is the call graph for this function:

◆ removeSample()

void SampleListModel::removeSample ( MultiLayerItem item)

Remove the given sample. nullptr is allowed.

Definition at line 113 of file SampleListModel.cpp.

114 {
115  QModelIndex index = indexForItem(item);
116  if (!index.isValid())
117  return;
118 
119  beginRemoveRows(index.parent(), index.row(), index.row());
121  endRemoveRows();
122 }
void removeMultiLayer(MultiLayerItem *sample)

References indexForItem(), m_sampleItems, and MultiLayerItems::removeMultiLayer().

Referenced by SampleListView::removeSample().

Here is the call graph for this function:

◆ rowCount()

int SampleListModel::rowCount ( const QModelIndex &  parent = QModelIndex()) const
override

Definition at line 34 of file SampleListModel.cpp.

35 {
36  if (parent.isValid())
37  return 0;
38 
39  return m_sampleItems->sampleItems().size();
40 }

References m_sampleItems, and MultiLayerItems::sampleItems().

Here is the call graph for this function:

◆ setData()

bool SampleListModel::setData ( const QModelIndex &  index,
const QVariant &  value,
int  role 
)
override

Definition at line 77 of file SampleListModel.cpp.

78 {
79  if (!index.isValid())
80  return false;
81 
82  if (role == Qt::EditRole && index.column() == 0) {
83  itemForIndex(index)->setSampleName(value.toString());
84  emit dataChanged(index, index);
85  return true;
86  }
87 
88  if (role == Qt::ToolTipRole && index.column() == 0) {
89  itemForIndex(index)->setDescription(value.toString());
90  emit dataChanged(index, index);
91  return true;
92  }
93 
94  return false;
95 }
void setDescription(const QString &description)

References itemForIndex(), MultiLayerItem::setDescription(), and MultiLayerItem::setSampleName().

Here is the call graph for this function:

Member Data Documentation

◆ m_sampleItems

MultiLayerItems* SampleListModel::m_sampleItems = nullptr
private

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