summaryrefslogtreecommitdiff
path: root/meshmc/launcher/launch/LogModel.cpp
blob: fd21c48f732a66a1d21494a69bee419e9074f7c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* SPDX-FileCopyrightText: 2026 Project Tick
 * SPDX-FileContributor: Project Tick
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
 *   MeshMC - A Custom Launcher for Minecraft
 *   Copyright (C) 2026 Project Tick
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "LogModel.h"

LogModel::LogModel(QObject* parent) : QAbstractListModel(parent)
{
	m_content.resize(m_maxLines);
}

int LogModel::rowCount(const QModelIndex& parent) const
{
	if (parent.isValid())
		return 0;

	return m_numLines;
}

QVariant LogModel::data(const QModelIndex& index, int role) const
{
	if (index.row() < 0 || index.row() >= m_numLines)
		return QVariant();

	auto row = index.row();
	auto realRow = (row + m_firstLine) % m_maxLines;
	if (role == Qt::DisplayRole || role == Qt::EditRole) {
		return m_content[realRow].line;
	}
	if (role == LevelRole) {
		return m_content[realRow].level;
	}

	return QVariant();
}

void LogModel::append(MessageLevel::Enum level, QString line)
{
	if (m_suspended) {
		return;
	}
	int lineNum = (m_firstLine + m_numLines) % m_maxLines;
	// overflow
	if (m_numLines == m_maxLines) {
		if (m_stopOnOverflow) {
			// nothing more to do, the buffer is full
			return;
		}
		beginRemoveRows(QModelIndex(), 0, 0);
		m_firstLine = (m_firstLine + 1) % m_maxLines;
		m_numLines--;
		endRemoveRows();
	} else if (m_numLines == m_maxLines - 1 && m_stopOnOverflow) {
		level = MessageLevel::Fatal;
		line = m_overflowMessage;
	}
	beginInsertRows(QModelIndex(), m_numLines, m_numLines);
	m_numLines++;
	m_content[lineNum].level = level;
	m_content[lineNum].line = line;
	endInsertRows();
}

void LogModel::suspend(bool suspend)
{
	m_suspended = suspend;
}

bool LogModel::suspended()
{
	return m_suspended;
}

void LogModel::clear()
{
	beginResetModel();
	m_firstLine = 0;
	m_numLines = 0;
	endResetModel();
}

QString LogModel::toPlainText()
{
	QString out;
	out.reserve(m_numLines * 80);
	for (int i = 0; i < m_numLines; i++) {
		QString& line = m_content[(m_firstLine + i) % m_maxLines].line;
		out.append(line + '\n');
	}
	out.squeeze();
	return out;
}

void LogModel::setMaxLines(int maxLines)
{
	// no-op
	if (maxLines == m_maxLines) {
		return;
	}
	// if it all still fits in the buffer, just resize it
	if (m_firstLine + m_numLines < m_maxLines) {
		m_maxLines = maxLines;
		m_content.resize(maxLines);
		return;
	}
	// otherwise, we need to reorganize the data because it crosses the wrap
	// boundary
	QVector<entry> newContent;
	newContent.resize(maxLines);
	if (m_numLines <= maxLines) {
		// if it all fits in the new buffer, just copy it over
		for (int i = 0; i < m_numLines; i++) {
			newContent[i] = m_content[(m_firstLine + i) % m_maxLines];
		}
		m_content.swap(newContent);
	} else {
		// if it doesn't fit, part of the data needs to be thrown away (the
		// oldest log messages)
		int lead = m_numLines - maxLines;
		beginRemoveRows(QModelIndex(), 0, lead - 1);
		for (int i = 0; i < maxLines; i++) {
			newContent[i] = m_content[(m_firstLine + lead + i) % m_maxLines];
		}
		m_numLines = m_maxLines;
		m_content.swap(newContent);
		endRemoveRows();
	}
	m_firstLine = 0;
	m_maxLines = maxLines;
}

int LogModel::getMaxLines()
{
	return m_maxLines;
}

void LogModel::setStopOnOverflow(bool stop)
{
	m_stopOnOverflow = stop;
}

void LogModel::setOverflowMessage(const QString& overflowMessage)
{
	m_overflowMessage = overflowMessage;
}

void LogModel::setLineWrap(bool state)
{
	if (m_lineWrap != state) {
		m_lineWrap = state;
	}
}

bool LogModel::wrapLines() const
{
	return m_lineWrap;
}