Changes the filter-text and refilters the data
| 74 | |
| 75 | ///Changes the filter-text and refilters the data |
| 76 | void setFilter(const QString& text) |
| 77 | { |
| 78 | if (m_oldFilterText == text) { |
| 79 | return; |
| 80 | } |
| 81 | if (text.isEmpty()) { |
| 82 | clearFilter(); |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | const QVector<Item> filterBase = text.startsWith(m_oldFilterText) ? |
| 87 | m_filtered : |
| 88 | m_items; //Start filtering based on the whole data |
| 89 | |
| 90 | m_filtered.clear(); |
| 91 | |
| 92 | QStringList typedFragments = text.split(QStringLiteral("::"), Qt::SkipEmptyParts); |
| 93 | if (typedFragments.isEmpty()) { |
| 94 | clearFilter(); |
| 95 | return; |
| 96 | } |
| 97 | if (typedFragments.last().endsWith(QLatin1Char(':'))) { |
| 98 | // remove the trailing colon if there's only one; otherwise, |
| 99 | // this breaks incremental filtering |
| 100 | typedFragments.last().chop(1); |
| 101 | } |
| 102 | if (typedFragments.size() == 1 && typedFragments.last().isEmpty()) { |
| 103 | clearFilter(); |
| 104 | return; |
| 105 | } |
| 106 | for (const Item& data : filterBase) { |
| 107 | const QString& itemData = itemText(data); |
| 108 | if (itemData.contains(text, Qt::CaseInsensitive) || matchesAbbreviationMulti(itemData, typedFragments)) { |
| 109 | m_filtered << data; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | m_oldFilterText = text; |
| 114 | } |
| 115 | |
| 116 | protected: |
| 117 | ///Should return the text an item should be filtered by. |