-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselectwidget.cpp
164 lines (154 loc) · 4.91 KB
/
selectwidget.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
/*
* created by hxz
* edited by syh
*
* 第一窗格左下角的窗体,提供范围匹配和表达式匹配
*
* */
#include "selectwidget.h"
#include <cmath>
#include <QMessageBox>
#include <QDir>
#include <QRegularExpression>
#include <QFileInfoList>
#include <queue>
#include "filestruct.h"
selectwidget::selectwidget(QWidget* parent):QWidget(parent)
{
cb=new QComboBox(this);
cb->addItem("数字匹配");
cb->addItem("表达式匹配");
sunWidget = new QWidget(this);
finish = new QPushButton("导入文件列表",this);
chooseRoot = new QPushButton("选择文件目录", this);
widget1=new matchwidget(sunWidget);
widget2=new QPlainTextEdit(sunWidget);
widget2->setPlaceholderText("默认是操作系统匹配(如*.mp4)\n如果想使用regex,请以~开头(开头的~将被忽略)");
widget2->hide();
videoRoot = "";
thislayout=new QGridLayout(this);
thislayout->addWidget(cb, 0, 0, 1, 1);
thislayout->addWidget(chooseRoot, 0, 1, 1, 1);
thislayout->addWidget(sunWidget, 1, 0, 8, 5);
thislayout->addWidget(finish, 9, 1, 1, 2);
connect(cb,&QComboBox::currentIndexChanged,this,&selectwidget::checkcurrenttext);
connect(chooseRoot, &QPushButton::clicked, this, [&](){videoRoot = QFileDialog::getExistingDirectory(this,"选择视频文件夹","");});
connect(finish, &QPushButton::clicked, this, &selectwidget::import);
}
selectwidget::~selectwidget()
{
delete widget1;
delete widget2;
delete cb;
delete thislayout;
}
void selectwidget::checkcurrenttext()
{
if(cb->currentIndex() == 0)
{
widget1->show();
widget2->hide();
}
else
{
widget2->show();
widget1->hide();
}
}
//syh, version 0, June 8
//aiming to solve problem of widget disappering, unsolved
//TODO
void selectwidget::resizeEvent(QResizeEvent* sizeChangd){
// qDebug() << "size changed !" << Qt::endl;
widget1->adjustSize();
widget2->adjustSize();
}
//syh, version 1, June 8
//syh, edited on June 10
//the slot for importing files to filelist
void selectwidget::import(){
//must choose the dir to import from first
if(videoRoot == ""){
QMessageBox::warning(this, "错误", "请先选择文件目录!");
return;
}
QDir::setCurrent(videoRoot);
QDir cur(".");
std::queue<QString> files;
//if contents not valid, jump to QMessageBox
bool valid = true;
if(cb->currentIndex()){
if(widget2->toPlainText().isEmpty()){
valid = false;
goto empty;
}
QString exp = widget2->toPlainText();
QRegularExpression re;
if(exp[0] != '~'){
re = QRegularExpression::fromWildcard(exp);
}
else{
exp.erase(exp.begin(), exp.begin()+1);
qDebug() << exp << Qt::endl;
re.setPattern(exp);
}
QFileInfoList thisdir = cur.entryInfoList(QDir::Files, QDir::Name);
for(auto x : thisdir){
if(re.match(x.fileName()).hasMatch()){
qDebug() << x.fileName() << Qt::endl;
files.push(x.absoluteFilePath());
}
}
}
else{
QString stable = widget1->lines[0]->text();
QString subfix = widget1->lines[1]->text();
QString from = widget1->lines[2]->text();
QString to = widget1->lines[3]->text();
bool withZero = true;
if(from.isEmpty() || to.isEmpty() || subfix.isEmpty()){
valid = false;
goto empty;
}
if(from.length() != to.length()){
withZero = false;
}
if(to[0] == '0' || from[0] == '0'){
withZero = true;
}
for(int i = from.toInt(); i <= to.toInt(); i++){
QString movable = "";
if(withZero)
for (int j = 1; j <= from.length() - std::ceil(std::log(i) / std::log(10) + 1); j++){
movable.push_back('0');
}
movable += QString::number(i);
QString final = "";
//末数字
if(widget1->cb->currentIndex() == 0){
final = stable + movable;
}
else{
final = movable + stable;
}
if(subfix[0] != '.'){
final.push_back('.');
}
final += subfix;
if(cur.exists(final)){
qDebug()<<final<<Qt::endl;
QString filepath = QDir::cleanPath(cur.absoluteFilePath(final));
files.push(filepath);
}
}
}
while(!files.empty()){
FileStruct fs(files.front());
filelist->addfile(fs);
files.pop();
}
empty:
if(! valid){
QMessageBox::warning(this, "错误", "没有填写完整,不能完成导入!");
}
}