-
Notifications
You must be signed in to change notification settings - Fork 26
/
MyTabWidget.cpp
225 lines (189 loc) · 6.27 KB
/
MyTabWidget.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
#include "mytabwidget.h"
#include <QApplication>
#include <QCursor>
#include <QDebug>
#include <QDir>
#include <QDrag>
#include <QMimeData>
#include <QMouseEvent>
#include <QPainter>
#include <QPixmap>
#include <QSettings>
#include "MyTabBar.h"
#include "MyTabPage.h"
#include "MyTabPopup.h"
#include "filesystemwatcher.h"
#include "mainwindow.h"
extern MainWindow* mw_one;
extern QString dragFileName;
extern int rowDrag;
extern int colDrag;
extern int vs;
extern int hs;
extern QVector<QString> openFileList;
MyTabWidget::MyTabWidget(QWidget* parent) : QTabWidget(parent) {
setAttribute(Qt::WA_StyledBackground);
initTabBar();
}
int MyTabWidget::appendNormalPage(QWidget* page) {
if (!page) return -1;
//设置为调用close时释放
page->setAttribute(Qt::WA_DeleteOnClose);
//最后是添加到stackedWidget去了
const int index = addTab(page, page->windowTitle());
//切换为当前新增页
setCurrentIndex(index);
return index;
}
void MyTabWidget::removeNormalPage(QWidget* page) {
if (!page) return;
removeNormalIndex(indexOf(page));
}
void MyTabWidget::takeNormalPage(QWidget* page) {
if (!page) return;
removeTab(indexOf(page));
}
void MyTabWidget::removeNormalIndex(int index) {
if (indexValid(index)) {
QWidget* page = this->widget(index);
//判断是否为固定不删除的
if (!page || fixedPage.contains(page)) return;
// removeTab只是从tabbar移除了,并没有释放
removeTab(index);
//可以自己调用delete,或者设置tab页为WA_DeleteOnClose,关闭时释放
page->close();
}
}
void MyTabWidget::removeCurrentPage() { removeNormalIndex(currentIndex()); }
void MyTabWidget::clearNormalPage() {
const int tab_count = this->count();
//从后往前删,这样index位置就是固定的
for (int index = tab_count - 1; index >= 0; index--) {
removeNormalIndex(index);
}
}
void MyTabWidget::clearAllPage() {
//全部移除
while (this->count() > 0) {
QWidget* page = this->widget(0);
removeTab(0);
if (page) page->close();
}
fixedPage.clear();
}
void MyTabWidget::appendFixedPage(QWidget* page) {
if (!page) return;
appendNormalPage(page);
setFixedPage(page);
}
void MyTabWidget::setFixedPage(QWidget* page) {
if (!page) return;
setFixedIndex(indexOf(page));
}
void MyTabWidget::setFixedIndex(int index) {
if (indexValid(index)) {
QWidget* page = this->widget(index);
if (page && !fixedPage.contains(page)) {
fixedPage.push_back(page);
//不显示关闭按钮,替换为nullptr
tabBar()->setTabButton(index, QTabBar::RightSide, nullptr);
}
}
}
bool MyTabWidget::indexValid(int index) const {
if (index < 0 || index >= this->count()) return false;
return true;
}
bool MyTabWidget::pageValid(QWidget* page) const {
if (!page) return false;
return indexValid(indexOf(page));
}
void MyTabWidget::showEvent(QShowEvent* event) {
QTabWidget::showEvent(event);
//初始化时把已有的设置为close释放
for (int index = 0; index < this->count(); index++) {
QWidget* page = this->widget(index);
if (page) page->setAttribute(Qt::WA_DeleteOnClose);
}
}
void MyTabWidget::initTabBar() {
MyTabBar* bar = new MyTabBar(this);
// setTabBar是protected成员函数,要使用就得继承
setTabBar(bar);
//点击页签上的关闭按钮时,触发信号(屏蔽,用tabBar自身的移除功能)
// connect(bar, &QTabBar::tabCloseRequested, this,
// &MyTabWidget::removeNormalIndex);
//拖拽到外部-还未释放鼠标
connect(bar, &MyTabBar::beginDragOut, this, [this, bar](int index) {
if (!indexValid(index)) return;
QWidget* drag_tab = this->widget(index);
//固定tab 或者 只有一个tab 不让拖出
if (!drag_tab || fixedPage.contains(drag_tab) || bar->count() == 1) return;
QPixmap pixmap(drag_tab->size() + QSize(2, 31));
pixmap.fill(Qt::transparent);
QPainter painter(&pixmap);
if (painter.isActive()) {
QRect title_rect{0, 0, pixmap.width(), 30};
painter.fillRect(title_rect, Qt::white);
painter.drawText(title_rect, Qt::AlignLeft | Qt::AlignVCenter,
" " + drag_tab->windowTitle());
painter.drawRect(pixmap.rect().adjusted(0, 0, -1, -1));
}
painter.end();
drag_tab->render(&pixmap, QPoint(1, 30));
QMimeData* mime = new QMimeData;
QDrag* drag = new QDrag(bar);
drag->setMimeData(mime);
drag->setPixmap(pixmap);
drag->setHotSpot(QPoint(10, 0));
//鼠标弹起后drag就释放了,这时候去判断是否拖拽到了外部
connect(drag, &QDrag::destroyed, this, [=] {
QPoint bar_point = bar->mapFromGlobal(QCursor::pos());
//不在范围,拖出
if (!bar->contentsRect().contains(bar_point)) {
popPage(drag_tab);
}
});
drag->exec(Qt::MoveAction);
});
}
void MyTabWidget::popPage(QWidget* page) {
takeNormalPage(page);
//这里套一个自定义标题栏的窗口给page
MyTabPopup* pop = new MyTabPopup(this);
pop->setAttribute(Qt::WA_DeleteOnClose);
pop->setContentWidget(page);
pop->setWindowTitle(page->windowTitle());
pop->resize(page->size());
//拖出来的位置有点偏移
pop->move(QCursor::pos() - QPoint(10, 10));
//判断独立窗口是否拖回tab
connect(pop, &MyTabPopup::dragRelease, this, [=](const QPoint& pos) {
const QPoint bar_pos = tabBar()->mapFromGlobal(pos);
//如果又拖回了tabbar范围内,就把widget取出来放回tab
if (tabBar()->contentsRect().contains(bar_pos)) {
QWidget* content = pop->getContentWidget();
this->appendNormalPage(content);
pop->disconnect();
pop->close();
}
});
QSettings Reg(mw_one->strIniFile, QSettings::IniFormat);
Reg.setValue("restore", true);
Reg.setValue("count", 1);
Reg.setValue(QString::number(0) + "/" + "file", dragFileName);
Reg.setValue(QString::number(0) + "/" + "row", rowDrag);
Reg.setValue(QString::number(0) + "/" + "col", colDrag);
Reg.setValue(QString::number(0) + "/" + "vs", vs);
Reg.setValue(QString::number(0) + "/" + "hs", hs);
// file.close();
FileSystemWatcher::removeWatchPath(dragFileName);
for (int i = 0; i < openFileList.count(); i++) {
if (dragFileName == openFileList.at(i)) {
openFileList.remove(i);
break;
}
}
mw_one->on_NewWindow();
mw_one->on_tabWidget_textEdit_tabBarClicked(this->currentIndex());
}