BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
materialtreeview.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file gui2/materialeditor/materialtreeview.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 
16 #include <QHeaderView>
17 #include <QMouseEvent>
18 
19 namespace gui2 {
20 
22 
23 MaterialTreeView::MaterialTreeView(QWidget* parent) : QTreeView(parent)
24 {
25  setAlternatingRowColors(true);
26  setSelectionBehavior(QAbstractItemView::SelectRows);
27  setSelectionMode(QAbstractItemView::ExtendedSelection);
28  // setTabKeyNavigation(true);
29  header()->setSectionResizeMode(QHeaderView::Stretch);
30 }
31 
32 void MaterialTreeView::setModel(QAbstractItemModel* model)
33 {
34  QTreeView::setModel(model);
35  expandAll();
36 }
37 
38 void MaterialTreeView::keyPressEvent(QKeyEvent* event)
39 {
40  if (!event || event->key() != Qt::Key_Return || state() == QAbstractItemView::EditingState)
41  return QTreeView::keyPressEvent(event);
42 
43  const QModelIndex index = currentIndex();
44  if (isKeyboardEditable(index))
45  edit(index);
46 }
47 
48 QModelIndex MaterialTreeView::moveCursor(QAbstractItemView::CursorAction cursorAction,
49  Qt::KeyboardModifiers modifiers)
50 {
51  const QModelIndex current_index = currentIndex();
52  bool filtered_action = cursorAction == QAbstractItemView::MoveNext
53  || cursorAction == QAbstractItemView::MovePrevious;
54 
55  if (!current_index.isValid() || !isTextField(current_index) || !filtered_action)
56  return QTreeView::moveCursor(cursorAction, modifiers);
57 
58  QModelIndex next = current_index;
59  do {
60  setCurrentIndex(next);
61  next = QTreeView::moveCursor(cursorAction, modifiers);
62  } while (!isTextField(next));
63  return next;
64 }
65 
66 bool MaterialTreeView::isTextField(const QModelIndex& index) const
67 {
68  return index.isValid() && index.column() > 0; // color is not keyboard editable
69 }
70 
71 bool MaterialTreeView::isKeyboardEditable(const QModelIndex& index) const
72 {
73  return index.isValid();
74 }
75 
76 } // namespace gui2
bool isKeyboardEditable(const QModelIndex &index) const
QModelIndex moveCursor(QAbstractItemView::CursorAction cursorAction, Qt::KeyboardModifiers modifiers) override
bool isTextField(const QModelIndex &index) const
void setModel(QAbstractItemModel *model) override
void keyPressEvent(QKeyEvent *event) override
MaterialTreeView(QWidget *parent=nullptr)
~MaterialTreeView() override
Defines class CLASS?
Based on Qt example "codeeditor" Copyright (C) 2016 The Qt Company Ltd.
Definition: app_constants.h:20