BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
AbstractDataLoaderResultModel.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/coregui/DataLoaders/AbstractDataLoaderResultModel.cpp
6 //! @brief Implements class AbstractDataLoaderResultModel
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2021
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
16 #include <QColor>
17 #include <QIcon>
18 #include <QVariant>
19 
20 namespace {
21 struct Palette {
22  static QColor backgroundColorFileContent;
23  static QColor backgroundColorRawContent;
24  static QColor backgroundColorProcessedContent;
25  static QColor backgroundColorErrors;
26  static QColor skippedLineTextColor;
27 };
28 
29 QColor Palette::backgroundColorFileContent(239, 237, 248);
30 QColor Palette::backgroundColorRawContent(247, 240, 210);
31 QColor Palette::backgroundColorProcessedContent(191, 232, 242);
32 QColor Palette::backgroundColorErrors(247, 140, 146);
33 QColor Palette::skippedLineTextColor(Qt::lightGray);
34 
35 } // namespace
36 
38 {
39  m_warningIcon = QIcon(":/images/warning_16x16.png");
40 }
41 
42 int AbstractDataLoaderResultModel::columnCount(const QModelIndex& parent /*= QModelIndex()*/) const
43 {
44  if (parent.isValid())
45  return 0;
46 
49 
50  int cols = 0;
51  for (ColumnType type : colTypes)
52  cols += columnCount(type);
53 
54  return cols;
55 }
56 
57 int AbstractDataLoaderResultModel::rowCount(const QModelIndex& parent) const
58 {
59  return parent.isValid() ? 0 : rowCount();
60 }
61 
62 QVariant AbstractDataLoaderResultModel::data(const QModelIndex& index,
63  int role /*= Qt::DisplayRole*/) const
64 {
65  if (!index.isValid())
66  return QVariant();
67 
68  const auto colType = columnType(index);
69 
70  if (role == Qt::BackgroundRole) {
71  switch (colType) {
72  case ColumnType::line:
73  return Palette::backgroundColorFileContent;
75  return Palette::backgroundColorFileContent;
76  case ColumnType::raw:
77  return rowIsSkipped(index) ? QVariant() : Palette::backgroundColorRawContent;
79  return rowIsSkipped(index) || rowHasError(index)
80  ? QVariant()
81  : Palette::backgroundColorProcessedContent;
82  case ColumnType::error:
83  return rowIsSkipped(index) || !rowHasError(index) ? QVariant()
84  : Palette::backgroundColorErrors;
85  default:
86  return QVariant();
87  }
88  }
89 
90  if (role == Qt::ForegroundRole) {
91  if (colType == ColumnType::fileContent && rowIsSkipped(index))
92  return Palette::skippedLineTextColor;
93  return QVariant();
94  }
95 
96  if (role == Qt::DisplayRole) {
97  switch (colType) {
98  case ColumnType::line:
99  return QString::number(index.row() + 1);
100 
102  QString lineContent =
103  cellText(colType, index.row(), index.column() - firstSectionOfColumnType(colType));
104  lineContent.replace("\t", " --> ");
105  return lineContent;
106  }
107 
108  case ColumnType::raw:
109  case ColumnType::error: {
110  if (!rowIsSkipped(index))
111  return cellText(colType, index.row(),
112  index.column() - firstSectionOfColumnType(colType));
113  }
114 
115  case ColumnType::processed: {
116  if (!rowIsSkipped(index) && !rowHasError(index))
117  return cellText(colType, index.row(),
118  index.column() - firstSectionOfColumnType(colType));
119  }
120  }
121 
122  return QVariant();
123  }
124 
125  if (role == Qt::DecorationRole && (colType == ColumnType::line || colType == ColumnType::error)
126  && rowHasError(index))
127  return m_warningIcon;
128 
129  if (role == Qt::TextAlignmentRole && colType == ColumnType::line)
130  return QVariant(Qt::AlignRight | Qt::AlignVCenter);
131 
132  if (role == Qt::ToolTipRole && colType == ColumnType::line && rowHasError(index))
133  return cellText(ColumnType::error, index.row(), 0);
134 
135  return QVariant();
136 }
137 
138 QVariant AbstractDataLoaderResultModel::headerData(int section, Qt::Orientation orientation,
139  int role) const
140 {
141  if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
142  switch (columnType(section)) {
143  case ColumnType::line:
144  return "Line";
146  return "File content (text)";
147  case ColumnType::raw:
148  return QString("Column %1 raw")
149  .arg(section - firstSectionOfColumnType(ColumnType::raw) + 1);
151  return headerTextOfCalculatedColumn(section
153  case ColumnType::error:
154  return "Parser warnings";
155  default:
156  return QVariant();
157  }
158  }
159 
160  return QAbstractTableModel::headerData(section, orientation, role);
161 }
162 
164 {
165  QVector<int> sections;
166  for (int section = firstSectionOfColumnType(type); section <= lastSectionOfColumnType(type);
167  section++)
168  if (section >= 0)
169  sections << section;
170 
171  return sections;
172 }
173 
176 {
179 
180  for (ColumnType type : colTypes) {
181  const int firstSection = firstSectionOfColumnType(type);
182  if (firstSection < 0)
183  continue;
184 
185  if (section >= firstSection && section <= lastSectionOfColumnType(type))
186  return type;
187  }
188 
189  return ColumnType::none;
190 }
191 
193 AbstractDataLoaderResultModel::columnType(const QModelIndex& index) const
194 {
195  return columnType(index.column());
196 }
197 
199 {
200  const int lineColumnCount = columnCount(ColumnType::line);
201  const int fileContentColumnCount = columnCount(ColumnType::fileContent);
202 
203  if (type == ColumnType::line)
204  return lineColumnCount > 0 ? 0 : -1;
205 
206  if (type == ColumnType::fileContent)
207  return columnCount(ColumnType::fileContent) > 0 ? lineColumnCount : -1;
208 
209  if (type == ColumnType::raw) {
210  const bool hasRawContent = columnCount(ColumnType::raw) > 0;
211  return hasRawContent ? lineColumnCount + fileContentColumnCount : -1;
212  }
213 
214  if (type == ColumnType::processed) {
215  const bool hasProcessedContent = columnCount(ColumnType::processed) > 0;
216  return hasProcessedContent
217  ? (lineColumnCount + fileContentColumnCount + columnCount(ColumnType::raw))
218  : -1;
219  }
220 
221  if (type == ColumnType::error) {
222  const bool hasParsingErrors = columnCount(ColumnType::error) > 0;
223  return hasParsingErrors
224  ? (lineColumnCount + fileContentColumnCount + columnCount(ColumnType::raw)
226  : -1;
227  }
228 
229  return -1;
230 }
231 
233 {
234  const int firstSection = firstSectionOfColumnType(type);
235  if (firstSection == -1)
236  return -1;
237 
238  return firstSection + columnCount(type) - 1;
239 }
240 
241 bool AbstractDataLoaderResultModel::rowIsSkipped(const QModelIndex& index) const
242 {
243  return rowIsSkipped(index.row());
244 }
245 
246 bool AbstractDataLoaderResultModel::rowHasError(const QModelIndex& index) const
247 {
248  return rowHasError(index.row());
249 }
Defines class AbstractDataLoaderResultModel.
virtual QString headerTextOfCalculatedColumn(int column) const =0
Return the table header text for the given column.
virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
int firstSectionOfColumnType(ColumnType type) const
Calculates the first real header view section of the given column type.
QVector< int > sectionsOfColumnType(ColumnType type) const
The table header sections which belong to the given column type.
virtual bool rowIsSkipped(const QModelIndex &index) const
Returns whether the row given in the index is a skipped row.
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const override
virtual QString cellText(ColumnType type, int row, int column) const =0
The text of the given cell.
virtual bool rowHasError(const QModelIndex &index) const
Returns whether the row given in the index contains errors.
ColumnType columnType(const QModelIndex &index) const
Calculates the column type of the given index.
virtual int rowCount() const =0
The row count of the result table.
int lastSectionOfColumnType(ColumnType type) const
Calculates the last real header view section of the given column type.
virtual QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
QVariant DecorationRole(const SessionItem &item)
Returns tooltip for given item.
QVariant ToolTipRole(const SessionItem &item, int ncol=0)
Returns tooltip for given item.
QVariant ForegroundRole(const SessionItem &item)
Returns text color for given item.