1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
/*
* This file is part of KDevelop
*
* Copyright 2007 David Nolden <david.nolden.kdevelop@art-master.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef KDEVPLATFORM_QUICKOPEN_FILTER_H
#define KDEVPLATFORM_QUICKOPEN_FILTER_H
#include <QStringList>
#include "abbreviations.h"
#include <util/path.h>
namespace KDevelop {
/**
* This is a simple filter-implementation that helps you implementing own quickopen data-providers.
* You should use it when possible, because that way additional features(like regexp filtering) can
* be implemented in a central place.
*
* This implementation does incremental filtering while
* typing text, so it quite efficient for the most common case.
*
* The simplest way of using this is by reimplementing your data-provider
* based on QuickOpenDataProviderBase and KDevelop::Filter\<Item\>.
*
* What you need to do to use it:
*
* Reimplement itemText(..) to provide the text filtering
* should be performed on (This must be efficient).
*
* Call setItems(..) when starting a new quickopen session, or when the content
* changes, to initialize the filter with your data.
*
* Call setFilter(..) with the text that should be filtered for on user-input.
*
* Use filteredItems() to provide data to quickopen.
*
* @tparam Item should be the type that holds all the information you need.
* The filter will hold the data, and you can access it through "items()".
*/
template <class Item>
class Filter
{
public:
virtual ~Filter()
{
}
///Clears the filter, but not the data.
void clearFilter()
{
m_filtered = m_items;
m_oldFilterText.clear();
}
///Clears the filter and sets new data. The filter-text will be lost.
void setItems(const QVector<Item>& data)
{
m_items = data;
clearFilter();
}
const QVector<Item>& items() const
{
return m_items;
}
///Returns the data that is left after the filtering
const QVector<Item>& filteredItems() const
{
return m_filtered;
}
///Changes the filter-text and refilters the data
void setFilter(const QString& text)
{
if (m_oldFilterText == text) {
return;
}
if (text.isEmpty()) {
clearFilter();
return;
}
const QVector<Item> filterBase = text.startsWith(m_oldFilterText) ?
m_filtered :
m_items; //Start filtering based on the whole data
m_filtered.clear();
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
QStringList typedFragments = text.split(QStringLiteral("::"), Qt::SkipEmptyParts);
#else
QStringList typedFragments = text.split(QStringLiteral("::"), QString::SkipEmptyParts);
#endif
if (typedFragments.isEmpty()) {
clearFilter();
return;
}
if (typedFragments.last().endsWith(QLatin1Char(':'))) {
// remove the trailing colon if there's only one; otherwise,
// this breaks incremental filtering
typedFragments.last().chop(1);
}
if (typedFragments.size() == 1 && typedFragments.last().isEmpty()) {
clearFilter();
return;
}
for (const Item& data : filterBase) {
const QString& itemData = itemText(data);
if (itemData.contains(text, Qt::CaseInsensitive) || matchesAbbreviationMulti(itemData, typedFragments)) {
m_filtered << data;
}
}
m_oldFilterText = text;
}
protected:
///Should return the text an item should be filtered by.
virtual QString itemText(const Item& data) const = 0;
private:
QString m_oldFilterText;
QVector<Item> m_filtered;
QVector<Item> m_items;
};
template <class Item, class Parent>
class PathFilter
{
public:
///Clears the filter, but not the data.
void clearFilter()
{
m_filtered = m_items;
m_oldFilterText.clear();
}
///Clears the filter and sets new data. The filter-text will be lost.
void setItems(const QVector<Item>& data)
{
m_items = data;
clearFilter();
}
const QVector<Item>& items() const
{
return m_items;
}
///Returns the data that is left after the filtering
const QVector<Item>& filteredItems() const
{
return m_filtered;
}
///Changes the filter-text and refilters the data
void setFilter(const QStringList& text)
{
if (m_oldFilterText == text) {
return;
}
if (text.isEmpty()) {
clearFilter();
return;
}
QVector<Item> filterBase = m_filtered;
if (m_oldFilterText.isEmpty()) {
filterBase = m_items;
} else if (m_oldFilterText.mid(0, m_oldFilterText.count() - 1) == text.mid(0, text.count() - 1)
&& text.last().startsWith(m_oldFilterText.last())) {
//Good, the prefix is the same, and the last item has been extended
} else if (m_oldFilterText.size() == text.size() - 1 && m_oldFilterText == text.mid(0, text.size() - 1)) {
//Good, an item has been added
} else {
//Start filtering based on the whole data, there was a big change to the filter
filterBase = m_items;
}
QVector<QPair<int, int>> matches;
for (int i = 0, c = filterBase.size(); i < c; ++i) {
const auto& data = filterBase.at(i);
const auto matchQuality = matchPathFilter(static_cast<Parent*>(this)->itemPath(data), text,
static_cast<Parent*>(this)->itemPrefixPath(data));
if (matchQuality == -1) {
continue;
}
matches.push_back({matchQuality, i});
}
std::stable_sort(matches.begin(), matches.end(),
[](const QPair<int, int>& lhs, const QPair<int, int>& rhs)
{
return lhs.first < rhs.first;
});
m_filtered.resize(matches.size());
std::transform(matches.begin(), matches.end(), m_filtered.begin(),
[&filterBase](const QPair<int, int>& match) {
return filterBase.at(match.second);
});
m_oldFilterText = text;
}
private:
QStringList m_oldFilterText;
QVector<Item> m_filtered;
QVector<Item> m_items;
};
}
#endif
|