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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
/*******************************************************************************
**
** smolbote: yet another qute browser
** Copyright (C) 2017 Xian Nox
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
**
******************************************************************************/
#include "webviewtabbar.h"
#include "browser.h"
#include <QAction>
#include <QContextMenuEvent>
#include <QMenu>
#include <QShortcut>
WebViewTabBar::WebViewTabBar(WebEngineProfile *profile, QWidget *parent) :
QTabBar(parent)
{
m_profile = profile;
setElideMode(Qt::ElideRight);
setTabsClosable(true);
setMovable(true);
setContextMenuPolicy(Qt::DefaultContextMenu);
connect(this, SIGNAL(tabCloseRequested(int)), this, SLOT(removeTab(int)));
connect(this, SIGNAL(currentChanged(int)), this, SLOT(handleCurrentChanged(int)));
connect(this, SIGNAL(tabMoved(int,int)), this, SLOT(updateVectorArrangement(int,int)));
QShortcut *tabCloseShortcut = new QShortcut(this);
tabCloseShortcut->setKey(QKeySequence::fromString(browser->settings()->value("window.shortcuts.tabClose").toString()));
connect(tabCloseShortcut, &QShortcut::activated, [this]() {
this->removeTab(currentIndex());
});
QShortcut *tabLeftShortcut = new QShortcut(this);
tabLeftShortcut->setKey(QKeySequence::fromString(browser->settings()->value("window.shortcuts.tabLeft").toString()));
connect(tabLeftShortcut, &QShortcut::activated, [this]() {
this->setCurrentIndex(currentIndex()-1);
});
QShortcut *tabRightShortcut = new QShortcut(this);
tabRightShortcut->setKey(QKeySequence::fromString(browser->settings()->value("window.shortcuts.tabRight").toString()));
connect(tabRightShortcut, &QShortcut::activated, [this]() {
this->setCurrentIndex(currentIndex()+1);
});
m_signalMapper = new QSignalMapper(this);
connect(m_signalMapper, SIGNAL(mapped(int)), this, SLOT(webAction(int)));
}
WebViewTabBar::~WebViewTabBar()
{
// cleanup
qDeleteAll(m_views);
m_views.clear();
}
QSignalMapper *WebViewTabBar::signalMapper()
{
return m_signalMapper;
}
int WebViewTabBar::addTab(const QUrl &url)
{
WebView *view = new WebView(0);
QWebEnginePage *page = new QWebEnginePage(m_profile);
view->setPage(page);
page->load(url);
m_views.append(view);
connect(view, &QWebEngineView::titleChanged, [this, view](const QString &title) {
int index = m_views.indexOf(view);
setTabText(index, title);
});
connect(view, &QWebEngineView::iconChanged, [this, view](const QIcon &icon) {
int index = m_views.indexOf(view);
setTabIcon(index, icon);
});
return QTabBar::addTab("New Tab");
}
void WebViewTabBar::setProfile(WebEngineProfile *profile)
{
Q_ASSERT(profile != nullptr);
m_profile = profile;
for(auto view : qAsConst(m_views)) {
QWebEnginePage *page = new QWebEnginePage(profile);
page->load(view->url());
view->setPage(page);
}
}
WebEngineProfile *WebViewTabBar::profile()
{
return m_profile;
}
WebView *WebViewTabBar::currentView()
{
return m_views.at(currentIndex());
}
void WebViewTabBar::contextMenuEvent(QContextMenuEvent *event)
{
int tabIndex = tabAt(event->pos());
if(tabIndex < 0) {
return;
}
QMenu menu(this);
QAction* closeAction = menu.addAction(tr("Close tab"));
connect(closeAction, &QAction::triggered, this, [tabIndex, this]() {
removeTab(tabIndex);
});
menu.exec(event->globalPos());
}
QSize WebViewTabBar::tabSizeHint(int index) const
{
Q_UNUSED(index)
return QSize(200, this->height());
}
void WebViewTabBar::handleCurrentChanged(int index)
{
if(index < 0) {
addTab(profile()->newtab());
return;
}
emit currentTabChanged(m_views.at(index));
}
void WebViewTabBar::removeTab(int index)
{
// remove the tab data from the index
m_views.at(index)->deleteLater();
m_views.remove(index);
// remove the tab from the QTabBar
// this emits the currentTabChanged signal, so it should be done after the view is removed from the index
QTabBar::removeTab(index);
}
void WebViewTabBar::updateTabText(WebView *view, const QString &text)
{
int index = m_views.indexOf(view);
setTabText(index, text);
}
void WebViewTabBar::updateVectorArrangement(int from, int to)
{
m_views.move(from, to);
}
void WebViewTabBar::webAction(int action)
{
switch (action) {
case WebActions::Back:
currentView()->pageAction(QWebEnginePage::Back)->trigger();
break;
case WebActions::Forward:
currentView()->pageAction(QWebEnginePage::Forward)->trigger();
break;
case WebActions::Reload:
currentView()->pageAction(QWebEnginePage::Reload)->trigger();
break;
case WebActions::Homepage:
currentView()->load(m_profile->homepage());
break;
}
}
|