-
Notifications
You must be signed in to change notification settings - Fork 105
/
ocd_file_format.cpp
162 lines (133 loc) · 4.78 KB
/
ocd_file_format.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
/*
* Copyright 2013-2016, 2018 Kai Pastor
*
* Some parts taken from file_format_oc*d8{.h,_p.h,cpp} which are
* Copyright 2012 Pete Curtis
*
* This file is part of OpenOrienteering.
*
* OpenOrienteering is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenOrienteering is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenOrienteering. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ocd_file_format.h"
#include <memory>
#include <QtGlobal>
#include <QFlags>
#include <QString>
#include "fileformats/file_import_export.h"
#include "fileformats/ocad8_file_format_p.h"
#include "fileformats/ocd_file_export.h"
#include "fileformats/ocd_file_import.h"
namespace OpenOrienteering {
namespace {
QString labelForVersion(quint16 version)
{
switch (version)
{
case OcdFileFormat::autoDeterminedVersion():
return ::OpenOrienteering::ImportExport::tr("OCAD");
case OcdFileFormat::legacyVersion():
return ::OpenOrienteering::ImportExport::tr("OCAD version 8, old implementation");
default:
return ::OpenOrienteering::ImportExport::tr("OCAD version %1").arg(version);
}
}
FileFormat::Features featuresForVersion(quint16 version)
{
switch (version)
{
case OcdFileFormat::legacyVersion():
return FileFormat::Feature::FileOpen | FileFormat::Feature::FileImport | FileFormat::Feature::ReadingLossy |
FileFormat::Feature::FileSave | FileFormat::Feature::FileSaveAs | FileFormat::Feature::WritingLossy;
case OcdFileFormat::autoDeterminedVersion():
// Intentionally no FileFormat::ExportSupported. This prevents this
// format from being shown in the Save-as dialog. However, it is legal
// to create an exporter for the autoDeterminedVersion(). The actual
// export version will be determined from the Map's versionProperty()
// if possible, or from OcdFileExport::default_version.
return FileFormat::Feature::FileOpen | FileFormat::Feature::FileImport | FileFormat::Feature::ReadingLossy |
FileFormat::Feature::FileSave | FileFormat::Feature::WritingLossy;
default:
// Intentionally no FileFormat::ImportSupported. Import is handled
// by the autoDeterminedVersion().
return FileFormat::Feature::FileSave | FileFormat::Feature::FileSaveAs | FileFormat::Feature::WritingLossy;
}
}
} // namespace
// ### OcdFileFormat ###
// static
const char* OcdFileFormat::idForVersion(quint16 version)
{
switch (version)
{
case OcdFileFormat::autoDeterminedVersion():
return "OCD";
case 8:
return "OCD8";
case 9:
return "OCD9";
case 10:
return "OCD10";
case 11:
return "OCD11";
case 12:
return "OCD12";
case OcdFileFormat::legacyVersion():
return "OCD-legacy";
default:
qFatal("Unsupported OCD version");
}
}
// static
std::vector<std::unique_ptr<OcdFileFormat>> OcdFileFormat::makeAll()
{
std::vector<std::unique_ptr<OcdFileFormat>> result;
result.reserve(7);
result.push_back(std::make_unique<OcdFileFormat>(autoDeterminedVersion()));
result.push_back(std::make_unique<OcdFileFormat>(12));
result.push_back(std::make_unique<OcdFileFormat>(11));
result.push_back(std::make_unique<OcdFileFormat>(10));
result.push_back(std::make_unique<OcdFileFormat>(9));
result.push_back(std::make_unique<OcdFileFormat>(8));
result.push_back(std::make_unique<OcdFileFormat>(legacyVersion()));
return result;
}
OcdFileFormat::OcdFileFormat(quint16 version)
: FileFormat { MapFile, idForVersion(version), labelForVersion(version), QStringLiteral("ocd"), featuresForVersion(version) }
, version { version }
{
// Nothing
}
FileFormat::ImportSupportAssumption OcdFileFormat::understands(const char* buffer, int size) const
{
// The first two bytes of the file must be AD 0C.
if (size < 2)
return Unknown;
else if (quint8(buffer[0]) == 0xAD && buffer[1] == 0x0C)
return FullySupported;
else
return NotSupported;
}
std::unique_ptr<Importer> OcdFileFormat::makeImporter(const QString& path, Map *map, MapView *view) const
{
if (version == legacyVersion())
return std::make_unique<OCAD8FileImport>(path, map, view);
return std::make_unique<OcdFileImport>(path, map, view);
}
std::unique_ptr<Exporter> OcdFileFormat::makeExporter(const QString& path, const Map* map, const MapView* view) const
{
if (version == legacyVersion())
return std::make_unique<OCAD8FileExport>(path, map, view);
return std::make_unique<OcdFileExport>(path, map, view, version);
}
} // namespace OpenOrienteering