#1607 Refactored export UI code to make 2D and 3D export use the same code path.

This commit is contained in:
Marius Kintel 2016-03-25 12:56:21 -04:00
parent e56648930d
commit 8308bd71ba
2 changed files with 19 additions and 83 deletions

View file

@ -11,18 +11,12 @@
#include "Tree.h"
#include "memory.h"
#include "editor.h"
#include "export.h"
#include <vector>
#include <QMutex>
#include <QTime>
#include <QIODevice>
enum export_type_e {
EXPORT_TYPE_UNKNOWN,
EXPORT_TYPE_STL,
EXPORT_TYPE_AMF,
EXPORT_TYPE_OFF
};
class MainWindow : public QMainWindow, public Ui::MainWindow
{
Q_OBJECT
@ -186,7 +180,7 @@ private slots:
void actionDisplayAST();
void actionDisplayCSGTree();
void actionDisplayCSGProducts();
void actionExport(export_type_e, const char *, const char *);
void actionExport(FileFormat format, const char *type_name, const char *suffix, unsigned int dim);
void actionExportSTL();
void actionExportOFF();
void actionExportAMF();

View file

@ -37,7 +37,6 @@
#include "polyset.h"
#include "csgnode.h"
#include "highlighter.h"
#include "export.h"
#include "builtin.h"
#include "memory.h"
#include "expression.h"
@ -2049,9 +2048,9 @@ void MainWindow::actionCheckValidity() {
}
#ifdef ENABLE_CGAL
void MainWindow::actionExport(export_type_e export_type, const char *type_name, const char *suffix)
void MainWindow::actionExport(FileFormat format, const char *type_name, const char *suffix, unsigned int dim)
#else
void MainWindow::actionExport(export_type_e, QString, QString)
void MainWindow::actionExport(FileFormat, QString, QString, unsigned int)
#endif
{
if (GuiLocker::isLocked()) return;
@ -2060,12 +2059,12 @@ void MainWindow::actionExport(export_type_e, QString, QString)
setCurrentOutput();
if (!this->root_geom) {
PRINT("WARNING: Nothing to export! Try building first (press F6).");
PRINT("ERROR: Nothing to export! Try rendering first (press F6).");
clearCurrentOutput();
return;
}
// editor has changed since last F6
// editor has changed since last render
if (this->contentschanged) {
QMessageBox::StandardButton ret;
ret = QMessageBox::warning(this, "Application",
@ -2077,14 +2076,14 @@ void MainWindow::actionExport(export_type_e, QString, QString)
}
}
if (this->root_geom->getDimension() != 3) {
PRINT("Current top level object is not a 3D object.");
if (this->root_geom->getDimension() != dim) {
PRINTB("ERROR: Current top level object is not a %dD object.", dim);
clearCurrentOutput();
return;
}
if (this->root_geom->isEmpty()) {
PRINT("Current top level object is empty.");
PRINT("ERROR: Current top level object is empty.");
clearCurrentOutput();
return;
}
@ -2103,15 +2102,6 @@ void MainWindow::actionExport(export_type_e, QString, QString)
return;
}
enum FileFormat format = (enum FileFormat)-1;
switch (export_type) {
case EXPORT_TYPE_STL: format = OPENSCAD_STL; break;
case EXPORT_TYPE_OFF: format = OPENSCAD_OFF; break;
case EXPORT_TYPE_AMF: format = OPENSCAD_AMF; break;
default:
assert(false && "Unknown export type");
break;
}
exportFileByName(this->root_geom, format,
export_filename.toLocal8Bit().constData(),
export_filename.toUtf8());
@ -2123,75 +2113,27 @@ void MainWindow::actionExport(export_type_e, QString, QString)
void MainWindow::actionExportSTL()
{
actionExport(EXPORT_TYPE_STL, "STL", ".stl");
actionExport(OPENSCAD_STL, "STL", ".stl", 3);
}
void MainWindow::actionExportOFF()
{
actionExport(EXPORT_TYPE_OFF, "OFF", ".off");
actionExport(OPENSCAD_OFF, "OFF", ".off", 3);
}
void MainWindow::actionExportAMF()
{
actionExport(EXPORT_TYPE_AMF, "AMF", ".amf");
}
QString MainWindow::get2dExportFilename(QString format, QString extension) {
setCurrentOutput();
if (!this->root_geom) {
PRINT("WARNING: Nothing to export! Try building first (press F6).");
clearCurrentOutput();
return QString();
}
if (this->root_geom->getDimension() != 2) {
PRINT("WARNING: Current top level object is not a 2D object.");
clearCurrentOutput();
return QString();
}
QString caption = QString(_("Export %1 File")).arg(format);
QString suggestion = this->fileName.isEmpty()
? QString(_("Untitled%1")).arg(extension)
: QFileInfo(this->fileName).baseName() + extension;
QString filter = QString(_("%1 Files (*%2)")).arg(format, extension);
QString exportFilename = QFileDialog::getSaveFileName(this, caption, suggestion, filter);
if (exportFilename.isEmpty()) {
PRINT("No filename specified. DXF export aborted.");
clearCurrentOutput();
return QString();
}
return exportFilename;
actionExport(OPENSCAD_AMF, "AMF", ".amf", 3);
}
void MainWindow::actionExportDXF()
{
#ifdef ENABLE_CGAL
QString dxf_filename = get2dExportFilename("DXF", ".dxf");
if (dxf_filename.isEmpty()) {
return;
}
exportFileByName(this->root_geom, OPENSCAD_DXF, dxf_filename.toUtf8(),
dxf_filename.toLocal8Bit().constData());
PRINT("DXF export finished.");
clearCurrentOutput();
#endif /* ENABLE_CGAL */
actionExport(OPENSCAD_DXF, "DXF", ".dxf", 2);
}
void MainWindow::actionExportSVG()
{
QString svg_filename = get2dExportFilename("SVG", ".svg");
if (svg_filename.isEmpty()) {
return;
}
exportFileByName(this->root_geom, OPENSCAD_SVG, svg_filename.toUtf8(),
svg_filename.toLocal8Bit().constData());
PRINT("SVG export finished.");
clearCurrentOutput();
actionExport(OPENSCAD_SVG, "SVG", ".svg", 2);
}
void MainWindow::actionExportCSG()
@ -2199,7 +2141,7 @@ void MainWindow::actionExportCSG()
setCurrentOutput();
if (!this->root_node) {
PRINT("WARNING: Nothing to export. Please try compiling first...");
PRINT("ERROR: Nothing to export. Please try compiling first.");
clearCurrentOutput();
return;
}
@ -2209,7 +2151,6 @@ void MainWindow::actionExportCSG()
_("CSG Files (*.csg)"));
if (csg_filename.isEmpty()) {
PRINT("No filename specified. CSG export aborted.");
clearCurrentOutput();
return;
}
@ -2237,10 +2178,11 @@ void MainWindow::actionExportImage()
QString img_filename = QFileDialog::getSaveFileName(this,
_("Export Image"), filename, _("PNG Files (*.png)"));
if (img_filename.isEmpty()) {
PRINT("No filename specified. Image export aborted.");
} else {
qglview->save(img_filename.toLocal8Bit().constData());
clearCurrentOutput();
return;
}
qglview->save(img_filename.toLocal8Bit().constData());
clearCurrentOutput();
return;
}