Mantid
Loading...
Searching...
No Matches
QtStandardItemTreeAdapter.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7#include <utility>
8
14
16
18 : m_model(model), m_emptyCellStyle(std::move(emptyCellStyle)) {}
19
21
23 if (index.isValid()) {
24 m_model.removeRows(index.row(), 1, m_model.parent(index.untyped()));
25 } else {
26 m_model.removeRows(0, m_model.rowCount());
27 }
28}
29
31 auto newIndex = appendEmptyChildRow(index.parent());
32 return newIndex.sibling(newIndex.row(), index.column());
33}
34
36 std::vector<Cell> const &cells) {
37 auto newIndex = appendChildRow(index.parent(), cells);
38 return newIndex.sibling(newIndex.row(), index.column());
39}
40
42 return appendChildRow(parent, emptyRow(m_model.columnCount()));
43}
44
46 std::vector<Cell> const &cells) {
47 auto parentRow = QModelIndexForMainModel(firstCellOnRowOf(parent.untyped()));
48 auto *const parentItem = modelItemFromIndex(m_model, parentRow);
49 parentItem->appendRow(rowItemsFromCells(cells));
50 return QModelIndexForMainModel(lastChildRowOf(parentRow.untyped(), m_model));
51}
52
54 std::vector<Cell> const &cells) {
55 auto *const parentItem = modelItemFromIndex(m_model, parent);
56 parentItem->insertRow(row, rowItemsFromCells(cells));
57 return QModelIndexForMainModel(m_model.index(row, 0, parent.untyped()));
58}
59
61 int row) {
62 return insertChildRow(parent, row, emptyRow(m_model.columnCount()));
63}
64
65std::vector<Cell> QtStandardItemTreeModelAdapter::emptyRow(int columnCount) const {
66 return std::vector<Cell>(columnCount, m_emptyCellStyle);
67}
68
70 auto *item = modelItemFromIndex(m_model, cellIndex);
71 applyCellPropertiesToItem(cell, *item);
72}
73
75 std::vector<Cell> const &cells) {
76 enumerateCellsInRow(firstCellIndex, m_model.columnCount(),
77 [this, &cells](QModelIndexForMainModel const &cellIndex, int i) -> void {
78 auto *item = modelItemFromIndex(m_model, cellIndex);
79 applyCellPropertiesToItem(cells[i], *item);
80 });
81}
82
84 auto rowCells = QList<QStandardItem *>();
85 for (auto &&cell : cells) {
86 auto *item = new QStandardItem();
87 applyCellPropertiesToItem(cell, *item);
88 rowCells.append(item);
89 }
90 return rowCells;
91}
92
93std::vector<Cell> QtStandardItemTreeModelAdapter::cellsAtRow(QModelIndexForMainModel const &firstCellIndex) const {
94 auto cells = std::vector<Cell>();
95 cells.reserve(m_model.columnCount());
96 enumerateCellsInRow(firstCellIndex, m_model.columnCount(),
97 [this, &cells](QModelIndexForMainModel const &cellIndex, int /*unused*/) -> void {
98 cells.emplace_back(cellFromCellIndex(cellIndex));
99 });
100 return cells;
101}
102
104 auto *cellItem = modelItemFromIndex(m_model, index);
105 return extractCellPropertiesFromItem(*cellItem);
106}
107
108QStandardItem const *modelItemFromIndex(QStandardItemModel const &model, QModelIndexForMainModel const &index) {
109 if (index.isValid()) {
110 auto *item = model.itemFromIndex(index.untyped());
111 assertOrThrow(item != nullptr, "modelItemFromIndex: Index must point to a valid item.");
112 return item;
113 } else
114 return model.invisibleRootItem();
115}
116
117QStandardItem *modelItemFromIndex(QStandardItemModel &model, QModelIndexForMainModel const &index) {
118 if (index.isValid()) {
119 auto *item = model.itemFromIndex(index.untyped());
120 assertOrThrow(item != nullptr, "modelItemFromIndex: Index must point to a valid item.");
121 return item;
122 } else
123 return model.invisibleRootItem();
124}
125} // namespace MantidQt::MantidWidgets::Batch
void assertOrThrow(bool condition, std::string const &message)
See the developer documentation for Batch Widget at developer.mantidproject.org/BatchWidget/index....
Definition: AssertOrThrow.h:15
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
void setCellAtCellIndex(QModelIndexForMainModel const &index, Cell const &newCellProperties)
QModelIndexForMainModel appendEmptyChildRow(QModelIndexForMainModel const &parent)
QModelIndexForMainModel appendChildRow(QModelIndexForMainModel const &parent, std::vector< Cell > const &cells)
void setCellsAtRow(QModelIndexForMainModel const &rowIndex, std::vector< Cell > const &cells)
QModelIndexForMainModel appendEmptySiblingRow(QModelIndexForMainModel const &index)
std::vector< Cell > cellsAtRow(QModelIndexForMainModel const &firstCellIndex) const
QModelIndexForMainModel insertChildRow(QModelIndexForMainModel const &parent, int row, std::vector< Cell > const &cells)
QModelIndexForMainModel insertEmptyChildRow(QModelIndexForMainModel const &parent, int column)
void enumerateCellsInRow(QModelIndexForMainModel const &startIndex, int columns, Action const &action) const
Enumerates the first columnCount number of cells to the right of startAtCell, moving left to right.
Cell cellFromCellIndex(QModelIndexForMainModel const &index) const
QModelIndexForMainModel appendSiblingRow(QModelIndexForMainModel const &index, std::vector< Cell > const &cells)
QList< QStandardItem * > rowItemsFromCells(std::vector< Cell > const &cells)
QtStandardItemTreeModelAdapter(QStandardItemModel &model, Cell emptyCellStyle)
void applyCellPropertiesToItem(Cell const &cell, QStandardItem &item)
EXPORT_OPT_MANTIDQT_COMMON QStandardItem * modelItemFromIndex(QStandardItemModel &model, QModelIndexForMainModel const &index)
Cell extractCellPropertiesFromItem(QStandardItem const &item)
QModelIndex lastChildRowOf(QModelIndex const &index, QAbstractItemModel const &model)
QModelIndex firstCellOnRowOf(QModelIndex const &index)
STL namespace.