-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathQPinnableTabWidget.cpp
263 lines (204 loc) · 6.33 KB
/
QPinnableTabWidget.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include "QPinnableTabWidget.h"
#include "FakeCloseButton.h"
#include "RealCloseButton.h"
#include <QMenu>
#include <QMouseEvent>
#include <QStyle>
#include <QTabBar>
namespace
{
class TabBarPin : public QTabBar
{
public:
explicit TabBarPin(QWidget *parent = nullptr)
: QTabBar(parent)
{
}
protected:
void mousePressEvent(QMouseEvent *event) override
{
mIndexToMove = indexAtPos(event->pos());
mDistToStart = event->pos().x() - tabRect(mIndexToMove).x();
QTabBar::mousePressEvent(event);
}
void mouseMoveEvent(QMouseEvent *event) override
{
const auto pinnableWidget = dynamic_cast<QPinnableTabWidget *>(parentWidget());
if (pinnableWidget)
{
const auto currentPinned = pinnableWidget->isPinned(mIndexToMove);
const auto newPosIsPinned = pinnableWidget->isPinned(indexAtPos(event->pos()));
const auto lastPinnedTab = pinnableWidget->getLastPinnedTabIndex();
if (!currentPinned && !newPosIsPinned && (event->pos().x() - mDistToStart) > tabRect(lastPinnedTab).right())
QTabBar::mouseMoveEvent(event);
}
}
void mouseReleaseEvent(QMouseEvent *event) override
{
mIndexToMove = -1;
mDistToStart = 0;
QTabBar::mouseReleaseEvent(event);
}
private:
int mIndexToMove = -1;
int mDistToStart = 0;
int indexAtPos(const QPoint &p)
{
if (tabRect(currentIndex()).contains(p))
return currentIndex();
for (int i = 0; i < count(); ++i)
if (isTabEnabled(i) && tabRect(i).contains(p))
return i;
return -1;
}
};
}
QPinnableTabWidget::QPinnableTabWidget(QWidget *parent)
: QTabWidget(parent)
{
setTabBar(new TabBarPin());
setTabsClosable(true);
setMovable(true);
connect(this, &QTabWidget::tabCloseRequested, this, &QPinnableTabWidget::removeTab, Qt::QueuedConnection);
connect(this, &QTabWidget::tabBarClicked, this, &QPinnableTabWidget::clickRequested);
}
int QPinnableTabWidget::addPinnedTab(QWidget *page, const QString &label)
{
const auto tabIndex = addTab(page, label);
tabBar()->setTabButton(
tabIndex,
static_cast<QTabBar::ButtonPosition>(style()->styleHint(QStyle::SH_TabBar_CloseButtonPosition, 0, this)),
new FakeCloseButton());
tabBar()->moveTab(tabIndex, mLastPinTab);
mTabState.insert(mLastPinTab++, true);
return tabIndex;
}
int QPinnableTabWidget::addPinnedTab(QWidget *page, const QIcon &icon, const QString &label)
{
const auto tabIndex = addTab(page, icon, label);
tabBar()->setTabButton(
tabIndex,
static_cast<QTabBar::ButtonPosition>(style()->styleHint(QStyle::SH_TabBar_CloseButtonPosition, 0, this)),
new FakeCloseButton());
tabBar()->moveTab(tabIndex, mLastPinTab);
mTabState.insert(mLastPinTab++, true);
return tabIndex;
}
int QPinnableTabWidget::addTab(QWidget *widget, const QString &s)
{
return QTabWidget::addTab(widget, s);
}
int QPinnableTabWidget::addTab(QWidget *widget, const QIcon &icon, const QString &label)
{
return QTabWidget::addTab(widget, icon, label);
}
int QPinnableTabWidget::insertTab(int index, QWidget *widget, const QString &s)
{
if (index <= mLastPinTab)
index = mLastPinTab + 1;
return QTabWidget::insertTab(index, widget, s);
}
int QPinnableTabWidget::insertTab(int index, QWidget *widget, const QIcon &icon, const QString &label)
{
if (index <= mLastPinTab)
index = mLastPinTab + 1;
return QTabWidget::insertTab(index, widget, icon, label);
}
void QPinnableTabWidget::removeTab(int index)
{
QTabWidget::removeTab(index);
if (mTabState.value(index))
--mLastPinTab;
mTabState.remove(index);
}
void QPinnableTabWidget::clear()
{
QTabWidget::clear();
mLastPinnedTab = -1;
mTabState.clear();
mPrepareMenu = false;
mClickedTab = -1;
mLastPinTab = 0;
}
bool QPinnableTabWidget::isPinned(int index)
{
return mTabState.contains(index);
}
int QPinnableTabWidget::getLastPinnedTabIndex() const
{
return mLastPinTab - 1;
}
void QPinnableTabWidget::mouseReleaseEvent(QMouseEvent *event)
{
const auto inPos = QRect(tabBar()->pos(), tabBar()->size()).contains(event->pos());
if (event->button() == Qt::RightButton && mPrepareMenu && inPos)
{
showContextMenu();
}
else
{
mClickedTab = -1;
mPrepareMenu = false;
}
}
void QPinnableTabWidget::clickRequested(int index)
{
mPrepareMenu = true;
mClickedTab = index;
}
void QPinnableTabWidget::showContextMenu()
{
if (!mPrepareMenu)
return;
const auto actions = new QMenu(this);
if (mTabState.value(mClickedTab))
connect(actions->addAction("Unpin"), &QAction::triggered, this, &QPinnableTabWidget::unpinTab);
else
connect(actions->addAction("Pin"), &QAction::triggered, this, &QPinnableTabWidget::pinTab);
connect(actions->addAction("Close"), &QAction::triggered, this, [this]() { emit tabCloseRequested(mClickedTab); });
connect(actions->addAction("Close all"), &QAction::triggered, this, [this]() {
std::vector<int> tabsToClose;
for (auto i = count() - 1; i >= 0; --i)
{
if (!isPinned(i))
emit tabCloseRequested(i);
}
});
actions->exec(QCursor::pos());
}
void QPinnableTabWidget::pinTab()
{
tabBar()->setTabButton(
mClickedTab,
static_cast<QTabBar::ButtonPosition>(style()->styleHint(QStyle::SH_TabBar_CloseButtonPosition, 0, this)),
new FakeCloseButton());
if (mClickedTab != mLastPinTab)
tabBar()->moveTab(mClickedTab, mLastPinTab);
mTabState.insert(mLastPinTab, true);
mLastPinTab = mTabState.count();
mClickedTab = -1;
}
void QPinnableTabWidget::unpinTab()
{
const auto closeBtn = new RealCloseButton();
tabBar()->setTabButton(
mClickedTab,
static_cast<QTabBar::ButtonPosition>(style()->styleHint(QStyle::SH_TabBar_CloseButtonPosition, 0, this)),
closeBtn);
mTabState.remove(mClickedTab);
mLastPinTab = mTabState.count();
auto deletions = false;
for (auto pair : mTabState.toStdMap())
{
if (pair.first > mClickedTab)
{
mTabState[pair.first - 1] = pair.second;
deletions = true;
}
}
if (deletions)
mTabState.remove(mTabState.lastKey());
tabBar()->moveTab(mClickedTab, mLastPinTab);
connect(closeBtn, &RealCloseButton::clicked, this, [this]() { emit tabBar()->tabCloseRequested(mLastPinTab); });
mClickedTab = -1;
}