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.clear, SIGNAL(clicked()), this, SLOT(clear()));
20 connect(m_ui.maxLines, SIGNAL(valueChanged(int)), this, SLOT(setMaxLines(int)));
21 m_logLevel = GBA_LOG_WARN | GBA_LOG_ERROR | GBA_LOG_FATAL;
22 m_lines = 0;
23 m_ui.maxLines->setValue(DEFAULT_LINE_LIMIT);
24}
25
26void LogView::postLog(int level, const QString& log) {
27 if (!(level & m_logLevel)) {
28 return;
29 }
30 m_ui.view->appendPlainText(QString("%1:\t%2").arg(toString(level)).arg(log));
31 ++m_lines;
32 if (m_lines > m_lineLimit) {
33 clearLine();
34 }
35}
36
37void LogView::clear() {
38 m_ui.view->clear();
39 m_lines = 0;
40}
41
42void LogView::setLevelDebug(bool set) {
43 if (set) {
44 setLevel(GBA_LOG_DEBUG);
45 } else {
46 clearLevel(GBA_LOG_DEBUG);
47 }
48}
49
50void LogView::setLevelStub(bool set) {
51 if (set) {
52 setLevel(GBA_LOG_STUB);
53 } else {
54 clearLevel(GBA_LOG_STUB);
55 }
56}
57
58void LogView::setLevelInfo(bool set) {
59 if (set) {
60 setLevel(GBA_LOG_INFO);
61 } else {
62 clearLevel(GBA_LOG_INFO);
63 }
64}
65
66void LogView::setLevelWarn(bool set) {
67 if (set) {
68 setLevel(GBA_LOG_WARN);
69 } else {
70 clearLevel(GBA_LOG_WARN);
71 }
72}
73
74void LogView::setLevelError(bool set) {
75 if (set) {
76 setLevel(GBA_LOG_ERROR);
77 } else {
78 clearLevel(GBA_LOG_ERROR);
79 }
80}
81
82void LogView::setLevelFatal(bool set) {
83 if (set) {
84 setLevel(GBA_LOG_FATAL);
85 } else {
86 clearLevel(GBA_LOG_FATAL);
87 }
88}
89
90void LogView::setLevelGameError(bool set) {
91 if (set) {
92 setLevel(GBA_LOG_GAME_ERROR);
93 } else {
94 clearLevel(GBA_LOG_GAME_ERROR);
95 }
96}
97
98void LogView::setMaxLines(int limit) {
99 m_lineLimit = limit;
100 while (m_lines > m_lineLimit) {
101 clearLine();
102 }
103}
104
105QString LogView::toString(int level) {
106 switch (level) {
107 case GBA_LOG_DEBUG:
108 return tr("DEBUG");
109 case GBA_LOG_STUB:
110 return tr("STUB");
111 case GBA_LOG_INFO:
112 return tr("INFO");
113 case GBA_LOG_WARN:
114 return tr("WARN");
115 case GBA_LOG_ERROR:
116 return tr("ERROR");
117 case GBA_LOG_FATAL:
118 return tr("FATAL");
119 case GBA_LOG_GAME_ERROR:
120 return tr("GAME ERROR");
121 }
122 return QString();
123}
124
125void LogView::clearLine() {
126 QTextCursor cursor(m_ui.view->document());
127 cursor.setPosition(0);
128 cursor.select(QTextCursor::BlockUnderCursor);
129 cursor.removeSelectedText();
130 cursor.deleteChar();
131 --m_lines;
132}