BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
SampleTreeWidget.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/coregui/Views/SampleDesigner/SampleTreeWidget.cpp
6 //! @brief Implements class SampleTreeWidget
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 
20 #include <QAction>
21 #include <QMenu>
22 #include <QVBoxLayout>
23 
25  : QWidget(parent), m_treeView(new ItemTreeView), m_sampleModel(model)
26 {
27  setWindowTitle("Sample Tree");
28  setObjectName(QLatin1String("SampleTreeWidget"));
29 
30  auto mainLayout = new QVBoxLayout;
31  mainLayout->setMargin(0);
32  mainLayout->setSpacing(0);
33  mainLayout->setContentsMargins(0, 0, 0, 0);
34  mainLayout->addWidget(m_treeView);
35  setLayout(mainLayout);
36 
37  FilterPropertyProxy* proxy = new FilterPropertyProxy(1, parent);
38  proxy->setSourceModel(m_sampleModel);
39  m_treeView->setModel(proxy);
40  m_treeView->setAttribute(Qt::WA_MacShowFocusRect, false);
41 
42  connect(m_treeView, &ItemTreeView::customContextMenuRequested, this,
44 
45  m_delete_item_action = new QAction("Delete", this);
46  m_delete_item_action->setStatusTip("Delete current object");
47  connect(m_delete_item_action, &QAction::triggered, this, &SampleTreeWidget::deleteItem);
48 
49  m_treeView->expandAll();
50  connect(m_treeView->model(), &QAbstractItemModel::rowsInserted, this,
51  [this]() { m_treeView->expandAll(); });
52 }
53 
55 {
56  return m_treeView;
57 }
58 
59 void SampleTreeWidget::showContextMenu(const QPoint& pnt)
60 {
61  QMenu menu;
62  QMenu add_menu("Add");
63  QVector<QString> addItemNames;
64  QModelIndex parent_index = FilterPropertyProxy::toSourceIndex(treeView()->indexAt(pnt));
65  treeView()->setCurrentIndex(parent_index);
66  if (!parent_index.isValid()) {
67  addItemNames = ItemFactory::ValidTopItemTypes().toVector();
68  } else {
69  addItemNames = m_sampleModel->acceptableDefaultItemTypes(parent_index);
70  }
71  if (addItemNames.size() > 0) {
72  for (QString item_name : addItemNames) {
73  QAction* add_action = nullptr;
74  if (m_add_action_map.contains(item_name)) {
75  add_action = m_add_action_map[item_name];
76  } else {
77  add_action = new QAction(item_name, this);
78  m_add_action_map[item_name] = add_action;
79  connect(add_action, &QAction::triggered, [=] { addItem(item_name); });
80  }
81  add_menu.addAction(add_action);
82  }
83  menu.addMenu(&add_menu);
84  }
85  if (parent_index.isValid()) {
86  menu.addAction(m_delete_item_action);
87  }
88  if (!menu.isEmpty()) {
89  menu.exec(treeView()->mapToGlobal(pnt));
90  }
91 }
92 
93 void SampleTreeWidget::addItem(const QString& item_name)
94 {
95  QModelIndex currentIndex = FilterPropertyProxy::toSourceIndex(treeView()->currentIndex());
96 
97  QModelIndex currentIndexAtColumnZero = getIndexAtColumnZero(currentIndex);
98  SessionItem* new_item = m_sampleModel->insertNewItem(item_name, currentIndexAtColumnZero);
99  if (new_item) {
100  QModelIndex new_index = m_sampleModel->indexOfItem(new_item);
101  scrollToIndex(new_index);
102  }
103 }
104 
106 {
107  QModelIndex currentIndex = FilterPropertyProxy::toSourceIndex(treeView()->currentIndex());
108 
109  if (!currentIndex.isValid())
110  return;
111  QModelIndex parent_index = m_sampleModel->parent(currentIndex);
112  int row = currentIndex.row();
113  if (currentIndex.isValid()) {
114  m_sampleModel->removeRows(row, 1, parent_index);
115  }
116 }
117 
118 void SampleTreeWidget::scrollToIndex(const QModelIndex& index)
119 {
120  if (index.isValid()) {
121  treeView()->scrollTo(index);
122  }
123 }
124 
125 QModelIndex SampleTreeWidget::getIndexAtColumnZero(const QModelIndex& index)
126 {
127  if (index == QModelIndex() || index.column() == 0)
128  return index;
129  QModelIndex parent_index = m_sampleModel->parent(index);
130  return m_sampleModel->index(index.row(), 0, parent_index);
131 }
Defines class FilterPropertyProxy.
Defines class ItemFactory.
Defines class ItemTreeView.
Defines class SampleModel.
Defines class SampleTreeWidget.
The FilterPropertyProxy class filters out all PropertyItem's and similar from SessionModel to have on...
static QModelIndex toSourceIndex(QModelIndex index)
Main model to hold sample items.
Definition: SampleModel.h:24
QMap< QString, QAction * > m_add_action_map
SampleTreeWidget(QWidget *parent, SampleModel *model)
SampleModel * m_sampleModel
void addItem(const QString &item_name)
void scrollToIndex(const QModelIndex &index)
QTreeView * treeView()
QAction * m_delete_item_action
ItemTreeView * m_treeView
QModelIndex getIndexAtColumnZero(const QModelIndex &index)
void showContextMenu(const QPoint &pnt)
virtual QModelIndex parent(const QModelIndex &child) const
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const
virtual bool removeRows(int row, int count, const QModelIndex &parent)
SessionItem * insertNewItem(QString model_type, SessionItem *parent_item=nullptr, int row=-1, QString tag="")
QVector< QString > acceptableDefaultItemTypes(const QModelIndex &parent) const
QModelIndex indexOfItem(SessionItem *item) const
QStringList ValidTopItemTypes()
retrieve list of all possible item types suitable for
Definition: ItemFactory.cpp:44