BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
SampleTreeWidget Class Reference

Holds tree to select top level sample items. Part of SampleView. More...

Inheritance diagram for SampleTreeWidget:
[legend]
Collaboration diagram for SampleTreeWidget:
[legend]

Public Member Functions

 SampleTreeWidget (QWidget *parent, SampleModel *model)
 
QTreeView * treeView ()
 

Protected Slots

void addItem (const QString &item_name)
 
void deleteItem ()
 
void showContextMenu (const QPoint &pnt)
 

Private Member Functions

QModelIndex getIndexAtColumnZero (const QModelIndex &index)
 
void scrollToIndex (const QModelIndex &index)
 

Private Attributes

QMap< QString, QAction * > m_add_action_map
 
QAction * m_delete_item_action
 
SampleModelm_sampleModel
 
ItemTreeViewm_treeView
 

Detailed Description

Holds tree to select top level sample items. Part of SampleView.

Definition at line 29 of file SampleTreeWidget.h.

Constructor & Destructor Documentation

◆ SampleTreeWidget()

SampleTreeWidget::SampleTreeWidget ( QWidget *  parent,
SampleModel model 
)

Definition at line 24 of file SampleTreeWidget.cpp.

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 }
The FilterPropertyProxy class filters out all PropertyItem's and similar from SessionModel to have on...
SampleModel * m_sampleModel
QAction * m_delete_item_action
ItemTreeView * m_treeView
void showContextMenu(const QPoint &pnt)

References deleteItem(), m_delete_item_action, m_sampleModel, m_treeView, and showContextMenu().

Here is the call graph for this function:

Member Function Documentation

◆ addItem

void SampleTreeWidget::addItem ( const QString &  item_name)
protectedslot

Definition at line 93 of file SampleTreeWidget.cpp.

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 }
static QModelIndex toSourceIndex(QModelIndex index)
void scrollToIndex(const QModelIndex &index)
QTreeView * treeView()
QModelIndex getIndexAtColumnZero(const QModelIndex &index)
SessionItem * insertNewItem(QString model_type, SessionItem *parent_item=nullptr, int row=-1, QString tag="")
QModelIndex indexOfItem(SessionItem *item) const

References getIndexAtColumnZero(), SessionModel::indexOfItem(), SessionModel::insertNewItem(), m_sampleModel, scrollToIndex(), FilterPropertyProxy::toSourceIndex(), and treeView().

Referenced by showContextMenu().

Here is the call graph for this function:

◆ deleteItem

void SampleTreeWidget::deleteItem ( )
protectedslot

Definition at line 105 of file SampleTreeWidget.cpp.

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 }
virtual QModelIndex parent(const QModelIndex &child) const
virtual bool removeRows(int row, int count, const QModelIndex &parent)

References m_sampleModel, SessionModel::parent(), SessionModel::removeRows(), FilterPropertyProxy::toSourceIndex(), and treeView().

Referenced by SampleTreeWidget().

Here is the call graph for this function:

◆ getIndexAtColumnZero()

QModelIndex SampleTreeWidget::getIndexAtColumnZero ( const QModelIndex &  index)
private

Definition at line 125 of file SampleTreeWidget.cpp.

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 }
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const

References SessionModel::index(), m_sampleModel, and SessionModel::parent().

Referenced by addItem().

Here is the call graph for this function:

◆ scrollToIndex()

void SampleTreeWidget::scrollToIndex ( const QModelIndex &  index)
private

Definition at line 118 of file SampleTreeWidget.cpp.

119 {
120  if (index.isValid()) {
121  treeView()->scrollTo(index);
122  }
123 }

References treeView().

Referenced by addItem().

Here is the call graph for this function:

◆ showContextMenu

void SampleTreeWidget::showContextMenu ( const QPoint &  pnt)
protectedslot

Definition at line 59 of file SampleTreeWidget.cpp.

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 }
QMap< QString, QAction * > m_add_action_map
void addItem(const QString &item_name)
QVector< QString > acceptableDefaultItemTypes(const QModelIndex &parent) const
QStringList ValidTopItemTypes()
retrieve list of all possible item types suitable for
Definition: ItemFactory.cpp:44

References SessionModel::acceptableDefaultItemTypes(), addItem(), m_add_action_map, m_delete_item_action, m_sampleModel, FilterPropertyProxy::toSourceIndex(), treeView(), and ItemFactory::ValidTopItemTypes().

Referenced by SampleTreeWidget().

Here is the call graph for this function:

◆ treeView()

QTreeView * SampleTreeWidget::treeView ( )

Definition at line 54 of file SampleTreeWidget.cpp.

55 {
56  return m_treeView;
57 }

References m_treeView.

Referenced by addItem(), SampleView::createSubWindows(), deleteItem(), scrollToIndex(), and showContextMenu().

Member Data Documentation

◆ m_add_action_map

QMap<QString, QAction*> SampleTreeWidget::m_add_action_map
private

Definition at line 45 of file SampleTreeWidget.h.

Referenced by showContextMenu().

◆ m_delete_item_action

QAction* SampleTreeWidget::m_delete_item_action
private

Definition at line 46 of file SampleTreeWidget.h.

Referenced by SampleTreeWidget(), and showContextMenu().

◆ m_sampleModel

SampleModel* SampleTreeWidget::m_sampleModel
private

◆ m_treeView

ItemTreeView* SampleTreeWidget::m_treeView
private

Definition at line 48 of file SampleTreeWidget.h.

Referenced by SampleTreeWidget(), and treeView().


The documentation for this class was generated from the following files: