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