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