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
|
#include <QAbstractItemView>
#include <QHeaderView>
#include <QKeyEvent>
#include <QLineEdit>
#include <QTreeView>
#include <QWidget>
#include <QtCore/qabstractitemmodel.h>
#include <QtCore/qitemselectionmodel.h>
#include <QtCore/qnamespace.h>
#include <QtCore/qsortfilterproxymodel.h>
#include <QtCore>
#include <QtWidgets/qboxlayout.h>
#include "completion/Completer.hpp"
#include "completion/CompleterDelegate.hpp"
const char *completerStyles = R"(
background-color: #111;
color: #fff;
border-radius: 0;
)";
Completer::Completer(QWidget *parentNode) : QWidget(parentNode) {
layout = new QHBoxLayout(this);
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(0);
setLayout(layout);
setStyleSheet(completerStyles);
setContentsMargins(0, 0, 0, 0);
view = new QTreeView();
layout->addWidget(view, 0);
viewDelegate = new CompleterDelegate(view);
view->setItemDelegate(viewDelegate);
view->setRootIsDecorated(false);
view->setUniformRowHeights(true);
view->header()->hide();
view->setColumnWidth(0, 500);
view->setModel(&proxyModel);
view->setFocusPolicy(Qt::NoFocus);
view->setSelectionMode(QAbstractItemView::SingleSelection);
view->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
view->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);
view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}
void Completer::acceptHighlighted() {
auto index = proxyModel.index(viewDelegate->currentRow(), 0);
auto text = proxyModel.data(index, Qt::DisplayRole).toString();
setHighlightedRow(0);
emit accepted(text);
}
void Completer::onTextChange(QString text) {
setHighlightedRow(0);
proxyModel.setFilterWildcard(text);
}
bool Completer::onKeyPressEvent(QKeyEvent *event) {
auto combo = event->keyCombination();
auto row = viewDelegate->currentRow();
// If there are no matches, nothing to handle here
if (proxyModel.rowCount() == 0)
return false;
if (combo.key() == Qt::Key_Up) {
setHighlightedRow(row <= 0 ? proxyModel.rowCount() - 1 : row - 1);
return true;
}
if (combo.key() == Qt::Key_Down) {
setHighlightedRow((row + 1) % proxyModel.rowCount());
return true;
}
if (combo.key() == Qt::Key_Tab) {
acceptHighlighted();
return true;
}
if (combo.key() == Qt::Key_Return) {
acceptHighlighted();
return false;
}
return false;
}
void Completer::setHighlightedRow(uint32_t row) {
viewDelegate->setCurrentRow(row);
view->update();
}
|