all repos — mgba @ 686380b6c4d8acf219a704a8932668b11efc47c2

mGBA Game Boy Advance Emulator

src/platform/qt/LogView.cpp (view raw)

  1/* Copyright (c) 2013-2014 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 "LogView.h"
  7
  8#include "LogController.h"
  9
 10#include <QTextBlock>
 11#include <QTextCursor>
 12
 13using namespace QGBA;
 14
 15LogView::LogView(LogController* log, QWidget* parent)
 16	: QWidget(parent)
 17	, m_lines(0)
 18	, m_lineLimit(DEFAULT_LINE_LIMIT)
 19{
 20	m_ui.setupUi(this);
 21	connect(m_ui.levelDebug, &QAbstractButton::toggled, [this](bool set) {
 22		setLevel(mLOG_DEBUG, set);
 23	});
 24	connect(m_ui.levelStub, &QAbstractButton::toggled, [this](bool set) {
 25		setLevel(mLOG_STUB, set);
 26	});
 27	connect(m_ui.levelInfo, &QAbstractButton::toggled, [this](bool set) {
 28		setLevel(mLOG_INFO, set);
 29	});
 30	connect(m_ui.levelWarn, &QAbstractButton::toggled, [this](bool set) {
 31		setLevel(mLOG_WARN, set);
 32	});
 33	connect(m_ui.levelError, &QAbstractButton::toggled, [this](bool set) {
 34		setLevel(mLOG_ERROR, set);
 35	});
 36	connect(m_ui.levelFatal, &QAbstractButton::toggled, [this](bool set) {
 37		setLevel(mLOG_FATAL, set);
 38	});
 39	connect(m_ui.levelGameError, &QAbstractButton::toggled, [this](bool set) {
 40		setLevel(mLOG_GAME_ERROR, set);
 41	});
 42	connect(m_ui.clear, &QAbstractButton::clicked, this, &LogView::clear);
 43	connect(m_ui.maxLines, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged),
 44	        this, &LogView::setMaxLines);
 45	m_ui.maxLines->setValue(DEFAULT_LINE_LIMIT);
 46
 47	connect(log, &LogController::logPosted, this, &LogView::postLog);
 48	connect(log, &LogController::levelsSet, this, &LogView::setLevels);
 49	connect(log, &LogController::levelsEnabled, [this](int level) {
 50		bool s = blockSignals(true);
 51		setLevel(level, true);
 52		blockSignals(s);
 53	});
 54	connect(log, &LogController::levelsDisabled, [this](int level) {
 55		bool s = blockSignals(true);
 56		setLevel(level, false);
 57		blockSignals(s);
 58	});
 59	connect(this, &LogView::levelsEnabled, log, &LogController::enableLevels);
 60	connect(this, &LogView::levelsDisabled, log, &LogController::disableLevels);
 61}
 62
 63void LogView::postLog(int level, int category, const QString& log) {
 64	QString line = QString("[%1] %2:\t%3").arg(LogController::toString(level)).arg(mLogCategoryName(category)).arg(log);
 65	// TODO: Log to file
 66	m_pendingLines.enqueue(line);
 67	++m_lines;
 68	if (m_lines > m_lineLimit) {
 69		clearLine();
 70	}
 71	update();
 72}
 73
 74void LogView::clear() {
 75	m_ui.view->clear();
 76	m_lines = 0;
 77}
 78
 79void LogView::setLevels(int levels) {
 80	m_ui.levelDebug->setCheckState(levels & mLOG_DEBUG ? Qt::Checked : Qt::Unchecked);
 81	m_ui.levelStub->setCheckState(levels & mLOG_STUB ? Qt::Checked : Qt::Unchecked);
 82	m_ui.levelInfo->setCheckState(levels & mLOG_INFO ? Qt::Checked : Qt::Unchecked);
 83	m_ui.levelWarn->setCheckState(levels & mLOG_WARN ? Qt::Checked : Qt::Unchecked);
 84	m_ui.levelError->setCheckState(levels & mLOG_ERROR ? Qt::Checked : Qt::Unchecked);
 85	m_ui.levelFatal->setCheckState(levels & mLOG_FATAL ? Qt::Checked : Qt::Unchecked);
 86	m_ui.levelGameError->setCheckState(levels & mLOG_GAME_ERROR ? Qt::Checked : Qt::Unchecked);
 87}
 88
 89void LogView::setLevel(int level, bool set) {
 90	if (level & mLOG_DEBUG) {
 91		m_ui.levelDebug->setCheckState(set ? Qt::Checked : Qt::Unchecked);
 92	}
 93	if (level & mLOG_STUB) {
 94		m_ui.levelStub->setCheckState(set ? Qt::Checked : Qt::Unchecked);
 95	}
 96	if (level & mLOG_INFO) {
 97		m_ui.levelInfo->setCheckState(set ? Qt::Checked : Qt::Unchecked);
 98	}
 99	if (level & mLOG_WARN) {
100		m_ui.levelWarn->setCheckState(set ? Qt::Checked : Qt::Unchecked);
101	}
102	if (level & mLOG_ERROR) {
103		m_ui.levelError->setCheckState(set ? Qt::Checked : Qt::Unchecked);
104	}
105	if (level & mLOG_FATAL) {
106		m_ui.levelFatal->setCheckState(set ? Qt::Checked : Qt::Unchecked);
107	}
108	if (level & mLOG_GAME_ERROR) {
109		m_ui.levelGameError->setCheckState(set ? Qt::Checked : Qt::Unchecked);
110	}
111
112	if (set) {
113		emit levelsEnabled(level);
114	} else {
115		emit levelsDisabled(level);
116	}
117}
118
119void LogView::setMaxLines(int limit) {
120	m_lineLimit = limit;
121	while (m_lines > m_lineLimit) {
122		clearLine();
123	}
124}
125
126void LogView::paintEvent(QPaintEvent* event) {
127	while (!m_pendingLines.isEmpty()) {
128		m_ui.view->appendPlainText(m_pendingLines.dequeue());
129	}
130	QWidget::paintEvent(event);
131}
132
133void LogView::clearLine() {
134	if (m_ui.view->document()->isEmpty()) {
135		m_pendingLines.dequeue();
136	} else {
137		QTextCursor cursor(m_ui.view->document());
138		cursor.setPosition(0);
139		cursor.select(QTextCursor::BlockUnderCursor);
140		cursor.removeSelectedText();
141		cursor.deleteChar();
142	}
143	--m_lines;
144}