BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
layerselectionmodel.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file gui2/layereditor/layerselectionmodel.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 
16 #include "gui2/model/sampleitems.h"
19 #include <QItemSelection>
20 
21 namespace gui2 {
22 
24  : QItemSelectionModel(view_model, parent)
25 {
26  // FIXME cover with unit tests after implementing ViewItemSelectionModel
27  connect(view_model, &ModelView::ViewModel::modelAboutToBeReset, [this]() { clearSelection(); });
28 }
29 
30 //! Selects all rows corresponding to given items.
31 
32 void LayerSelectionModel::selectItems(std::vector<ModelView::SessionItem*> items)
33 {
34  QModelIndexList indexes;
35  for (auto item : items)
36  indexes << viewModel()->indexOfSessionItem(item->getItem(LayerItem::P_NAME));
37 
38  if (indexes.empty())
39  return;
40 
41  clearSelection();
42 
43  QItemSelection selection(indexes.front(), indexes.back());
44  auto flags = QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows;
45  select(selection, flags);
46  // setCurrentIndex(id, flags); What to do?
47 }
48 
49 //! Selects whole row corresponding to given item.
50 
52 {
53  selectItems({item});
54 }
55 
56 //! Returns vector of selected layers or multilayers.
57 //! We assume, that there is a single line selection mode switched on, and that
58 //! the columns contains property items related to either LayerItem or MultiLayerItem.
59 
60 std::vector<ModelView::SessionItem*> LayerSelectionModel::selectedItems() const
61 {
62  const QModelIndexList& selection = selectedRows();
63  if (selection.empty())
64  return {};
65 
66  std::vector<ModelView::SessionItem*> result;
67  for (const auto& index : selection)
68  if (auto item = viewModel()->sessionItemFromIndex(index); item)
69  result.push_back(item->parent());
70 
71  return result;
72 }
73 
74 //! Return the casted view model
76 {
77  return static_cast<const ModelView::ViewModel*>(model());
78 }
79 
80 //! Checks if the first row is presen in the selection
82 {
83  const QModelIndexList& selection = selectedRows();
84  for (const auto& index : selection) {
85  if (index.row() == 0)
86  return true;
87  }
88  return false;
89 }
90 
91 //! checks if the last row is present in the selection
93 {
94  const QModelIndexList& selection = selectedRows();
95  for (const auto& index : selection) {
96  if (index.row() == viewModel()->rowCount() - 1)
97  return true;
98  }
99  return false;
100 }
101 
102 } // 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
static const std::string P_NAME
Definition: sampleitems.h:41
bool firstSelected() const
Checks if the first row is presen in the selection.
bool lastSelected() const
checks if the last row is present in the selection
const ModelView::ViewModel * viewModel() const
Return the casted view model.
void selectItems(std::vector< ModelView::SessionItem * > items)
Selects all rows corresponding to given items.
LayerSelectionModel(ModelView::ViewModel *view_model, QObject *parent=nullptr)
void selectItem(ModelView::SessionItem *item)
Selects whole row corresponding to given item.
std::vector< ModelView::SessionItem * > selectedItems() const
Returns vector of selected layers or multilayers.
Defines class CLASS?
Based on Qt example "codeeditor" Copyright (C) 2016 The Qt Company Ltd.
Definition: app_constants.h:20
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?