forked from 4lex4/scantailor-advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDefaultParamsProfileManager.cpp
158 lines (128 loc) · 4.45 KB
/
DefaultParamsProfileManager.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
#include "DefaultParamsProfileManager.h"
#include <config.h>
#include <QtGui/QtGui>
#include <QtXml/QDomDocument>
#include "Application.h"
#include "DefaultParams.h"
#include "version.h"
using namespace page_split;
using namespace output;
using namespace page_layout;
DefaultParamsProfileManager::DefaultParamsProfileManager() {
auto* app = dynamic_cast<Application*>(qApp);
if (app->isPortableVersion()) {
m_path = app->getPortableConfigPath() + "/profiles";
} else {
m_path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/profiles";
}
}
DefaultParamsProfileManager::DefaultParamsProfileManager(const QString& path) : m_path(path) {}
std::list<QString> DefaultParamsProfileManager::getProfileList() const {
std::list<QString> profileList;
QDir dir(m_path);
if (dir.exists()) {
QList<QFileInfo> fileInfoList = dir.entryInfoList();
for (const QFileInfo& fileInfo : fileInfoList) {
if (fileInfo.isFile() && ((fileInfo.suffix() == "stp") || (fileInfo.suffix() == "xml"))) {
profileList.push_back(fileInfo.completeBaseName());
}
}
}
return profileList;
}
std::unique_ptr<DefaultParams> DefaultParamsProfileManager::readProfile(const QString& name, LoadStatus* status) const {
QDir dir(m_path);
QFileInfo profile(dir.absoluteFilePath(name + ".stp"));
if (!profile.exists()) {
profile = dir.absoluteFilePath(name + ".xml");
if (!profile.exists()) {
if (status) {
*status = IO_ERROR;
}
return nullptr;
}
}
QFile profileFile(profile.filePath());
if (!profileFile.open(QIODevice::ReadOnly)) {
if (status) {
*status = IO_ERROR;
}
return nullptr;
}
QDomDocument doc;
if (!doc.setContent(&profileFile)) {
if (status) {
*status = IO_ERROR;
}
return nullptr;
}
profileFile.close();
const QDomElement profileElement(doc.documentElement());
const QString version = profileElement.attribute("version");
if (version.isNull() || (version.toInt() != PROJECT_VERSION)) {
if (status) {
*status = INCOMPATIBLE_VERSION_ERROR;
}
return nullptr;
}
const QDomElement defaultParamsElement(profileElement.namedItem("default-params").toElement());
if (status) {
*status = SUCCESS;
}
return std::make_unique<DefaultParams>(defaultParamsElement);
}
bool DefaultParamsProfileManager::writeProfile(const DefaultParams& params, const QString& name) const {
QDomDocument doc;
QDomElement rootElement(doc.createElement("profile"));
doc.appendChild(rootElement);
rootElement.setAttribute("version", PROJECT_VERSION);
rootElement.appendChild(params.toXml(doc, "default-params"));
QDir dir(m_path);
if (!dir.exists()) {
dir.mkpath(".");
}
QFile file(dir.absoluteFilePath(name + ".stp"));
if (file.open(QIODevice::WriteOnly)) {
QTextStream textStream(&file);
doc.save(textStream, 2);
return true;
}
return false;
}
std::unique_ptr<DefaultParams> DefaultParamsProfileManager::createDefaultProfile() const {
return std::make_unique<DefaultParams>();
}
std::unique_ptr<DefaultParams> DefaultParamsProfileManager::createSourceProfile() const {
DefaultParams::DeskewParams deskewParams;
deskewParams.setMode(MODE_MANUAL);
DefaultParams::PageSplitParams pageSplitParams;
pageSplitParams.setLayoutType(SINGLE_PAGE_UNCUT);
DefaultParams::SelectContentParams selectContentParams;
selectContentParams.setContentDetectEnabled(false);
DefaultParams::PageLayoutParams pageLayoutParams;
pageLayoutParams.setHardMargins(Margins(0, 0, 0, 0));
Alignment alignment;
alignment.setNull(true);
pageLayoutParams.setAlignment(alignment);
DefaultParams::OutputParams outputParams;
ColorParams colorParams;
colorParams.setColorMode(COLOR_GRAYSCALE);
ColorCommonOptions colorCommonOptions;
colorCommonOptions.setFillMargins(false);
colorParams.setColorCommonOptions(colorCommonOptions);
outputParams.setColorParams(colorParams);
return std::make_unique<DefaultParams>(DefaultParams::FixOrientationParams(), deskewParams, pageSplitParams,
selectContentParams, pageLayoutParams, outputParams);
}
bool DefaultParamsProfileManager::deleteProfile(const QString& name) const {
QDir dir(m_path);
QFileInfo profile(dir.absoluteFilePath(name + ".stp"));
if (!profile.exists()) {
profile = dir.absoluteFilePath(name + ".xml");
if (!profile.exists()) {
return false;
}
}
QFile profileFile(profile.filePath());
return profileFile.remove();
}