File indexing completed on 2024-04-21 03:56:04
0001 /* 0002 SPDX-FileCopyrightText: 2009 Stephen Kelly <steveire@gmail.com> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #ifndef INDEXFINDER_H 0008 #define INDEXFINDER_H 0009 0010 #include <QModelIndex> 0011 0012 class IndexFinder 0013 { 0014 public: 0015 IndexFinder(QList<int> rows = QList<int>()) 0016 : m_rows(rows) 0017 , m_model(nullptr) 0018 { 0019 } 0020 0021 IndexFinder(const QAbstractItemModel *model, QList<int> rows = QList<int>()) 0022 : m_rows(rows) 0023 , m_model(model) 0024 { 0025 Q_ASSERT(model); 0026 } 0027 0028 QModelIndex getIndex() const 0029 { 0030 if (!m_model) { 0031 return QModelIndex(); 0032 } 0033 static const int col = 0; 0034 QModelIndex parent = QModelIndex(); 0035 QListIterator<int> i(m_rows); 0036 while (i.hasNext()) { 0037 parent = m_model->index(i.next(), col, parent); 0038 Q_ASSERT(parent.isValid()); 0039 } 0040 return parent; 0041 } 0042 0043 static IndexFinder indexToIndexFinder(const QModelIndex &_idx) 0044 { 0045 if (!_idx.isValid()) { 0046 return IndexFinder(); 0047 } 0048 0049 QList<int> list; 0050 QModelIndex idx = _idx; 0051 while (idx.isValid()) { 0052 list.prepend(idx.row()); 0053 idx = idx.parent(); 0054 } 0055 return IndexFinder(_idx.model(), list); 0056 } 0057 0058 bool operator==(const IndexFinder &other) const 0059 { 0060 return (m_rows == other.m_rows && m_model == other.m_model); 0061 } 0062 0063 QList<int> rows() const 0064 { 0065 return m_rows; 0066 } 0067 void appendRow(int row) 0068 { 0069 m_rows.append(row); 0070 } 0071 void setRows(const QList<int> &rows) 0072 { 0073 m_rows = rows; 0074 } 0075 void setModel(QAbstractItemModel *model) 0076 { 0077 m_model = model; 0078 } 0079 0080 private: 0081 QList<int> m_rows; 0082 const QAbstractItemModel *m_model; 0083 }; 0084 0085 Q_DECLARE_METATYPE(IndexFinder) 0086 0087 #endif