From cd876a0f9ffd03c711812c2ade92836e5d6c0203 Mon Sep 17 00:00:00 2001 From: isanae <14251494+isanae@users.noreply.github.com> Date: Sun, 24 Nov 2019 20:11:50 -0500 Subject: split loot dialog, added ui file --- src/CMakeLists.txt | 10 +- src/loot.cpp | 271 +---------------------------------------------------- src/lootdialog.cpp | 222 +++++++++++++++++++++++++++++++++++++++++++ src/lootdialog.h | 47 ++++++++++ src/lootdialog.ui | 214 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 492 insertions(+), 272 deletions(-) create mode 100644 src/lootdialog.cpp create mode 100644 src/lootdialog.h create mode 100644 src/lootdialog.ui (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3b74ea37..db3bda73 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -143,6 +143,7 @@ SET(organizer_SRCS processrunner.cpp uilocker.cpp loot.cpp + lootdialog.cpp shared/windows_error.cpp shared/error_report.cpp @@ -265,6 +266,7 @@ SET(organizer_HDRS processrunner.h uilocker.h loot.h + lootdialog.h json.h shared/windows_error.h @@ -392,6 +394,11 @@ set(executables editexecutablesdialog ) +set(loot + loot + lootdialog +) + set(modinfo modinfo modinfobackup @@ -468,7 +475,6 @@ set(utilities shared/util usvfsconnector shared/windows_error - loot json ) @@ -490,7 +496,7 @@ set(widgets ) set(src_filters - application core browser dialogs downloads env executables modinfo + application core browser dialogs downloads env executables loot modinfo modinfo\\dialog modlist plugins previews profiles settings settingsdialog utilities widgets ) diff --git a/src/loot.cpp b/src/loot.cpp index c73dcaa7..f0618b0b 100644 --- a/src/loot.cpp +++ b/src/loot.cpp @@ -1,4 +1,5 @@ #include "loot.h" +#include "lootdialog.h" #include "spawn.h" #include "organizercore.h" #include "json.h" @@ -32,276 +33,6 @@ log::Levels levelFromLoot(lootcli::LogLevels level) } } -class LootDialog : public QDialog -{ -public: - LootDialog(QWidget* parent, OrganizerCore& core, Loot& loot) : - QDialog(parent), m_core(core), m_loot(loot), - m_label(nullptr), m_progress(nullptr), - m_report(nullptr), m_output(nullptr), m_buttons(nullptr), - m_finished(false), m_cancelling(false) - { - createUI(); - m_progress->setMaximum(0); - - QObject::connect( - &m_loot, &Loot::output, this, - [&](auto&& s){ addOutput(s); }, Qt::QueuedConnection); - - QObject::connect( - &m_loot, &Loot::progress, - this, [&](auto&& p){ setProgress(p); }, Qt::QueuedConnection); - - QObject::connect( - &m_loot, &Loot::log, this, - [&](auto&& lv, auto&& s){ log(lv, s); }, Qt::QueuedConnection); - - QObject::connect( - &m_loot, &Loot::finished, this, - [&]{ onFinished(); }, Qt::QueuedConnection); - } - - void setText(const QString& s) - { - m_label->setText(s); - } - - void setProgress(lootcli::Progress p) - { - setText(progressToString(p)); - - if (p == lootcli::Progress::Done) { - m_progress->setRange(0, 1); - m_progress->setValue(1); - } - } - - QString progressToString(lootcli::Progress p) - { - using P = lootcli::Progress; - - switch (p) - { - case P::CheckingMasterlistExistence: return tr("Checking masterlist existence"); - case P::UpdatingMasterlist: return tr("Updating masterlist"); - case P::LoadingLists: return tr("Loading lists"); - case P::ReadingPlugins: return tr("Reading plugins"); - case P::SortingPlugins: return tr("Sorting plugins"); - case P::WritingLoadorder: return tr("Writing loadorder.txt"); - case P::ParsingLootMessages: return tr("Parsing loot messages"); - case P::Done: return tr("Done"); - default: return QString("unknown progress %1").arg(static_cast(p)); - } - } - - void addOutput(const QString& s) - { - if (m_core.settings().diagnostics().lootLogLevel() > lootcli::LogLevels::Debug) { - return; - } - - const auto lines = s.split(QRegExp("[\\r\\n]"), QString::SkipEmptyParts); - - for (auto&& line : lines) { - if (line.isEmpty()) { - continue; - } - - addLineOutput(line); - } - } - - bool result() const - { - return m_loot.result(); - } - - void cancel() - { - if (!m_finished && !m_cancelling) { - addLineOutput(tr("Stopping LOOT...")); - m_loot.cancel(); - m_buttons->setEnabled(false); - m_cancelling = true; - } - } - - int exec() override - { - return QDialog::exec(); - } - - void openReport() - { - const auto path = m_loot.outPath(); - shell::Open(path); - } - -private: - OrganizerCore& m_core; - Loot& m_loot; - QLabel* m_label; - QProgressBar* m_progress; - QTextEdit* m_messages; - QPushButton* m_report; - QPlainTextEdit* m_output; - QDialogButtonBox* m_buttons; - bool m_finished; - bool m_cancelling; - - void createUI() - { - auto* root = new QWidget(this); - auto* ly = new QVBoxLayout(root); - - setLayout(new QVBoxLayout); - layout()->setContentsMargins(0, 0, 0, 0); - layout()->addWidget(root); - - m_label = new QLabel; - ly->addWidget(m_label); - - m_progress = new QProgressBar; - ly->addWidget(m_progress); - - m_messages = new QTextEdit; - ly->addWidget(m_messages); - - auto* more = createMoreUI(); - ly->addWidget(more); - - resize(700, 400); - } - - QWidget* createMoreUI() - { - auto* more = new QWidget; - auto* ly = new QVBoxLayout(more); - ly->setContentsMargins(0, 0, 0, 0); - - auto* buttons = new QHBoxLayout; - buttons->setContentsMargins(0, 0, 0, 0); - m_report = new QPushButton(tr("Open JSON report")); - m_report->setEnabled(false); - connect(m_report, &QPushButton::clicked, [&]{ openReport(); }); - buttons->addWidget(m_report); - buttons->addStretch(1); - ly->addLayout(buttons); - - m_output = new QPlainTextEdit; - ly->addWidget(m_output); - - m_buttons = new QDialogButtonBox(QDialogButtonBox::Cancel); - connect(m_buttons, &QDialogButtonBox::clicked, [&](auto* b){ onButton(b); }); - ly->addWidget(m_buttons); - - return more; - } - - void closeEvent(QCloseEvent* e) override - { - if (m_finished) { - QDialog::closeEvent(e); - } else { - cancel(); - e->ignore(); - } - } - - void onButton(QAbstractButton* b) - { - if (m_buttons->buttonRole(b) == QDialogButtonBox::RejectRole) { - if (m_finished) { - close(); - } else { - cancel(); - } - } - } - - void addLineOutput(const QString& line) - { - m_output->appendPlainText(line); - } - - void onFinished() - { - m_finished = true; - - if (m_cancelling) { - close(); - } else { - handleReport(); - m_report->setEnabled(true); - m_buttons->setStandardButtons(QDialogButtonBox::Close); - } - } - - void log(log::Levels lv, const QString& s) - { - if (lv >= log::Levels::Warning) { - log::log(lv, "{}", s); - } - - if (m_core.settings().diagnostics().lootLogLevel() > lootcli::LogLevels::Debug) { - addLineOutput(QString("[%1] %2").arg(log::levelToString(lv)).arg(s)); - } - } - - void handleReport() - { - const auto& report = m_loot.report(); - - if (!report.messages.empty()) { - addLineOutput(""); - } - - QString html; - - if (!report.messages.empty()) { - html += ""; - } else { - html += QObject::tr("No messages."); - } - - m_messages->setHtml(html); - - for (auto&& p : report.plugins) { - m_core.pluginList()->addLootReport(p.name, p); - } - } -}; - QString Loot::Dirty::toString(bool isClean) const { diff --git a/src/lootdialog.cpp b/src/lootdialog.cpp new file mode 100644 index 00000000..db41959d --- /dev/null +++ b/src/lootdialog.cpp @@ -0,0 +1,222 @@ +#include "lootdialog.h" +#include "ui_lootdialog.h" +#include "loot.h" +#include "organizercore.h" +#include +#include + +using namespace MOBase; + +LootDialog::LootDialog(QWidget* parent, OrganizerCore& core, Loot& loot) : + QDialog(parent), ui(new Ui::LootDialog), m_core(core), m_loot(loot), + m_finished(false), m_cancelling(false) +{ + createUI(); + + QObject::connect( + &m_loot, &Loot::output, this, + [&](auto&& s){ addOutput(s); }, Qt::QueuedConnection); + + QObject::connect( + &m_loot, &Loot::progress, + this, [&](auto&& p){ setProgress(p); }, Qt::QueuedConnection); + + QObject::connect( + &m_loot, &Loot::log, this, + [&](auto&& lv, auto&& s){ log(lv, s); }, Qt::QueuedConnection); + + QObject::connect( + &m_loot, &Loot::finished, this, + [&]{ onFinished(); }, Qt::QueuedConnection); +} + +LootDialog::~LootDialog() = default; + +void LootDialog::setText(const QString& s) +{ + ui->progressText->setText(s); +} + +void LootDialog::setProgress(lootcli::Progress p) +{ + setText(progressToString(p)); + + if (p == lootcli::Progress::Done) { + ui->progressBar->setRange(0, 1); + ui->progressBar->setValue(1); + } +} + +QString LootDialog::progressToString(lootcli::Progress p) +{ + using P = lootcli::Progress; + + switch (p) + { + case P::CheckingMasterlistExistence: return tr("Checking masterlist existence"); + case P::UpdatingMasterlist: return tr("Updating masterlist"); + case P::LoadingLists: return tr("Loading lists"); + case P::ReadingPlugins: return tr("Reading plugins"); + case P::SortingPlugins: return tr("Sorting plugins"); + case P::WritingLoadorder: return tr("Writing loadorder.txt"); + case P::ParsingLootMessages: return tr("Parsing loot messages"); + case P::Done: return tr("Done"); + default: return QString("unknown progress %1").arg(static_cast(p)); + } +} + +void LootDialog::addOutput(const QString& s) +{ + if (m_core.settings().diagnostics().lootLogLevel() > lootcli::LogLevels::Debug) { + return; + } + + const auto lines = s.split(QRegExp("[\\r\\n]"), QString::SkipEmptyParts); + + for (auto&& line : lines) { + if (line.isEmpty()) { + continue; + } + + addLineOutput(line); + } +} + +bool LootDialog::result() const +{ + return m_loot.result(); +} + +void LootDialog::cancel() +{ + if (!m_finished && !m_cancelling) { + addLineOutput(tr("Stopping LOOT...")); + m_loot.cancel(); + ui->buttons->setEnabled(false); + m_cancelling = true; + } +} + +void LootDialog::openReport() +{ + const auto path = m_loot.outPath(); + shell::Open(path); +} + +void LootDialog::createUI() +{ + ui->setupUi(this); + ui->progressBar->setMaximum(0); + + ui->openJsonReport->setEnabled(false); + connect(ui->openJsonReport, &QPushButton::clicked, [&]{ openReport(); }); + + new ExpanderWidget(ui->details, ui->detailsPanel); + + connect(ui->buttons, &QDialogButtonBox::clicked, [&](auto* b){ onButton(b); }); + + resize(480, 275); +} + +void LootDialog::closeEvent(QCloseEvent* e) +{ + if (m_finished) { + QDialog::closeEvent(e); + } else { + cancel(); + e->ignore(); + } +} + +void LootDialog::onButton(QAbstractButton* b) +{ + if (ui->buttons->buttonRole(b) == QDialogButtonBox::RejectRole) { + if (m_finished) { + close(); + } else { + cancel(); + } + } +} + +void LootDialog::addLineOutput(const QString& line) +{ + ui->output->appendPlainText(line); +} + +void LootDialog::onFinished() +{ + m_finished = true; + + if (m_cancelling) { + close(); + } else { + handleReport(); + ui->openJsonReport->setEnabled(true); + ui->buttons->setStandardButtons(QDialogButtonBox::Close); + } +} + +void LootDialog::log(log::Levels lv, const QString& s) +{ + if (lv >= log::Levels::Warning) { + log::log(lv, "{}", s); + } + + if (m_core.settings().diagnostics().lootLogLevel() > lootcli::LogLevels::Debug) { + addLineOutput(QString("[%1] %2").arg(log::levelToString(lv)).arg(s)); + } +} + +void LootDialog::handleReport() +{ + const auto& report = m_loot.report(); + + if (!report.messages.empty()) { + addLineOutput(""); + } + + QString html; + + if (!report.messages.empty()) { + html += "
    "; + + for (auto&& m : report.messages) { + log(m.type, m.text); + + html += "
  • "; + + switch (m.type) + { + case log::Error: + { + html += "" + QObject::tr("Error") + ": "; + break; + } + + case log::Warning: + { + html += "" + QObject::tr("Warning") + ": "; + break; + } + + default: + { + break; + } + } + + html += m.text + "
  • "; + } + + html + "
"; + } else { + html += QObject::tr("No messages."); + } + + ui->report->setHtml(html); + + for (auto&& p : report.plugins) { + m_core.pluginList()->addLootReport(p.name, p); + } +} diff --git a/src/lootdialog.h b/src/lootdialog.h new file mode 100644 index 00000000..e4647b5c --- /dev/null +++ b/src/lootdialog.h @@ -0,0 +1,47 @@ +#ifndef MODORGANIZER_LOOTDIALOG_H +#define MODORGANIZER_LOOTDIALOG_H + +#include +#include + +namespace Ui { class LootDialog; } + +class OrganizerCore; +class Loot; + +class LootDialog : public QDialog +{ +public: + LootDialog(QWidget* parent, OrganizerCore& core, Loot& loot); + ~LootDialog(); + + void setText(const QString& s); + void setProgress(lootcli::Progress p); + + QString progressToString(lootcli::Progress p); + + void addOutput(const QString& s); + + bool result() const; + + void cancel(); + + void openReport(); + +private: + std::unique_ptr ui; + OrganizerCore& m_core; + Loot& m_loot; + bool m_finished; + bool m_cancelling; + + void createUI(); + void closeEvent(QCloseEvent* e) override; + void onButton(QAbstractButton* b); + void addLineOutput(const QString& line); + void onFinished(); + void log(MOBase::log::Levels lv, const QString& s); + void handleReport(); +}; + +#endif // MODORGANIZER_LOOTDIALOG_H diff --git a/src/lootdialog.ui b/src/lootdialog.ui new file mode 100644 index 00000000..7e10b1db --- /dev/null +++ b/src/lootdialog.ui @@ -0,0 +1,214 @@ + + + LootDialog + + + + 0 + 0 + 457 + 343 + + + + LOOT + + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Progress + + + + + + + 24 + + + false + + + + + + + true + + + Qt::TextBrowserInteraction + + + LOOT Report + + + + + + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Details + + + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Open JSON report + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + + + + + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Close + + + + + + + + + buttons + accepted() + LootDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttons + rejected() + LootDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + + -- cgit v1.3.1