Mantid
Loading...
Searching...
No Matches
RowLocationAdapter.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 +
9
10RowLocationAdapter::RowLocationAdapter(QStandardItemModel const &model) : m_model(model) {}
11
13 if (index.isValid()) {
14 auto pathComponents = RowPath();
15 auto currentIndex = index.untyped();
16 while (currentIndex.isValid()) {
17 pathComponents.insert(pathComponents.begin(), currentIndex.row());
18 currentIndex = currentIndex.parent();
19 }
20 return RowLocation(pathComponents);
21 } else {
22 return RowLocation();
23 }
24}
25
27 auto parentIndex = QModelIndex();
28 auto &path = location.path();
29 for (auto it = path.cbegin(); it != path.cend() - 1; ++it)
30 parentIndex = m_model.index(*it, 0, parentIndex);
31 return parentIndex;
32}
33
34boost::optional<QModelIndexForMainModel> RowLocationAdapter::indexIfExistsAt(RowLocation const &location,
35 int column) const {
36 if (location.isRoot()) {
37 return fromMainModel(QModelIndex(), m_model);
38 } else {
39 auto const parentIndex = walkFromRootToParentIndexOf(location);
40 auto const row = location.rowRelativeToParent();
41 if (m_model.hasIndex(row, column, parentIndex))
42 return fromMainModel(m_model.index(row, column, parentIndex), m_model);
43 else
44 return boost::none;
45 }
46}
47
49 auto maybeIndex = indexIfExistsAt(location, column);
50 if (maybeIndex.is_initialized())
51 return maybeIndex.get();
52 else {
53 throw std::runtime_error("indexAt: Attempted to get model index for "
54 "row location which does not exist.");
55 }
56}
57} // namespace MantidQt::MantidWidgets::Batch
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
RowLocation atIndex(QModelIndexForMainModel const &index) const
QModelIndex walkFromRootToParentIndexOf(RowLocation const &location) const
boost::optional< QModelIndexForMainModel > indexIfExistsAt(RowLocation const &location, int column=0) const
QModelIndexForMainModel indexAt(RowLocation const &location, int column=0) const
std::vector< int > RowPath
Definition: RowLocation.h:20
QModelIndexForMainModel fromMainModel(QModelIndex const &mainModelIndex, QAbstractItemModel const &model)