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