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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
/*
* This file is part of smolbote. It's copyrighted by the contributors recorded
* in the version control history of the file, available from its original
* location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote
*
* SPDX-License-Identifier: GPL-3.0
*/
#include "navigationbar.h"
#include "addressbar/urllineedit.h"
#include "configuration/configuration.h"
#include "webengine/webview.h"
#include <QHBoxLayout>
#include <QMenu>
#include <QShortcut>
#include <QStyle>
#include <QToolBar>
#include <QToolButton>
#include <QWebEngineHistory>
NavigationBar::NavigationBar(const QHash<QString, QString> &conf, QWidget *parent)
: QToolBar(parent)
{
qStyle = parent->style();
// Back button
backAction = addAction(qStyle->standardIcon(QStyle::SP_ArrowBack), tr("Back"));
if(conf.count("navigation.shortcuts.back") > 0) {
backAction->setShortcut(QKeySequence(conf.value("navigation.shortcuts.back")));
backAction->setToolTip(tr("Back (%1)").arg(conf.value("navigation.shortcuts.back")));
}
connect(backAction, &QAction::triggered, this, [this]() {
m_view->history()->back();
});
auto *backMenu = new QMenu(this);
connect(backMenu, &QMenu::aboutToShow, this, [this, backMenu]() {
backMenu->clear();
const auto history = m_view->history()->backItems(10);
for(const QWebEngineHistoryItem &item : history) {
auto *action = backMenu->addAction(item.title());
connect(action, &QAction::triggered, this, [item, this]() {
m_view->history()->goToItem(item);
});
}
});
backAction->setMenu(backMenu);
auto *backMenuShortcut = new QShortcut(QKeySequence(conf.value("navigation.shortcuts.backMenu")), this);
connect(backMenuShortcut, &QShortcut::activated, backMenu, [this, backMenu]() {
if(backAction->isEnabled()) {
auto *widget = this->widgetForAction(backAction);
backMenu->exec(this->mapToGlobal(widget->pos() + QPoint(0, widget->height())));
}
});
// Forward button
forwardAction = addAction(qStyle->standardIcon(QStyle::SP_ArrowForward), tr("Forward"));
if(conf.count("navigation.shortcuts.forward") > 0) {
forwardAction->setShortcut(QKeySequence(conf.value("navigation.shortcuts.forward")));
forwardAction->setToolTip(tr("Forward (%1)").arg(conf.value("navigation.shortcuts.forward")));
}
connect(forwardAction, &QAction::triggered, this, [this]() {
m_view->history()->forward();
});
auto *forwardMenu = new QMenu(this);
connect(forwardMenu, &QMenu::aboutToShow, this, [this, forwardMenu]() {
forwardMenu->clear();
const auto history = m_view->history()->forwardItems(10);
for(const QWebEngineHistoryItem &item : history) {
auto *action = forwardMenu->addAction(item.title());
connect(action, &QAction::triggered, this, [item, this]() {
m_view->history()->goToItem(item);
});
}
});
forwardAction->setMenu(forwardMenu);
auto *forwardMenuShortcut = new QShortcut(QKeySequence(conf.value("navigation.shortcuts.forwardMenu")), this);
connect(forwardMenuShortcut, &QShortcut::activated, forwardMenu, [this, forwardMenu]() {
if(forwardAction->isEnabled()) {
auto *widget = this->widgetForAction(forwardAction);
forwardMenu->exec(this->mapToGlobal(widget->pos() + QPoint(0, widget->height())));
}
});
// Stop/Refresh button
stopReloadAction = addAction(qStyle->standardIcon(QStyle::SP_BrowserReload), tr("Refresh"));
stopReloadAction->setShortcut(QKeySequence(conf.value("navigation.shortcuts.refresh")));
connect(stopReloadAction, &QAction::triggered, this, [this]() {
if(m_view->isLoaded())
m_view->reload();
else
m_view->stop();
});
auto *reloadShortcut = new QShortcut(QKeySequence(conf.value("navigation.shortcuts.reload")), this);
connect(reloadShortcut, &QShortcut::activated, this, [this]() {
m_view->triggerPageAction(QWebEnginePage::ReloadAndBypassCache);
});
// Home button
homeAction = addAction(qStyle->standardIcon(QStyle::SP_DirHomeIcon), tr("Home"));
homeAction->setShortcut(QKeySequence(conf.value("navigation.shortcuts.home")));
connect(homeAction, &QAction::triggered, this, [this]() {
m_view->triggerViewAction(WebView::GoHome);
});
}
void NavigationBar::connectWebView(WebView *view)
{
m_view = view;
disconnect(loadStartedConnection);
disconnect(loadFinishedConnection);
if(view == nullptr) {
backAction->setEnabled(false);
forwardAction->setEnabled(false);
stopReloadAction->setEnabled(false);
homeAction->setEnabled(false);
return;
}
if(view->isLoaded()) {
update_loadFinished();
} else {
update_loadStarted();
}
loadStartedConnection = connect(view, &QWebEngineView::loadStarted, this, &NavigationBar::update_loadStarted);
loadFinishedConnection = connect(view, &QWebEngineView::loadFinished, this, &NavigationBar::update_loadFinished);
stopReloadAction->setEnabled(true);
homeAction->setEnabled(true);
}
void NavigationBar::update_loadStarted()
{
backAction->setEnabled(m_view->history()->canGoForward());
forwardAction->setEnabled(m_view->history()->canGoForward());
stopReloadAction->setIcon(qStyle->standardIcon(QStyle::SP_BrowserStop));
}
void NavigationBar::update_loadFinished()
{
backAction->setEnabled(m_view->history()->canGoBack());
forwardAction->setEnabled(m_view->history()->canGoForward());
stopReloadAction->setIcon(qStyle->standardIcon(QStyle::SP_BrowserReload));
}
|