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 "LogController.h"
9
10#include <QTextBlock>
11#include <QTextCursor>
12
13using namespace QGBA;
14
15LogView::LogView(LogController* log, QWidget* parent)
16 : QWidget(parent)
17 , m_lines(0)
18 , m_lineLimit(DEFAULT_LINE_LIMIT)
19{
20 m_ui.setupUi(this);
21 connect(m_ui.levelDebug, &QAbstractButton::toggled, [this](bool set) {
22 setLevel(mLOG_DEBUG, set);
23 });
24 connect(m_ui.levelStub, &QAbstractButton::toggled, [this](bool set) {
25 setLevel(mLOG_STUB, set);
26 });
27 connect(m_ui.levelInfo, &QAbstractButton::toggled, [this](bool set) {
28 setLevel(mLOG_INFO, set);
29 });
30 connect(m_ui.levelWarn, &QAbstractButton::toggled, [this](bool set) {
31 setLevel(mLOG_WARN, set);
32 });
33 connect(m_ui.levelError, &QAbstractButton::toggled, [this](bool set) {
34 setLevel(mLOG_ERROR, set);
35 });
36 connect(m_ui.levelFatal, &QAbstractButton::toggled, [this](bool set) {
37 setLevel(mLOG_FATAL, set);
38 });
39 connect(m_ui.levelGameError, &QAbstractButton::toggled, [this](bool set) {
40 setLevel(mLOG_GAME_ERROR, set);
41 });
42 connect(m_ui.clear, SIGNAL(clicked()), this, SLOT(clear()));
43 connect(m_ui.maxLines, SIGNAL(valueChanged(int)), this, SLOT(setMaxLines(int)));
44 m_ui.maxLines->setValue(DEFAULT_LINE_LIMIT);
45
46 connect(log, SIGNAL(logPosted(int, int, const QString&)), this, SLOT(postLog(int, int, const QString&)));
47 connect(log, SIGNAL(levelsSet(int)), this, SLOT(setLevels(int)));
48 connect(log, &LogController::levelsEnabled, [this](int level) {
49 bool s = blockSignals(true);
50 setLevel(level, true);
51 blockSignals(s);
52 });
53 connect(log, &LogController::levelsDisabled, [this](int level) {
54 bool s = blockSignals(true);
55 setLevel(level, false);
56 blockSignals(s);
57 });
58 connect(this, SIGNAL(levelsEnabled(int)), log, SLOT(enableLevels(int)));
59 connect(this, SIGNAL(levelsDisabled(int)), log, SLOT(disableLevels(int)));
60}
61
62void LogView::postLog(int level, int category, const QString& log) {
63 QString line = QString("[%1] %2:\t%3").arg(LogController::toString(level)).arg(mLogCategoryName(category)).arg(log);
64 // TODO: Log to file
65 m_pendingLines.enqueue(line);
66 ++m_lines;
67 if (m_lines > m_lineLimit) {
68 clearLine();
69 }
70 update();
71}
72
73void LogView::clear() {
74 m_ui.view->clear();
75 m_lines = 0;
76}
77
78void LogView::setLevels(int levels) {
79 m_ui.levelDebug->setCheckState(levels & mLOG_DEBUG ? Qt::Checked : Qt::Unchecked);
80 m_ui.levelStub->setCheckState(levels & mLOG_STUB ? Qt::Checked : Qt::Unchecked);
81 m_ui.levelInfo->setCheckState(levels & mLOG_INFO ? Qt::Checked : Qt::Unchecked);
82 m_ui.levelWarn->setCheckState(levels & mLOG_WARN ? Qt::Checked : Qt::Unchecked);
83 m_ui.levelError->setCheckState(levels & mLOG_ERROR ? Qt::Checked : Qt::Unchecked);
84 m_ui.levelFatal->setCheckState(levels & mLOG_FATAL ? Qt::Checked : Qt::Unchecked);
85 m_ui.levelGameError->setCheckState(levels & mLOG_GAME_ERROR ? Qt::Checked : Qt::Unchecked);
86}
87
88void LogView::setLevel(int level, bool set) {
89 if (level & mLOG_DEBUG) {
90 m_ui.levelDebug->setCheckState(set ? Qt::Checked : Qt::Unchecked);
91 }
92 if (level & mLOG_STUB) {
93 m_ui.levelStub->setCheckState(set ? Qt::Checked : Qt::Unchecked);
94 }
95 if (level & mLOG_INFO) {
96 m_ui.levelInfo->setCheckState(set ? Qt::Checked : Qt::Unchecked);
97 }
98 if (level & mLOG_WARN) {
99 m_ui.levelWarn->setCheckState(set ? Qt::Checked : Qt::Unchecked);
100 }
101 if (level & mLOG_ERROR) {
102 m_ui.levelError->setCheckState(set ? Qt::Checked : Qt::Unchecked);
103 }
104 if (level & mLOG_FATAL) {
105 m_ui.levelFatal->setCheckState(set ? Qt::Checked : Qt::Unchecked);
106 }
107 if (level & mLOG_GAME_ERROR) {
108 m_ui.levelGameError->setCheckState(set ? Qt::Checked : Qt::Unchecked);
109 }
110
111 if (set) {
112 emit levelsEnabled(level);
113 } else {
114 emit levelsDisabled(level);
115 }
116}
117
118void LogView::setMaxLines(int limit) {
119 m_lineLimit = limit;
120 while (m_lines > m_lineLimit) {
121 clearLine();
122 }
123}
124
125void LogView::paintEvent(QPaintEvent* event) {
126 while (!m_pendingLines.isEmpty()) {
127 m_ui.view->appendPlainText(m_pendingLines.dequeue());
128 }
129 QWidget::paintEvent(event);
130}
131
132void LogView::clearLine() {
133 if (m_ui.view->document()->isEmpty()) {
134 m_pendingLines.dequeue();
135 } else {
136 QTextCursor cursor(m_ui.view->document());
137 cursor.setPosition(0);
138 cursor.select(QTextCursor::BlockUnderCursor);
139 cursor.removeSelectedText();
140 cursor.deleteChar();
141 }
142 --m_lines;
143}