blob: 27a7b770f7380017b65b1590b1269d4667746b03 (
plain) (
blame)
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
|
#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 "completion/Completer.hpp"
#include "completion/CompleterDelegate.hpp"
const char *completerStyles = R"(
background-color: #111;
color: #fff;
border-radius: 0;
width: 100%;
)";
Completer::Completer() : QWidget() {
view = new QTreeView(this);
viewDelegate = new CompleterDelegate(view);
view->setStyleSheet(completerStyles);
view->setItemDelegate(viewDelegate);
view->setRootIsDecorated(false);
view->setUniformRowHeights(true);
view->header()->hide();
view->setColumnWidth(1, 500);
view->setModel(&proxyModel);
view->setFocusPolicy(Qt::NoFocus);
view->setEditTriggers(QAbstractItemView::NoEditTriggers);
view->setSelectionMode(QAbstractItemView::SingleSelection);
}
void Completer::setSourceModel(QAbstractItemModel *model) {
proxyModel.setSourceModel(model);
}
void Completer::onTextChange(QString text) {
proxyModel.setFilterWildcard(text);
}
void Completer::setHighlightedRow(uint32_t row) {
viewDelegate->setCurrentRow(row);
view->update();
}
void Completer::acceptHighlighted() {
auto index = proxyModel.index(viewDelegate->currentRow(), 0);
auto text = proxyModel.data(index, Qt::DisplayRole).toString();
emit accepted(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;
}
|