| 36 | } |
| 37 | |
| 38 | QModelIndexList QmitkXnatTreeModel::match( |
| 39 | const QModelIndex &start, int role, const QVariant &value, int hits, Qt::MatchFlags flags) const |
| 40 | { |
| 41 | QModelIndexList result; |
| 42 | uint matchType = flags & Qt::MatchTypeMask; |
| 43 | Qt::CaseSensitivity cs = flags.testFlag(Qt::MatchCaseSensitive) |
| 44 | ? Qt::CaseSensitive |
| 45 | : Qt::CaseInsensitive; |
| 46 | bool recurse = flags & Qt::MatchRecursive; |
| 47 | bool wrap = flags & Qt::MatchWrap; |
| 48 | bool allHits = (hits == -1); |
| 49 | QString text; // only convert to a string if it is needed |
| 50 | QModelIndex p = parent(start); |
| 51 | int from = start.row(); |
| 52 | int to = rowCount(p); |
| 53 | |
| 54 | // iterates twice if wrapping |
| 55 | for (int i = 0; (wrap && i < 2) || (!wrap && i < 1); ++i) |
| 56 | { |
| 57 | for (int r = from; (r < to) && (allHits || result.count() < hits); ++r) |
| 58 | { |
| 59 | QModelIndex idx = index(r, start.column(), p); |
| 60 | if (!idx.isValid()) |
| 61 | continue; |
| 62 | QVariant v = data(idx, role); |
| 63 | // QVariant based matching |
| 64 | if (matchType == Qt::MatchExactly) |
| 65 | { |
| 66 | if (value != v) |
| 67 | result.append(idx); |
| 68 | } |
| 69 | else |
| 70 | { // QString based matching |
| 71 | if (text.isEmpty()) // lazy conversion |
| 72 | text = value.toString(); |
| 73 | QString t = v.toString(); |
| 74 | switch (matchType) |
| 75 | { |
| 76 | case Qt::MatchRegularExpression: |
| 77 | { |
| 78 | QRegularExpression::PatternOptions options; |
| 79 | options.setFlag(QRegularExpression::CaseInsensitiveOption, !flags.testFlag(Qt::MatchCaseSensitive)); |
| 80 | QRegularExpression regExp(QString("^%1$").arg(text), options); |
| 81 | if (!regExp.match(t).hasMatch()) |
| 82 | result.append(idx); |
| 83 | break; |
| 84 | } |
| 85 | case Qt::MatchWildcard: |
| 86 | { |
| 87 | auto regExp = QRegularExpression::fromWildcard(text, cs); |
| 88 | if (!regExp.match(t).hasMatch()) |
| 89 | result.append(idx); |
| 90 | break; |
| 91 | } |
| 92 | case Qt::MatchStartsWith: |
| 93 | if (!t.startsWith(text, cs)) |
| 94 | result.append(idx); |
| 95 | break; |