Qt: Refactor out log widget

This commit is contained in:
Vicki Pfau 2022-05-08 22:40:29 -07:00
parent 86f7acbbb9
commit ab3cd84ed4
6 changed files with 56 additions and 12 deletions

View File

@ -109,6 +109,7 @@ set(SOURCE_FILES
LogController.cpp LogController.cpp
LogConfigModel.cpp LogConfigModel.cpp
LogView.cpp LogView.cpp
LogWidget.cpp
MapView.cpp MapView.cpp
MemoryDump.cpp MemoryDump.cpp
MemoryModel.cpp MemoryModel.cpp

View File

@ -8,7 +8,7 @@
#include "DebuggerConsoleController.h" #include "DebuggerConsoleController.h"
#include "GBAApp.h" #include "GBAApp.h"
#include <QScrollBar> #include <QKeyEvent>
using namespace QGBA; using namespace QGBA;
@ -19,23 +19,16 @@ DebuggerConsole::DebuggerConsole(DebuggerConsoleController* controller, QWidget*
m_ui.setupUi(this); m_ui.setupUi(this);
m_ui.prompt->installEventFilter(this); m_ui.prompt->installEventFilter(this);
m_ui.log->setFont(GBAApp::app()->monospaceFont());
m_ui.prompt->setFont(GBAApp::app()->monospaceFont()); m_ui.prompt->setFont(GBAApp::app()->monospaceFont());
connect(m_ui.prompt, &QLineEdit::returnPressed, this, &DebuggerConsole::postLine); connect(m_ui.prompt, &QLineEdit::returnPressed, this, &DebuggerConsole::postLine);
connect(controller, &DebuggerConsoleController::log, this, &DebuggerConsole::log); connect(controller, &DebuggerConsoleController::log, m_ui.log, &LogWidget::log);
connect(m_ui.breakpoint, &QAbstractButton::clicked, controller, &DebuggerController::attach); connect(m_ui.breakpoint, &QAbstractButton::clicked, controller, &DebuggerController::attach);
connect(m_ui.breakpoint, &QAbstractButton::clicked, controller, &DebuggerController::breakInto); connect(m_ui.breakpoint, &QAbstractButton::clicked, controller, &DebuggerController::breakInto);
controller->historyLoad(); controller->historyLoad();
} }
void DebuggerConsole::log(const QString& line) {
m_ui.log->moveCursor(QTextCursor::End);
m_ui.log->insertPlainText(line);
m_ui.log->verticalScrollBar()->setValue(m_ui.log->verticalScrollBar()->maximum());
}
void DebuggerConsole::postLine() { void DebuggerConsole::postLine() {
m_consoleController->attach(); m_consoleController->attach();
QString line = m_ui.prompt->text(); QString line = m_ui.prompt->text();
@ -44,7 +37,7 @@ void DebuggerConsole::postLine() {
m_consoleController->enterLine(QString("\n")); m_consoleController->enterLine(QString("\n"));
} else { } else {
m_historyOffset = 0; m_historyOffset = 0;
log(QString("> %1\n").arg(line)); m_ui.log->log(QString("> %1\n").arg(line));
m_consoleController->enterLine(line); m_consoleController->enterLine(line);
} }
} }

View File

@ -18,7 +18,6 @@ public:
DebuggerConsole(DebuggerConsoleController* controller, QWidget* parent = nullptr); DebuggerConsole(DebuggerConsoleController* controller, QWidget* parent = nullptr);
private slots: private slots:
void log(const QString&);
void postLine(); void postLine();
protected: protected:

View File

@ -29,7 +29,7 @@
</widget> </widget>
</item> </item>
<item row="0" column="0" colspan="2"> <item row="0" column="0" colspan="2">
<widget class="QPlainTextEdit" name="log"> <widget class="QGBA::LogWidget" name="log">
<property name="readOnly"> <property name="readOnly">
<bool>true</bool> <bool>true</bool>
</property> </property>
@ -37,6 +37,13 @@
</item> </item>
</layout> </layout>
</widget> </widget>
<customwidgets>
<customwidget>
<class>QGBA::LogWidget</class>
<extends>QPlainTextEdit</extends>
<header>LogWidget.h</header>
</customwidget>
</customwidgets>
<resources/> <resources/>
<connections/> <connections/>
</ui> </ui>

View File

@ -0,0 +1,24 @@
/* Copyright (c) 2013-2022 Jeffrey Pfau
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "LogWidget.h"
#include "GBAApp.h"
#include <QScrollBar>
using namespace QGBA;
LogWidget::LogWidget(QWidget* parent)
: QTextEdit(parent)
{
setFont(GBAApp::app()->monospaceFont());
}
void LogWidget::log(const QString& line) {
moveCursor(QTextCursor::End);
insertPlainText(line);
verticalScrollBar()->setValue(verticalScrollBar()->maximum());
}

View File

@ -0,0 +1,20 @@
/* Copyright (c) 2013-2022 Jeffrey Pfau
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#pragma once
#include <QTextEdit>
namespace QGBA {
class LogWidget : public QTextEdit {
public:
LogWidget(QWidget* parent = nullptr);
public slots:
void log(const QString&);
};
}