BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
dataselectionmodel.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file gui2/importdataview/dataselectionmodel.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 
18 #include "mvvm/model/itemutils.h"
19 #include "mvvm/model/mvvm_types.h"
20 #include "mvvm/model/sessionitem.h"
24 
25 namespace gui2 {
26 
27 //! The constructor
29  : QItemSelectionModel(view_model, parent)
30 {
31  // FIXME cover with unit tests after implementing ViewItemSelectionModel
32  connect(view_model, &ModelView::ViewModel::modelAboutToBeReset, [this]() { clearSelection(); });
33 }
34 
35 //! Set the selection on a single item
37 {
38  selectItems({item});
39 }
40 
41 //! Set the selection on a list of items
42 void DataSelectionModel::selectItems(std::vector<ModelView::SessionItem*> items)
43 {
44  QModelIndexList indexes;
45  for (auto item : items)
46  indexes << viewModel()->indexOfSessionItem(item);
47 
48  if (indexes.empty())
49  return;
50 
51  clearSelection();
52 
53  QItemSelection selection(indexes.front(), indexes.back());
54  auto flags = QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows;
55  select(selection, flags);
56 }
57 
58 //! Return the selected items
59 std::vector<ModelView::SessionItem*> DataSelectionModel::selectedItems() const
60 {
61  auto items = ModelView::Utils::ItemsFromIndex(selectedIndexes());
62  return ModelView::Utils::UniqueItems(items);
63 }
64 
66 {
67  return static_cast<const ModelView::ViewModel*>(model());
68 }
69 
70 //! Returns active canvas. The canvas is active when it is either selected, or one of its own
71 //! graph is selected. If more than one canvas is selected, will return the first one.
72 
74 {
75  for (auto item : selectedItems()) {
76  if (item->modelType() == Constants::CanvasItemType)
77  return static_cast<CanvasItem*>(item);
78  else if (item->modelType() == ModelView::Constants::GraphItemType)
79  return static_cast<CanvasItem*>(item->parent());
80  }
81  return nullptr;
82 }
83 
84 //! Returns currently selected graph. If more than one graph is selected, will return first one.
85 
87 {
88  auto graphs = selectedGraphs();
89  return graphs.empty() ? nullptr : graphs.at(0);
90 }
91 
92 //! Returns vector of currently slected canvas.
93 
94 std::vector<CanvasItem*> DataSelectionModel::selectedCanvas() const
95 {
96  return ModelView::Utils::CastedItems<CanvasItem>(selectedItems());
97 }
98 
99 std::vector<ModelView::GraphItem*> DataSelectionModel::selectedGraphs() const
100 {
101  return ModelView::Utils::CastedItems<ModelView::GraphItem>(selectedItems());
102 }
103 
104 } // namespace gui2
One-dimensional graph representation of Data1DItem.
Definition: graphitem.h:29
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
Holds a collection of GraphItem's for simultaneous plotting, as well as all information related to pl...
CanvasItem * activeCanvas() const
Returns active canvas.
void selectItem(ModelView::SessionItem *item)
Set the selection on a single item.
std::vector< CanvasItem * > selectedCanvas() const
Returns vector of currently slected canvas.
const ModelView::ViewModel * viewModel() const
void selectItems(std::vector< ModelView::SessionItem * > items)
Set the selection on a list of items.
ModelView::GraphItem * selectedGraph() const
Returns currently selected graph. If more than one graph is selected, will return first one.
std::vector< ModelView::SessionItem * > selectedItems() const
Return the selected items.
DataSelectionModel(ModelView::ViewModel *view_model, QObject *parent=nullptr)
The constructor.
std::vector< ModelView::GraphItem * > selectedGraphs() const
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
const model_type GraphItemType
Definition: mvvm_types.h:53
MVVM_VIEWMODEL_EXPORT std::vector< SessionItem * > ItemsFromIndex(const QModelIndexList &index_list)
Returns vector of underlying SessionItem's for given index list.
MVVM_MODEL_EXPORT std::vector< SessionItem * > UniqueItems(const std::vector< SessionItem * > &items)
Returns vector with duplicates and 'nullptr' filtered out.
Definition: itemutils.cpp:170
const std::string CanvasItemType
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?