BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
ComponentTreeView.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/coregui/Views/PropertyEditor/ComponentTreeView.cpp
6 //! @brief Implements class ComponentTreeView
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
22 #include <QBoxLayout>
23 #include <QStandardItemModel>
24 #include <QTreeView>
25 
27  : ComponentView(parent)
28  , m_tree(new QTreeView)
29  , m_delegate(new SessionModelDelegate(this))
30  , m_proxyModel(new ComponentProxyModel(this))
31  , m_placeHolderModel(new QStandardItemModel(this))
32  , m_eventFilter(new RightMouseButtonEater)
33  , m_actions(new ComponentTreeActions(this))
34  , m_show_root_item(false)
35 {
36  auto layout = new QVBoxLayout;
37 
38  layout->setMargin(0);
39  layout->setSpacing(0);
40  layout->addWidget(m_tree);
41 
42  setLayout(layout);
43 
44  QStringList labels = {"Name", "Value"};
45  m_placeHolderModel->setHorizontalHeaderLabels(labels);
46 
48  m_tree->setRootIsDecorated(false);
49  m_tree->setModel(m_placeHolderModel);
50  m_tree->setItemDelegate(m_delegate);
51 
52  // provide one click editing, but still keeping custom context menu alive
53  m_tree->setEditTriggers(QAbstractItemView::AllEditTriggers);
54  m_tree->viewport()->installEventFilter(m_eventFilter.get());
55 
56  // custom context menu setup
57  m_tree->setContextMenuPolicy(Qt::CustomContextMenu);
58  connect(m_tree, &QTreeView::customContextMenuRequested, this,
60 }
61 
63 
65 {
66  if (!item) {
67  setModel(nullptr);
68  return;
69  }
70  setModel(item->model());
72  m_tree->expandAll();
73 }
74 
76 {
77  m_tree->setModel(m_placeHolderModel);
78 }
79 
81 {
83  if (model)
84  m_tree->setModel(m_proxyModel);
85  else
86  m_tree->setModel(m_placeHolderModel);
87 }
88 
89 void ComponentTreeView::setRootIndex(const QModelIndex& index, bool show_root_item)
90 {
91  if (QWidget* editor = m_tree->indexWidget(m_tree->currentIndex()))
92  m_delegate->closeEditor(editor, QAbstractItemDelegate::NoHint);
94  m_proxyModel->setRootIndex(index);
95  if (!show_root_item)
96  m_tree->setRootIndex(m_proxyModel->mapFromSource(index));
97 }
98 
100 {
101  m_tree->setHeaderHidden(!show);
102 }
103 
105 {
106  m_show_root_item = show;
107 }
108 
110 {
111  auto point = m_tree->mapToGlobal(pos);
112  auto treeIndex = m_tree->indexAt(pos);
113  if (!treeIndex.isValid())
114  return;
115 
116  auto index = m_proxyModel->mapToSource(treeIndex);
117 
118  SessionItem* item = static_cast<SessionItem*>(index.internalPointer());
119  if (item->value().type() != QVariant::Double)
120  return;
121 
123 }
#define ASSERT(condition)
Definition: Assert.h:31
Defines class ComponentProxyModel.
Defines class ComponentTreeActions.
Defines class ComponentTreeView.
Defines classes releted to event filtering.
Defines class SessionModelDelegate.
Defines class SessionModel.
DefinesStyleUtils namespace.
Proxy model to adjust SessionModel for component editor (right bottom corner of SampleView and simila...
void setRootIndex(const QModelIndex &sourceRootIndex)
QModelIndex mapFromSource(const QModelIndex &sourceIndex) const
QModelIndex mapToSource(const QModelIndex &proxyIndex) const
void setSessionModel(SessionModel *model)
Additional action for ComponentTreeView.
void onCustomContextMenuRequested(const QPoint &point, SessionItem &item)
Creates right-mouse-click context menu on top of ComponentTreeView which will allow user to switch be...
SessionModelDelegate * m_delegate
std::unique_ptr< RightMouseButtonEater > m_eventFilter
ComponentTreeView(QWidget *parent=nullptr)
void setItem(SessionItem *item)
bool m_show_root_item
Tree will starts from item itself, if true.
QStandardItemModel * m_placeHolderModel
void setModel(SessionModel *model)
ComponentProxyModel * m_proxyModel
void setRootIndex(const QModelIndex &index, bool show_root_item=true)
void setShowRootItem(bool show)
void onCustomContextMenuRequested(const QPoint &pos)
ComponentTreeActions * m_actions
void setShowHeader(bool show)
Base class for ComponentTreeView and ComponentFlatView.
Definition: ComponentView.h:24
Filter out right mouse button events.
QVariant value() const
Get value.
SessionModel * model() const
Returns model of this item.
Definition: SessionItem.cpp:66
QModelIndex index() const
Returns model index of this item.
Definition: SessionItem.cpp:80
The SessionModelDelegate class presents the content of SessionModel items in standard QTreeView.
void setPropertyStyle(QTreeView *tree)
Sets style for the tree to use in property editors.
Definition: StyleUtils.cpp:36