-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontentviewwidget.cpp
More file actions
113 lines (94 loc) · 3.54 KB
/
Copy pathcontentviewwidget.cpp
File metadata and controls
113 lines (94 loc) · 3.54 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "contentviewwidget.h"
#include "commandbarwidget.h"
#include "dndwebengineview.h"
#include <QDragEnterEvent>
#include <QFileInfo>
#include <QHBoxLayout>
#include <QPushButton>
#include <QStringLiteral>
#include <QWebChannel>
#include <QWebEngineProfile>
#include <QWebEngineScript>
#include <QWebEngineScriptCollection>
#include <QWebEngineSettings>
ContentViewWidget::ContentViewWidget(QWidget *parent, std::string id)
: QWidget{parent}
{
_id = id;
lastPos = new QPointF();
isProgScroll = false;
// container
QWidget *containerWidget = new QWidget();
// command bar widget
CommandBarWidget *cmdBar = new CommandBarWidget();
QObject::connect(cmdBar,
&CommandBarWidget::newUrlSelected,
this,
&ContentViewWidget::handleFileOrUrlChange);
QObject::connect(cmdBar,
&CommandBarWidget::newFileSelected,
this,
&ContentViewWidget::handleFileOrUrlChange);
// Web view
webView = new DNDWebEngineView();
// Set attributes to handle PDFs
webView->settings()->setAttribute(QWebEngineSettings::PluginsEnabled, true);
webView->settings()->setAttribute(QWebEngineSettings::FullScreenSupportEnabled, true);
webView->settings()->setAttribute(QWebEngineSettings::PdfViewerEnabled, true);
QObject::connect(webView->page(),
&QWebEnginePage::loadFinished,
this,
&ContentViewWidget::updateMaxYPos);
QObject::connect(webView->page(),
&QWebEnginePage::scrollPositionChanged,
this,
&ContentViewWidget::handleScrollPositionChanged);
QObject::connect(webView,
&DNDWebEngineView::updateCommandStatusText,
cmdBar,
&CommandBarWidget::setSelectedLabelText);
// Container layout
QVBoxLayout *containerLayout = new QVBoxLayout(containerWidget);
containerLayout->setContentsMargins(0, 0, 0, 0);
containerLayout->addWidget(cmdBar);
containerLayout->addWidget(webView);
setLayout(containerLayout);
}
void ContentViewWidget::updateMaxYPos()
{
webView->page()->runJavaScript(
"document.documentElement.scrollHeight - document.documentElement.clientHeight;",
[this](const QVariant &v) { maxYPos = v.toInt(); });
}
void ContentViewWidget::handleScrollPositionChanged(const QPointF &position)
{
// TODO: This results in extra overhead every time position is updated, need to figure out alternative in JS land (webchannel possibly)
updateMaxYPos();
// Calc delta
int deltaX = position.x() - lastPos->x();
int deltaY = position.y() - lastPos->y();
// Update lastPos
lastPos->setX(position.x());
lastPos->setY(position.y());
// Only emit scroll changes if it was scrolled by user
if (!isProgScroll)
emit changedScrollPosition(_id, deltaX, deltaY);
else
isProgScroll = false;
}
void ContentViewWidget::setProgScroll(bool isProgScroll)
{
this->isProgScroll = isProgScroll;
}
void ContentViewWidget::scrollBy(int deltaX, int deltaY)
{
// Update position, even if out of bounds (keeps track of other view)
lastPos->setY(lastPos->y() + deltaY);
// Only scroll if in bounds
if (lastPos->y() > 0 && lastPos->y() <= maxYPos)
webView->page()->runJavaScript(QString("window.scrollTo({top:%1});").arg(lastPos->y()));
}
void ContentViewWidget::handleFileOrUrlChange(QString newFileOrUrl)
{
webView->setUrl(newFileOrUrl);
}