-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.cpp
64 lines (60 loc) · 1.78 KB
/
file.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
#include "file.h"
#include "error.h"
#include <QDir>
#include <QFileDialog>
#include <QFileInfo>
#include <QImageReader>
#include <QTemporaryDir>
#include <QTemporaryFile>
QString File::generateTempSourceFile(const QString& sourceFilePath, const QString& tempFileName)
{
if (sourceFilePath.isEmpty() || !QFile::exists(sourceFilePath)) {
return QString::number(ERROR_FILE_NOT_FOUND);
}
QFileInfo* fi = new QFileInfo(sourceFilePath);
QString sourceTempFileDir = getTempDir(tempFileName + "." + QImageReader::imageFormat(sourceFilePath));
bool status = copyFileToPath(sourceFilePath, sourceTempFileDir, true);
delete fi;
if (status) {
return sourceTempFileDir;
}
return QString::number(ERROR_IO);
}
QString File::getTempDir(const QString& fileName)
{
return QDir::tempPath() + QDir::separator() + fileName;
}
bool File::copyFileToPath(const QString& sourceFile, QString& toFile, bool coverFileIfExist)
{
if (sourceFile.isEmpty() || toFile.isEmpty()) {
return false;
}
toFile.replace("\\", "/");
if (!QFile::exists(sourceFile)) {
return false;
}
if (sourceFile == toFile) {
return true;
}
if (QFile::exists(toFile)) {
if (coverFileIfExist) {
QFile::remove(toFile);
} else {
return false;
}
}
if (QFile::copy(sourceFile, toFile)) {
return true;
}
return false;
}
void File::saveImage(QWidget* obj, const QString& imagePath)
{
if (imagePath.isEmpty()) {
return;
}
QFileInfo* fileInfo = new QFileInfo(imagePath);
QString savePath = QFileDialog::getSaveFileName(obj, "保存", fileInfo->absoluteFilePath(), "*." + QImageReader::imageFormat(imagePath));
copyFileToPath(imagePath, savePath, true);
delete fileInfo;
}