Skip to content

Commit d8cdc56

Browse files
authored
Refactoring: Split openscad_gui.cc from openscad.cc, slimmed down ExportInfo (openscad#5368)
1 parent 5259f36 commit d8cdc56

File tree

15 files changed

+720
-679
lines changed

15 files changed

+720
-679
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,7 @@ set(INPUT_DRIVER_SOURCES
10341034

10351035
set(GUI_SOURCES
10361036
${GUI_SOURCES}
1037+
src/openscad_gui.cc
10371038
src/gui/AutoUpdater.cc
10381039
src/gui/CGALWorker.cc
10391040
src/gui/ViewportControl.cc

src/gui/ExportPdfDialog.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ ExportPdfDialog::ExportPdfDialog()
113113
bool ExportPdfDialog::getShowScaleMsg() {
114114
return cbScaleUsg->isChecked();
115115
}
116-
bool ExportPdfDialog::getShowDsnFn() {
116+
bool ExportPdfDialog::getShowDesignFilename() {
117117
return cbDsnFn->isChecked();
118118
}
119119
bool ExportPdfDialog::getShowGrid() {
@@ -127,7 +127,7 @@ ExportPdfDialog::ExportPdfDialog()
127127
void ExportPdfDialog::setShowScaleMsg(bool state) {
128128
cbScaleUsg->setChecked(state);
129129
}
130-
void ExportPdfDialog::setShowDsnFn(bool state) {
130+
void ExportPdfDialog::setShowDesignFilename(bool state) {
131131
cbDsnFn->setChecked(state);
132132
}
133133
void ExportPdfDialog::setShowGrid(bool state) {

src/gui/ExportPdfDialog.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ class ExportPdfDialog : public QDialog, public Ui::ExportPdfDialog
4343
paperOrientations getOrientation();
4444
bool getShowScale();
4545
bool getShowScaleMsg();
46-
bool getShowDsnFn();
46+
bool getShowDesignFilename();
4747
bool getShowGrid();
4848

4949
void setShowScale(bool state);
5050
void setShowScaleMsg(bool state);
51-
void setShowDsnFn(bool state);
51+
void setShowDesignFilename(bool state);
5252
void setShowGrid(bool state);
5353

5454
void setPaperSize(paperSizes paper);

src/gui/MainWindow.cc

Lines changed: 24 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2147,25 +2147,6 @@ void MainWindow::action3DPrint()
21472147
#endif // ifdef ENABLE_3D_PRINTING
21482148
}
21492149

2150-
namespace {
2151-
2152-
ExportInfo createExportInfo(FileFormat format, const QString& exportFilename, const QString& sourceFilePath)
2153-
{
2154-
const QFileInfo info(sourceFilePath);
2155-
2156-
ExportInfo exportInfo;
2157-
exportInfo.format = format;
2158-
exportInfo.fileName = exportFilename.toLocal8Bit().constData();
2159-
exportInfo.displayName = exportFilename.toUtf8().toStdString();
2160-
exportInfo.sourceFilePath = sourceFilePath.toUtf8().toStdString();
2161-
exportInfo.sourceFileName = info.fileName().toUtf8().toStdString();
2162-
exportInfo.useStdOut = false;
2163-
exportInfo.options = nullptr;
2164-
return exportInfo;
2165-
}
2166-
2167-
}
2168-
21692150
void MainWindow::sendToOctoPrint()
21702151
{
21712152
#ifdef ENABLE_3D_PRINTING
@@ -2176,20 +2157,22 @@ void MainWindow::sendToOctoPrint()
21762157
return;
21772158
}
21782159

2160+
// FIXME: To make this cleaner, we could define which formats are supported by OctoPrint separately,
2161+
// then using fileformat::fromIdentifier() to convert.
21792162
const QString fileFormat = QString::fromStdString(Settings::Settings::octoPrintFileFormat.value());
2180-
FileFormat exportFileFormat{FileFormat::STL};
2163+
FileFormat exportFileFormat{FileFormat::BINARY_STL};
21812164
if (fileFormat == "OBJ") {
21822165
exportFileFormat = FileFormat::OBJ;
21832166
} else if (fileFormat == "OFF") {
21842167
exportFileFormat = FileFormat::OFF;
21852168
} else if (fileFormat == "ASCIISTL") {
2186-
exportFileFormat = FileFormat::ASCIISTL;
2169+
exportFileFormat = FileFormat::ASCII_STL;
21872170
} else if (fileFormat == "AMF") {
21882171
exportFileFormat = FileFormat::AMF;
21892172
} else if (fileFormat == "3MF") {
21902173
exportFileFormat = FileFormat::_3MF;
21912174
} else {
2192-
exportFileFormat = FileFormat::STL;
2175+
exportFileFormat = FileFormat::BINARY_STL;
21932176
}
21942177

21952178
QTemporaryFile exportFile{QDir::temp().filePath("OpenSCAD.XXXXXX." + fileFormat.toLower())};
@@ -2208,8 +2191,8 @@ void MainWindow::sendToOctoPrint()
22082191
userFileName = fileInfo.baseName() + "." + fileFormat.toLower();
22092192
}
22102193

2211-
ExportInfo exportInfo = createExportInfo(exportFileFormat, exportFileName, activeEditor->filepath);
2212-
exportFileByName(this->root_geom, exportInfo);
2194+
ExportInfo exportInfo = {.format = exportFileFormat, .sourceFilePath = activeEditor->filepath.toStdString()};
2195+
exportFileByName(this->root_geom, exportFileName.toStdString(), exportInfo);
22132196

22142197
try {
22152198
this->progresswidget = new ProgressWidget(this);
@@ -2239,25 +2222,13 @@ void MainWindow::sendToLocalSlicer()
22392222
#ifdef ENABLE_3D_PRINTING
22402223
const QString slicer = QString::fromStdString(Settings::Settings::localSlicerExecutable.value());
22412224

2242-
const QString fileFormat = QString::fromStdString(Settings::Settings::localSlicerFileFormat.value());
2243-
FileFormat exportFileFormat{FileFormat::STL};
2244-
if (fileFormat == "OBJ") {
2245-
exportFileFormat = FileFormat::OBJ;
2246-
} else if (fileFormat == "OFF") {
2247-
exportFileFormat = FileFormat::OFF;
2248-
} else if (fileFormat == "ASCIISTL") {
2249-
exportFileFormat = FileFormat::ASCIISTL;
2250-
} else if (fileFormat == "AMF") {
2251-
exportFileFormat = FileFormat::AMF;
2252-
} else if (fileFormat == "3MF") {
2253-
exportFileFormat = FileFormat::_3MF;
2254-
} else if (fileFormat == "POV") {
2255-
exportFileFormat = FileFormat::POV;
2256-
} else {
2257-
exportFileFormat = FileFormat::STL;
2225+
const QString fileFormat = QString::fromStdString(Settings::Settings::localSlicerFileFormat.value()).toLower();
2226+
FileFormat exportFileFormat = FileFormat::BINARY_STL;
2227+
if (!fileformat::fromIdentifier(fileFormat.toStdString(), exportFileFormat)) {
2228+
LOG("Invalid suffix %1$s. Defaulting to binary STL.", fileFormat.toStdString());
22582229
}
22592230

2260-
const auto tmpPath = QDir::temp().filePath("OpenSCAD.XXXXXX."+fileFormat.toLower());
2231+
const auto tmpPath = QDir::temp().filePath("OpenSCAD.XXXXXX."+fileFormat);
22612232
auto exportFile = std::make_unique<QTemporaryFile>(tmpPath);
22622233
if (!exportFile->open()) {
22632234
LOG(message_group::Error, "Could not open temporary file '%1$s'.", tmpPath.toStdString());
@@ -2272,11 +2243,11 @@ void MainWindow::sendToLocalSlicer()
22722243
userFileName = exportFileName;
22732244
} else {
22742245
QFileInfo fileInfo{activeEditor->filepath};
2275-
userFileName = fileInfo.baseName() + fileFormat.toLower();
2246+
userFileName = fileInfo.baseName() + fileFormat;
22762247
}
22772248

2278-
ExportInfo exportInfo = createExportInfo(exportFileFormat, exportFileName, activeEditor->filepath);
2279-
exportFileByName(this->root_geom, exportInfo);
2249+
ExportInfo exportInfo = {.format = exportFileFormat, .sourceFilePath = activeEditor->filepath.toStdString()};
2250+
exportFileByName(this->root_geom, exportFileName.toStdString(), exportInfo);
22802251

22812252
QProcess process(this);
22822253
process.setProcessChannelMode(QProcess::MergedChannels);
@@ -2310,8 +2281,8 @@ void MainWindow::sendToPrintService()
23102281
const QString exportFilename = exportFile.fileName();
23112282

23122283
//Render the stl to a temporary file:
2313-
ExportInfo exportInfo = createExportInfo(FileFormat::STL, exportFilename, activeEditor->filepath);
2314-
exportFileByName(this->root_geom, exportInfo);
2284+
ExportInfo exportInfo = {.format = FileFormat::BINARY_STL, .sourceFilePath = activeEditor->filepath.toStdString()};
2285+
exportFileByName(this->root_geom, exportFilename.toStdString(), exportInfo);
23152286

23162287
//Create a name that the order process will use to refer to the file. Base it off of the project name
23172288
QString userFacingName = "unsaved.stl";
@@ -2942,11 +2913,11 @@ void MainWindow::actionExport(FileFormat format, const char *type_name, const ch
29422913
}
29432914
this->export_paths[suffix] = exportFilename;
29442915

2945-
ExportInfo exportInfo = createExportInfo(format, exportFilename, activeEditor->filepath);
2916+
ExportInfo exportInfo = {.format = format, .sourceFilePath = activeEditor->filepath.toStdString()};
29462917
// Add options
29472918
exportInfo.options = options;
29482919

2949-
bool exportResult = exportFileByName(this->root_geom, exportInfo);
2920+
bool exportResult = exportFileByName(this->root_geom, exportFilename.toStdString(), exportInfo);
29502921

29512922
if (exportResult) fileExportedMessage(type_name, exportFilename);
29522923
clearCurrentOutput();
@@ -2955,9 +2926,9 @@ void MainWindow::actionExport(FileFormat format, const char *type_name, const ch
29552926
void MainWindow::actionExportSTL()
29562927
{
29572928
if (Settings::Settings::exportUseAsciiSTL.value()) {
2958-
actionExport(FileFormat::ASCIISTL, "ASCIISTL", ".stl", 3);
2929+
actionExport(FileFormat::ASCII_STL, "ASCIISTL", ".stl", 3);
29592930
} else {
2960-
actionExport(FileFormat::STL, "STL", ".stl", 3);
2931+
actionExport(FileFormat::BINARY_STL, "STL", ".stl", 3);
29612932
}
29622933
}
29632934

@@ -3016,7 +2987,7 @@ void MainWindow::actionExportPDF()
30162987
QString::fromStdString(paperSizeStrings[static_cast<int>(exportPdfOptions.paperSize)])).toString())); // enum map
30172988
exportPdfDialog->setOrientation(orientationsString2Enum(settings.value("exportPdfOpts/orientation",
30182989
QString::fromStdString(paperOrientationsStrings[static_cast<int>(exportPdfOptions.Orientation)])).toString())); // enum map
3019-
exportPdfDialog->setShowDsnFn(settings.value("exportPdfOpts/showDsgnFN", exportPdfOptions.showDsgnFN).toBool());
2990+
exportPdfDialog->setShowDesignFilename(settings.value("exportPdfOpts/showDsgnFN", exportPdfOptions.showDesignFilename).toBool());
30202991
exportPdfDialog->setShowScale(settings.value("exportPdfOpts/showScale", exportPdfOptions.showScale).toBool());
30212992
exportPdfDialog->setShowScaleMsg(settings.value("exportPdfOpts/showScaleMsg", exportPdfOptions.showScaleMsg).toBool());
30222993
exportPdfDialog->setShowGrid(settings.value("exportPdfOpts/showGrid", exportPdfOptions.showGrid).toBool());
@@ -3029,15 +3000,15 @@ void MainWindow::actionExportPDF()
30293000

30303001
exportPdfOptions.paperSize = exportPdfDialog->getPaperSize();
30313002
exportPdfOptions.Orientation = exportPdfDialog->getOrientation();
3032-
exportPdfOptions.showDsgnFN = exportPdfDialog->getShowDsnFn();
3003+
exportPdfOptions.showDesignFilename = exportPdfDialog->getShowDesignFilename();
30333004
exportPdfOptions.showScale = exportPdfDialog->getShowScale();
30343005
exportPdfOptions.showScaleMsg = exportPdfDialog->getShowScaleMsg();
30353006
exportPdfOptions.showGrid = exportPdfDialog->getShowGrid();
30363007
exportPdfOptions.gridSize = exportPdfDialog->getGridSize();
30373008

30383009
settings.setValue("exportPdfOpts/paperSize", QString::fromStdString(paperSizeStrings[static_cast<int>(exportPdfDialog->getPaperSize())]));
30393010
settings.setValue("exportPdfOpts/orientation", QString::fromStdString(paperOrientationsStrings[static_cast<int>(exportPdfDialog->getOrientation())]));
3040-
settings.setValue("exportPdfOpts/showDsgnFN", exportPdfDialog->getShowDsnFn());
3011+
settings.setValue("exportPdfOpts/showDsgnFN", exportPdfDialog->getShowDesignFilename());
30413012
settings.setValue("exportPdfOpts/showScale", exportPdfDialog->getShowScale());
30423013
settings.setValue("exportPdfOpts/showScaleMsg", exportPdfDialog->getShowScaleMsg());
30433014
settings.setValue("exportPdfOpts/showGrid", exportPdfDialog->getShowGrid());

src/io/export.cc

Lines changed: 88 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@
3333
#include <functional>
3434
#include <cassert>
3535
#include <map>
36-
#include <iostream>
3736
#include <cstdint>
3837
#include <memory>
3938
#include <cstddef>
4039
#include <fstream>
4140
#include <vector>
41+
#include <boost/filesystem/path.hpp>
42+
#include <boost/filesystem/fstream.hpp>
43+
#include <iostream>
4244

4345
#ifdef _WIN32
4446
#include <io.h>
@@ -48,6 +50,78 @@
4850
#define QUOTE(x__) # x__
4951
#define QUOTED(x__) QUOTE(x__)
5052

53+
namespace {
54+
55+
struct FileFormatInfo {
56+
FileFormat format;
57+
std::string identifier;
58+
std::string suffix;
59+
std::string description;
60+
};
61+
62+
std::unordered_map<std::string, FileFormatInfo> &identifierToInfo() {
63+
static auto identifierToInfo = std::make_unique<std::unordered_map<std::string, FileFormatInfo>>();
64+
return *identifierToInfo;
65+
}
66+
67+
std::unordered_map<FileFormat, FileFormatInfo> &fileFormatToInfo() {
68+
static auto fileFormatToInfo = std::make_unique<std::unordered_map<FileFormat, FileFormatInfo>>();
69+
return *fileFormatToInfo;
70+
}
71+
72+
void add_item(const FileFormatInfo& info) {
73+
74+
identifierToInfo()[info.identifier] = info;
75+
fileFormatToInfo()[info.format] = info;
76+
}
77+
78+
bool initialized = false;
79+
bool initialize() {
80+
add_item({FileFormat::ASCII_STL, "asciistl", "stl", "STL (ascii)"});
81+
add_item({FileFormat::BINARY_STL, "binstl", "stl", "STL (binary)"});
82+
add_item({FileFormat::OBJ, "obj", "obj", "OBJ"});
83+
add_item({FileFormat::OFF, "off", "off", "OFF"});
84+
add_item({FileFormat::WRL, "wrl", "wrl", "VRML"});
85+
add_item({FileFormat::AMF, "amf", "amf", "AMF"});
86+
add_item({FileFormat::_3MF, "3mf", "3mf", "3MF"});
87+
add_item({FileFormat::DXF, "dxf", "dxf", "DXF"});
88+
add_item({FileFormat::SVG, "svg", "svg", "SVG"});
89+
add_item({FileFormat::NEFDBG, "nefdbg", "nefdbg", "nefdbg"});
90+
add_item({FileFormat::NEF3, "nef3", "nef3", "nef3"});
91+
add_item({FileFormat::CSG, "csg", "csg", "CSG"});
92+
add_item({FileFormat::PARAM, "param", "param", "param"});
93+
add_item({FileFormat::AST, "ast", "ast", "AST"});
94+
add_item({FileFormat::TERM, "term", "term", "term"});
95+
add_item({FileFormat::ECHO, "echo", "echo", "echo"});
96+
add_item({FileFormat::PNG, "png", "png", "PNG"});
97+
add_item({FileFormat::PDF, "pdf", "pdf", "PDF"});
98+
add_item({FileFormat::POV, "pov", "pov", "POV"});
99+
100+
// Alias
101+
identifierToInfo()["stl"] = identifierToInfo()["asciistl"];
102+
103+
return true;
104+
}
105+
106+
} // namespace
107+
108+
namespace fileformat {
109+
110+
bool fromIdentifier(const std::string& suffix, FileFormat& format)
111+
{
112+
if (!initialized) initialized = initialize();
113+
auto it = identifierToInfo().find(suffix);
114+
if (it == identifierToInfo().end()) return false;
115+
format = it->second.format;
116+
return true;
117+
}
118+
119+
const std::string& toSuffix(FileFormat& format)
120+
{
121+
if (!initialized) initialized = initialize();
122+
return fileFormatToInfo()[format].suffix;
123+
}
124+
51125
bool canPreview(const FileFormat format) {
52126
return (format == FileFormat::AST ||
53127
format == FileFormat::CSG ||
@@ -58,8 +132,8 @@ bool canPreview(const FileFormat format) {
58132
}
59133

60134
bool is3D(const FileFormat format) {
61-
return format == FileFormat::ASCIISTL ||
62-
format == FileFormat::STL ||
135+
return format == FileFormat::ASCII_STL ||
136+
format == FileFormat::BINARY_STL ||
63137
format == FileFormat::OBJ ||
64138
format == FileFormat::OFF ||
65139
format == FileFormat::WRL ||
@@ -76,13 +150,15 @@ bool is2D(const FileFormat format) {
76150
format == FileFormat::PDF;
77151
}
78152

153+
} // namespace FileFormat
154+
79155
void exportFile(const std::shared_ptr<const Geometry>& root_geom, std::ostream& output, const ExportInfo& exportInfo)
80156
{
81157
switch (exportInfo.format) {
82-
case FileFormat::ASCIISTL:
158+
case FileFormat::ASCII_STL:
83159
export_stl(root_geom, output, false);
84160
break;
85-
case FileFormat::STL:
161+
case FileFormat::BINARY_STL:
86162
export_stl(root_geom, output, true);
87163
break;
88164
case FileFormat::OBJ:
@@ -125,7 +201,7 @@ void exportFile(const std::shared_ptr<const Geometry>& root_geom, std::ostream&
125201
}
126202
}
127203

128-
bool exportFileByNameStdout(const std::shared_ptr<const Geometry>& root_geom, const ExportInfo& exportInfo)
204+
bool exportFileStdOut(const std::shared_ptr<const Geometry>& root_geom, const ExportInfo& exportInfo)
129205
{
130206
#ifdef _WIN32
131207
_setmode(_fileno(stdout), _O_BINARY);
@@ -134,15 +210,16 @@ bool exportFileByNameStdout(const std::shared_ptr<const Geometry>& root_geom, co
134210
return true;
135211
}
136212

137-
bool exportFileByNameStream(const std::shared_ptr<const Geometry>& root_geom, const ExportInfo& exportInfo)
213+
bool exportFileByName(const std::shared_ptr<const Geometry>& root_geom, const std::string& filename, const ExportInfo& exportInfo)
138214
{
139215
std::ios::openmode mode = std::ios::out | std::ios::trunc;
140-
if (exportInfo.format == FileFormat::_3MF || exportInfo.format == FileFormat::STL || exportInfo.format == FileFormat::PDF) {
216+
if (exportInfo.format == FileFormat::_3MF || exportInfo.format == FileFormat::BINARY_STL || exportInfo.format == FileFormat::PDF) {
141217
mode |= std::ios::binary;
142218
}
143-
std::ofstream fstream(exportInfo.fileName, mode);
219+
const boost::filesystem::path path(filename);
220+
boost::filesystem::ofstream fstream(path, mode);
144221
if (!fstream.is_open()) {
145-
LOG(_("Can't open file \"%1$s\" for export"), exportInfo.displayName);
222+
LOG(_("Can't open file \"%1$s\" for export"), filename);
146223
return false;
147224
} else {
148225
bool onerror = false;
@@ -158,21 +235,12 @@ bool exportFileByNameStream(const std::shared_ptr<const Geometry>& root_geom, co
158235
onerror = true;
159236
}
160237
if (onerror) {
161-
LOG(message_group::Error, _("\"%1$s\" write error. (Disk full?)"), exportInfo.displayName);
238+
LOG(message_group::Error, _("\"%1$s\" write error. (Disk full?)"), filename);
162239
}
163240
return !onerror;
164241
}
165242
}
166243

167-
bool exportFileByName(const std::shared_ptr<const Geometry>& root_geom, const ExportInfo& exportInfo)
168-
{
169-
if (exportInfo.useStdOut) {
170-
return exportFileByNameStdout(root_geom, exportInfo);
171-
} else {
172-
return exportFileByNameStream(root_geom, exportInfo);
173-
}
174-
}
175-
176244
namespace {
177245

178246
double remove_negative_zero(double x) {

0 commit comments

Comments
 (0)