BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
materialselectionmodel.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file gui2/materialeditor/materialselectionmodel.cpp
6 //! @brief Implements class CLASS?
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2020
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
19 
20 namespace gui2 {
21 
23  : QItemSelectionModel(view_model, parent)
24 {
25  // FIXME cover with unit tests after implementing ViewItemSelectionModel
26  connect(view_model, &ModelView::ViewModel::modelAboutToBeReset, [this]() { clearSelection(); });
27 }
28 
30 {
31  selectItems({item});
32 }
33 
34 void MaterialSelectionModel::selectItems(std::vector<ModelView::SessionItem*> items)
35 {
36  QModelIndexList indexes;
37  for (auto item : items)
38  indexes << viewModel()->indexOfSessionItem(item->getItem(MaterialBaseItem::P_NAME));
39 
40  if (indexes.empty())
41  return;
42 
43  clearSelection();
44 
45  QItemSelection selection(indexes.front(), indexes.back());
46  auto flags = QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows;
47  select(selection, flags);
48 }
49 
50 //! Returns vector of selected materials.
51 
52 std::vector<MaterialBaseItem*> MaterialSelectionModel::selectedMaterials() const
53 {
54  std::vector<MaterialBaseItem*> result;
55  auto selected_items = ModelView::Utils::ParentItemsFromIndex(selectedIndexes());
56  std::transform(std::begin(selected_items), std::end(selected_items), std::back_inserter(result),
57  [](auto item) { return dynamic_cast<MaterialBaseItem*>(item); });
58  return result;
59 }
60 
62 {
63  return static_cast<const ModelView::ViewModel*>(model());
64 }
65 
66 } // namespace gui2
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
Main class to represent content of SessionModel in Qt's trees and tables.
Definition: viewmodel.h:29
QModelIndexList indexOfSessionItem(const SessionItem *item) const
Returns list of model indices representing given SessionItem.
Definition: viewmodel.cpp:71
Base class with all materials with name and color defined.
Definition: materialitems.h:40
static const std::string P_NAME
Definition: materialitems.h:43
void selectItems(std::vector< ModelView::SessionItem * > items)
const ModelView::ViewModel * viewModel() const
void selectItem(ModelView::SessionItem *item)
std::vector< MaterialBaseItem * > selectedMaterials() const
Returns vector of selected materials.
MaterialSelectionModel(ModelView::ViewModel *view_model, QObject *parent=nullptr)
Defines class CLASS?
Defines class CLASS?
MVVM_VIEWMODEL_EXPORT std::vector< SessionItem * > ParentItemsFromIndex(const QModelIndexList &index_list)
Returns vector of parent items from given index list.
Based on Qt example "codeeditor" Copyright (C) 2016 The Qt Company Ltd.
Definition: app_constants.h:20
Defines class CLASS?
Defines class CLASS?