-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathtab.cpp
190 lines (156 loc) · 5.48 KB
/
tab.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
#include "tab.h"
#include "Index/Index.h"
#include "Index/Settings.h"
#include "Index/About.h"
#include "Task/Task.h"
#include "Task/TaskManage.h"
#include "Task/TaskData.h"
#include "Utils/models.h"
#include "Utils/database.h"
#include <QLabel>
#include <QTabBar>
#include <QMenu>
#include <QRandomGenerator>
//#include <QsLog.h>
Tab::Tab(QWidget *parent) : QTabWidget(parent)
{
QString style = "QTabWidget::pane{} \
QTabBar::tab {color:rgb(0,0,0);font-family:Microsoft YaHei;font-size:13px;min-width:110px;height:30px;padding:0px 5px;border-radius:4px;margin-left:6px;margin-top:4px;margin-bottom:2px;} \
QTabBar::tab:!selected {background:rgb(255,255,255);border:1px solid rgb(233,233,233);} \
QTabBar::tab:hover{background:rgba(255,255,255,0.8);border-radius: 0px;} \
QTabBar::tab:selected {background:rgb(219,228,241);border-radius: 0px;}";
setStyleSheet(style);
QTabBar *tabBar = this->tabBar();
tabBar->setFocusPolicy(Qt::NoFocus);//去除虚线框
tabBar->setTabsClosable(true);
tabBar->setSelectionBehaviorOnRemove(QTabBar::SelectPreviousTab);
tabBar->setMovable(true);
tabBar->setContextMenuPolicy(Qt::CustomContextMenu);
// connect(tabBar, &QTabBar::customContextMenuRequested, this, &Tab::onHandleContextMenuRequested);
connect(tabBar, &QTabBar::tabCloseRequested, this, &Tab::onCloseTab);
setDocumentMode(true);
// setElideMode(Qt::ElideRight);
}
void Tab::createIndex(){
bool create = true;
for (int i = 0; i < this->count(); ++i) {
if("Index"==QString(this->widget(i)->metaObject()->className())){
create = false;
this->setCurrentIndex(i);
break;
}
}
if(create){
Index *page = new Index(this);
addTab(page,formatTabName("首页"));
setCurrentWidget(page);
connect(page,&Index::notifyCreateTask,this,[this](const QString address){
createTask(address);
});
}
}
void Tab::createSettings(){
bool create = true;
for (int i = 0; i < this->count(); ++i) {
if("Settings"==QString(this->widget(i)->metaObject()->className())){
create = false;
this->setCurrentIndex(i);
break;
}
}
if(create){
Settings *page = new Settings(this);
addTab(page,formatTabName("设置"));
setCurrentWidget(page);
}
}
void Tab::createAbout(){
bool create = true;
for (int i = 0; i < this->count(); ++i) {
if("About"==QString(this->widget(i)->metaObject()->className())){
create = false;
this->setCurrentIndex(i);
break;
}
}
if(create){
About *page = new About(this);
addTab(page,formatTabName("关于"));
setCurrentWidget(page);
}
}
void Tab::createTask(const QString address){
// 新增任务
int r = QRandomGenerator::global()->bounded(100000,999999);
// qsrand(time(NULL));
// int r = qrand() % 1000000 + 1000000;
QString code = "code"+QString::number(r);
MTask *task = new MTask();
task->name = "新建任务";
task->code = code;
task->defaultUserAgent = Database::getInstance()->getRandomUserAgent();
if(!address.isNull()){
task->addressList = address.split("\n");
}
setTask(task);
}
void Tab::createTaskManage(){
bool create = true;
for (int i = 0; i < this->count(); ++i) {
if("TaskManage"==QString(this->widget(i)->metaObject()->className())){
create = false;
this->setCurrentIndex(i);
break;
}
}
if(create){
TaskManage *page = new TaskManage(this);
addTab(page,formatTabName("任务管理"));
setCurrentWidget(page);
connect(page,&TaskManage::notifyPageTaskData,this,[this](const QString &taskName,const QString &taskCode){
TaskData *page = new TaskData(this,taskName,taskCode);
addTab(page,formatTabName(taskName));
setCurrentWidget(page);
});
connect(page,&TaskManage::notifyEditCustomTask,this,[this](MTask *task){
// 编辑任务
setTask(task);
});
}
}
QString Tab::formatTabName(const QString &name){
if(name.length()>15){
return " "+name.mid(0,15) + "... ";
}else{
return " "+name+" ";
}
}
void Tab::setTask(MTask *task){
Task *page = new Task(this,task);
addTab(page,formatTabName(task->name));
setCurrentWidget(page);
connect(page,&Task::notifyChangeTabName,this,[this,page](const QString &name){
this->setTabText(this->indexOf(page),formatTabName(name));
});
connect(page,&Task::notifyChangeTabIcon,this,[this,page](const QIcon &icon){
this->setTabIcon(this->indexOf(page),icon);
});
}
void Tab::onCloseTab(int index){
QWidget *page = widget(index);
if("Index"==QString(page->metaObject()->className())){
}else{
removeTab(index);
page->deleteLater();
// delete currentWidget;
// if (WebView *view = webView(index)) {
// bool hasFocus = view->hasFocus();
// removeTab(index);
// if (hasFocus && count() > 0)
// currentWebView()->setFocus();
// if (count() == 0)
// createTab();
// view->deleteLater();
// }
}
}