diff options
| author | isanae <14251494+isanae@users.noreply.github.com> | 2019-11-24 20:11:50 -0500 |
|---|---|---|
| committer | isanae <14251494+isanae@users.noreply.github.com> | 2019-11-24 20:11:50 -0500 |
| commit | cd876a0f9ffd03c711812c2ade92836e5d6c0203 (patch) | |
| tree | cf472e0d3aa07e874721eb069a3a5d99f7ccf3f2 /src/loot.cpp | |
| parent | f5476531ae39fdae8c3adc63d4c4a11f92600ff8 (diff) | |
split loot dialog, added ui file
Diffstat (limited to 'src/loot.cpp')
| -rw-r--r-- | src/loot.cpp | 271 |
1 files changed, 1 insertions, 270 deletions
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<int>(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 += "<ul>"; - - for (auto&& m : report.messages) { - log(m.type, m.text); - - html += "<li>"; - - switch (m.type) - { - case log::Error: - { - html += "<b>" + QObject::tr("Error") + "</b>: "; - break; - } - - case log::Warning: - { - html += "<b>" + QObject::tr("Warning") + "</b>: "; - break; - } - - default: - { - break; - } - } - - html += m.text + "</li>"; - } - - html + "</ul>"; - } 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 { |
