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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
#include "LogPage.h"
#include "ui_LogPage.h"
#include "MultiMC.h"
#include <QIcon>
#include <QScrollBar>
#include <QShortcut>
#include "launch/LaunchTask.h"
#include <settings/Setting.h>
#include "GuiUtil.h"
#include <ColorCache.h>
LogPage::LogPage(std::shared_ptr<LaunchTask> proc, QWidget *parent)
: QWidget(parent), ui(new Ui::LogPage), m_process(proc)
{
ui->setupUi(this);
ui->tabWidget->tabBar()->hide();
connect(m_process.get(), SIGNAL(log(QString, MessageLevel::Enum)), this,
SLOT(write(QString, MessageLevel::Enum)));
// create the format and set its font
defaultFormat = new QTextCharFormat(ui->text->currentCharFormat());
QString fontFamily = MMC->settings()->get("ConsoleFont").toString();
bool conversionOk = false;
int fontSize = MMC->settings()->get("ConsoleFontSize").toInt(&conversionOk);
if(!conversionOk)
{
fontSize = 11;
}
defaultFormat->setFont(QFont(fontFamily, fontSize));
// ensure we don't eat all the RAM
auto lineSetting = MMC->settings()->getSetting("ConsoleMaxLines");
int maxLines = lineSetting->get().toInt(&conversionOk);
if(!conversionOk)
{
maxLines = lineSetting->defValue().toInt();
qWarning() << "ConsoleMaxLines has nonsensical value, defaulting to" << maxLines;
}
ui->text->setMaximumBlockCount(maxLines);
auto origForeground = ui->text->palette().color(ui->text->foregroundRole());
auto origBackground = ui->text->palette().color(ui->text->backgroundRole());
m_colors.reset(new LogColorCache(origForeground, origBackground));
m_stopOnOverflow = MMC->settings()->get("ConsoleOverflowStop").toBool();
auto findShortcut = new QShortcut(QKeySequence(QKeySequence::Find), this);
connect(findShortcut, SIGNAL(activated()), SLOT(findActivated()));
auto findNextShortcut = new QShortcut(QKeySequence(QKeySequence::FindNext), this);
connect(findNextShortcut, SIGNAL(activated()), SLOT(findNextActivated()));
connect(ui->searchBar, SIGNAL(returnPressed()), SLOT(on_findButton_clicked()));
auto findPreviousShortcut = new QShortcut(QKeySequence(QKeySequence::FindPrevious), this);
connect(findPreviousShortcut, SIGNAL(activated()), SLOT(findPreviousActivated()));
}
LogPage::~LogPage()
{
delete ui;
delete defaultFormat;
}
bool LogPage::apply()
{
return true;
}
bool LogPage::shouldDisplay() const
{
return m_process->instance()->isRunning();
}
void LogPage::on_btnPaste_clicked()
{
//FIXME: turn this into a proper task and move the upload logic out of GuiUtil!
write(tr("MultiMC: Log upload triggered at: %1").arg(QDateTime::currentDateTime().toString(Qt::RFC2822Date)), MessageLevel::MultiMC);
auto url = GuiUtil::uploadPaste(ui->text->toPlainText(), this);
if(!url.isEmpty())
{
write(tr("MultiMC: Log uploaded to: %1").arg(url), MessageLevel::MultiMC);
}
else
{
write(tr("MultiMC: Log upload failed!"), MessageLevel::Error);
}
}
void LogPage::on_btnCopy_clicked()
{
write(QString("Clipboard copy at: %1").arg(QDateTime::currentDateTime().toString(Qt::RFC2822Date)), MessageLevel::MultiMC);
GuiUtil::setClipboardText(ui->text->toPlainText());
}
void LogPage::on_btnClear_clicked()
{
ui->text->clear();
}
void LogPage::on_btnBottom_clicked()
{
ui->text->verticalScrollBar()->setSliderPosition(ui->text->verticalScrollBar()->maximum());
}
void LogPage::on_trackLogCheckbox_clicked(bool checked)
{
m_write_active = checked;
}
void LogPage::on_findButton_clicked()
{
auto modifiers = QApplication::keyboardModifiers();
if (modifiers & Qt::ShiftModifier)
{
findPreviousActivated();
}
else
{
findNextActivated();
}
}
void LogPage::findActivated()
{
// focus the search bar if it doesn't have focus
if (!ui->searchBar->hasFocus())
{
auto searchForCursor = ui->text->textCursor();
auto searchForString = searchForCursor.selectedText();
if (searchForString.size())
{
ui->searchBar->setText(searchForString);
}
ui->searchBar->setFocus();
ui->searchBar->selectAll();
}
}
void LogPage::findNextActivated()
{
auto toSearch = ui->searchBar->text();
if (toSearch.size())
{
ui->text->find(toSearch);
}
}
void LogPage::findPreviousActivated()
{
auto toSearch = ui->searchBar->text();
if (toSearch.size())
{
ui->text->find(toSearch, QTextDocument::FindBackward);
}
}
void LogPage::setParentContainer(BasePageContainer * container)
{
m_parentContainer = container;
}
void LogPage::write(QString data, MessageLevel::Enum mode)
{
if (!m_write_active)
{
if (mode != MessageLevel::MultiMC)
{
return;
}
}
if(m_stopOnOverflow && m_write_active)
{
if(mode != MessageLevel::MultiMC)
{
if(ui->text->blockCount() >= ui->text->maximumBlockCount())
{
m_write_active = false;
data = tr("MultiMC stopped watching the game log because the log length surpassed %1 lines.\n"
"You may have to fix your mods because the game is still loggging to files and"
" likely wasting harddrive space at an alarming rate!")
.arg(ui->text->maximumBlockCount());
mode = MessageLevel::Fatal;
ui->trackLogCheckbox->setCheckState(Qt::Unchecked);
if(!isVisible())
{
m_parentContainer->selectPage(id());
}
}
}
}
// save the cursor so it can be restored.
auto savedCursor = ui->text->cursor();
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 ¶graph : paragraphs)
{
//TODO: implement filtering here.
filtered.append(paragraph);
}
QListIterator<QString> iter(filtered);
QTextCharFormat format(*defaultFormat);
format.setForeground(m_colors->getFront(mode));
format.setBackground(m_colors->getBack(mode));
while (iter.hasNext())
{
// append a paragraph/line
auto workCursor = ui->text->textCursor();
workCursor.movePosition(QTextCursor::End);
workCursor.insertText(iter.next(), format);
workCursor.insertBlock();
}
if (isVisible())
{
if (m_scroll_active)
{
bar->setValue(bar->maximum());
}
m_last_scroll_value = bar->value();
}
ui->text->setCursor(savedCursor);
}
|