-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditorwgt.cpp
executable file
·289 lines (264 loc) · 8.38 KB
/
editorwgt.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
#include "editorwgt.h"
#include <QDebug>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QFormLayout>
#include <QGroupBox>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QComboBox>
#include <QCheckBox>
#include <QFileDialog>
#include <QImage>
#include <QMessageBox>
#include <QStringList>
#include "pix_viewer.h"
#include "exif.h"
EditorWgt::EditorWgt(QWidget *parent) : QWidget(parent)
{
lblOriginalName = new QLabel(this);
QStringList lst;
lst << "Изобразительное исскусство"
<< "Гознак"
<< "Планета"
<< "Плакат"
<< "Правда"
<< "Министерство связи СССР"
<< "Аврора"
<< "Советский художник"
<< "Художник РСФСР"
<< "ИЗОГИС";
cmbProducer = new QComboBox(this);
cmbProducer->addItems(lst);
lst.clear();
lst << "Новый год"
<< "23 февраля"
<< "8 марта"
<< "1 мая"
<< "День победы"
<< "Праздник Октября"
<< "День рождения"
<< "Свадьба"
<< "Город"
<< "Детские поздравления"
<< "Поздравление"
<< "Цветы"
<< "Театр"
<< "Картина, фото"
<< "Животные"
<< "Природа"
<< "Музей"
<< "Детские"
<< "Революция-1905"
<< "Фольклор"
<< "Школа"
<< "Техноген"
<< "Спорт"
<< "Космос"
<< "Новорожденный";
cmbSection = new QComboBox(this);
cmbSection->addItems(lst);
leYear = new QLineEdit("19",this);
bOK = new QPushButton("OK",this);
connect(bOK, &QPushButton::clicked, this, &EditorWgt::sltOKClicked);
bOK->setEnabled(false);
bOpenFolder = new QPushButton("Open Folder",this);
connect(bOpenFolder,&QPushButton::clicked, this, &EditorWgt::sltOpenFolderClicked);
QFormLayout *frmLay = new QFormLayout;
frmLay->addRow("File: ",lblOriginalName);
// lblPix = new QLabel("No Pix",this);
// lblPix->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
lblPix = new PixViewer("No Pix",this);
lblPix->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
connect(lblPix, &PixViewer::file_dropped, this, &EditorWgt::file_dropped_from_folder);
grbxExifDate = new QGroupBox("Exif Date",this);
connect(grbxExifDate, &QGroupBox::clicked, this, &EditorWgt::onGroupBoxChecked);
grbxExifDate->setCheckable(true);
QHBoxLayout *vExifDateLay = new QHBoxLayout;
chkAutoRenameAllFiles = new QCheckBox("Autorename all files", this);
connect(chkAutoRenameAllFiles, &QCheckBox::stateChanged, this, &EditorWgt::onAutoRenameChanged);
leExifDate = new QLineEdit(this);
vExifDateLay->addWidget(chkAutoRenameAllFiles);
vExifDateLay->addWidget(leExifDate);
grbxExifDate->setLayout(vExifDateLay);
grbxPaint = new QGroupBox("Picture",this);
connect(grbxPaint, &QGroupBox::clicked, this, &EditorWgt::onGroupBoxChecked);
grbxPaint->setCheckable(true);
grbxPaint->setChecked(false);
QVBoxLayout *vPaintLay = new QVBoxLayout;
vPaintLay->addWidget(cmbProducer);
vPaintLay->addWidget(cmbSection);
vPaintLay->addWidget(leYear);
grbxPaint->setLayout(vPaintLay);
QVBoxLayout *vMainLay = new QVBoxLayout;
vMainLay->addWidget(bOpenFolder);
vMainLay->addWidget(lblPix,2);
vMainLay->addLayout(frmLay);
vMainLay->addWidget(grbxExifDate);
vMainLay->addWidget(grbxPaint);
vMainLay->addWidget(bOK);
setLayout(vMainLay);
}
void EditorWgt::setFileNameList(const QStringList &lst)
{
lstFileNames.clear();
lstFileNames = lst;
}
void EditorWgt::sltFileClicked(QString fname)
{
// Заполнять поля и картинку
lblOriginalName->setText(fname);
QString path(currentDir.absolutePath()+"/"+fname);
if(currentImg.load(path))
{
QImage img1=currentImg.scaled(lblPix->width(),lblPix->height(),Qt::KeepAspectRatio);
lblPix->clear();
lblPix->setPixmap(QPixmap().fromImage(img1));
QString str_date(getDateFromExif(path));
leExifDate->setText(str_date);
}
else
{
QMessageBox::warning(nullptr, tr("Ошибка загрузки изображения"),tr("Ошибка загрузки изображения. Проверьте путь к файлу."));
}
}
void EditorWgt::sltOKClicked()
{
QString newFName("UnknownName");
QFileInfo fi(lblOriginalName->text());
if(grbxExifDate->isChecked())
{
if(chkAutoRenameAllFiles->isChecked())
{
autorenameAllFilesFromtable();
}
else
{
if(!leExifDate->text().isEmpty())
{
newFName = leExifDate->text()+"."+fi.suffix().toLower();
emit sgnFileNameChanged(newFName);
}
}
}
else if(grbxPaint->isChecked())
{
newFName = fi.baseName()+"-"+cmbProducer->currentText()+"-"+cmbSection->currentText()+"-"+leYear->text()+"."+fi.suffix().toLower();
emit sgnFileNameChanged(newFName);
}
}
void EditorWgt::sltOpenFolderClicked()
{
QFileDialog *fdlg = new QFileDialog(this,"Выбор директории с картинками");
fdlg->setFileMode(QFileDialog::Directory);
if (fdlg->exec()==QDialog::Accepted)
{
lblPix->clear();
lblPix->setText("No Pix");
QString strPath(fdlg->directory().absolutePath());
currentDir.setCurrent(strPath);
bOK->setEnabled(true);
// Передать текущую директорию наверх
emit sgnCurrentDirectoryChoosed(currentDir.currentPath());
}
delete fdlg;
}
void EditorWgt::onGroupBoxChecked(bool checked)
{
auto grb = dynamic_cast<QGroupBox*>(sender());
if(grb==grbxPaint)
{
grbxExifDate->setChecked(!checked);
}
else if(grb==grbxExifDate)
{
grbxPaint->setChecked(!checked);
}
}
void EditorWgt::onAutoRenameChanged(int state)
{
switch(state)
{
case Qt::Checked:
{
leExifDate->setEnabled(false);
leExifDate->clear();
break;
}
case Qt::PartiallyChecked:
{
break;
}
case Qt::Unchecked:
{
leExifDate->setEnabled(true);
break;
}
}
}
void EditorWgt::file_dropped_from_folder(const QString &fname) noexcept
{
auto idx = fname.lastIndexOf('/');
auto n = fname.mid(idx+1);
auto path = fname.mid(0, idx);
currentDir.setPath(path);
bOK->setEnabled(true);
emit file_dropped(fname);
}
QString EditorWgt::convertDateForFileName(const QString &strDate)
{
QString str;
if(!strDate.isEmpty())
{
auto lstSpase=strDate.split(" ");
QString date(lstSpase[0]);
QString time(lstSpase[1]);
date=date.replace(":","_");
time=time.replace(":","_");
str=date+"-"+time;
}
return str;
}
void EditorWgt::autorenameAllFilesFromtable()
{
for(const auto &fname : lstFileNames)
{
QString newFName("UnknownName");
QFileInfo fi(fname);
auto str_date=getDateFromExif(fname);
if(!str_date.isEmpty())
{
newFName = str_date+"."+fi.suffix().toLower();
emit sgnAutoRenameFileName(fname, newFName);
}
else
{
qDebug() << "No date in file: " << fname;
}
}
}
QString EditorWgt::getDateFromExif(QStringView fname)
{
easyexif::EXIFInfo info;
QFile file(fname.toString());
file.open(QIODevice::ReadOnly);
QByteArray data = file.readAll();
if (int code = info.parseFrom((unsigned char *)data.data(), data.size()))
{
qDebug() << "Error parsing EXIF: code " << code;
}
QString date(info.DateTimeOriginal.c_str());
file.close();
auto str=convertDateForFileName(date);
return str;
}
void EditorWgt::resizeEvent(QResizeEvent *event)
{
if(!currentImg.isNull())
{
QImage img1=currentImg.scaled(lblPix->width(),lblPix->height(),Qt::KeepAspectRatio);
lblPix->setPixmap(QPixmap().fromImage(img1));
}
QWidget::resizeEvent(event);
}