19
#include "AutoTypeMatchView.h"
20
#include "AutoTypeMatchModel.h"
21
#include "core/Entry.h"
25
#include <QSortFilterProxyModel>
27
class CustomSortFilterProxyModel : public QSortFilterProxyModel
30
explicit CustomSortFilterProxyModel(QObject* parent = nullptr)
31
: QSortFilterProxyModel(parent){};
32
~CustomSortFilterProxyModel() override = default;
35
bool filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const override
37
auto index0 = sourceModel()->index(sourceRow, 0, sourceParent);
38
auto index1 = sourceModel()->index(sourceRow, 1, sourceParent);
39
auto index2 = sourceModel()->index(sourceRow, 2, sourceParent);
41
return sourceModel()->data(index0).toString().contains(filterRegExp())
42
|| sourceModel()->data(index1).toString().contains(filterRegExp())
43
|| sourceModel()->data(index2).toString().contains(filterRegExp());
47
AutoTypeMatchView::AutoTypeMatchView(QWidget* parent)
49
, m_model(new AutoTypeMatchModel(this))
50
, m_sortModel(new CustomSortFilterProxyModel(this))
52
m_sortModel->setSourceModel(m_model);
53
m_sortModel->setDynamicSortFilter(true);
54
m_sortModel->setSortLocaleAware(true);
55
m_sortModel->setSortCaseSensitivity(Qt::CaseInsensitive);
56
m_sortModel->setFilterKeyColumn(-1);
57
m_sortModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
58
setModel(m_sortModel);
60
sortByColumn(0, Qt::AscendingOrder);
62
setContextMenuPolicy(Qt::CustomContextMenu);
64
connect(this, &QTableView::doubleClicked, this, [this](const QModelIndex& index) {
65
emit matchActivated(matchFromIndex(index));
69
void AutoTypeMatchView::keyPressEvent(QKeyEvent* event)
71
if ((event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return) && currentIndex().isValid()) {
72
emit matchActivated(matchFromIndex(currentIndex()));
73
} else if (event->key() == Qt::Key_PageUp) {
75
} else if (event->key() == Qt::Key_PageDown) {
78
QTableView::keyPressEvent(event);
82
void AutoTypeMatchView::setMatchList(const QList<AutoTypeMatch>& matches)
84
m_model->setMatchList(matches);
85
m_sortModel->setFilterWildcard({});
87
horizontalHeader()->resizeSections(QHeaderView::ResizeToContents);
89
selectionModel()->clear();
90
emit currentMatchChanged(currentMatch());
93
void AutoTypeMatchView::selectFirstMatch()
95
selectionModel()->setCurrentIndex(m_sortModel->index(0, 0),
96
QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
97
emit currentMatchChanged(currentMatch());
100
bool AutoTypeMatchView::selectMatch(const AutoTypeMatch& match)
102
QModelIndex index = m_model->closestIndexFromMatch(match);
104
if (index.isValid()) {
105
selectionModel()->setCurrentIndex(m_sortModel->mapFromSource(index),
106
QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
107
emit currentMatchChanged(currentMatch());
114
void AutoTypeMatchView::filterList(const QString& filter)
116
m_sortModel->setFilterWildcard(filter);
117
setCurrentIndex(m_sortModel->index(0, 0));
120
void AutoTypeMatchView::moveSelection(int offset)
122
auto index = currentIndex();
123
auto row = index.isValid() ? index.row() : -1;
124
selectRow(qBound(0, row + offset, model()->rowCount() - 1));
127
AutoTypeMatch AutoTypeMatchView::currentMatch()
129
QModelIndexList list = selectionModel()->selectedRows();
130
if (list.size() == 1) {
131
return m_model->matchFromIndex(m_sortModel->mapToSource(list.first()));
136
AutoTypeMatch AutoTypeMatchView::matchFromIndex(const QModelIndex& index)
138
if (index.isValid()) {
139
return m_model->matchFromIndex(m_sortModel->mapToSource(index));
144
void AutoTypeMatchView::currentChanged(const QModelIndex& current, const QModelIndex& previous)
146
auto match = matchFromIndex(current);
147
emit currentMatchChanged(match);
148
QTableView::currentChanged(current, previous);