-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidget.cpp
584 lines (504 loc) · 16.4 KB
/
widget.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
#include <QMimeData>
#include <QDebug>
#include "widget.h"
#include "qmimedata.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
, rHelper(nullptr)
{
ui->setupUi(this);
this->setWindowTitle("重命名工具");
fileTableInit();
replacePartInit();
suffixPartInit();
addIndexPartInit();
comboBoxInit();
isFirstRun_ = true;
// this->setStyleSheet("QToolTip { color: #ffffff; background-color: #666666; border: 1px solid white; }");
setAcceptDrops(true);
}
void Widget::comboBoxInit()
{
if (ui->conflictComboBox->count() == 0)
{
ui->conflictComboBox->addItems({"不解决"});
}
if (ui->letterCaseComboBox->count() == 0)
{
ui->letterCaseComboBox->addItems({"默认不修改", "小写", "大写", "首字母大写", "首字母小写", "大小写反转", "简体转繁体", "繁体转简体"}); // OpenCC
}
if (ui->caseTargetComboBox->count() == 0)
{
ui->caseTargetComboBox->addItems({"不可用"});//{"全文件名", "仅基础文件名", "仅后缀名"});
}
}
void Widget::fileTableInit()
{
QStringList horizontalLabels;
horizontalLabels << "原始文件绝对路径" << "目标文件名" << "结果";
ui->fileTableWidget->setColumnCount(horizontalLabels.size());
ui->fileTableWidget->setHorizontalHeaderLabels(horizontalLabels); // 设置行标签
// QStringList verticalLabels;
// verticalLabels << "1" << "2" << "3";
// ui->fileTableWidget->setRowCount(verticalLabels.size());
// ui->fileTableWidget->setVerticalHeaderLabels(verticalLabels);
ui->fileTableWidget->verticalHeader()->setVisible(false); // 隐藏列标签
ui->fileTableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
}
void Widget::replacePartInit()
{
ui->normalRadioButton->setChecked(true);
ui->regularHelperButton->hide();
QRegularExpression regExp("[^\\\\/:*?\"<>|]+");
QRegularExpressionValidator *regExpVal = new QRegularExpressionValidator();
regExpVal->setRegularExpression(regExp);
ui->outputLineEdit->setValidator(regExpVal);
connect(ui->outputLineEdit, &QLineEdit::inputRejected, this, &Widget::showNormalReplaceTip);
}
void Widget::suffixPartInit()
{
ui->suffixLineEdit->setPlaceholderText("添加优先于修改,无需包含\".\"");
QRegularExpression regExp("[^\\\\/:*?\"<>|]+");
QRegularExpressionValidator *regExpVal = new QRegularExpressionValidator();
regExpVal->setRegularExpression(regExp);
ui->suffixLineEdit->setValidator(regExpVal);
connect(ui->suffixLineEdit, &QLineEdit::inputRejected, this, &Widget::showSuffixLineEditToolTip);
}
void Widget::addIndexPartInit()
{
ui->addIndexRuleLineEdit->setPlaceholderText("查看帮助");
ui->addIndexRuleLineEdit->setToolTip("*表示基础文件名\n?代表编号\n\n\
例如: 原始文件名image.jpg\n\
new_*_?=new_image_<编号>.jpg");
ui->startSpinBox->setMinimum(1);
ui->startSpinBox->setMaximum(65535);
ui->stepSpinBox->setMinimum(1);
ui->stepSpinBox->setMaximum(65535);
ui->numOfDigitSpinBox->setMinimum(1);
ui->numOfDigitSpinBox->setMaximum(1024);
QRegularExpression regExp("[^\\\\/:\"<>|]+");
QRegularExpressionValidator *regExpVal = new QRegularExpressionValidator();
regExpVal->setRegularExpression(regExp);
ui->addIndexRuleLineEdit->setValidator(regExpVal);
connect(ui->addIndexRuleLineEdit, &QLineEdit::inputRejected, this, &Widget::showAddIndexToolTip);
}
void Widget::showAddIndexToolTip()
{
QToolTip::showText(ui->addIndexRuleLineEdit->mapToGlobal(QPoint()), R"(文件名不能包含非法字符 \ / : " < > |)");
}
void Widget::showSuffixLineEditToolTip()
{
QToolTip::showText(ui->suffixLineEdit->mapToGlobal(QPoint()), R"(后缀名不能包含非法字符 \ / : * ? " < > |)");
}
QString Widget::getNewName(QString source, const int row)
{
QFileInfo fileInfo(source);
QString sourceFileName = fileInfo.fileName();
QString dest = fileInfo.dir().filePath(sourceFileName);
if (ui->normalRadioButton->isChecked())
{
QFileInfo tmpDstFileInfo(dest);
QString input = ui->inputLineEdit->text();
if (!input.isEmpty())
{
QString output = ui->outputLineEdit->text();
dest = tmpDstFileInfo.dir().filePath(sourceFileName.replace(input, output));
qDebug() << "normal replace, dest: " << dest;
}
}
else if (ui->regExpRadioButton->isChecked())
{
QFileInfo tmpDstFileInfo(dest);
QString input = ui->inputLineEdit->text();
if (!input.isEmpty())
{
QRegularExpression regex(input);
QString output = ui->outputLineEdit->text();
dest = tmpDstFileInfo.dir().filePath(sourceFileName.replace(regex, output));
// todo: 分组替换
qDebug() << "regular replace, dest: " << dest;
}
}
QString suffix = ui->suffixLineEdit->text();
if ((!suffix.isEmpty()) && ui->addSuffixCheckBox->isChecked())
{
QFileInfo tmpDstFileInfo(dest);
if (tmpDstFileInfo.suffix().isEmpty())
{
dest = tmpDstFileInfo.dir().filePath(tmpDstFileInfo.completeBaseName() + "." + ui->suffixLineEdit->text());
}
else
{
dest = tmpDstFileInfo.dir().filePath(tmpDstFileInfo.completeBaseName() + "." + tmpDstFileInfo.suffix() + "." + ui->suffixLineEdit->text());
}
qDebug() << "suffix add, dest: " << dest;
}
else if (!suffix.isEmpty() && ui->modifySuffixCheckBox->isChecked())
{
QFileInfo tmpDstFileInfo(dest);
if (!tmpDstFileInfo.suffix().isEmpty())
{
dest = tmpDstFileInfo.dir().filePath(tmpDstFileInfo.completeBaseName() + "." + ui->suffixLineEdit->text());
}
qDebug() << "suffix modify, dest: " << dest;
}
QString rule = ui->addIndexRuleLineEdit->text();
if (!rule.isEmpty())
{
int start = ui->startSpinBox->value();
int step = ui->stepSpinBox->value();
int width = ui->numOfDigitSpinBox->value();
int currentIndex = start + row * step;
QString indexStr = QString("%1").arg(currentIndex, width, 10, QChar('0'));
qDebug() << "rule: " << rule;
qDebug() << "当前文件是第" << row << "行,index为" << currentIndex << "编号为:" << indexStr;
QFileInfo tmpDstFileInfo(dest);
QString tmp = rule;
while (tmp.contains(QString("?")))
{
tmp.replace(QString("?"), indexStr);
}
while (tmp.contains(QString("*")))
{
tmp.replace(QString("*"), tmpDstFileInfo.completeBaseName());
}
qDebug() << "tmp: " << tmp;
dest = tmpDstFileInfo.dir().filePath(tmpDstFileInfo.suffix().isEmpty() ? tmp : (tmp + "." + tmpDstFileInfo.suffix()));
}
// letterCaseComboBox->addItems({"默认不修改", "小写", "大写", "首字母大写", "首字母小写", "大小写反转"});
// caseTargetComboBox->addItems({"全文件名", "仅基础文件名", "仅后缀名"});
QString letterCase = ui->letterCaseComboBox->currentText();
if (letterCase == "小写")
{
}
else if (letterCase == "大写")
{
}
else if (letterCase == "首字母大写")
{
}
else if (letterCase == "首字母小写")
{
}
else if (letterCase == "大小写反转")
{
}
else
{
}
QFileInfo dstFile(dest);
return dstFile.fileName();
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_addFileButton_clicked()
{
QStringList files;
if (isFirstRun_)
{
isFirstRun_ = false;
QString desktopPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
files = QFileDialog::getOpenFileNames(nullptr, "选择文件", desktopPath);
}
else
{
files = QFileDialog::getOpenFileNames(nullptr, "选择文件");
}
for (int i = 0; i < files.size(); i++)
{
if (!files[i].isEmpty())
{
insertToFileTable(files[i]);
}
}
}
void Widget::on_renameButton_clicked()
{
for (int row = 0; row < ui->fileTableWidget->rowCount(); ++row)
{
// 重命名
QTableWidgetItem *sourceItem = ui->fileTableWidget->item(row, 0);
QTableWidgetItem *destItem = ui->fileTableWidget->item(row, 1);
QTableWidgetItem *resultItem = ui->fileTableWidget->item(row, 2);
if (sourceItem && destItem && resultItem)
{
QFileInfo srcFile(sourceItem->text());
if (sourceItem->text() == srcFile.dir().filePath(destItem->text()))
{
resultItem->setText("无需修改");
continue;
}
if (QFile::exists(srcFile.dir().filePath(destItem->text())))
{
resultItem->setText("文件名冲突");
}
else
{
bool result = QFile::rename(sourceItem->text(), srcFile.dir().filePath(destItem->text()));
// qDebug() << "src: " << sourceItem->text() << ", dst " << destItem->text();
if (result)
{
resultItem->setText("修改成功");
}
else
{
resultItem->setText("修改失败");
}
}
}
}
}
void Widget::refreshData()
{
for (int row = 0; row < ui->fileTableWidget->rowCount(); ++row)
{
// 重命名
QTableWidgetItem *sourceItem = ui->fileTableWidget->item(row, 0);
QTableWidgetItem *destItem = ui->fileTableWidget->item(row, 1);
QTableWidgetItem *resultItem = ui->fileTableWidget->item(row, 2);
if (sourceItem && destItem && resultItem)
{
QString newName = getNewName(sourceItem->text(), row);
destItem->setText(newName);
}
}
}
void Widget::on_clearFileButton_clicked()
{
qDebug() << __func__;
ui->fileTableWidget->clearContents();
ui->fileTableWidget->setRowCount(0);
observedFiles_.clear();
refreshData();
}
void Widget::on_normalRadioButton_clicked()
{
if (isClearing)
return;
ui->regularHelperButton->hide();
if (ui->outputLineEdit->validator())
{
delete ui->outputLineEdit->validator();
ui->outputLineEdit->setValidator(nullptr);
}
QRegularExpression regExp("[^\\\\/:*?\"<>|]+");
QRegularExpressionValidator *regExpVal = new QRegularExpressionValidator();
regExpVal->setRegularExpression(regExp);
ui->outputLineEdit->setValidator(regExpVal);
connect(ui->outputLineEdit, &QLineEdit::inputRejected, this, &Widget::showNormalReplaceTip);
qDebug() << __func__;
refreshData();
}
void Widget::showNormalReplaceTip()
{
QToolTip::showText(ui->outputLineEdit->mapToGlobal(QPoint()), R"(文件名不能包含非法字符 \ / : * ? " < > |)");
}
void Widget::on_regExpRadioButton_clicked()
{
if (isClearing)
return;
ui->regularHelperButton->show();
if (ui->outputLineEdit->validator())
{
delete ui->outputLineEdit->validator();
ui->outputLineEdit->setValidator(nullptr);
}
QRegularExpression regExp("[^\\/:*?\"<>|]+");
QRegularExpressionValidator *regExpVal = new QRegularExpressionValidator();
regExpVal->setRegularExpression(regExp);
ui->outputLineEdit->setValidator(regExpVal);
connect(ui->outputLineEdit, &QLineEdit::inputRejected, this, &Widget::showRegularReplaceTip);
qDebug() << __func__;
refreshData();
}
void Widget::showRegularReplaceTip()
{
QToolTip::showText(ui->outputLineEdit->mapToGlobal(QPoint()), R"(文件名不能包含非法字符 / : * ? " < > |)");
}
void Widget::on_addSuffixCheckBox_clicked()
{
if (isClearing)
return;
qDebug() << __func__;
refreshData();
}
void Widget::on_modifySuffixCheckBox_clicked()
{
if (isClearing)
return;
qDebug() << __func__;
refreshData();
}
void Widget::on_suffixLineEdit_textChanged(const QString &arg1)
{
if (isClearing)
return;
qDebug() << __func__ << ": " << arg1;
refreshData();
}
void Widget::on_addIndexRuleLineEdit_textChanged(const QString &arg1)
{
if (isClearing)
return;
qDebug() << __func__ << ": " << arg1;
refreshData();
}
void Widget::on_inputLineEdit_textChanged(const QString &arg1)
{
if (isClearing)
return;
qDebug() << __func__ << ": " << arg1;
refreshData();
}
void Widget::on_outputLineEdit_textChanged(const QString &arg1)
{
if (isClearing)
return;
qDebug() << __func__ << ": " << arg1;
refreshData();
}
void Widget::on_startSpinBox_textChanged(const QString &arg1)
{
if (isClearing)
return;
qDebug() << __func__ << ": " << arg1;
refreshData();
}
void Widget::on_stepSpinBox_textChanged(const QString &arg1)
{
if (isClearing)
return;
qDebug() << __func__ << ": " << arg1;
refreshData();
}
void Widget::on_numOfDigitSpinBox_textChanged(const QString &arg1)
{
if (isClearing)
return;
qDebug() << __func__ << ": " << arg1;
refreshData();
}
void Widget::on_rmFileButton_clicked()
{
QList<QTableWidgetItem *> listToRemove = ui->fileTableWidget->selectedItems();
std::set<int> rowToRemove;
for(int i = 0; i < listToRemove.count(); i++)
{
QTableWidgetItem *item = listToRemove.at(i);
if (item)
{
rowToRemove.insert(item->row());
}
//qDebug() << "selecteditem " << item->text();
}
for (auto& it: rowToRemove)
{
qDebug() << "remove row " << it;
QTableWidgetItem *sourceItem = ui->fileTableWidget->item(it, 0);
if (sourceItem)
{
observedFiles_.erase(sourceItem->text());
}
else
{
qDebug() << "warning: source item is not exist for row " << it;
}
ui->fileTableWidget->removeRow(it);
}
}
void Widget::dragEnterEvent(QDragEnterEvent *event){
if (event->mimeData()->hasUrls()) {
event->acceptProposedAction(); // 接受拖放事件
}
}
void Widget::dropEvent(QDropEvent *event){
const QMimeData *mimeData = event->mimeData();
if (mimeData->hasUrls())
{
QList<QUrl> urlList = mimeData->urls();
for (const QUrl &url : urlList)
{
QString filePath = url.toLocalFile(); // 获取文件路径
insertToFileTable(filePath);
}
event->acceptProposedAction();
}
}
void Widget::insertToFileTable(QString& filePath)
{
auto result = observedFiles_.insert(filePath);
if (result.second)
{
int row = ui->fileTableWidget->rowCount();
qDebug() << "insert file:" << filePath << ", insert to row " << row;
ui->fileTableWidget->insertRow(row);
ui->fileTableWidget->setItem(row, 0, new QTableWidgetItem(filePath));
QString newName = getNewName(filePath, row);
ui->fileTableWidget->setItem(row, 1, new QTableWidgetItem(newName));
ui->fileTableWidget->setItem(row, 2, new QTableWidgetItem("还未执行"));
}
else
{
qDebug() << "insert file:" << filePath << " already exists, do nothing";
}
}
void Widget::on_letterCaseComboBox_currentTextChanged(const QString &arg1)
{
if (isClearing)
return;
qDebug() << __func__ << ": " << arg1;
refreshData();
}
void Widget::on_caseTargetComboBox_currentTextChanged(const QString &arg1)
{
if (isClearing)
return;
qDebug() << __func__ << ": " << arg1;
refreshData();
}
void Widget::on_clearAllPlanButton_clicked()
{
qDebug() << __func__;
isClearing = true;
ui->addIndexRuleLineEdit->clear();
ui->startSpinBox->setValue(1);
ui->stepSpinBox->setValue(1);
ui->numOfDigitSpinBox->setValue(1);
ui->addSuffixCheckBox->setCheckState(Qt::Unchecked);
ui->modifySuffixCheckBox->setCheckState(Qt::Unchecked);
ui->suffixLineEdit->clear();
replacePartInit();
ui->inputLineEdit->clear();
ui->outputLineEdit->clear();
if (ui->conflictComboBox->count() != 0)
{
ui->conflictComboBox->setCurrentIndex(0);
}
if (ui->letterCaseComboBox->count() != 0)
{
ui->letterCaseComboBox->setCurrentIndex(0);
}
if (ui->caseTargetComboBox->count() != 0)
{
ui->caseTargetComboBox->setCurrentIndex(0);
}
isClearing = false;
refreshData();
}
void Widget::on_regularHelperButton_clicked()
{
qDebug() << __func__;
if (rHelper == nullptr)
{
rHelper = new regularHelper();
rHelper->show();
}
else
{
rHelper->show();
}
}