-
Notifications
You must be signed in to change notification settings - Fork 0
/
markdownbrowser.cpp
79 lines (68 loc) · 2.42 KB
/
markdownbrowser.cpp
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// markdownbrowser.cpp
#include "markdownbrowser.h"
#include <QFile>
#include <QWebEnginePage>
#include <QMenu>
MarkdownBrowser::MarkdownBrowser(const QString& path, QWidget* parent)
: QWidget(parent) {
try {
browser = new QWebEngineView(this);
browser->settings()->setAttribute(QWebEngineSettings::PluginsEnabled, true);
browser->settings()->setAttribute(QWebEngineSettings::PdfViewerEnabled, true);
layout = new QVBoxLayout(this);
layout->addWidget(browser);
markdownPath = path;
BuildHTML();
shortcut_refresh = new QShortcut(QKeySequence("F5"), this);
connect(shortcut_refresh, &QShortcut::activated, this, &MarkdownBrowser::BuildHTML);
browser->setContextMenuPolicy(Qt::CustomContextMenu);
connect(browser, &QWebEngineView::customContextMenuRequested,
this, &MarkdownBrowser::showContextMenu);
setLayout(layout);
} catch (const std::exception& e) {
qDebug() << "DocumentBrowser::__init__";
qDebug() << e.what();
}
}
void MarkdownBrowser::showContextMenu(const QPoint &point) {
qDebug() << "Custom Context Menu Requested";
QMenu* context = new QMenu;
context->addAction("Reload Markdown");
connect(context, &QMenu::triggered, this, &MarkdownBrowser::contextExecute);
context->exec(mapToGlobal(point));
}
void MarkdownBrowser::contextExecute(QAction* action) {
if (action->text() == "Reload Markdown") {
BuildHTML();
}
}
void MarkdownBrowser::BuildHTML() {
qDebug() << "Building Markdown HTML";
QFile header("/usr/share/adrenlinerush/markdown/markdown_header.html");
QFile body(markdownPath);
QFile footer("/usr/share/adrenlinerush/markdown/markdown_footer.html");
QString html;
if (header.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream stream(&header);
while (!stream.atEnd()) {
html.append(stream.readLine()+"\n");
}
}
header.close();
if (body.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream stream(&body);
while (!stream.atEnd()) {
qDebug() << "Reading a line from the markdown file.";
html.append(stream.readLine()+"\n");
}
}
body.close();
if (footer.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream stream(&footer);
while (!stream.atEnd()) {
html.append(stream.readLine()+"\n");
}
}
footer.close();
browser->setHtml(html);
}