all repos — mgba @ 9c07698068ec289786178c36a273d0de588dcab0

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 <QTextBlock>
  9#include <QTextCursor>
 10
 11using namespace QGBA;
 12
 13LogView::LogView(QWidget* parent)
 14	: QWidget(parent)
 15	, m_logLevel(0)
 16	, m_lines(0)
 17	, m_lineLimit(DEFAULT_LINE_LIMIT)
 18{
 19	m_ui.setupUi(this);
 20	connect(m_ui.levelDebug, SIGNAL(toggled(bool)), this, SLOT(setLevelDebug(bool)));
 21	connect(m_ui.levelStub, SIGNAL(toggled(bool)), this, SLOT(setLevelStub(bool)));
 22	connect(m_ui.levelInfo, SIGNAL(toggled(bool)), this, SLOT(setLevelInfo(bool)));
 23	connect(m_ui.levelWarn, SIGNAL(toggled(bool)), this, SLOT(setLevelWarn(bool)));
 24	connect(m_ui.levelError, SIGNAL(toggled(bool)), this, SLOT(setLevelError(bool)));
 25	connect(m_ui.levelFatal, SIGNAL(toggled(bool)), this, SLOT(setLevelFatal(bool)));
 26	connect(m_ui.levelGameError, SIGNAL(toggled(bool)), this, SLOT(setLevelGameError(bool)));
 27	connect(m_ui.levelSWI, SIGNAL(toggled(bool)), this, SLOT(setLevelSWI(bool)));
 28	connect(m_ui.levelStatus, SIGNAL(toggled(bool)), this, SLOT(setLevelStatus(bool)));
 29	connect(m_ui.clear, SIGNAL(clicked()), this, SLOT(clear()));
 30	connect(m_ui.maxLines, SIGNAL(valueChanged(int)), this, SLOT(setMaxLines(int)));
 31	m_ui.maxLines->setValue(DEFAULT_LINE_LIMIT);
 32}
 33
 34void LogView::postLog(int level, const QString& log) {
 35	if (!(level & m_logLevel)) {
 36		return;
 37	}
 38	m_ui.view->appendPlainText(QString("%1:\t%2").arg(toString(level)).arg(log));
 39	++m_lines;
 40	if (m_lines > m_lineLimit) {
 41		clearLine();
 42	}
 43}
 44
 45void LogView::clear() {
 46	m_ui.view->clear();
 47	m_lines = 0;
 48}
 49
 50void LogView::setLevels(int levels) {
 51	m_logLevel = levels;
 52
 53	m_ui.levelDebug->setCheckState(levels & GBA_LOG_DEBUG ? Qt::Checked : Qt::Unchecked);
 54	m_ui.levelStub->setCheckState(levels & GBA_LOG_STUB ? Qt::Checked : Qt::Unchecked);
 55	m_ui.levelInfo->setCheckState(levels & GBA_LOG_INFO ? Qt::Checked : Qt::Unchecked);
 56	m_ui.levelWarn->setCheckState(levels & GBA_LOG_WARN ? Qt::Checked : Qt::Unchecked);
 57	m_ui.levelError->setCheckState(levels & GBA_LOG_ERROR ? Qt::Checked : Qt::Unchecked);
 58	m_ui.levelFatal->setCheckState(levels & GBA_LOG_FATAL ? Qt::Checked : Qt::Unchecked);
 59	m_ui.levelGameError->setCheckState(levels & GBA_LOG_GAME_ERROR ? Qt::Checked : Qt::Unchecked);
 60	m_ui.levelSWI->setCheckState(levels & GBA_LOG_SWI ? Qt::Checked : Qt::Unchecked);
 61	m_ui.levelStatus->setCheckState(levels & GBA_LOG_STATUS ? Qt::Checked : Qt::Unchecked);
 62
 63	emit levelsSet(levels);
 64}
 65
 66void LogView::setLevelDebug(bool set) {
 67	if (set) {
 68		setLevel(GBA_LOG_DEBUG);
 69	} else {
 70		clearLevel(GBA_LOG_DEBUG);
 71	}
 72}
 73
 74void LogView::setLevelStub(bool set) {
 75	if (set) {
 76		setLevel(GBA_LOG_STUB);
 77	} else {
 78		clearLevel(GBA_LOG_STUB);
 79	}
 80}
 81
 82void LogView::setLevelInfo(bool set) {
 83	if (set) {
 84		setLevel(GBA_LOG_INFO);
 85	} else {
 86		clearLevel(GBA_LOG_INFO);
 87	}
 88}
 89
 90void LogView::setLevelWarn(bool set) {
 91	if (set) {
 92		setLevel(GBA_LOG_WARN);
 93	} else {
 94		clearLevel(GBA_LOG_WARN);
 95	}
 96}
 97
 98void LogView::setLevelError(bool set) {
 99	if (set) {
100		setLevel(GBA_LOG_ERROR);
101	} else {
102		clearLevel(GBA_LOG_ERROR);
103	}
104}
105
106void LogView::setLevelFatal(bool set) {
107	if (set) {
108		setLevel(GBA_LOG_FATAL);
109	} else {
110		clearLevel(GBA_LOG_FATAL);
111	}
112}
113
114void LogView::setLevelGameError(bool set) {
115	if (set) {
116		setLevel(GBA_LOG_GAME_ERROR);
117	} else {
118		clearLevel(GBA_LOG_GAME_ERROR);
119	}
120}
121
122void LogView::setLevelSWI(bool set) {
123	if (set) {
124		setLevel(GBA_LOG_SWI);
125	} else {
126		clearLevel(GBA_LOG_SWI);
127	}
128}
129
130void LogView::setLevelStatus(bool set) {
131	if (set) {
132		setLevel(GBA_LOG_STATUS);
133	} else {
134		clearLevel(GBA_LOG_STATUS);
135	}
136}
137
138void LogView::setMaxLines(int limit) {
139	m_lineLimit = limit;
140	while (m_lines > m_lineLimit) {
141		clearLine();
142	}
143}
144
145QString LogView::toString(int level) {
146	switch (level) {
147	case GBA_LOG_DEBUG:
148		return tr("DEBUG");
149	case GBA_LOG_STUB:
150		return tr("STUB");
151	case GBA_LOG_INFO:
152		return tr("INFO");
153	case GBA_LOG_WARN:
154		return tr("WARN");
155	case GBA_LOG_ERROR:
156		return tr("ERROR");
157	case GBA_LOG_FATAL:
158		return tr("FATAL");
159	case GBA_LOG_GAME_ERROR:
160		return tr("GAME ERROR");
161	case GBA_LOG_SWI:
162		return tr("SWI");
163	case GBA_LOG_STATUS:
164		return tr("STATUS");
165	}
166	return QString();
167}
168
169void LogView::setLevel(int level) {
170	m_logLevel |= level;
171	emit levelsEnabled(level);
172}
173
174void LogView::clearLevel(int level) {
175	m_logLevel &= ~level;
176	emit levelsDisabled(level);
177}
178
179void LogView::clearLine() {
180	QTextCursor cursor(m_ui.view->document());
181	cursor.setPosition(0);
182	cursor.select(QTextCursor::BlockUnderCursor);
183	cursor.removeSelectedText();
184	cursor.deleteChar();
185	--m_lines;
186}