| 18 | using namespace GammaRay; |
| 19 | |
| 20 | QModelIndexList ModelUtils::match(const QModelIndex &start, int role, MatchAcceptor accept, |
| 21 | int hits, Qt::MatchFlags flags) |
| 22 | { |
| 23 | if (!start.isValid() || role < 0) |
| 24 | return QModelIndexList(); |
| 25 | |
| 26 | const QAbstractItemModel *model = start.model(); |
| 27 | const QModelIndex parentIndex = model->parent(start); |
| 28 | bool recurse = flags & Qt::MatchRecursive; |
| 29 | bool wrap = flags & Qt::MatchWrap; |
| 30 | bool allHits = (hits == -1); |
| 31 | int from = start.row(); |
| 32 | int to = model->rowCount(parentIndex); |
| 33 | |
| 34 | QModelIndexList result; |
| 35 | |
| 36 | // iterates twice if wrapping |
| 37 | for (int i = 0; (wrap && i < 2) || (!wrap && i < 1); ++i) { |
| 38 | for (int r = from; (r < to) && (allHits || result.size() < hits); ++r) { |
| 39 | QModelIndex idx = model->index(r, start.column(), parentIndex); |
| 40 | if (!idx.isValid()) |
| 41 | continue; |
| 42 | |
| 43 | const QVariant v = model->data(idx, role); |
| 44 | if (accept(v)) |
| 45 | result << idx; |
| 46 | |
| 47 | // search the hierarchy |
| 48 | if (recurse && model->hasChildren(idx)) { |
| 49 | result += match(model->index(0, idx.column(), idx), role, |
| 50 | accept, (allHits ? -1 : hits - result.size()), flags); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // prepare for the next iteration |
| 55 | from = 0; |
| 56 | to = start.row(); |
| 57 | } |
| 58 | |
| 59 | return result; |
| 60 | } |