-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathtabbar.cpp
183 lines (150 loc) · 6.04 KB
/
tabbar.cpp
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
#include "tabbar.h"
#include "constants.h"
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/command.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/fileiconprovider.h>
#include <coreplugin/idocument.h>
#include <projectexplorer/session.h>
#include <QMenu>
#include <QMouseEvent>
#include <QShortcut>
#include <QTabBar>
using namespace Core::Internal;
using namespace TabbedEditor::Internal;
/// TODO: Use Core::DocumentModel for everything
TabBar::TabBar(QWidget *parent) :
QTabBar(parent)
{
setExpanding(false);
setMovable(true);
setTabsClosable(true);
setUsesScrollButtons(true);
QSizePolicy sp(QSizePolicy::Preferred, QSizePolicy::Fixed);
sp.setHorizontalStretch(1);
sp.setVerticalStretch(0);
sp.setHeightForWidth(sizePolicy().hasHeightForWidth());
setSizePolicy(sp);
connect(this, &QTabBar::tabMoved, [this](int from, int to) {
m_editors.move(from, to);
});
Core::EditorManager *em = Core::EditorManager::instance();
connect(em, &Core::EditorManager::editorOpened, this, &TabBar::addEditorTab);
connect(em, &Core::EditorManager::editorsClosed, this, &TabBar::removeEditorTabs);
connect(em, SIGNAL(currentEditorChanged(Core::IEditor*)), SLOT(selectEditorTab(Core::IEditor*)));
connect(this, &QTabBar::currentChanged, this, &TabBar::activateEditor);
connect(this, &QTabBar::tabCloseRequested, this, &TabBar::closeTab);
ProjectExplorer::SessionManager *sm = ProjectExplorer::SessionManager::instance();
connect(sm, &ProjectExplorer::SessionManager::sessionLoaded, [this, em]() {
foreach (Core::DocumentModel::Entry *entry, Core::DocumentModel::entries())
em->activateEditorForEntry(entry, Core::EditorManager::DoNotChangeCurrentEditor);
});
const QString shortCutSequence = QStringLiteral("Ctrl+Alt+%1");
for (int i = 1; i <= 10; ++i) {
QShortcut *shortcut = new QShortcut(shortCutSequence.arg(i % 10), this);
connect(shortcut, &QShortcut::activated, [this, shortcut]() {
setCurrentIndex(m_shortcuts.indexOf(shortcut));
});
m_shortcuts.append(shortcut);
}
QAction *prevTabAction = new QAction(tr("Switch to previous tab"), this);
Core::Command *prevTabCommand
= Core::ActionManager::registerAction(prevTabAction,
TabbedEditor::Constants::PREV_TAB_ID,
Core::Context(Core::Constants::C_GLOBAL));
prevTabCommand->setDefaultKeySequence(QKeySequence(tr("Ctrl+Shift+J")));
connect(prevTabAction, SIGNAL(triggered()), this, SLOT(prevTabAction()));
QAction *nextTabAction = new QAction(tr("Switch to next tab"), this);
Core::Command *nextTabCommand
= Core::ActionManager::registerAction(nextTabAction,
TabbedEditor::Constants::NEXT_TAB_ID,
Core::Context(Core::Constants::C_GLOBAL));
nextTabCommand->setDefaultKeySequence(QKeySequence(tr("Ctrl+Shift+K")));
connect(nextTabAction, SIGNAL(triggered()), this, SLOT(nextTabAction()));
}
void TabBar::activateEditor(int index)
{
if (index < 0 || index >= m_editors.size())
return;
Core::EditorManager::instance()->activateEditor(m_editors[index]);
}
void TabBar::addEditorTab(Core::IEditor *editor)
{
Core::IDocument *document = editor->document();
const int index = addTab(document->displayName());
setTabIcon(index, Core::FileIconProvider::icon(document->filePath().toFileInfo()));
setTabToolTip(index, document->filePath().toString());
m_editors.append(editor);
connect(document, &Core::IDocument::changed, [this, editor, document]() {
const int index = m_editors.indexOf(editor);
if (index == -1)
return;
QString tabText = document->displayName();
if (document->isModified())
tabText += QLatin1Char('*');
setTabText(index, tabText);
});
}
void TabBar::removeEditorTabs(QList<Core::IEditor *> editors)
{
blockSignals(true); // Avoid calling activateEditor()
foreach (Core::IEditor *editor, editors) {
const int index = m_editors.indexOf(editor);
if (index == -1)
continue;
m_editors.removeAt(index);
removeTab(index);
}
blockSignals(false);
}
void TabBar::selectEditorTab(Core::IEditor *editor)
{
const int index = m_editors.indexOf(editor);
if (index == -1)
return;
setCurrentIndex(index);
}
void TabBar::closeTab(int index)
{
if (index < 0 || index >= m_editors.size())
return;
Core::EditorManager::instance()->closeEditor(m_editors.takeAt(index));
removeTab(index);
}
void TabBar::prevTabAction()
{
int index = currentIndex();
if (index >= 1)
setCurrentIndex(index - 1);
else
setCurrentIndex(count() - 1);
}
void TabBar::nextTabAction()
{
int index = currentIndex();
if (index < count() - 1)
setCurrentIndex(index + 1);
else
setCurrentIndex(0);
}
void TabBar::contextMenuEvent(QContextMenuEvent *event)
{
const int index = tabAt(event->pos());
if (index == -1)
return;
QScopedPointer<QMenu> menu(new QMenu());
Core::IEditor *editor = m_editors[index];
Core::DocumentModel::Entry *entry = Core::DocumentModel::entryForDocument(editor->document());
Core::EditorManager::addSaveAndCloseEditorActions(menu.data(), entry, editor);
menu->addSeparator();
Core::EditorManager::addNativeDirAndOpenWithActions(menu.data(), entry);
menu->exec(mapToGlobal(event->pos()));
}
void TabBar::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::MiddleButton)
closeTab(tabAt(event->pos()));
QTabBar::mouseReleaseEvent(event);
}