Remove some duplicate code

Signed-off-by: Yihe Li <winmikedows@hotmail.com>
This commit is contained in:
Yihe Li 2025-06-05 16:15:29 +08:00
parent e4a801fdf7
commit ef3bf75715
No known key found for this signature in database
7 changed files with 31 additions and 50 deletions

View file

@ -696,8 +696,8 @@ Application::Application(int& argc, char** argv) : QApplication(argc, argv)
m_settings->registerSetting("ConsoleMaxLines", 100000); m_settings->registerSetting("ConsoleMaxLines", 100000);
m_settings->registerSetting("ConsoleOverflowStop", true); m_settings->registerSetting("ConsoleOverflowStop", true);
logModel->setMaxLines(getConsoleMaxLines()); logModel->setMaxLines(getConsoleMaxLines(settings()));
logModel->setStopOnOverflow(shouldStopOnConsoleOverflow()); logModel->setStopOnOverflow(shouldStopOnConsoleOverflow(settings()));
logModel->setOverflowMessage(tr("Cannot display this log since the log length surpassed %1 lines.").arg(logModel->getMaxLines())); logModel->setOverflowMessage(tr("Cannot display this log since the log length surpassed %1 lines.").arg(logModel->getMaxLines()));
// Folders // Folders
@ -1605,23 +1605,6 @@ void Application::updateIsRunning(bool running)
m_updateRunning = running; m_updateRunning = running;
} }
int Application::getConsoleMaxLines() const
{
auto lineSetting = settings()->getSetting("ConsoleMaxLines");
bool conversionOk = false;
int maxLines = lineSetting->get().toInt(&conversionOk);
if (!conversionOk) {
maxLines = lineSetting->defValue().toInt();
qWarning() << "ConsoleMaxLines has nonsensical value, defaulting to" << maxLines;
}
return maxLines;
}
bool Application::shouldStopOnConsoleOverflow() const
{
return settings()->get("ConsoleOverflowStop").toBool();
}
void Application::controllerSucceeded() void Application::controllerSucceeded()
{ {
auto controller = qobject_cast<LaunchController*>(QObject::sender()); auto controller = qobject_cast<LaunchController*>(QObject::sender());

View file

@ -162,9 +162,6 @@ class Application : public QApplication {
QString getModrinthAPIToken(); QString getModrinthAPIToken();
QString getUserAgent(); QString getUserAgent();
int getConsoleMaxLines() const;
bool shouldStopOnConsoleOverflow() const;
/// this is the root of the 'installation'. Used for automatic updates /// this is the root of the 'installation'. Used for automatic updates
const QString& root() { return m_rootPath; } const QString& root() { return m_rootPath; }

View file

@ -53,6 +53,23 @@
#include "Commandline.h" #include "Commandline.h"
#include "FileSystem.h" #include "FileSystem.h"
int getConsoleMaxLines(SettingsObjectPtr settings)
{
auto lineSetting = settings->getSetting("ConsoleMaxLines");
bool conversionOk = false;
int maxLines = lineSetting->get().toInt(&conversionOk);
if (!conversionOk) {
maxLines = lineSetting->defValue().toInt();
qWarning() << "ConsoleMaxLines has nonsensical value, defaulting to" << maxLines;
}
return maxLines;
}
bool shouldStopOnConsoleOverflow(SettingsObjectPtr settings)
{
return settings->get("ConsoleOverflowStop").toBool();
}
BaseInstance::BaseInstance(SettingsObjectPtr globalSettings, SettingsObjectPtr settings, const QString& rootDir) : QObject() BaseInstance::BaseInstance(SettingsObjectPtr globalSettings, SettingsObjectPtr settings, const QString& rootDir) : QObject()
{ {
m_settings = settings; m_settings = settings;
@ -184,23 +201,6 @@ void BaseInstance::copyManagedPack(BaseInstance& other)
} }
} }
int BaseInstance::getConsoleMaxLines() const
{
auto lineSetting = m_settings->getSetting("ConsoleMaxLines");
bool conversionOk = false;
int maxLines = lineSetting->get().toInt(&conversionOk);
if (!conversionOk) {
maxLines = lineSetting->defValue().toInt();
qWarning() << "ConsoleMaxLines has nonsensical value, defaulting to" << maxLines;
}
return maxLines;
}
bool BaseInstance::shouldStopOnConsoleOverflow() const
{
return m_settings->get("ConsoleOverflowStop").toBool();
}
QStringList BaseInstance::getLinkedInstances() const QStringList BaseInstance::getLinkedInstances() const
{ {
auto setting = m_settings->get("linkedInstances").toString(); auto setting = m_settings->get("linkedInstances").toString();

View file

@ -78,6 +78,10 @@ struct ShortcutData {
ShortcutTarget target = ShortcutTarget::Other; ShortcutTarget target = ShortcutTarget::Other;
}; };
/// Console settings
int getConsoleMaxLines(SettingsObjectPtr settings);
bool shouldStopOnConsoleOverflow(SettingsObjectPtr settings);
/*! /*!
* \brief Base class for instances. * \brief Base class for instances.
* This class implements many functions that are common between instances and * This class implements many functions that are common between instances and
@ -272,9 +276,6 @@ class BaseInstance : public QObject, public std::enable_shared_from_this<BaseIns
Status currentStatus() const; Status currentStatus() const;
int getConsoleMaxLines() const;
bool shouldStopOnConsoleOverflow() const;
QStringList getLinkedInstances() const; QStringList getLinkedInstances() const;
void setLinkedInstances(const QStringList& list); void setLinkedInstances(const QStringList& list);
void addLinkedInstanceId(const QString& id); void addLinkedInstanceId(const QString& id);

View file

@ -27,6 +27,6 @@ MessageLevel::Enum getLevel(QtMsgType type);
/* Get message level from a line. Line is modified if it was successful. */ /* Get message level from a line. Line is modified if it was successful. */
MessageLevel::Enum fromLine(QString& line); MessageLevel::Enum fromLine(QString& line);
/* Get message level from a line from the launcher log. Line is modified if it was successful. */ /* Get message level from a line from the launcher log. Line is modified if it was successful. */
MessageLevel::Enum fromLauncherLine(QString& line); MessageLevel::Enum fromLauncherLine(QString& line);
} // namespace MessageLevel } // namespace MessageLevel

View file

@ -203,8 +203,8 @@ shared_qobject_ptr<LogModel> LaunchTask::getLogModel()
{ {
if (!m_logModel) { if (!m_logModel) {
m_logModel.reset(new LogModel()); m_logModel.reset(new LogModel());
m_logModel->setMaxLines(m_instance->getConsoleMaxLines()); m_logModel->setMaxLines(getConsoleMaxLines(m_instance->settings()));
m_logModel->setStopOnOverflow(m_instance->shouldStopOnConsoleOverflow()); m_logModel->setStopOnOverflow(shouldStopOnConsoleOverflow(m_instance->settings()));
// FIXME: should this really be here? // FIXME: should this really be here?
m_logModel->setOverflowMessage(tr("Stopped watching the game log because the log length surpassed %1 lines.\n" m_logModel->setOverflowMessage(tr("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 logging to files and" "You may have to fix your mods because the game is still logging to files and"

View file

@ -66,7 +66,7 @@ OtherLogsPage::OtherLogsPage(QString id, QString displayName, QString helpPage,
m_proxy = new LogFormatProxyModel(this); m_proxy = new LogFormatProxyModel(this);
if (m_instance) { if (m_instance) {
m_model.reset(new LogModel(this)); m_model.reset(new LogModel(this));
ui->trackLogCheckbox->setVisible(false); ui->trackLogCheckbox->hide();
} else { } else {
m_model = APPLICATION->logModel; m_model = APPLICATION->logModel;
} }
@ -85,8 +85,8 @@ OtherLogsPage::OtherLogsPage(QString id, QString displayName, QString helpPage,
ui->text->setModel(m_proxy); ui->text->setModel(m_proxy);
if (m_instance) { if (m_instance) {
m_model->setMaxLines(m_instance->getConsoleMaxLines()); m_model->setMaxLines(getConsoleMaxLines(m_instance->settings()));
m_model->setStopOnOverflow(m_instance->shouldStopOnConsoleOverflow()); m_model->setStopOnOverflow(shouldStopOnConsoleOverflow(m_instance->settings()));
m_model->setOverflowMessage(tr("Cannot display this log since the log length surpassed %1 lines.").arg(m_model->getMaxLines())); m_model->setOverflowMessage(tr("Cannot display this log since the log length surpassed %1 lines.").arg(m_model->getMaxLines()));
} else { } else {
modelStateToUI(); modelStateToUI();
@ -310,8 +310,8 @@ void OtherLogsPage::reload()
ui->text->setModel(nullptr); ui->text->setModel(nullptr);
if (!m_instance) { if (!m_instance) {
m_model.reset(new LogModel(this)); m_model.reset(new LogModel(this));
m_model->setMaxLines(APPLICATION->getConsoleMaxLines()); m_model->setMaxLines(getConsoleMaxLines(APPLICATION->settings()));
m_model->setStopOnOverflow(APPLICATION->shouldStopOnConsoleOverflow()); m_model->setStopOnOverflow(shouldStopOnConsoleOverflow(APPLICATION->settings()));
m_model->setOverflowMessage(tr("Cannot display this log since the log length surpassed %1 lines.").arg(m_model->getMaxLines())); m_model->setOverflowMessage(tr("Cannot display this log since the log length surpassed %1 lines.").arg(m_model->getMaxLines()));
} }
m_model->clear(); m_model->clear();