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

Description

List view to select one sample (left side of layer-oriented sample editor)

Definition at line 25 of file SampleListView.h.

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

Public Slots

void onSampleModified ()
 

Signals

void currentSampleChanged (MultiLayerItem *current)
 

Public Member Functions

 SampleListView (QWidget *parent, ProjectDocument *document)
 
QAction * chooseFromLibraryAction ()
 
MultiLayerItemcurrentSample ()
 
QAction * importSampleAction ()
 
QAction * newSampleAction ()
 
void selectFirstSample ()
 Select first sample, if any sample exists. More...
 
void setCurrentSample (MultiLayerItem *sample)
 
QSize sizeHint () const override
 

Private Member Functions

void createNewSample ()
 
QAction * createRemoveAction (QObject *parent, MultiLayerItem *item)
 
void createSampleFromLibrary (const QString &classname, const QString &title, const QString &description)
 
QList< QAction * > getOverlayActions (const QModelIndex &index, bool asHover)
 
void importSampleFromPython ()
 
void onCurrentChanged (const QModelIndex &index)
 
void removeSample (MultiLayerItem *item)
 
void showContextMenu (const QPoint &pos)
 

Private Attributes

QAction * m_chooseFromLibraryAction
 
ProjectDocumentm_document
 
QAction * m_importSampleAction
 
SampleListModelm_model
 
QAction * m_newSampleAction
 

Constructor & Destructor Documentation

◆ SampleListView()

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

Definition at line 64 of file SampleListView.cpp.

