BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
itemstreeview.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // qt-mvvm: Model-view-view-model framework for large GUI applications
4 //
5 //! @file mvvm/view/mvvm/widgets/itemstreeview.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 Gennady Pospelov et al, Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
18 #include <QTreeView>
19 #include <QVBoxLayout>
20 
21 using namespace ModelView;
22 
24  : QWidget(parent)
25  , m_treeView(new QTreeView)
26  , m_delegate(std::make_unique<ViewModelDelegate>())
27  , m_block_selection(false)
28 {
29  auto layout = new QVBoxLayout;
30  layout->setMargin(0);
31  layout->setSpacing(0);
32  layout->addWidget(m_treeView);
33  setLayout(layout);
34 }
35 
37 
38 void ItemsTreeView::setViewModel(std::unique_ptr<ViewModel> viewModel)
39 {
40  m_viewModel = std::move(viewModel);
41  m_treeView->setItemDelegate(m_delegate.get());
42  m_treeView->setModel(m_viewModel.get());
43  m_treeView->expandAll();
44  m_treeView->resizeColumnToContents(0);
45  set_connected(true);
46 }
47 
48 void ItemsTreeView::setViewModelDelegate(std::unique_ptr<ViewModelDelegate> delegate)
49 {
50  m_delegate = std::move(delegate);
51 }
52 
53 //! Make given item selected in QTreeView.
54 
56 {
57  if (!m_viewModel)
58  return;
59 
60  auto indexes = m_viewModel->indexOfSessionItem(item);
61  if (!indexes.empty())
62  selectionModel()->select(indexes.at(0), QItemSelectionModel::SelectCurrent);
63 }
64 
66 {
67  m_viewModel->setRootSessionItem(item);
68  m_treeView->expandAll();
69 }
70 
72 {
73  return m_viewModel.get();
74 }
75 
76 //! Processes selections in QTreeView. Finds SessionItem corresponding to selected indexes
77 //! and emit itemSelected signal.
78 
79 void ItemsTreeView::onSelectionChanged(const QItemSelection&, const QItemSelection&)
80 {
82  return;
83 
84  auto indexes = m_treeView->selectionModel()->selectedIndexes();
85  if (!indexes.empty()) {
86  auto item = m_viewModel->sessionItemFromIndex(indexes.at(0));
87  m_block_selection = true;
88  itemSelected(item);
89  m_block_selection = false;
90  }
91 }
92 
94 {
95  Q_ASSERT(selectionModel());
96 
97  if (flag)
98  connect(selectionModel(), &QItemSelectionModel::selectionChanged, this,
100  else
101  disconnect(selectionModel(), &QItemSelectionModel::selectionChanged, this,
103 }
104 
106 {
107  return m_treeView;
108 }
109 
110 QItemSelectionModel* ItemsTreeView::selectionModel()
111 {
112  return m_treeView->selectionModel();
113 }
void setRootSessionItem(SessionItem *item)
std::unique_ptr< ViewModelDelegate > m_delegate
Definition: itemstreeview.h:67
std::unique_ptr< ViewModel > m_viewModel
Definition: itemstreeview.h:66
void setViewModelDelegate(std::unique_ptr< ViewModelDelegate > delegate)
QItemSelectionModel * selectionModel()
void set_connected(bool flag)
void onSelectionChanged(const QItemSelection &, const QItemSelection &)
Processes selections in QTreeView.
void setViewModel(std::unique_ptr< ViewModel > viewModel)
void itemSelected(ModelView::SessionItem *)
void setSelected(SessionItem *item)
Make given item selected in QTreeView.
ItemsTreeView(QWidget *parent=nullptr)
ViewModel * viewModel() const
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
Model delegate to provide editing/painting for custom variants.
Main class to represent content of SessionModel in Qt's trees and tables.
Definition: viewmodel.h:29
Defines class CLASS?
materialitems.h Collection of materials to populate MaterialModel.
Definition: filesystem.h:81
Defines class CLASS?
Defines class CLASS?