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::setLevels(int levels) {
43 m_logLevel = levels;
44
45 m_ui.levelDebug->setCheckState(levels & GBA_LOG_DEBUG ? Qt::Checked : Qt::Unchecked);
46 m_ui.levelStub->setCheckState(levels & GBA_LOG_STUB ? Qt::Checked : Qt::Unchecked);
47 m_ui.levelInfo->setCheckState(levels & GBA_LOG_INFO ? Qt::Checked : Qt::Unchecked);
48 m_ui.levelWarn->setCheckState(levels & GBA_LOG_WARN ? Qt::Checked : Qt::Unchecked);
49 m_ui.levelError->setCheckState(levels & GBA_LOG_ERROR ? Qt::Checked : Qt::Unchecked);
50 m_ui.levelFatal->setCheckState(levels & GBA_LOG_FATAL ? Qt::Checked : Qt::Unchecked);
51 m_ui.levelGameError->setCheckState(levels & GBA_LOG_GAME_ERROR ? Qt::Checked : Qt::Unchecked);
52}
53
54void LogView::setLevelDebug(bool set) {
55 if (set) {
56 setLevel(GBA_LOG_DEBUG);
57 } else {
58 clearLevel(GBA_LOG_DEBUG);
59 }
60}
61
62void LogView::setLevelStub(bool set) {
63 if (set) {
64 setLevel(GBA_LOG_STUB);
65 } else {
66 clearLevel(GBA_LOG_STUB);
67 }
68}
69
70void LogView::setLevelInfo(bool set) {
71 if (set) {
72 setLevel(GBA_LOG_INFO);
73 } else {
74 clearLevel(GBA_LOG_INFO);
75 }
76}
77
78void LogView::setLevelWarn(bool set) {
79 if (set) {
80 setLevel(GBA_LOG_WARN);
81 } else {
82 clearLevel(GBA_LOG_WARN);
83 }
84}
85
86void LogView::setLevelError(bool set) {
87 if (set) {
88 setLevel(GBA_LOG_ERROR);
89 } else {
90 clearLevel(GBA_LOG_ERROR);
91 }
92}
93
94void LogView::setLevelFatal(bool set) {
95 if (set) {
96 setLevel(GBA_LOG_FATAL);
97 } else {
98 clearLevel(GBA_LOG_FATAL);
99 }
100}
101
102void LogView::setLevelGameError(bool set) {
103 if (set) {
104 setLevel(GBA_LOG_GAME_ERROR);
105 } else {
106 clearLevel(GBA_LOG_GAME_ERROR);
107 }
108}
109
110void LogView::setMaxLines(int limit) {
111 m_lineLimit = limit;
112 while (m_lines > m_lineLimit) {
113 clearLine();
114 }
115}
116
117QString LogView::toString(int level) {
118 switch (level) {
119 case GBA_LOG_DEBUG:
120 return tr("DEBUG");
121 case GBA_LOG_STUB:
122 return tr("STUB");
123 case GBA_LOG_INFO:
124 return tr("INFO");
125 case GBA_LOG_WARN:
126 return tr("WARN");
127 case GBA_LOG_ERROR:
128 return tr("ERROR");
129 case GBA_LOG_FATAL:
130 return tr("FATAL");
131 case GBA_LOG_GAME_ERROR:
132 return tr("GAME ERROR");
133 }
134 return QString();
135}
136
137void LogView::clearLine() {
138 QTextCursor cursor(m_ui.view->document());
139 cursor.setPosition(0);
140 cursor.select(QTextCursor::BlockUnderCursor);
141 cursor.removeSelectedText();
142 cursor.deleteChar();
143 --m_lines;
144}