BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
importtablemodel.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file gui2/dataloader/importtablemodel.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 
18 #include <QBrush>
19 
20 namespace gui2 {
21 
22 namespace {
23 //! Returns maximum number of columns in 2D data vector.
24 int maxColumnCount(const ImportTableModel::raw_data_t& data)
25 {
26  int result{0};
27  for (const auto& x : data)
28  result = std::max(result, static_cast<int>(x.size()));
29  return result;
30 }
31 
32 const int default_header_ncols = 2;
33 }; // namespace
34 
36  : QAbstractTableModel(parent)
37  , m_header(std::make_unique<ImportTableHeader>(default_header_ncols))
38 {
39 }
40 
42 
43 //! Sets content of the model.
44 
46 {
47  beginResetModel();
48  m_maxColumnCount = maxColumnCount(raw_data);
49  m_header = std::make_unique<ImportTableHeader>(m_maxColumnCount);
50  m_rawData = raw_data;
51  endResetModel();
52 }
53 
54 int ImportTableModel::rowCount(const QModelIndex&) const
55 {
56  return static_cast<int>(m_rawData.size()) + utilityRowCount();
57 }
58 
59 int ImportTableModel::columnCount(const QModelIndex&) const
60 {
61  return m_maxColumnCount;
62 }
63 
64 QVariant ImportTableModel::data(const QModelIndex& index, int role) const
65 {
66  if (!index.isValid())
67  return QVariant();
68 
69  if (role == Qt::DisplayRole || role == Qt::EditRole)
70  return dataFromIndex(index);
71 
72  else if (role == Qt::BackgroundRole && index.row() < utilityRowCount())
73  return QBrush(Qt::lightGray);
74 
75  return QVariant();
76 }
77 
78 bool ImportTableModel::setData(const QModelIndex& index, const QVariant& value, int role)
79 {
80  if (!index.isValid())
81  return false;
82 
83  if (index.row() < utilityRowCount() && role == Qt::EditRole)
84  return m_header->setData(index.row(), index.column(), value);
85 
86  return false;
87 }
88 
89 QVariant ImportTableModel::headerData(int section, Qt::Orientation orientation, int role) const
90 {
91  if (orientation == Qt::Horizontal || role != Qt::DisplayRole)
92  return QVariant();
93 
94  return section < utilityRowCount() ? QString::fromStdString(m_header->rowName(section))
95  : QVariant(section - utilityRowCount() + 1);
96 }
97 
98 Qt::ItemFlags ImportTableModel::flags(const QModelIndex& index) const
99 {
100  Qt::ItemFlags result = QAbstractItemModel::flags(index);
101  if (index.row() < utilityRowCount())
102  result |= Qt::ItemIsEnabled | Qt::ItemIsEditable;
103  else
104  result |= Qt::ItemIsEnabled | Qt::ItemIsSelectable;
105  return result;
106 }
107 
108 std::vector<ColumnInfo> ImportTableModel::columnInfo() const
109 {
110  return m_header->columnInfo();
111 }
112 
114 {
115  return m_header ? m_header->rowCount() : 0;
116 }
117 
118 //! Returns data from index. Combines header data with parsed user data.
119 
120 QVariant ImportTableModel::dataFromIndex(const QModelIndex& index) const
121 {
122  if (!index.isValid())
123  return QVariant();
124 
125  if (index.row() < utilityRowCount()) {
126  return m_header->data(index.row(), index.column());
127 
128  } else {
129  int row = index.row() - utilityRowCount();
130  if (row >= 0 && row < static_cast<int>(m_rawData.size())) {
131  int col = index.column();
132  if (col >= 0 && col < static_cast<int>(m_rawData[row].size())) {
133  auto str = m_rawData[static_cast<int>(row)][static_cast<int>(col)];
134  return QString::fromStdString(str);
135  }
136  }
137  }
138  return QVariant();
139 }
140 
141 } // namespace gui2
Holds all data related to the content of utility rows in ImportTableModel.
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
std::unique_ptr< ImportTableHeader > m_header
void setRawData(const raw_data_t &raw_data)
Sets content of the model.
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Qt::ItemFlags flags(const QModelIndex &index) const override
ImportTableModel(QObject *parent=nullptr)
~ImportTableModel() override
std::vector< ColumnInfo > columnInfo() const
bool setData(const QModelIndex &index, const QVariant &value, int role) override
int rowCount(const QModelIndex &=QModelIndex()) const override
int m_maxColumnCount
parsed column data
std::vector< std::vector< std::string > > raw_data_t
QVariant dataFromIndex(const QModelIndex &index) const
Returns data from index. Combines header data with parsed user data.
int columnCount(const QModelIndex &=QModelIndex()) const override
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Based on Qt example "codeeditor" Copyright (C) 2016 The Qt Company Ltd.
Definition: app_constants.h:20
Definition: filesystem.h:81