-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathimagefile.cpp
93 lines (72 loc) · 2.54 KB
/
imagefile.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
#include <QtCore>
#include "imagefile.h"
#include "utils.h"
ImageFile::ImageFile()
{}
ImageFile::ImageFile(const QFileInfo &fileInfo)
: m_fileInfo(fileInfo)
, m_moved(false)
{
// extract "/data/car" from "/data/car.jpg"
m_plateNameTemplate = m_fileInfo.canonicalPath() + QDir::separator() + m_fileInfo.completeBaseName();
m_plates = PlateFile::fromImageFile(m_plateNameTemplate);
}
void ImageFile::addPlate(const PlateFile &plateFile)
{
m_plates.append(plateFile);
}
void ImageFile::setPlates(const PlateFileList &newPlateFileList)
{
// determine plates to be removed and added
PlateFileList platesToRemove = m_plates;
foreach(const PlateFile &newPlateFile, newPlateFileList)
{
QMutableListIterator<PlateFile> itOldPlates(platesToRemove);
while(itOldPlates.hasNext())
{
const PlateFile &oldPlateFile = itOldPlates.next();
if(oldPlateFile.plateFile() == newPlateFile.plateFile())
itOldPlates.remove();
}
}
foreach(const PlateFile &plateFile, platesToRemove)
{
// standalone YAML
if(plateFile.plateFileTemplate().isEmpty())
{
qDebug() << "Removing HEAP YAML" << plateFile.plateFile();
if(QFile::exists(plateFile.plateFile()) && !QFile::remove(plateFile.plateFile()))
Utils::error(QObject::tr("Cannot remove YAML file %1").arg(plateFile.plateFile()));
}
}
m_plates = newPlateFileList;
int numberedIndex = 0;
// sync to disk
for(int i = 0;i < m_plates.size();i++)
{
QString error;
PlateFile plateFile = m_plates.at(i);
// new plates don't have an image file set
if(plateFile.imageFile().isEmpty())
plateFile.setImageFile(m_fileInfo.fileName());
bool written = plateFile.plateFileTemplate().isEmpty()
? plateFile.writeToStandaloneFile(&error)
: plateFile.writeToNumberedFile(numberedIndex++, &error);
if(!written)
{
Utils::error(QObject::tr("Cannot save YAML file. %1")
.arg(error.isEmpty()
? QObject::tr("Unknown error")
: QObject::tr("Error: %1").arg(error)));
}
}
int index = numberedIndex;
while(true)
{
const QString &file = m_plateNameTemplate + QString("-%1.yaml").arg(index);
if(!QFile::exists(file))
break;
qDebug() << "Removing INDEXED YAML" << file;
QFile::remove(file);
}
}