-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlinknewfolder_window.cpp
More file actions
188 lines (172 loc) · 9.34 KB
/
linknewfolder_window.cpp
File metadata and controls
188 lines (172 loc) · 9.34 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
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
#include "linknewfolder_window.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QFileDialog>
#include <QRegularExpression>
#include <QMessageBox>
#include "ElaCheckBox.h"
#include "ElaLineEdit.h"
#include "ElaPushButton.h"
#include "ElaText.h"
#include "ElaComboBox.h"
#include "ElaMessageBar.h"
#include "synctask.h"
#include <QCompleter>
linkNewFolder_window::linkNewFolder_window(QWidget *parent)
: ElaWidget(parent,900,400)
{
this->setWindowTitle("珞珈云"); // 设置窗口标题
this->setWindowModality(Qt::ApplicationModal); // 设置窗口模态
this->hide(); // 初始隐藏当前窗口
this->setWindowButtonFlag(ElaAppBarType::StayTopButtonHint,false);
this->setWindowButtonFlag(ElaAppBarType::MinimizeButtonHint,false);
this->setWindowButtonFlag(ElaAppBarType::MaximizeButtonHint,false);
QWidget*centerarea=new QWidget(); // 创建一个新的QWidget对象作为主区域
QVBoxLayout* centerVLayout = new QVBoxLayout(centerarea); // 为中心部件设置垂直布局
centerVLayout->setContentsMargins(60, 0, 60, 30); // 设置布局边距
ElaText* Title = new ElaText("链接新文件夹", this);
Title->setTextSize(25); // 设置文字大小
QWidget* lineEditArea1 = new QWidget();
lineEditArea1->setWindowFlags(Qt::FramelessWindowHint); // 去除窗口边框
lineEditArea1->setAttribute(Qt::WA_TranslucentBackground); // 设置背景透明
QHBoxLayout* lineEditLayout1 = new QHBoxLayout(lineEditArea1);// 创建一个水平布局
lineEditLayout1->setContentsMargins(0, 0, 0, 0); // 设置布局的边距
folderName1 = new ElaLineEdit(this); // 创建一个 ElaLineEdit 对象
folderName1->setPlaceholderText("本地文件夹地址"); // 设置输入提示文本
folderName1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
folderName1->setFixedHeight(40); // 固定高度为40
_pushButton1 = new ElaPushButton("选择文件夹", this);
_pushButton1->setFixedSize(130, 40); // 设置按钮的固定大小
lineEditLayout1->addWidget(folderName1, 1); // 将输入框添加到布局中,并设置拉伸系数为1
lineEditLayout1->addWidget(_pushButton1); // 将按钮添加到布局中
connect(_pushButton1, &ElaPushButton::clicked, [=]() {
QString folderPath = QFileDialog::getExistingDirectory(this, "选择文件夹", QString(), QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
if (!folderPath.isEmpty()) {
folderName1->setText(folderPath);
}
});
QWidget* lineEditArea2 = new QWidget();
lineEditArea2->setWindowFlags(Qt::FramelessWindowHint); // 去除窗口边框
lineEditArea2->setAttribute(Qt::WA_TranslucentBackground); // 设置背景透明
QHBoxLayout* lineEditLayout2 = new QHBoxLayout(lineEditArea2);// 创建一个水平布局
lineEditLayout2->setContentsMargins(0, 0, 0, 0); // 设置布局的边距
folderName2 = new ElaComboBox(this);
folderName2->setEditable(true); // 设置为可编辑
folderName2->lineEdit()->setPlaceholderText("云端文件夹地址(只能包含大小写字母、数字、'-'和'/',且必须以'/'结尾)"); // 设置输入提示文本
folderName2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
folderName2->setFixedHeight(40); // 固定高度为40
folderName2->setCompleter(new QCompleter(folderName2->model(), folderName2));// 为下拉菜单设置自动完成器,使用下拉菜单自身的模型作为数据源
folderName2->completer()->setCompletionMode(QCompleter::PopupCompletion);// 设置自动完成器的完成模式为弹出补全,即输入时显示匹配的选项列表
folderName2->completer()->setCaseSensitivity(Qt::CaseInsensitive);// 设置自动完成器的大小写敏感性为不敏感,即输入时忽略大小写差异
folderName2->setCurrentIndex(-1); // 设置当前选中项为-1,即不选中任何项
_checkBox = new ElaCheckBox("使用原文件夹名", this);
_checkBox->setFixedSize(130, 20); // 设置按钮的固定大小
lineEditLayout2->addWidget(folderName2, 1); // 将输入框添加到布局中
lineEditLayout2->addWidget(_checkBox); // 将按钮添加到布局中
// 首先,定义一个用于显示错误信息的标签
QLabel* errorLabel = new QLabel(lineEditArea2);
errorLabel->setText("输入格式错误:只能包含大小写字母、数字、'-'和'/',且必须以'/'结尾");
errorLabel->setStyleSheet("QLabel { color : red; }"); // 设置错误信息为红色字体
//errorLabel->setAlignment(Qt::AlignCenter); // 设置文本居中显示
errorLabel->setFixedSize(500,20);
errorLabel->hide(); // 默认隐藏错误信息标签
// 然后,为folderName2添加失焦事件的处理
connect(folderName2, &ElaComboBox::editTextChanged, [=]() {
QString inputText = folderName2->currentText();
QRegularExpression re("^[a-zA-Z0-9/-]+/$");
if (!re.match(inputText).hasMatch()) {
// 如果输入不符合要求,显示错误信息标签
errorLabel->show();
} else {
// 如果输入符合要求,隐藏错误信息标签
errorLabel->hide();
}
});
// 为checkBox添加状态改变事件的处理
connect(_checkBox, &ElaCheckBox::stateChanged, [=](int state) {
if (state == Qt::Checked) {
QString folderName1Text = folderName1->text();
int lastSlashIndex = folderName1Text.lastIndexOf('/');
// 如果找到了'/',则复制其后的内容到folderName2
if (lastSlashIndex != -1) {
QString lastPart = folderName1Text.mid(lastSlashIndex + 1); // 包含最后一个'/'及其后的所有内容
folderName2->setCurrentText(lastPart + "/"); // 在folderName2中设置文本,并在末尾加上一个'/'
QString inputText = folderName2->currentText();
QRegularExpression re("^[a-zA-Z0-9/-]+/$");
if (!re.match(inputText).hasMatch())
errorLabel->show();
else
errorLabel->hide();
}
}
});
_comboBox = new ElaComboBox(this);
QStringList comboList{"仅上传", "仅下载", "同步上传与下载"};
_comboBox->addItems(comboList);
_comboBox->setFixedWidth(140);
// 创建一个映射,将文本选项映射到数字
QMap<QString, int> comboOptionToNumber;
comboOptionToNumber["同步上传与下载"] = 1;
comboOptionToNumber["仅上传"] = 2;
comboOptionToNumber["仅下载"] = 3;
QWidget* buttonArea = new QWidget();
buttonArea->setWindowFlags(Qt::FramelessWindowHint); // 去除窗口边框
buttonArea->setAttribute(Qt::WA_TranslucentBackground); // 设置背景透明
QHBoxLayout* buttonLayout = new QHBoxLayout(buttonArea);// 创建一个水平布局
buttonLayout->setContentsMargins(0, 0, 0, 0); // 设置布局的边距
_pushButton2 = new ElaPushButton("取消", this);
_pushButton2->setFixedSize(120, 40);
_pushButton3 = new ElaPushButton("确认", this);
_pushButton3->setFixedSize(120, 40);
buttonLayout->addWidget(_pushButton2);
buttonLayout->addWidget(_pushButton3);
connect(_pushButton2,&ElaPushButton::clicked,[=](){
this->hide();
});
connect(_pushButton3, &ElaPushButton::clicked, [=]() {
QString inputText = folderName2->currentText();
QRegularExpression re("^[a-zA-Z0-9/-]+/$");
if (!re.match(inputText).hasMatch()) {
// 如果输入不符合要求,显示警告对话框
//QMessageBox::warning(this, "输入错误", "云端文件夹地址只能包含大小写字母、数字、'-'和'/',且必须以'/'结尾。");
ElaMessageBar::error(ElaMessageBarType::TopRight,"输入错误", "云端文件夹地址输入错误,只能包含大小写字母、数字、'-'和'/',且必须以'/'结尾。", 2000,this);
} else {
// 如果输入符合要求,执行后续操作
this->hide();
qDebug() << "本地文件夹地址:" << folderName1->text();
qDebug() << "云端文件夹地址:" << folderName2->currentText();
int syncOption = comboOptionToNumber[_comboBox->currentText()];
qDebug() << "同步方式:" << syncOption;//["仅上传"] = 2;["仅下载"] = 3;["同步上传与下载"] = 1;
SyncTask task(folderName1->text(), folderName2->currentText(), syncOption);
emit onNewTask(task);
}
});
centerVLayout->addWidget(Title);
centerVLayout->addWidget(lineEditArea1);
//centerVLayout->addWidget(errorLabel);
centerVLayout->addWidget(lineEditArea2);
centerVLayout->addWidget(_comboBox);
centerVLayout->addWidget(buttonArea);
this->setCentralWidget(centerarea);// 将中心部件添加到窗口
QPalette palette = folderName2->lineEdit()->palette();
palette.setColor(QPalette::Text, Qt::transparent); // 设置文本颜色为透明
folderName2->lineEdit()->setPalette(palette);
}
linkNewFolder_window::~linkNewFolder_window()
{
}
void linkNewFolder_window::setItems(QStringList &folderName2List)
{
qDebug() << folderName2List;
int index = folderName2List.size();
folderName2->removeItem(index);
folderName2->addItems(folderName2List);
}
void linkNewFolder_window::clearItems()
{
folderName2->clear();
}
ElaLineEdit* linkNewFolder_window::getfolderName1()
{
return folderName1;
}