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 = GBA_LOG_WARN | GBA_LOG_ERROR | GBA_LOG_FATAL;
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
56void LogView::setLevelDebug(bool set) {
57 if (set) {
58 setLevel(GBA_LOG_DEBUG);
59 } else {
60 clearLevel(GBA_LOG_DEBUG);
61 }
62}
63
64void LogView::setLevelStub(bool set) {
65 if (set) {
66 setLevel(GBA_LOG_STUB);
67 } else {
68 clearLevel(GBA_LOG_STUB);
69 }
70}
71
72void LogView::setLevelInfo(bool set) {
73 if (set) {
74 setLevel(GBA_LOG_INFO);
75 } else {
76 clearLevel(GBA_LOG_INFO);
77 }
78}
79
80void LogView::setLevelWarn(bool set) {
81 if (set) {
82 setLevel(GBA_LOG_WARN);
83 } else {
84 clearLevel(GBA_LOG_WARN);
85 }
86}
87
88void LogView::setLevelError(bool set) {
89 if (set) {
90 setLevel(GBA_LOG_ERROR);
91 } else {
92 clearLevel(GBA_LOG_ERROR);
93 }
94}
95
96void LogView::setLevelFatal(bool set) {
97 if (set) {
98 setLevel(GBA_LOG_FATAL);
99 } else {
100 clearLevel(GBA_LOG_FATAL);
101 }
102}
103
104void LogView::setLevelGameError(bool set) {
105 if (set) {
106 setLevel(GBA_LOG_GAME_ERROR);
107 } else {
108 clearLevel(GBA_LOG_GAME_ERROR);
109 }
110}
111
112void LogView::setLevelSWI(bool set) {
113 if (set) {
114 setLevel(GBA_LOG_SWI);
115 } else {
116 clearLevel(GBA_LOG_SWI);
117 }
118}
119
120void LogView::setMaxLines(int limit) {
121 m_lineLimit = limit;
122 while (m_lines > m_lineLimit) {
123 clearLine();
124 }
125}
126
127QString LogView::toString(int level) {
128 switch (level) {
129 case GBA_LOG_DEBUG:
130 return tr("DEBUG");
131 case GBA_LOG_STUB:
132 return tr("STUB");
133 case GBA_LOG_INFO:
134 return tr("INFO");
135 case GBA_LOG_WARN:
136 return tr("WARN");
137 case GBA_LOG_ERROR:
138 return tr("ERROR");
139 case GBA_LOG_FATAL:
140 return tr("FATAL");
141 case GBA_LOG_GAME_ERROR:
142 return tr("GAME ERROR");
143 case GBA_LOG_SWI:
144 return tr("SWI");
145 }
146 return QString();
147}
148
149void LogView::clearLine() {
150 QTextCursor cursor(m_ui.view->document());
151 cursor.setPosition(0);
152 cursor.select(QTextCursor::BlockUnderCursor);
153 cursor.removeSelectedText();
154 cursor.deleteChar();
155 --m_lines;
156}