summaryrefslogtreecommitdiffstats
path: root/gui/pages/LogPage.cpp
blob: 65b84b03ee67a3d73975f2ac036971d64b4daf2f (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
#include "LogPage.h"
#include <gui/dialogs/CustomMessageBox.h>
#include <gui/dialogs/ProgressDialog.h>
#include <logic/MinecraftProcess.h>
#include <QtGui/QIcon>
#include "ui_LogPage.h"
#include "logic/net/PasteUpload.h"
#include <QScrollBar>
#include <QtGui/QClipboard>
#include <QtGui/QDesktopServices>

QString LogPage::displayName()
{
	return tr("Minecraft Log");
}

QIcon LogPage::icon()
{
	return QIcon::fromTheme("refresh");
}

QString LogPage::id()
{
	return "console";
}

LogPage::LogPage(MinecraftProcess *proc, QWidget *parent)
	: QWidget(parent), ui(new Ui::LogPage), m_process(proc)
{
	ui->setupUi(this);
	connect(m_process, SIGNAL(log(QString, MessageLevel::Enum)), this,
		SLOT(write(QString, MessageLevel::Enum)));
}

LogPage::~LogPage()
{
	delete ui;
}

bool LogPage::apply()
{
	return true;
}

bool LogPage::shouldDisplay()
{
	return m_process->instance()->isRunning();
}

void LogPage::on_btnPaste_clicked()
{
	auto text = ui->text->toPlainText();
	ProgressDialog dialog(this);
	PasteUpload *paste = new PasteUpload(this, text);
	dialog.exec(paste);
	if (!paste->successful())
	{
		CustomMessageBox::selectable(this, "Upload failed", paste->failReason(),
									 QMessageBox::Critical)->exec();
	}
	else
	{
		QString link = paste->pasteLink();
		QClipboard *clipboard = QApplication::clipboard();
		clipboard->setText(link);
		QDesktopServices::openUrl(link);
		CustomMessageBox::selectable(
			this, tr("Upload finished"),
		tr("The <a href=\"%1\">link to the uploaded log</a> has been opened in the default browser and placed in your clipboard.")
		.arg(link),
		QMessageBox::Information)->exec();
	}
	delete paste;
}

void LogPage::on_btnCopy_clicked()
{
	auto text = ui->text->toPlainText();
	QClipboard *clipboard = QApplication::clipboard();
	clipboard->setText(text);
}

void LogPage::on_btnClear_clicked()
{
	ui->text->clear();
}

void LogPage::writeColor(QString text, const char *color, const char * background)
{
	// append a paragraph
	QString newtext;
	newtext += "<span style=\"";
	{
		if (color)
			newtext += QString("color:") + color + ";";
		if (background)
			newtext += QString("background-color:") + background + ";";
		newtext += "font-family: monospace;";
	}
	newtext += "\">";
	newtext += text.toHtmlEscaped();
	newtext += "</span>";
	ui->text->appendHtml(newtext);
}

void LogPage::write(QString data, MessageLevel::Enum mode)
{
	QScrollBar *bar = ui->text->verticalScrollBar();
	int max_bar = bar->maximum();
	int val_bar = bar->value();
	if(isVisible())
	{
		if (m_scroll_active)
		{
			m_scroll_active = (max_bar - val_bar) <= 1;
		}
		else
		{
			m_scroll_active = val_bar == max_bar;
		}
	}
	if (data.endsWith('\n'))
		data = data.left(data.length() - 1);
	QStringList paragraphs = data.split('\n');
	QStringList filtered;
	for (QString &paragraph : paragraphs)
	{
		// Quick hack for
		if(paragraph.contains("Detected an attempt by a mod null to perform game activity during mod construction"))
			continue;
		filtered.append(paragraph.trimmed());
	}
	QListIterator<QString> iter(filtered);
	if (mode == MessageLevel::MultiMC)
		while (iter.hasNext())
			writeColor(iter.next(), "blue", 0);
	else if (mode == MessageLevel::Error)
		while (iter.hasNext())
			writeColor(iter.next(), "red", 0);
	else if (mode == MessageLevel::Warning)
		while (iter.hasNext())
			writeColor(iter.next(), "orange", 0);
	else if (mode == MessageLevel::Fatal)
		while (iter.hasNext())
			writeColor(iter.next(), "red", "black");
	else if (mode == MessageLevel::Debug)
		while (iter.hasNext())
			writeColor(iter.next(), "green", 0);
	else if (mode == MessageLevel::PrePost)
		while (iter.hasNext())
			writeColor(iter.next(), "grey", 0);
	// TODO: implement other MessageLevels
	else
		while (iter.hasNext())
			writeColor(iter.next(), 0, 0);
	if(isVisible())
	{
		if (m_scroll_active)
		{
			bar->setValue(bar->maximum());
		}
		m_last_scroll_value = bar->value();
	}
}