-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepl_widget.cpp
82 lines (60 loc) · 1.53 KB
/
repl_widget.cpp
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
// File: repl_widget.cpp
// Author: Samuel McFalls
#include "repl_widget.hpp"
#include <QWidget>
#include <QLineEdit>
#include <QString>
#include <QLabel>
#include <QLayout>
#include <QKeyEvent>
#include <QDebug>
REPLWidget::REPLWidget(QWidget * parent) : QWidget(parent) {
prompt = new QLabel(this);
prompt->setText("vtscript>");
input = new QLineEdit(this);
QHBoxLayout * layout = new QHBoxLayout(this);
layout->addWidget(prompt);
layout->addWidget(input);
this->setLayout(layout);
history.push_front("");
historyPos = history.begin();
}
void REPLWidget::keyPressEvent(QKeyEvent * evt) {
if (evt->key() == Qt::Key::Key_Return) {
if (input->text().isEmpty()) {
return;
}
// Get an iterator to the second element in the list
// This is always safe because begin != end
std::list<QString>::iterator first = history.begin();
first++;
// Insert before first
history.insert(first, input->text());
// Reset the history position
historyPos = history.begin();
lineEntered(input->text());
input->clear();
}
else if (evt->key() == Qt::Key::Key_Up) {
historyUp();
}
else if (evt->key() == Qt::Key::Key_Down) {
historyDown();
}
}
void REPLWidget::historyUp() {
// Get an iterator to the last element in the list
// end points past the last element
std::list<QString>::iterator last = history.end();
last--;
if (historyPos != last) {
historyPos++;
}
input->setText(*historyPos);
}
void REPLWidget::historyDown() {
if (historyPos != history.begin()) {
historyPos--;
}
input->setText(*historyPos);
}