-
Notifications
You must be signed in to change notification settings - Fork 0
/
renamewidget.cpp
193 lines (152 loc) · 5.95 KB
/
renamewidget.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
#include "renamewidget.h"
#include "ui_renamewidget.h"
RenameWidget::RenameWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::RenameWidget)
{
ui->setupUi(this);
ui->radioButton_without->setChecked(true);
ui->lineEdit_suffix->setEnabled(false);
}
RenameWidget::~RenameWidget()
{
delete ui;
}
void RenameWidget::on_radioButton_1Typ_clicked()
{
ui->lineEdit_suffix->setEnabled(false);
}
void RenameWidget::on_radioButton_2Typ_clicked()
{
ui->lineEdit_suffix->setEnabled(false);
}
void RenameWidget::on_radioButton_without_clicked()
{
ui->lineEdit_suffix->setEnabled(false);
}
void RenameWidget::on_radioButton_other_clicked()
{
ui->lineEdit_suffix->setEnabled(true);
}
void RenameWidget::on_pushButton_upload_clicked()
{
this->close();
emit mainWindow();
}
void RenameWidget::on_pushButton_protocol_clicked()
{
QString path_protocol = QFileDialog::getOpenFileName(this,
tr("Выберите протокол"), "",
tr("Книга Microsoft Excel (*.xlsx)"));
ui->lineEdit_protocol->setText(path_protocol);
}
void RenameWidget::on_pushButton_namelessDir_clicked()
{
QString path_nameless = QFileDialog::getExistingDirectory(this,
tr("Выберите папку с безымянными работами"), "");
ui->lineEdit_namelessDir->setText(path_nameless);
}
void RenameWidget::on_pushButton_distDir_clicked()
{
QString path_dist = QFileDialog::getExistingDirectory(this,
tr("Выберите папку для переименованных работ"), "");
ui->lineEdit_distDir->setText(path_dist);
}
void RenameWidget::showErrorMessage(QString text)
{
QMessageBox errorMessage(this);
errorMessage.setInformativeText(text);
errorMessage.setText("Ошибка!");
errorMessage.exec();
}
void RenameWidget::on_pushButton_rename_clicked()
{
QString participants_text = remove_yo(ui->textEdit_participant->toPlainText());
QString grades_text = remove_yo(ui->textEdit_grades->toPlainText());
QStringList participants_text_splited = participants_text.split(QRegExp("[\n]"),QString::SkipEmptyParts);
qInfo("Participant names(%d) getted successfuly", participants_text_splited.length());
QStringList grades_list = grades_text.split(QRegExp("[\n]"),QString::SkipEmptyParts);
qInfo("Participant grades(%d) getted successfuly", grades_list.length());
if(participants_text_splited.length() != grades_list.length())
{
showErrorMessage("Количество учеников не соответствует количеству их классов!");
return;
}
QVector<Participant> participant_list;
for(int index_of_participant = 0; index_of_participant < participants_text_splited.length(); index_of_participant++)
{
QString participant_fio = participants_text_splited[index_of_participant];
QString grade = grades_list[index_of_participant];
QStringList splited_fio = participant_fio.split(QRegExp("[ \t]"),QString::SkipEmptyParts);
if(splited_fio.length() < 2)
{
showErrorMessage("Неправильно указано имя одного или нескольких участников!");
return;
}
participant_list.append(Participant(splited_fio.at(0), splited_fio.at(1), grade));
}
qInfo("ParticipantInfo merged successfuly");
QString suffix = "";
if(ui->radioButton_1Typ->isChecked())
{
suffix = "_1тур";
}
else if(ui->radioButton_2Typ->isChecked())
{
suffix = "_2тур";
}
else if(ui->radioButton_other->isChecked())
{
suffix = "_" + ui->lineEdit_suffix->text();
}
qInfo("Suffix(%s) getted successfuly", suffix.toLocal8Bit().data());
QList<QString> newFileNames;
for(auto participant: participant_list)
{
newFileNames.append(participant.lastName + "_" + participant.firstName + "_" + participant.grade + "кл" + suffix + ".pdf");
}
qInfo("New names(%d) generated successfuly", newFileNames.length());
QString dirName_namelessDir = ui->lineEdit_namelessDir->text();
QString dirName_distDir = ui->lineEdit_distDir->text();
QDir namelessDir = QDir(dirName_namelessDir);
if(!namelessDir.exists())
{
showErrorMessage("Папка с безымянными работами указана неверно!");
return;
}
QDir distDir = QDir(dirName_distDir);
if(!distDir.exists())
{
showErrorMessage("Папка для переименованных работ указана неверно!");
return;
}
QFileInfoList fileName_list = namelessDir.entryInfoList(QDir::Files);
if(participants_text_splited.length() != fileName_list.length())
{
showErrorMessage("Количество файлов не соответствует количеству учеников!");
return;
}
qInfo("Nameless files(%d) getted successfuly", fileName_list.length());
QProgressDialog progressDialog(this);
progressDialog.setLabelText("Переименовывание");
progressDialog.setMinimum(0);
progressDialog.setMaximum(newFileNames.length());
progressDialog.show();
for(int index_of_file = 0; index_of_file < newFileNames.length(); index_of_file++)
{
progressDialog.setValue(index_of_file);
QString oldFileName = fileName_list[index_of_file].absoluteFilePath();
QString newFileName = dirName_distDir + "/" + newFileNames[index_of_file];
qInfo("%s -> %s", oldFileName.toLocal8Bit().data(), newFileName.toLocal8Bit().data());
if (QFile::exists(newFileName))
{
QFile::remove(newFileName);
}
if(!QFile::copy(oldFileName, newFileName))
{
showErrorMessage("При переименовывании произоошла ошибка!");
}
}
progressDialog.setValue(newFileNames.length());
qInfo("All files renamed successfuly");
}