-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmainwindow.cpp
103 lines (91 loc) · 3.6 KB
/
mainwindow.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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
treeModel = new LocFileSystemModel();
QObject::connect(treeModel,SIGNAL(directoryLoaded(const QString&)),
this,SLOT(slot_treeModel_directoryLoaded(const QString&)));
ui->treePreview->setSortingEnabled(true);
ui->treePreview->setExpandsOnDoubleClick(true);
codecs=QTextCodec::availableCodecs();
QStringList codeclist;
int systemindex=-1;
int latin1index=-1;//mac doesn't have the system codec option
int sjisindex=-1;
for(int i=0;i<codecs.length();i++){
QByteArray currcodec=codecs.at(i);
codeclist.push_back(currcodec);
if(currcodec=="System")systemindex=i;
if(currcodec=="latin1")latin1index=i;
if(currcodec=="Shift_JIS")sjisindex=i;
}
ui->cmboEnc->addItems(codeclist);
ui->cmboSysEnc->addItems(codeclist);
//set system codec index
if(systemindex!=-1)ui->cmboSysEnc->setCurrentIndex(systemindex);
else if(latin1index!=-1)ui->cmboSysEnc->setCurrentIndex(latin1index);
//set target index
if(sjisindex!=-1)ui->cmboEnc->setCurrentIndex(sjisindex);
}
MainWindow::~MainWindow()
{
delete ui;
delete treeModel;
}
void MainWindow::rename(QDir root)
{
QStringList files = root.entryList(QDir::Files);
QStringList dirs = root.entryList(QDir::Dirs|QDir::NoDotAndDotDot);
for(auto it=dirs.begin();it<dirs.end();it++){
QString path=root.absoluteFilePath(*it);
rename(QDir(path));
root.rename(*it,treeModel->fileName(treeModel->index(path)));
//qDebug()<<"Renaming "<<*it<<" to "<<treeModel->fileName(treeModel->index(path))<<", path "<<path;
}
for(auto it=files.begin();it<files.end();it++){
QString path=root.absoluteFilePath(*it);
root.rename(*it,treeModel->fileName(treeModel->index(path)));
//qDebug()<<"Renaming "<<*it<<" to "<<treeModel->fileName(treeModel->index(path))<<", path "<<path;
}
}
void MainWindow::on_btnBrowse_clicked()
{
QString dir=QFileDialog::getExistingDirectory(this, "Select Folder", ui->txtFolder->text());
if(dir!=""){
ui->txtFolder->setText(dir);
QModelIndex rootidx=treeModel->setRootPath(dir);
ui->treePreview->setModel(treeModel);
ui->treePreview->setColumnWidth(0,500);
ui->treePreview->setRootIndex(rootidx);
//ui->treePreview->expandToDepth(3);
//ui->treePreview->rootIndex().row();
}
}
void MainWindow::on_cmboSysEnc_currentIndexChanged(int index)
{
treeModel->setSysEncoding(codecs.at(index));
}
void MainWindow::on_cmboEnc_currentIndexChanged(int index)
{
treeModel->setEncoding(codecs.at(index));
}
void MainWindow::slot_treeModel_directoryLoaded(const QString & path){
ui->treePreview->expandToDepth(4);
}
void MainWindow::on_btnApply_clicked()
{
QMessageBox::StandardButton reply;
reply=QMessageBox::question(this,"Confirmation","Are you sure you want to rename files and folders as shown in the preview?\n"
"Make sure that you have a backup in case something goes wrong! \n"
"Please confirm the path is correct before clicking Yes: \n"+ui->txtFolder->text(),
QMessageBox::Yes|QMessageBox::No);
if(reply==QMessageBox::Yes){
//QModelIndex idx=treeModel->index(ui->txtFolder->text());
QDir traverseRoot=QDir(ui->txtFolder->text());
rename(traverseRoot);
}
}