src/platform/qt/DebuggerConsole.cpp (view raw)
1/* Copyright (c) 2013-2016 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "DebuggerConsole.h"
7
8#include "DebuggerConsoleController.h"
9
10#include <QScrollBar>
11
12using namespace QGBA;
13
14DebuggerConsole::DebuggerConsole(DebuggerConsoleController* controller, QWidget* parent)
15 : QWidget(parent)
16 , m_consoleController(controller)
17{
18 m_ui.setupUi(this);
19
20 m_ui.prompt->installEventFilter(this);
21
22 connect(m_ui.prompt, &QLineEdit::returnPressed, this, &DebuggerConsole::postLine);
23 connect(controller, &DebuggerConsoleController::log, this, &DebuggerConsole::log);
24 connect(m_ui.breakpoint, &QAbstractButton::clicked, controller, &DebuggerController::attach);
25 connect(m_ui.breakpoint, &QAbstractButton::clicked, controller, &DebuggerController::breakInto);
26}
27
28void DebuggerConsole::log(const QString& line) {
29 m_ui.log->moveCursor(QTextCursor::End);
30 m_ui.log->insertPlainText(line);
31 m_ui.log->verticalScrollBar()->setValue(m_ui.log->verticalScrollBar()->maximum());
32}
33
34void DebuggerConsole::postLine() {
35 m_consoleController->attach();
36 QString line = m_ui.prompt->text();
37 m_ui.prompt->clear();
38 if (line.isEmpty()) {
39 m_consoleController->enterLine(QString("\n"));
40 } else {
41 m_history.append(line);
42 m_historyOffset = 0;
43 log(QString("> %1\n").arg(line));
44 m_consoleController->enterLine(line);
45 }
46}
47
48bool DebuggerConsole::eventFilter(QObject*, QEvent* event) {
49 if (event->type() != QEvent::KeyPress) {
50 return false;
51 }
52 if (m_history.isEmpty()) {
53 return false;
54 }
55 QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
56 switch (keyEvent->key()) {
57 case Qt::Key_Down:
58 if (m_historyOffset <= 0) {
59 return false;
60 }
61 --m_historyOffset;
62 break;
63 case Qt::Key_Up:
64 if (m_historyOffset >= m_history.size()) {
65 return false;
66 }
67 ++m_historyOffset;
68 break;
69 case Qt::Key_End:
70 m_historyOffset = 0;
71 break;
72 case Qt::Key_Home:
73 m_historyOffset = m_history.size();
74 break;
75 default:
76 return false;
77 }
78 if (m_historyOffset == 0) {
79 m_ui.prompt->clear();
80 } else {
81 m_ui.prompt->setText(m_history[m_history.size() - m_historyOffset]);
82 }
83 return true;
84}