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