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