sneedmc/launcher/ui/pages/instance/LogPage.cpp

332 lines
8.5 KiB
C++
Raw Normal View History

#include "LogPage.h"
#include "ui_LogPage.h"
2021-11-20 15:22:22 +00:00
#include "Application.h"
2014-11-09 18:48:35 +00:00
#include <QIcon>
#include <QScrollBar>
#include <QShortcut>
2022-02-20 02:55:58 +00:00
#include <QIdentityProxyModel>
#include "launch/LaunchTask.h"
#include "settings/Setting.h"
#include "ui/GuiUtil.h"
#include "ui/ColorCache.h"
2021-10-17 22:47:02 +00:00
#include <BuildConfig.h>
class LogFormatProxyModel : public QIdentityProxyModel
{
public:
2018-07-15 12:51:05 +00:00
LogFormatProxyModel(QObject* parent = nullptr) : QIdentityProxyModel(parent)
{
}
QVariant data(const QModelIndex &index, int role) const override
{
switch(role)
{
case Qt::FontRole:
return m_font;
case Qt::TextColorRole:
{
MessageLevel::Enum level = (MessageLevel::Enum) QIdentityProxyModel::data(index, LogModel::LevelRole).toInt();
return m_colors->getFront(level);
}
case Qt::BackgroundRole:
{
MessageLevel::Enum level = (MessageLevel::Enum) QIdentityProxyModel::data(index, LogModel::LevelRole).toInt();
return m_colors->getBack(level);
}
default:
return QIdentityProxyModel::data(index, role);
}
}
void setFont(QFont font)
{
m_font = font;
}
void setColors(LogColorCache* colors)
{
m_colors.reset(colors);
}
QModelIndex find(const QModelIndex &start, const QString &value, bool reverse) const
{
QModelIndex parentIndex = parent(start);
auto compare = [&](int r) -> QModelIndex
{
QModelIndex idx = index(r, start.column(), parentIndex);
if (!idx.isValid() || idx == start)
{
return QModelIndex();
}
QVariant v = data(idx, Qt::DisplayRole);
QString t = v.toString();
if (t.contains(value, Qt::CaseInsensitive))
return idx;
return QModelIndex();
};
if(reverse)
{
int from = start.row();
int to = 0;
for (int i = 0; i < 2; ++i)
{
for (int r = from; (r >= to); --r)
{
auto idx = compare(r);
if(idx.isValid())
return idx;
}
// prepare for the next iteration
from = rowCount() - 1;
to = start.row();
}
}
else
{
int from = start.row();
int to = rowCount(parentIndex);
for (int i = 0; i < 2; ++i)
{
for (int r = from; (r < to); ++r)
{
auto idx = compare(r);
if(idx.isValid())
return idx;
}
// prepare for the next iteration
from = 0;
to = start.row();
}
}
return QModelIndex();
}
private:
2018-07-15 12:51:05 +00:00
QFont m_font;
std::unique_ptr<LogColorCache> m_colors;
};
LogPage::LogPage(InstancePtr instance, QWidget *parent)
2018-07-15 12:51:05 +00:00
: QWidget(parent), ui(new Ui::LogPage), m_instance(instance)
{
2018-07-15 12:51:05 +00:00
ui->setupUi(this);
ui->tabWidget->tabBar()->hide();
m_proxy = new LogFormatProxyModel(this);
// set up text colors in the log proxy and adapt them to the current theme foreground and background
{
auto origForeground = ui->text->palette().color(ui->text->foregroundRole());
auto origBackground = ui->text->palette().color(ui->text->backgroundRole());
m_proxy->setColors(new LogColorCache(origForeground, origBackground));
}
// set up fonts in the log proxy
{
2021-11-20 15:22:22 +00:00
QString fontFamily = APPLICATION->settings()->get("ConsoleFont").toString();
2018-07-15 12:51:05 +00:00
bool conversionOk = false;
2021-11-20 15:22:22 +00:00
int fontSize = APPLICATION->settings()->get("ConsoleFontSize").toInt(&conversionOk);
2018-07-15 12:51:05 +00:00
if(!conversionOk)
{
fontSize = 11;
}
m_proxy->setFont(QFont(fontFamily, fontSize));
}
ui->text->setModel(m_proxy);
// set up instance and launch process recognition
{
auto launchTask = m_instance->getLaunchTask();
if(launchTask)
{
setInstanceLaunchTaskChanged(launchTask, true);
}
connect(m_instance.get(), &BaseInstance::launchTaskChanged, this, &LogPage::onInstanceLaunchTaskChanged);
}
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()
{
2018-07-15 12:51:05 +00:00
delete ui;
}
void LogPage::modelStateToUI()
{
2018-07-15 12:51:05 +00:00
if(m_model->wrapLines())
{
ui->text->setWordWrap(true);
ui->wrapCheckbox->setCheckState(Qt::Checked);
}
else
{
ui->text->setWordWrap(false);
ui->wrapCheckbox->setCheckState(Qt::Unchecked);
}
if(m_model->suspended())
{
ui->trackLogCheckbox->setCheckState(Qt::Unchecked);
}
else
{
ui->trackLogCheckbox->setCheckState(Qt::Checked);
}
}
void LogPage::UIToModelState()
{
2018-07-15 12:51:05 +00:00
if(!m_model)
{
return;
}
m_model->setLineWrap(ui->wrapCheckbox->checkState() == Qt::Checked);
m_model->suspend(ui->trackLogCheckbox->checkState() != Qt::Checked);
}
void LogPage::setInstanceLaunchTaskChanged(shared_qobject_ptr<LaunchTask> proc, bool initial)
{
2018-07-15 12:51:05 +00:00
m_process = proc;
if(m_process)
{
m_model = proc->getLogModel();
m_proxy->setSourceModel(m_model.get());
if(initial)
{
modelStateToUI();
}
else
{
UIToModelState();
}
}
else
{
m_proxy->setSourceModel(nullptr);
m_model.reset();
}
}
void LogPage::onInstanceLaunchTaskChanged(shared_qobject_ptr<LaunchTask> proc)
{
2018-07-15 12:51:05 +00:00
setInstanceLaunchTaskChanged(proc, false);
}
bool LogPage::apply()
{
2018-07-15 12:51:05 +00:00
return true;
}
bool LogPage::shouldDisplay() const
{
2018-07-15 12:51:05 +00:00
return m_instance->isRunning() || m_proxy->rowCount() > 0;
}
void LogPage::on_btnPaste_clicked()
{
2018-07-15 12:51:05 +00:00
if(!m_model)
return;
//FIXME: turn this into a proper task and move the upload logic out of GuiUtil!
2021-10-17 22:47:02 +00:00
m_model->append(
MessageLevel::Launcher,
QString("%2: Log upload triggered at: %1").arg(
QDateTime::currentDateTime().toString(Qt::RFC2822Date),
BuildConfig.LAUNCHER_NAME
)
);
2018-07-15 12:51:05 +00:00
auto url = GuiUtil::uploadPaste(m_model->toPlainText(), this);
if(!url.isEmpty())
{
2021-10-17 22:47:02 +00:00
m_model->append(
MessageLevel::Launcher,
QString("%2: Log uploaded to: %1").arg(
url,
BuildConfig.LAUNCHER_NAME
)
);
2018-07-15 12:51:05 +00:00
}
else
{
2021-10-17 22:47:02 +00:00
m_model->append(
MessageLevel::Error,
QString("%1: Log upload failed!").arg(BuildConfig.LAUNCHER_NAME)
);
2018-07-15 12:51:05 +00:00
}
}
void LogPage::on_btnCopy_clicked()
{
2018-07-15 12:51:05 +00:00
if(!m_model)
return;
2021-10-17 22:47:02 +00:00
m_model->append(MessageLevel::Launcher, QString("Clipboard copy at: %1").arg(QDateTime::currentDateTime().toString(Qt::RFC2822Date)));
2018-07-15 12:51:05 +00:00
GuiUtil::setClipboardText(m_model->toPlainText());
}
void LogPage::on_btnClear_clicked()
{
2018-07-15 12:51:05 +00:00
if(!m_model)
return;
m_model->clear();
m_container->refreshContainer();
}
void LogPage::on_btnBottom_clicked()
{
2018-07-15 12:51:05 +00:00
ui->text->scrollToBottom();
}
void LogPage::on_trackLogCheckbox_clicked(bool checked)
{
2018-07-15 12:51:05 +00:00
if(!m_model)
return;
m_model->suspend(!checked);
}
void LogPage::on_wrapCheckbox_clicked(bool checked)
{
2018-07-15 12:51:05 +00:00
ui->text->setWordWrap(checked);
if(!m_model)
return;
m_model->setLineWrap(checked);
}
void LogPage::on_findButton_clicked()
{
2018-07-15 12:51:05 +00:00
auto modifiers = QApplication::keyboardModifiers();
bool reverse = modifiers & Qt::ShiftModifier;
ui->text->findNext(ui->searchBar->text(), reverse);
}
void LogPage::findNextActivated()
{
2018-07-15 12:51:05 +00:00
ui->text->findNext(ui->searchBar->text(), false);
}
void LogPage::findPreviousActivated()
{
2018-07-15 12:51:05 +00:00
ui->text->findNext(ui->searchBar->text(), true);
}
void LogPage::findActivated()
{
2018-07-15 12:51:05 +00:00
// focus the search bar if it doesn't have focus
if (!ui->searchBar->hasFocus())
{
ui->searchBar->setFocus();
ui->searchBar->selectAll();
}
}