-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHistoryLineEdit.cpp
More file actions
64 lines (55 loc) · 1.35 KB
/
HistoryLineEdit.cpp
File metadata and controls
64 lines (55 loc) · 1.35 KB
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
#include "HistoryLineEdit.h"
#include <QWheelEvent>
HistoryLineEdit::HistoryLineEdit(QWidget *parent)
: QLineEdit(parent)
, _history(0)
, _history_index(-1)
{
_history_index = -1;
}
void HistoryLineEdit::keyPressEvent(QKeyEvent *event) {
switch (event->key()) {
case Qt::Key_Up:
ShowPreviousItem();
break;
case Qt::Key_Down:
ShowNextItem();
break;
default:
// For other keys, proceed as normal
QLineEdit::keyPressEvent(event);
}
}
void HistoryLineEdit::wheelEvent(QWheelEvent *event) {
if (event->angleDelta().y() > 0) {
ShowPreviousItem();
} else if (event->angleDelta().y() < 0) {
ShowNextItem();
}
}
void HistoryLineEdit::AddToHistory() {
_history.push_back(text());
_history_index = _history.count()-1;
}
void HistoryLineEdit::ShowPreviousItem() {
if (_history_index == -1) {
_history_index = _history.size() - 1;
} else if (_history_index > 0) {
_history_index--;
}
if (_history_index >= 0) {
setText(_history[_history_index]);
}
}
void HistoryLineEdit::ShowNextItem() {
if (_history_index == -1) {
return;
}
if (_history_index < _history.size() - 1) {
_history_index++;
setText(_history[_history_index]);
} else {
_history_index = -1;
clear();
}
}