From 70ee786102c8436c7f8f9e8a5d2ea71035d8d572 Mon Sep 17 00:00:00 2001 From: isanae <14251494+isanae@users.noreply.github.com> Date: Sun, 24 Nov 2019 22:19:23 -0500 Subject: changed loot report to webengine with markdown support --- src/loot.cpp | 4 +- src/lootdialog.cpp | 74 +++++++++-- src/lootdialog.h | 31 +++++ src/lootdialog.ui | 49 ++++++-- src/resources/markdown.html | 299 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 434 insertions(+), 23 deletions(-) create mode 100644 src/resources/markdown.html diff --git a/src/loot.cpp b/src/loot.cpp index f0618b0b..5d75eeaa 100644 --- a/src/loot.cpp +++ b/src/loot.cpp @@ -67,7 +67,9 @@ Loot::Loot() Loot::~Loot() { - m_thread->wait(); + if (m_thread) { + m_thread->wait(); + } if (!m_outPath.isEmpty() && QFile::exists(m_outPath)) { const auto r = shell::Delete(m_outPath); diff --git a/src/lootdialog.cpp b/src/lootdialog.cpp index db41959d..9c7b482e 100644 --- a/src/lootdialog.cpp +++ b/src/lootdialog.cpp @@ -4,9 +4,44 @@ #include "organizercore.h" #include #include +#include using namespace MOBase; + +MarkdownDocument::MarkdownDocument(QObject* parent) + : QObject(parent) +{ +} + +void MarkdownDocument::setText(const QString& text) +{ + if (m_text == text) + return; + + m_text = text; + emit textChanged(m_text); +} + + +MarkdownPage::MarkdownPage(QObject* parent) + : QWebEnginePage(parent) +{ +} + +bool MarkdownPage::acceptNavigationRequest(const QUrl &url, NavigationType, bool) +{ + static const QStringList allowed = {"qrc", "data"}; + + if (!allowed.contains(url.scheme())) { + QDesktopServices::openUrl(url); + return false; + } + + return true; +} + + 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) @@ -108,6 +143,27 @@ void LootDialog::createUI() ui->setupUi(this); ui->progressBar->setMaximum(0); + auto* page = new MarkdownPage(this); + ui->report->setPage(page); + + auto* channel = new QWebChannel(this); + channel->registerObject("content", &m_report); + page->setWebChannel(channel); + + const QString path = QApplication::applicationDirPath() + "/resources/markdown.html"; + QFile f(path); + + if (f.open(QFile::ReadOnly)) { + const QString html = f.readAll(); + if (!html.isEmpty()) { + ui->report->setHtml(html); + } else { + log::error("failed to read '{}', {}", path, f.errorString()); + } + } else { + log::error("can't open '{}', {}", path, f.errorString()); + } + ui->openJsonReport->setEnabled(false); connect(ui->openJsonReport, &QPushButton::clicked, [&]{ openReport(); }); @@ -176,27 +232,25 @@ void LootDialog::handleReport() addLineOutput(""); } - QString html; + QString md; if (!report.messages.empty()) { - html += "
    "; - for (auto&& m : report.messages) { log(m.type, m.text); - html += "
  • "; + md += " - "; switch (m.type) { case log::Error: { - html += "" + QObject::tr("Error") + ": "; + md += "**" + QObject::tr("Error") + "**: "; break; } case log::Warning: { - html += "" + QObject::tr("Warning") + ": "; + md += "**" + QObject::tr("Warning") + "**: "; break; } @@ -206,15 +260,13 @@ void LootDialog::handleReport() } } - html += m.text + "
  • "; + md += m.text + "\n"; } - - html + "
"; } else { - html += QObject::tr("No messages."); + md += QObject::tr("**No messages.**"); } - ui->report->setHtml(html); + m_report.setText(md); for (auto&& p : report.plugins) { m_core.pluginList()->addLootReport(p.name, p); diff --git a/src/lootdialog.h b/src/lootdialog.h index e4647b5c..df9e546d 100644 --- a/src/lootdialog.h +++ b/src/lootdialog.h @@ -9,6 +9,36 @@ namespace Ui { class LootDialog; } class OrganizerCore; class Loot; + +class MarkdownDocument : public QObject +{ + Q_OBJECT; + Q_PROPERTY(QString text MEMBER m_text NOTIFY textChanged FINAL); + +public: + explicit MarkdownDocument(QObject* parent=nullptr); + void setText(const QString& text); + +signals: + void textChanged(const QString &text); + +private: + QString m_text; +}; + + +class MarkdownPage : public QWebEnginePage +{ + Q_OBJECT; + +public: + explicit MarkdownPage(QObject* parent=nullptr); + +protected: + bool acceptNavigationRequest(const QUrl &url, NavigationType, bool) override; +}; + + class LootDialog : public QDialog { public: @@ -34,6 +64,7 @@ private: Loot& m_loot; bool m_finished; bool m_cancelling; + MarkdownDocument m_report; void createUI(); void closeEvent(QCloseEvent* e) override; diff --git a/src/lootdialog.ui b/src/lootdialog.ui index 7e10b1db..e366ce41 100644 --- a/src/lootdialog.ui +++ b/src/lootdialog.ui @@ -7,7 +7,7 @@ 0 0 457 - 343 + 600 @@ -16,7 +16,7 @@ - + 0 @@ -31,7 +31,7 @@ - + 0 @@ -62,16 +62,36 @@ - - - true + + + QFrame::StyledPanel - - Qt::TextBrowserInteraction - - - LOOT Report + + QFrame::Sunken + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + about:blank + + + + + @@ -176,6 +196,13 @@ + + + QWebEngineView + QWidget +
QtWebEngineWidgets/QWebEngineView
+
+
diff --git a/src/resources/markdown.html b/src/resources/markdown.html new file mode 100644 index 00000000..a09b2209 --- /dev/null +++ b/src/resources/markdown.html @@ -0,0 +1,299 @@ + + + + + + + + + +
+ + + \ No newline at end of file -- cgit v1.3.1