Commit 5a345b58 authored by Waqar Ahmed's avatar Waqar Ahmed Committed by Christoph Cullmann
Browse files

quickopen: Allow wildcard matching (optionally)

Its trivial to add and if people want it, why not.

utilities/kate!897
parent ec289573
Pipeline #239315 canceled with stage
in 5 minutes and 53 seconds
......@@ -52,12 +52,16 @@ protected:
return l < r;
}
bool filterAcceptsRow(int sourceRow, const QModelIndex &) const override
bool filterAcceptsRow(int sourceRow, const QModelIndex &parent) const override
{
if (pattern.isEmpty()) {
return true;
}
if (filterMode == Wildcard) {
return QSortFilterProxyModel::filterAcceptsRow(sourceRow, parent);
}
auto sm = static_cast<KateQuickOpenModel *>(sourceModel());
if (!sm->isValid(sourceRow)) {
return false;
......@@ -125,6 +129,12 @@ public Q_SLOTS:
return false;
}
if (filterMode == Wildcard) {
pattern = splitted;
setFilterWildcard(pattern);
return true;
}
beginResetModel();
pattern = splitted;
matchPath = pattern.contains(QLatin1Char('/'));
......@@ -133,6 +143,16 @@ public Q_SLOTS:
return true;
}
void setFilterMode(FilterMode m)
{
beginResetModel();
filterMode = m;
if (!pattern.isEmpty() && m == Wildcard) {
setFilterWildcard(pattern);
}
endResetModel();
}
private:
static inline bool filterByPath(QStringView path, QStringView pattern, int &score)
{
......@@ -147,6 +167,7 @@ private:
private:
QString pattern;
bool matchPath = false;
FilterMode filterMode = Fuzzy;
};
class QuickOpenStyleDelegate : public QStyledItemDelegate
......@@ -159,6 +180,11 @@ public:
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
{
if (m_filterMode != Fuzzy) {
QStyledItemDelegate::paint(painter, option, index);
return;
}
QStyleOptionViewItem options = option;
initStyleOption(&options, index);
......@@ -228,8 +254,14 @@ public Q_SLOTS:
m_filterString = text;
}
void setFilterMode(FilterMode m)
{
m_filterMode = m;
}
private:
QString m_filterString;
FilterMode m_filterMode;
};
Q_DECLARE_METATYPE(QPointer<KTextEditor::Document>)
......@@ -254,45 +286,39 @@ KateQuickOpen::KateQuickOpen(KateMainWindow *mainWindow)
m_listView->setTextElideMode(Qt::ElideLeft);
m_listView->setUniformRowHeights(true);
m_base_model = new KateQuickOpenModel(this);
m_model = new QuickOpenFilterProxyModel(this);
m_model->setFilterRole(Qt::DisplayRole);
m_model->setSortRole(KateQuickOpenModel::Score);
m_model->setFilterCaseSensitivity(Qt::CaseInsensitive);
m_model->setSortCaseSensitivity(Qt::CaseInsensitive);
m_model->setFilterKeyColumn(Qt::DisplayRole);
m_model = new KateQuickOpenModel(this);
m_styleDelegate = new QuickOpenStyleDelegate(this);
m_listView->setItemDelegate(m_styleDelegate);
connect(m_inputLine, &QuickOpenLineEdit::textChanged, m_styleDelegate, &QuickOpenStyleDelegate::setFilterString);
connect(m_inputLine, &QuickOpenLineEdit::textChanged, this, [this](const QString &text) {
if (m_model->setFilterText(text)) {
m_styleDelegate->setFilterString(text);
m_listView->viewport()->update();
reselectFirst(); // hacky way
}
});
connect(m_inputLine, &QuickOpenLineEdit::textChanged, this, &KateQuickOpen::reselectFirst, Qt::QueuedConnection);
connect(m_inputLine, &QuickOpenLineEdit::returnPressed, this, &KateQuickOpen::slotReturnPressed);
connect(m_inputLine, &QuickOpenLineEdit::listModeChanged, this, &KateQuickOpen::slotListModeChanged);
connect(m_inputLine, &QuickOpenLineEdit::filterModeChanged, this, &KateQuickOpen::setFilterMode);
connect(m_listView, &QTreeView::activated, this, &KateQuickOpen::slotReturnPressed);
connect(m_listView, &QTreeView::clicked, this, &KateQuickOpen::slotReturnPressed); // for single click
m_listView->setModel(m_model);
m_listView->setSortingEnabled(true);
m_model->setSourceModel(m_base_model);
m_inputLine->installEventFilter(this);
m_listView->installEventFilter(this);
m_listView->setHeaderHidden(true);
m_listView->setRootIsDecorated(false);
m_listView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_proxyModel = new QuickOpenFilterProxyModel(this);
m_proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
m_proxyModel->setSourceModel(m_model);
m_listView->setModel(m_proxyModel);
connect(m_inputLine, &QuickOpenLineEdit::textChanged, m_proxyModel, [this](const QString &text) {
if (m_proxyModel->setFilterText(text)) {
m_styleDelegate->setFilterString(text);
m_listView->viewport()->update();
}
});
setHidden(true);
m_base_model->setListMode(m_inputLine->listMode());
setFilterMode();
m_model->setListMode(m_inputLine->listMode());
// fill stuff
updateState();
......@@ -327,17 +353,18 @@ bool KateQuickOpen::eventFilter(QObject *obj, QEvent *event)
void KateQuickOpen::reselectFirst()
{
int first = 0;
if (m_mainWindow->viewManager()->views().size() > 1 && m_model->rowCount() > 1 && m_inputLine->text().isEmpty()) {
const auto *model = m_listView->model();
if (m_mainWindow->viewManager()->views().size() > 1 && model->rowCount() > 1 && m_inputLine->text().isEmpty()) {
first = 1;
}
QModelIndex index = m_model->index(first, 0);
QModelIndex index = model->index(first, 0);
m_listView->setCurrentIndex(index);
}
void KateQuickOpen::updateState()
{
m_base_model->refresh(m_mainWindow);
m_model->refresh(m_mainWindow);
reselectFirst();
updateViewGeometry();
......@@ -399,11 +426,20 @@ void KateQuickOpen::slotReturnPressed()
void KateQuickOpen::slotListModeChanged(KateQuickOpenModel::List mode)
{
m_base_model->setListMode(mode);
m_model->setListMode(mode);
// this changes things again, needs refresh, let's go all the way
updateState();
}
void KateQuickOpen::setFilterMode()
{
auto newMode = m_inputLine->filterMode();
m_proxyModel->setFilterMode(newMode);
m_listView->setSortingEnabled(newMode == Fuzzy);
m_styleDelegate->setFilterMode(newMode);
m_listView->viewport()->update();
}
void KateQuickOpen::updateViewGeometry()
{
const QSize centralSize = m_mainWindow->size();
......
......@@ -51,6 +51,8 @@ private Q_SLOTS:
void slotListModeChanged(KateQuickOpenModelList mode);
void setFilterMode();
private:
KateMainWindow *m_mainWindow;
QTreeView *m_listView;
......@@ -60,12 +62,12 @@ private:
/**
* our model we search in
*/
KateQuickOpenModel *m_base_model;
KateQuickOpenModel *m_model = nullptr;
/**
* filtered model we search in
* fuzzy filter model
*/
QuickOpenFilterProxyModel *m_model;
QuickOpenFilterProxyModel *m_proxyModel = nullptr;
};
#endif
......@@ -14,6 +14,7 @@
#include <KSharedConfig>
static const QString CONFIG_QUICKOPEN_LISTMODE{QStringLiteral("Quickopen List Mode")};
static const QString CONFIG_QUICKOPEN_FILTERMODE{QStringLiteral("Quickopen Filter Mode")};
QuickOpenLineEdit::QuickOpenLineEdit(QWidget *parent)
: QLineEdit(parent)
......@@ -26,6 +27,8 @@ QuickOpenLineEdit::QuickOpenLineEdit(QWidget *parent)
const bool cfgListMode = cg.readEntry(CONFIG_QUICKOPEN_LISTMODE, true);
m_listMode = cfgListMode ? KateQuickOpenModelList::CurrentProject : KateQuickOpenModelList::AllProjects;
m_filterMode = (FilterMode)cg.readEntry(CONFIG_QUICKOPEN_FILTERMODE, (int)Fuzzy);
}
QuickOpenLineEdit::~QuickOpenLineEdit()
......@@ -34,6 +37,7 @@ QuickOpenLineEdit::~QuickOpenLineEdit()
KConfigGroup cg(cfg, "General");
cg.writeEntry(CONFIG_QUICKOPEN_LISTMODE, m_listMode == KateQuickOpenModelList::CurrentProject);
cg.writeEntry(CONFIG_QUICKOPEN_FILTERMODE, (int)m_filterMode);
}
void QuickOpenLineEdit::contextMenuEvent(QContextMenuEvent *event)
......@@ -75,6 +79,31 @@ void QuickOpenLineEdit::setupMenu()
}
});
act->setChecked(cfgListMode);
actGp->addAction(act);
// filter mode
menu->addSeparator();
actGp = new QActionGroup(this);
act = menu->addAction(i18n("Fuzzy Filtering"));
act->setCheckable(true);
connect(act, &QAction::toggled, this, [this](bool checked) {
if (checked) {
m_filterMode = Fuzzy;
Q_EMIT filterModeChanged();
}
});
act->setChecked(m_filterMode == Fuzzy);
actGp->addAction(act);
act = menu->addAction(i18n("Wildcard Filtering"));
act->setCheckable(true);
connect(act, &QAction::toggled, this, [this](bool checked) {
if (checked) {
m_filterMode = Wildcard;
Q_EMIT filterModeChanged();
}
});
act->setChecked(m_filterMode == Wildcard);
actGp->addAction(act);
}
......@@ -11,6 +11,11 @@
#include "katequickopenmodel.h"
enum FilterMode {
Fuzzy = 0,
Wildcard,
};
class QuickOpenLineEdit : public QLineEdit
{
Q_OBJECT
......@@ -23,6 +28,11 @@ public:
return m_listMode;
}
FilterMode filterMode() const
{
return m_filterMode;
}
protected:
void contextMenuEvent(QContextMenuEvent *event) override;
......@@ -32,9 +42,11 @@ private:
private:
std::unique_ptr<QMenu> menu;
KateQuickOpenModelList m_listMode;
FilterMode m_filterMode;
Q_SIGNALS:
void listModeChanged(KateQuickOpenModelList mode);
void filterModeChanged();
};
#endif // QUICKOPENLINEEDIT_H
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment