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
|
/*
* 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/smolbote.hg
*
* SPDX-License-Identifier: GPL-3.0
*/
#include "mainwindowtabbar.h"
#include <QContextMenuEvent>
#include <QShortcut>
#include <settings/configuration.h>
#include "src/mainwindow/mainwindow.h"
MainWindowTabBar::MainWindowTabBar(const std::shared_ptr<Configuration> &config, MainWindow *parent)
: QTabBar(parent)
{
Q_CHECK_PTR(parent);
m_parent = parent;
setElideMode(Qt::ElideRight);
setTabsClosable(true);
setMovable(true);
setContextMenuPolicy(Qt::DefaultContextMenu);
connect(this, &MainWindowTabBar::tabCloseRequested, this, &MainWindowTabBar::removeTab);
connect(this, &MainWindowTabBar::currentChanged, this, &MainWindowTabBar::handleCurrentChanged);
connect(this, &MainWindowTabBar::tabMoved, this, &MainWindowTabBar::updateVectorArrangement);
QShortcut *tabCloseShortcut = new QShortcut(parent);
tabCloseShortcut->setKey(QKeySequence(QString::fromStdString(config->value<std::string>("browser.shortcuts.tabClose").value())));
connect(tabCloseShortcut, &QShortcut::activated, [this]() {
this->removeTab(currentIndex());
});
QShortcut *tabLeftShortcut = new QShortcut(parent);
tabLeftShortcut->setKey(QKeySequence(QString::fromStdString(config->value<std::string>("browser.shortcuts.tabLeft").value())));
connect(tabLeftShortcut, &QShortcut::activated, [this]() {
this->setCurrentIndex(currentIndex() - 1);
});
QShortcut *tabRightShortcut = new QShortcut(parent);
tabRightShortcut->setKey(QKeySequence(QString::fromStdString(config->value<std::string>("browser.shortcuts.tabRight").value())));
connect(tabRightShortcut, &QShortcut::activated, [this]() {
this->setCurrentIndex(currentIndex() + 1);
});
}
MainWindowTabBar::~MainWindowTabBar()
{
// cleanup
qDeleteAll(m_views);
m_views.clear();
}
int MainWindowTabBar::addTab(WebView *view)
{
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 MainWindowTabBar::setProfile(WebEngineProfile *profile)
{
Q_CHECK_PTR(profile);
for(auto view : qAsConst(m_views)) {
WebPage *page = new WebPage(profile);
page->load(view->url());
view->setPage(page);
}
}
WebView *MainWindowTabBar::currentView()
{
return m_views.at(currentIndex());
}
void MainWindowTabBar::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 MainWindowTabBar::tabSizeHint(int index) const
{
Q_UNUSED(index)
return QSize(200, this->height());
}
void MainWindowTabBar::handleCurrentChanged(int index)
{
if(index < 0) {
addTab(createWebView(m_parent->profile()->homepage(), m_parent->profile(), m_parent));
return;
}
emit currentTabChanged(m_views.at(index));
}
void MainWindowTabBar::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 MainWindowTabBar::updateTabText(WebView *view, const QString &text)
{
int index = m_views.indexOf(view);
setTabText(index, text);
}
void MainWindowTabBar::updateVectorArrangement(int from, int to)
{
m_views.move(from, to);
}
WebView *createWebView(const QUrl &url, WebEngineProfile *profile, MainWindow *parent)
{
WebView *view = new WebView(parent);
WebPage *page = new WebPage(profile);
view->setPage(page);
page->load(url);
return view;
}
|