File indexing completed on 2024-05-19 05:44:25
0001 /* 0002 SPDX-FileCopyrightText: 2015-2017 Milian Wolff <mail@milianw.de> 0003 0004 SPDX-License-Identifier: LGPL-2.1-or-later 0005 */ 0006 0007 #include "stacksmodel.h" 0008 #include "treemodel.h" 0009 0010 #include <KLocalizedString> 0011 0012 #include <QDebug> 0013 0014 StacksModel::StacksModel(QObject* parent) 0015 : QAbstractListModel(parent) 0016 { 0017 } 0018 0019 StacksModel::~StacksModel() = default; 0020 0021 void StacksModel::setStackIndex(int index) 0022 { 0023 beginResetModel(); 0024 m_stackIndex = index - 1; 0025 endResetModel(); 0026 } 0027 0028 static void findLeafs(const QModelIndex& index, QVector<QModelIndex>* leafs) 0029 { 0030 auto model = index.model(); 0031 Q_ASSERT(model); 0032 int rows = model->rowCount(index); 0033 if (!rows) { 0034 leafs->append(index); 0035 return; 0036 } 0037 for (int i = 0; i < rows; ++i) { 0038 findLeafs(model->index(i, 0, index), leafs); 0039 } 0040 } 0041 0042 void StacksModel::fillFromIndex(const QModelIndex& index) 0043 { 0044 if (index.column() != 0) { 0045 // only the first column has children 0046 fillFromIndex(index.sibling(index.row(), 0)); 0047 return; 0048 } 0049 0050 QVector<QModelIndex> leafs; 0051 findLeafs(index, &leafs); 0052 0053 beginResetModel(); 0054 m_data.clear(); 0055 m_data.resize(leafs.size()); 0056 m_stackIndex = 0; 0057 int stackIndex = 0; 0058 for (auto leaf : leafs) { 0059 auto& stack = m_data[stackIndex]; 0060 while (leaf.isValid()) { 0061 stack << leaf.sibling(leaf.row(), TreeModel::LocationColumn); 0062 leaf = leaf.parent(); 0063 } 0064 std::reverse(stack.begin(), stack.end()); 0065 ++stackIndex; 0066 } 0067 endResetModel(); 0068 0069 emit stacksFound(m_data.size()); 0070 } 0071 0072 void StacksModel::clear() 0073 { 0074 beginResetModel(); 0075 m_data.clear(); 0076 endResetModel(); 0077 emit stacksFound(0); 0078 } 0079 0080 int StacksModel::rowCount(const QModelIndex& parent) const 0081 { 0082 if (parent.isValid() || m_data.isEmpty()) { 0083 return 0; 0084 } 0085 return m_data.value(m_stackIndex).size(); 0086 } 0087 0088 QVariant StacksModel::data(const QModelIndex& index, int role) const 0089 { 0090 if (!hasIndex(index.row(), index.column(), index.parent())) { 0091 return {}; 0092 } 0093 return m_data.value(m_stackIndex).value(index.row()).data(role); 0094 } 0095 0096 QVariant StacksModel::headerData(int section, Qt::Orientation orientation, int role) const 0097 { 0098 if (section == 0 && role == Qt::DisplayRole && orientation == Qt::Horizontal) { 0099 return i18n("Backtrace"); 0100 } 0101 return {}; 0102 } 0103 0104 #include "moc_stacksmodel.cpp"