2014-06-30 05:32:57 +05:30
|
|
|
#include "LogPage.h"
|
|
|
|
#include "ui_LogPage.h"
|
2014-07-12 21:28:23 +05:30
|
|
|
|
2014-11-10 00:18:35 +05:30
|
|
|
#include "MultiMC.h"
|
|
|
|
|
2014-07-12 21:28:23 +05:30
|
|
|
#include <QIcon>
|
2014-06-30 05:32:57 +05:30
|
|
|
#include <QScrollBar>
|
2014-11-09 05:29:22 +05:30
|
|
|
#include <QShortcut>
|
2014-06-30 05:32:57 +05:30
|
|
|
|
2015-07-10 04:41:06 +05:30
|
|
|
#include "launch/LaunchTask.h"
|
2015-06-11 05:19:13 +05:30
|
|
|
#include <settings/Setting.h>
|
2015-02-09 06:21:14 +05:30
|
|
|
#include "GuiUtil.h"
|
2015-08-20 05:19:03 +05:30
|
|
|
#include <ColorCache.h>
|
2014-07-12 21:28:23 +05:30
|
|
|
|
2016-08-10 12:11:58 +05:30
|
|
|
LogPage::LogPage(InstancePtr instance, QWidget *parent)
|
|
|
|
: QWidget(parent), ui(new Ui::LogPage), m_instance(instance)
|
2014-06-30 05:32:57 +05:30
|
|
|
{
|
2016-08-10 12:11:58 +05:30
|
|
|
ui->setupUi(this);
|
|
|
|
ui->tabWidget->tabBar()->hide();
|
2016-08-08 03:30:11 +05:30
|
|
|
|
2016-08-10 12:11:58 +05:30
|
|
|
// create the format and set its font
|
2016-08-08 03:30:11 +05:30
|
|
|
{
|
2016-08-10 12:11:58 +05:30
|
|
|
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));
|
2016-08-08 03:30:11 +05:30
|
|
|
}
|
|
|
|
|
2016-08-10 12:11:58 +05:30
|
|
|
// ensure we don't eat all the RAM
|
2016-08-08 03:30:11 +05:30
|
|
|
{
|
2016-08-10 12:11:58 +05:30
|
|
|
auto lineSetting = MMC->settings()->getSetting("ConsoleMaxLines");
|
|
|
|
bool conversionOk = false;
|
|
|
|
int maxLines = lineSetting->get().toInt(&conversionOk);
|
|
|
|
if(!conversionOk)
|
2016-08-08 03:30:11 +05:30
|
|
|
{
|
2016-08-10 12:11:58 +05:30
|
|
|
maxLines = lineSetting->defValue().toInt();
|
|
|
|
qWarning() << "ConsoleMaxLines has nonsensical value, defaulting to" << maxLines;
|
2016-08-06 19:09:29 +05:30
|
|
|
}
|
2016-08-10 12:11:58 +05:30
|
|
|
ui->text->setMaximumBlockCount(maxLines);
|
2016-08-06 19:09:29 +05:30
|
|
|
|
2016-08-10 12:11:58 +05:30
|
|
|
m_stopOnOverflow = MMC->settings()->get("ConsoleOverflowStop").toBool();
|
2015-06-11 05:19:13 +05:30
|
|
|
}
|
|
|
|
|
2016-08-06 19:09:29 +05:30
|
|
|
// set up instance and launch process recognition
|
|
|
|
{
|
|
|
|
auto launchTask = m_instance->getLaunchTask();
|
|
|
|
if(launchTask)
|
|
|
|
{
|
|
|
|
on_InstanceLaunchTask_changed(launchTask);
|
|
|
|
}
|
|
|
|
connect(m_instance.get(), &BaseInstance::launchTaskChanged,
|
|
|
|
this, &LogPage::on_InstanceLaunchTask_changed);
|
|
|
|
}
|
2015-08-20 05:19:03 +05:30
|
|
|
|
2016-08-10 12:11:58 +05:30
|
|
|
// set up text colors and adapt them to the current theme foreground and background
|
2016-08-06 19:09:29 +05:30
|
|
|
{
|
2016-08-10 12:11:58 +05:30
|
|
|
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));
|
2016-08-06 19:09:29 +05:30
|
|
|
}
|
2015-06-11 05:19:13 +05:30
|
|
|
|
2014-11-09 05:29:22 +05:30
|
|
|
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()));
|
2014-06-30 05:32:57 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
LogPage::~LogPage()
|
|
|
|
{
|
|
|
|
delete ui;
|
2016-08-10 12:11:58 +05:30
|
|
|
delete defaultFormat;
|
2014-06-30 05:32:57 +05:30
|
|
|
}
|
|
|
|
|
2016-08-06 19:09:29 +05:30
|
|
|
void LogPage::on_InstanceLaunchTask_changed(std::shared_ptr<LaunchTask> proc)
|
|
|
|
{
|
|
|
|
if(m_process)
|
|
|
|
{
|
2016-08-10 12:11:58 +05:30
|
|
|
disconnect(m_process.get(), &LaunchTask::log, this, &LogPage::write);
|
2016-08-06 19:09:29 +05:30
|
|
|
}
|
2016-08-10 12:11:58 +05:30
|
|
|
m_process = proc;
|
|
|
|
if(m_process)
|
2016-08-06 19:09:29 +05:30
|
|
|
{
|
2016-08-10 12:11:58 +05:30
|
|
|
ui->text->clear();
|
|
|
|
connect(m_process.get(), &LaunchTask::log, this, &LogPage::write);
|
2016-08-06 19:09:29 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-30 05:32:57 +05:30
|
|
|
bool LogPage::apply()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-12 21:28:23 +05:30
|
|
|
bool LogPage::shouldDisplay() const
|
2014-06-30 05:32:57 +05:30
|
|
|
{
|
2016-08-10 12:11:58 +05:30
|
|
|
return m_instance->isRunning() || ui->text->blockCount() > 1;
|
2014-06-30 05:32:57 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void LogPage::on_btnPaste_clicked()
|
|
|
|
{
|
2016-01-20 07:40:02 +05:30
|
|
|
//FIXME: turn this into a proper task and move the upload logic out of GuiUtil!
|
2016-08-10 12:11:58 +05:30
|
|
|
write(tr("MultiMC: Log upload triggered at: %1").arg(QDateTime::currentDateTime().toString(Qt::RFC2822Date)), MessageLevel::MultiMC);
|
|
|
|
auto url = GuiUtil::uploadPaste(ui->text->toPlainText(), this);
|
2016-01-20 07:40:02 +05:30
|
|
|
if(!url.isEmpty())
|
|
|
|
{
|
2016-08-10 12:11:58 +05:30
|
|
|
write(tr("MultiMC: Log uploaded to: %1").arg(url), MessageLevel::MultiMC);
|
2016-01-20 07:40:02 +05:30
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-08-10 12:11:58 +05:30
|
|
|
write(tr("MultiMC: Log upload failed!"), MessageLevel::Error);
|
2016-01-20 07:40:02 +05:30
|
|
|
}
|
2014-06-30 05:32:57 +05:30
|
|
|
}
|
|
|
|
|
2014-07-11 05:20:36 +05:30
|
|
|
void LogPage::on_btnCopy_clicked()
|
|
|
|
{
|
2016-08-10 12:11:58 +05:30
|
|
|
write(QString("Clipboard copy at: %1").arg(QDateTime::currentDateTime().toString(Qt::RFC2822Date)), MessageLevel::MultiMC);
|
|
|
|
GuiUtil::setClipboardText(ui->text->toPlainText());
|
2014-07-11 05:20:36 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void LogPage::on_btnClear_clicked()
|
|
|
|
{
|
2016-08-10 12:11:58 +05:30
|
|
|
ui->text->clear();
|
2014-07-11 05:20:36 +05:30
|
|
|
}
|
|
|
|
|
2015-04-29 04:58:58 +05:30
|
|
|
void LogPage::on_btnBottom_clicked()
|
|
|
|
{
|
2016-08-10 12:11:58 +05:30
|
|
|
ui->text->verticalScrollBar()->setSliderPosition(ui->text->verticalScrollBar()->maximum());
|
2015-04-29 04:58:58 +05:30
|
|
|
}
|
|
|
|
|
2014-11-09 05:29:22 +05:30
|
|
|
void LogPage::on_trackLogCheckbox_clicked(bool checked)
|
|
|
|
{
|
|
|
|
m_write_active = checked;
|
|
|
|
}
|
|
|
|
|
2016-04-11 01:22:01 +05:30
|
|
|
void LogPage::on_wrapCheckbox_clicked(bool checked)
|
|
|
|
{
|
|
|
|
if(checked)
|
|
|
|
{
|
2016-08-10 12:11:58 +05:30
|
|
|
ui->text->setWordWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
|
2016-04-11 01:22:01 +05:30
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-08-10 12:11:58 +05:30
|
|
|
ui->text->setWordWrapMode(QTextOption::WrapMode::NoWrap);
|
2016-04-11 01:22:01 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-10 12:11:58 +05:30
|
|
|
void LogPage::on_findButton_clicked()
|
2014-11-09 05:29:22 +05:30
|
|
|
{
|
2016-08-10 12:11:58 +05:30
|
|
|
auto modifiers = QApplication::keyboardModifiers();
|
|
|
|
if (modifiers & Qt::ShiftModifier)
|
2014-11-09 05:29:22 +05:30
|
|
|
{
|
2016-08-10 12:11:58 +05:30
|
|
|
findPreviousActivated();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
findNextActivated();
|
2014-11-09 05:29:22 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-10 12:11:58 +05:30
|
|
|
void LogPage::findActivated()
|
2014-11-09 05:29:22 +05:30
|
|
|
{
|
2016-08-10 12:11:58 +05:30
|
|
|
// 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();
|
|
|
|
}
|
2014-11-09 05:29:22 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void LogPage::findNextActivated()
|
|
|
|
{
|
2016-08-10 12:11:58 +05:30
|
|
|
auto toSearch = ui->searchBar->text();
|
|
|
|
if (toSearch.size())
|
|
|
|
{
|
|
|
|
ui->text->find(toSearch);
|
|
|
|
}
|
2014-11-09 05:29:22 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void LogPage::findPreviousActivated()
|
|
|
|
{
|
2016-08-10 12:11:58 +05:30
|
|
|
auto toSearch = ui->searchBar->text();
|
|
|
|
if (toSearch.size())
|
2014-11-09 05:29:22 +05:30
|
|
|
{
|
2016-08-10 12:11:58 +05:30
|
|
|
ui->text->find(toSearch, QTextDocument::FindBackward);
|
2014-11-09 05:29:22 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-11 05:19:13 +05:30
|
|
|
void LogPage::setParentContainer(BasePageContainer * container)
|
|
|
|
{
|
|
|
|
m_parentContainer = container;
|
|
|
|
}
|
|
|
|
|
2016-08-10 12:11:58 +05:30
|
|
|
void LogPage::write(QString data, MessageLevel::Enum mode)
|
2014-06-30 05:32:57 +05:30
|
|
|
{
|
2016-08-10 12:11:58 +05:30
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-09 19:23:08 +05:30
|
|
|
|
2016-08-10 12:11:58 +05:30
|
|
|
// 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)
|
2014-06-30 05:32:57 +05:30
|
|
|
{
|
2016-08-10 12:11:58 +05:30
|
|
|
//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();
|
2014-06-30 05:32:57 +05:30
|
|
|
}
|
2016-08-10 12:11:58 +05:30
|
|
|
ui->text->setCursor(savedCursor);
|
2014-06-30 05:32:57 +05:30
|
|
|
}
|