-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtabmanager.cpp
More file actions
97 lines (77 loc) · 2.09 KB
/
tabmanager.cpp
File metadata and controls
97 lines (77 loc) · 2.09 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
#include "tabmanager.h"
#include "browsertab.h"
#include <QtCore/QLoggingCategory>
Q_LOGGING_CATEGORY(lcTabs, "browser.tabs")
TabManager::TabManager(QWidget *parent)
: QTabWidget(parent)
{
setTabsClosable(true);
setMovable(true);
setDocumentMode(true);
connect(this, &QTabWidget::currentChanged,
this, &TabManager::onCurrentChanged);
connect(this, &QTabWidget::tabCloseRequested,
this, &TabManager::onTabCloseRequested);
}
BrowserTab *TabManager::createTab(const QUrl &url)
{
auto *tab = new BrowserTab(this);
connectTabSignals(tab);
const int idx = addTab(tab, tr("New Tab"));
setCurrentIndex(idx);
if (url.isValid() && !url.isEmpty())
tab->loadUrl(url);
qCInfo(lcTabs) << "Created tab" << idx << url.toString();
emit tabCreated(tab);
return tab;
}
BrowserTab *TabManager::currentTab() const
{
return qobject_cast<BrowserTab *>(currentWidget());
}
BrowserTab *TabManager::tabAt(int index) const
{
return qobject_cast<BrowserTab *>(widget(index));
}
void TabManager::closeTab(int index)
{
if (index < 0 || index >= count())
return;
QWidget *w = widget(index);
removeTab(index);
w->deleteLater();
qCInfo(lcTabs) << "Closed tab" << index << "remaining:" << count();
emit tabClosed(index);
if (count() == 0)
emit lastTabClosed();
}
void TabManager::closeCurrentTab()
{
closeTab(currentIndex());
}
void TabManager::onCurrentChanged(int index)
{
auto *tab = tabAt(index);
emit currentTabChanged(tab);
}
void TabManager::onTabCloseRequested(int index)
{
closeTab(index);
}
void TabManager::onTabTitleChanged(const QString &title)
{
auto *tab = qobject_cast<BrowserTab *>(sender());
if (!tab)
return;
const int idx = indexOf(tab);
if (idx >= 0) {
const QString display = title.isEmpty() ? tr("New Tab") : title;
setTabText(idx, display);
setTabToolTip(idx, title);
}
}
void TabManager::connectTabSignals(BrowserTab *tab)
{
connect(tab, &BrowserTab::titleChanged,
this, &TabManager::onTabTitleChanged);
}