65  : QListView(parent)
66  , m_document(document)
67 {
68  m_model = new SampleListModel(this, document->sampleItems());
69 
70  setContextMenuPolicy(Qt::CustomContextMenu);
71  setModel(m_model);
72  setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
73 
75  this, [=](const QModelIndex& i, bool h) { return getOverlayActions(i, h); });
76  setItemDelegate(new ItemDelegateForSampleTree(this));
77 
78  connect(selectionModel(), &QItemSelectionModel::currentChanged, this,
80 
81  connect(this, &QWidget::customContextMenuRequested, this, &SampleListView::showContextMenu);
82 
83  m_newSampleAction = new QAction(this);
84  m_newSampleAction->setText("Create new sample");
85  m_newSampleAction->setIcon(QIcon(":/images/shape-square-plus.svg"));
86  m_newSampleAction->setIconText("New");
87  m_newSampleAction->setToolTip("Create new sample");
88  connect(m_newSampleAction, &QAction::triggered, this, &SampleListView::createNewSample);
89 
90  m_importSampleAction = new QAction(this);
91  m_importSampleAction->setText("Import sample from python script (experimental)");
92  m_importSampleAction->setIcon(QIcon(":/images/import.svg"));
93  m_importSampleAction->setIconText("Import");
94  m_importSampleAction->setToolTip(
95  "Import sample from Python script.\n The script should contain a function "
96  "returning a valid multi-layer.");
97 
98 #ifdef Q_OS_MAC
99  if (BaseUtils::System::getenv("PYTHONHOME").empty())
100  m_importSampleAction->setEnabled(false);
101 #endif
102 
103 #ifdef BORNAGAIN_PYTHON
104  connect(m_importSampleAction, &QAction::triggered, this,
106 #else
107  m_importSampleAction->setVisible(false);
108 #endif
109 
110  m_chooseFromLibraryAction = new QAction(this);
111  m_chooseFromLibraryAction->setText("Choose from sample examples");
112  m_chooseFromLibraryAction->setIcon(QIcon(":/images/library.svg"));
113  m_chooseFromLibraryAction->setIconText("Examples");
114  m_chooseFromLibraryAction->setToolTip("Choose from sample examples");
115 
116  auto* menu = new QMenu(this);
117  m_chooseFromLibraryAction->setMenu(menu);
118 
119  for (const auto& exampleName : GUI::ExamplesFactory::exampleNames()) {
120  QString title, description;
121  std::tie(title, description) = GUI::ExamplesFactory::exampleInfo(exampleName);
122  auto icon = QIcon(":/SampleDesignerToolbox/images/sample_layers2.png");
123  auto* action = menu->addAction(icon, title);
124  action->setToolTip(description);
125  connect(action, &QAction::triggered,
126  [=]() { createSampleFromLibrary(exampleName, title, description); });
127  }
128 }
static void install(QAbstractItemView *view, FnGetActions fnGetActions)
MultiLayerItems * sampleItems()
List model for sample selection (used in the left pane of the layer oriented sample editor)
QAction * m_chooseFromLibraryAction
ProjectDocument * m_document
SampleListModel * m_model
QList< QAction * > getOverlayActions(const QModelIndex &index, bool asHover)
void onCurrentChanged(const QModelIndex &index)
void showContextMenu(const QPoint &pos)
QAction * m_newSampleAction
void createSampleFromLibrary(const QString &classname, const QString &title, const QString &description)
void importSampleFromPython()
QAction * m_importSampleAction
QStringList exampleNames()
The internal example name, e.g. for creation with itemizeSample.
std::tuple< QString, QString > exampleInfo(const QString &name)
Returns human readable name and description.

References createNewSample(), createSampleFromLibrary(), GUI::ExamplesFactory::exampleInfo(), GUI::ExamplesFactory::exampleNames(), getOverlayActions(), importSampleFromPython(), ItemViewOverlayButtons::install(), m_chooseFromLibraryAction, m_importSampleAction, m_model, m_newSampleAction, onCurrentChanged(), ProjectDocument::sampleItems(), and showContextMenu().

Here is the call graph for this function:

Member Function Documentation

◆ chooseFromLibraryAction()

QAction * SampleListView::chooseFromLibraryAction ( )

Definition at line 157 of file SampleListView.cpp.

158 {
160 }

References m_chooseFromLibraryAction.

Referenced by SampleView::SampleView().

◆ createNewSample()

void SampleListView::createNewSample ( )
private

Definition at line 176 of file SampleListView.cpp.

177 {
178  const QModelIndex newIndex = m_model->createSample();
179  setCurrentIndex(newIndex);
181 }
QModelIndex createSample()
Create a new sample (sample) and return the index of it.

References SampleListModel::createSample(), m_document, m_model, and ProjectDocument::setModified().

Referenced by SampleListView().

Here is the call graph for this function:

◆ createRemoveAction()

QAction * SampleListView::createRemoveAction ( QObject *  parent,
MultiLayerItem item 
)
private

Definition at line 227 of file SampleListView.cpp.

228 {
229  auto* removeAction = new QAction(parent);
230  removeAction->setText("Remove");
231  removeAction->setIcon(QIcon(":/images/delete.svg"));
232  removeAction->setIconText("Remove");
233  removeAction->setToolTip("Remove this sample");
234  connect(removeAction, &QAction::triggered, [=]() { removeSample(item); });
235 
236  return removeAction;
237 }
void removeSample(MultiLayerItem *item)

References removeSample().

Referenced by getOverlayActions(), and showContextMenu().

Here is the call graph for this function:

◆ createSampleFromLibrary()

void SampleListView::createSampleFromLibrary ( const QString &  classname,
const QString &  title,
const QString &  description 
)
private

Definition at line 183 of file SampleListView.cpp.

185 {
186  const QModelIndex newIndex = m_model->createSampleFromExamples(classname, title, description);
187  if (newIndex.isValid()) {
188  setCurrentIndex(newIndex);
190  }
191 }
QModelIndex createSampleFromExamples(const QString &className, const QString &title, const QString &description)
Create sample from list of built-in examples.

References SampleListModel::createSampleFromExamples(), m_document, m_model, and ProjectDocument::setModified().

Referenced by SampleListView().

Here is the call graph for this function:

◆ currentSample()

MultiLayerItem * SampleListView::currentSample ( )

Definition at line 135 of file SampleListView.cpp.

136 {
137  return m_model->itemForIndex(currentIndex());
138 }
MultiLayerItem * itemForIndex(const QModelIndex &index) const

References SampleListModel::itemForIndex(), and m_model.

Referenced by SampleView::onRequestViewInRealSpace(), and SampleView::showEvent().

Here is the call graph for this function:

◆ currentSampleChanged

void SampleListView::currentSampleChanged ( MultiLayerItem current)
signal

◆ getOverlayActions()

QList< QAction * > SampleListView::getOverlayActions ( const QModelIndex &  index,
bool  asHover 
)
private

Definition at line 204 of file SampleListView.cpp.

205 {
206  if (!asHover)
207  return {};
208 
209  auto* item = m_model->itemForIndex(index);
210  if (item == nullptr)
211  return {};
212 
213  return {createRemoveAction(this, item)};
214 }
QAction * createRemoveAction(QObject *parent, MultiLayerItem *item)

References createRemoveAction(), SampleListModel::itemForIndex(), and m_model.

Referenced by SampleListView().

Here is the call graph for this function:

◆ importSampleAction()

QAction * SampleListView::importSampleAction ( )

Definition at line 152 of file SampleListView.cpp.

153 {
154  return m_importSampleAction;
155 }

References m_importSampleAction.

Referenced by SampleView::SampleView().

◆ importSampleFromPython()

void SampleListView::importSampleFromPython ( )
private

Definition at line 193 of file SampleListView.cpp.

194 {
195 #ifdef BORNAGAIN_PYTHON
196  const QModelIndex newIndex = m_model->createSampleFromPython();
197  if (newIndex.isValid()) {
198  setCurrentIndex(newIndex);
200  }
201 #endif
202 }
QModelIndex createSampleFromPython()
Create sample from an imported python code.

References SampleListModel::createSampleFromPython(), m_document, m_model, and ProjectDocument::setModified().

Referenced by SampleListView().

Here is the call graph for this function:

◆ newSampleAction()

QAction * SampleListView::newSampleAction ( )

Definition at line 147 of file SampleListView.cpp.

148 {
149  return m_newSampleAction;
150 }

References m_newSampleAction.

Referenced by SampleView::SampleView().

◆ onCurrentChanged()

void SampleListView::onCurrentChanged ( const QModelIndex &  index)
private

Definition at line 216 of file SampleListView.cpp.

217 {
219 }
void currentSampleChanged(MultiLayerItem *current)

References currentSampleChanged(), SampleListModel::itemForIndex(), and m_model.

Referenced by SampleListView().

Here is the call graph for this function:

◆ onSampleModified

void SampleListView::onSampleModified ( )
slot

Definition at line 169 of file SampleListView.cpp.

170 {
171  // if length of description changes height of TreeItem, the position of the overlay buttons
172  // has to be updated -> schedule a re-layout
173  scheduleDelayedItemsLayout();
174 }

Referenced by SampleView::SampleView().

◆ removeSample()

void SampleListView::removeSample ( MultiLayerItem item)
private

Definition at line 221 of file SampleListView.cpp.

222 {
223  m_model->removeSample(item);
225 }
void removeSample(MultiLayerItem *item)
Remove the given sample. nullptr is allowed.

References m_document, m_model, SampleListModel::removeSample(), and ProjectDocument::setModified().

Referenced by createRemoveAction().

Here is the call graph for this function:

◆ selectFirstSample()

void SampleListView::selectFirstSample ( )

Select first sample, if any sample exists.

Definition at line 140 of file SampleListView.cpp.

141 {
142  if (m_document->sampleItems()->sampleItems().isEmpty())
143  return;
145 }
QVector< MultiLayerItem * > sampleItems() const
void setCurrentSample(MultiLayerItem *sample)

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

Referenced by SampleView::showEvent().

Here is the call graph for this function:

◆ setCurrentSample()

void SampleListView::setCurrentSample ( MultiLayerItem sample)

Definition at line 130 of file SampleListView.cpp.

131 {
132  setCurrentIndex(m_model->indexForItem(sample));
133 }
QModelIndex indexForItem(MultiLayerItem *item) const

References SampleListModel::indexForItem(), and m_model.

Referenced by selectFirstSample().

Here is the call graph for this function:

◆ showContextMenu()

void SampleListView::showContextMenu ( const QPoint &  pos)
private

Definition at line 239 of file SampleListView.cpp.

240 {
241  auto* sampleAtPoint = m_model->itemForIndex(indexAt(pos));
242 
243  QMenu menu;
244  menu.setToolTipsVisible(true);
245 
246  if (sampleAtPoint != nullptr) {
247  menu.addAction(createRemoveAction(&menu, sampleAtPoint));
248  menu.addSeparator();
249  }
250 
251  menu.addAction(m_newSampleAction);
252  menu.addAction(m_chooseFromLibraryAction);
253  menu.exec(mapToGlobal(pos));
254 }

References createRemoveAction(), SampleListModel::itemForIndex(), m_chooseFromLibraryAction, m_model, and m_newSampleAction.

Referenced by SampleListView().

Here is the call graph for this function:

◆ sizeHint()

QSize SampleListView::sizeHint ( ) const
override

Definition at line 162 of file SampleListView.cpp.

163 {
164  QSize s = QListView::sizeHint();
165  s.setWidth(std::max(300, s.width()));
166  return s;
167 }

Member Data Documentation

◆ m_chooseFromLibraryAction

QAction* SampleListView::m_chooseFromLibraryAction
private

Definition at line 66 of file SampleListView.h.

Referenced by SampleListView(), chooseFromLibraryAction(), and showContextMenu().

◆ m_document

ProjectDocument* SampleListView::m_document
private

◆ m_importSampleAction

QAction* SampleListView::m_importSampleAction
private

Definition at line 65 of file SampleListView.h.

Referenced by SampleListView(), and importSampleAction().

◆ m_model

◆ m_newSampleAction

QAction* SampleListView::m_newSampleAction
private

Definition at line 64 of file SampleListView.h.

Referenced by SampleListView(), newSampleAction(), and showContextMenu().


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