summaryrefslogtreecommitdiff
path: root/src/updatedialog.cpp
blob: 1be58576dbb9d6ec3809c50e7a4890c2a9f147cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "updatedialog.h"
#include "ui_updatedialog.h"

#include "lootdialog.h" // for MarkdownPage
#include <QWebChannel>

using namespace MOBase;

UpdateDialog::UpdateDialog(QWidget* parent, const QString& title) :
  QDialog(parent, Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint), ui(new Ui::UpdateDialog)
{
  // Basic UI stuff
  ui->setupUi(this);
  setWindowTitle(title);
  connect(ui->installButton, &QPushButton::pressed, this, [&]{ done(QDialog::Accepted); });
  connect(ui->cancelButton, &QPushButton::pressed, this, [&]{ done(QDialog::Rejected); });

  // Replace a label with an icon
  QIcon icon = style()->standardIcon(QStyle::SP_MessageBoxQuestion);
  QPixmap pixmap = icon.pixmap(QSize(32, 32));
  ui->iconLabel->setPixmap(pixmap);
  ui->iconLabel->setScaledContents(true);

  // Setting up the Markdown stuff
  auto* page = new MarkdownPage(this);
  ui->detailsWebView->setPage(page);

  auto* channel = new QWebChannel(this);
  channel->registerObject("content", &m_changeLogs);
  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->detailsWebView->setHtml(html);
    } else {
      log::error("failed to read '{}', {}", path, f.errorString());
    }
  } else {
    log::error("can't open '{}', {}", path, f.errorString());
  }

  // Setting up the expander
  m_expander.set(ui->detailsButton, ui->detailsWidget);
  connect(&m_expander, &ExpanderWidget::toggled, this, [&]{ adjustSize(); });

  // Adjust sizes after the expander hides stuff
  adjustSize();
}

UpdateDialog::~UpdateDialog() = default;

void UpdateDialog::setChangeLogs(const QString& text)
{
  m_changeLogs.setText(text);
}