From 7095f825eb63b3f9ea5663a162ea6c69935d8304 Mon Sep 17 00:00:00 2001 From: kintel Date: Sun, 28 Feb 2010 20:54:55 +0000 Subject: [PATCH] Removed MainWindow::current_win - made this a tiny bit better, decoupled MainWindow from printutils git-svn-id: http://svn.clifford.at/openscad/trunk@467 b57f626f-c46c-0410-a088-ec61d464b74c --- src/MainWindow.h | 6 +++- src/export.cc | 4 +-- src/mainwin.cc | 80 ++++++++++++++++++++++++++--------------------- src/printutils.cc | 16 +++++++--- src/printutils.h | 6 ++++ 5 files changed, 68 insertions(+), 44 deletions(-) diff --git a/src/MainWindow.h b/src/MainWindow.h index 192a9b5a..7623789a 100644 --- a/src/MainWindow.h +++ b/src/MainWindow.h @@ -14,7 +14,6 @@ class MainWindow : public QMainWindow, public Ui::MainWindow Q_OBJECT public: - static QPointer current_win; static void requestOpenFile(const QString &filename); QString fileName; @@ -73,6 +72,9 @@ private: void compile(bool procevents); void compileCSG(bool procevents); bool maybeSave(); + static void consoleOutput(const QString &msg, void *userdata) { + static_cast(userdata)->console->append(msg); + } private slots: void actionNew(); @@ -111,6 +113,8 @@ private slots: public: void viewModeActionsUncheck(); + void setCurrentOutput(); + void clearCurrentOutput(); public slots: #ifdef ENABLE_OPENCSG diff --git a/src/export.cc b/src/export.cc index 72e63ea2..36059cd1 100644 --- a/src/export.cc +++ b/src/export.cc @@ -87,7 +87,7 @@ void export_stl(CGAL_Nef_polyhedron *root_N, QString filename, QProgressDialog * if (!f) { PRINTA("Can't open STL file \"%1\" for STL export: %2", filename, QString(strerror(errno))); - MainWindow::current_win = NULL; + set_output_handler(NULL, NULL); return; } fprintf(f, "solid OpenSCAD_Model\n"); @@ -155,7 +155,7 @@ void export_dxf(CGAL_Nef_polyhedron *root_N, QString filename, QProgressDialog * if (!f) { PRINTA("Can't open DXF file \"%1\" for DXF export: %2", filename, QString(strerror(errno))); - MainWindow::current_win = NULL; + set_output_handler(NULL, NULL); return; } diff --git a/src/mainwin.cc b/src/mainwin.cc index 5dee4a78..e66b861d 100644 --- a/src/mainwin.cc +++ b/src/mainwin.cc @@ -115,8 +115,6 @@ static char copyrighttext[] = "the Free Software Foundation; either version 2 of the License, or" "(at your option) any later version."; -QPointer MainWindow::current_win = NULL; - MainWindow::MainWindow(const QString &filename) { setupUi(this); @@ -293,7 +291,7 @@ MainWindow::MainWindow(const QString &filename) console->setReadOnly(true); - current_win = this; + setCurrentOutput(); PRINT(helptitle); PRINT(copyrighttext); @@ -333,7 +331,7 @@ MainWindow::MainWindow(const QString &filename) viewPerspective(); setAcceptDrops(true); - current_win = NULL; + clearCurrentOutput(); } MainWindow::~MainWindow() @@ -401,7 +399,7 @@ MainWindow::openFile(const QString &new_filename) #ifdef ENABLE_MDI if (!editor->toPlainText().isEmpty()) { new MainWindow(new_filename); - current_win = NULL; + clearCurrentOutput(); return; } #endif @@ -481,7 +479,7 @@ void MainWindow::updateTVal() void MainWindow::load() { - current_win = this; + setCurrentOutput(); if (!this->fileName.isEmpty()) { QFile file(this->fileName); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { @@ -493,7 +491,7 @@ void MainWindow::load() editor->setPlainText(text); } } - current_win = this; + setCurrentOutput(); } AbstractNode *MainWindow::find_root_tag(AbstractNode *n) @@ -784,10 +782,10 @@ void MainWindow::actionNew() void MainWindow::actionOpen() { - current_win = this; + setCurrentOutput(); QString new_filename = QFileDialog::getOpenFileName(this, "Open File", "", "OpenSCAD Designs (*.scad)"); if (!new_filename.isEmpty()) openFile(new_filename); - current_win = NULL; + clearCurrentOutput(); } void MainWindow::actionOpenRecent() @@ -856,7 +854,7 @@ void MainWindow::actionSave() actionSaveAs(); } else { - current_win = this; + setCurrentOutput(); QFile file(this->fileName); if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) { PRINTA("Failed to open file for writing: %1 (%2)", this->fileName, file.errorString()); @@ -866,7 +864,7 @@ void MainWindow::actionSave() PRINTA("Saved design `%1'.", this->fileName); this->editor->document()->setModified(false); } - current_win = NULL; + clearCurrentOutput(); } } @@ -1002,7 +1000,7 @@ void MainWindow::actionReloadCompile() load(); - current_win = this; + setCurrentOutput(); compile(true); if (this->root_node) compileCSG(true); @@ -1016,12 +1014,12 @@ void MainWindow::actionReloadCompile() { screen->updateGL(); } - current_win = NULL; + clearCurrentOutput(); } void MainWindow::actionCompile() { - current_win = this; + setCurrentOutput(); console->clear(); compile(!viewActionAnimate->isChecked()); @@ -1048,14 +1046,14 @@ void MainWindow::actionCompile() img.save(filename, "PNG"); } - current_win = NULL; + clearCurrentOutput(); } #ifdef ENABLE_CGAL void MainWindow::actionRenderCGAL() { - current_win = this; + setCurrentOutput(); console->clear(); compile(true); @@ -1163,14 +1161,14 @@ void MainWindow::actionRenderCGAL() this->statusBar()->removeWidget(pd); #endif delete pd; - current_win = NULL; + clearCurrentOutput(); } #endif /* ENABLE_CGAL */ void MainWindow::actionDisplayAST() { - current_win = this; + setCurrentOutput(); QTextEdit *e = new QTextEdit(this); e->setWindowFlags(Qt::Window); e->setTabStopWidth(30); @@ -1183,12 +1181,12 @@ void MainWindow::actionDisplayAST() } e->show(); e->resize(600, 400); - current_win = NULL; + clearCurrentOutput(); } void MainWindow::actionDisplayCSGTree() { - current_win = this; + setCurrentOutput(); QTextEdit *e = new QTextEdit(this); e->setWindowFlags(Qt::Window); e->setTabStopWidth(30); @@ -1201,12 +1199,12 @@ void MainWindow::actionDisplayCSGTree() } e->show(); e->resize(600, 400); - current_win = NULL; + clearCurrentOutput(); } void MainWindow::actionDisplayCSGProducts() { - current_win = this; + setCurrentOutput(); QTextEdit *e = new QTextEdit(this); e->setWindowFlags(Qt::Window); e->setTabStopWidth(30); @@ -1215,7 +1213,7 @@ void MainWindow::actionDisplayCSGProducts() e->setPlainText(QString("\nCSG before normalization:\n%1\n\n\nCSG after normalization:\n%2\n\n\nCSG rendering chain:\n%3\n\n\nHighlights CSG rendering chain:\n%4\n\n\nBackground CSG rendering chain:\n%5\n").arg(root_raw_term ? root_raw_term->dump() : "N/A", root_norm_term ? root_norm_term->dump() : "N/A", root_chain ? root_chain->dump() : "N/A", highlights_chain ? highlights_chain->dump() : "N/A", background_chain ? background_chain->dump() : "N/A")); e->show(); e->resize(600, 400); - current_win = NULL; + clearCurrentOutput(); } #ifdef ENABLE_CGAL @@ -1225,23 +1223,23 @@ void MainWindow::actionExportSTLorOFF(bool) #endif { #ifdef ENABLE_CGAL - current_win = this; + setCurrentOutput(); if (!this->root_N) { PRINT("Nothing to export! Try building first (press F6)."); - current_win = NULL; + clearCurrentOutput(); return; } if (this->root_N->dim != 3) { PRINT("Current top level object is not a 3D object."); - current_win = NULL; + clearCurrentOutput(); return; } if (!this->root_N->p3.is_simple()) { PRINT("Object isn't a valid 2-manifold! Modify your design.."); - current_win = NULL; + clearCurrentOutput(); return; } @@ -1250,7 +1248,7 @@ void MainWindow::actionExportSTLorOFF(bool) stl_mode ? "STL Files (*.stl)" : "OFF Files (*.off)"); if (stl_filename.isEmpty()) { PRINTF("No filename specified. %s export aborted.", stl_mode ? "STL" : "OFF"); - current_win = NULL; + clearCurrentOutput(); return; } @@ -1271,7 +1269,7 @@ void MainWindow::actionExportSTLorOFF(bool) delete pd; - current_win = NULL; + clearCurrentOutput(); #endif /* ENABLE_CGAL */ } @@ -1288,17 +1286,17 @@ void MainWindow::actionExportOFF() void MainWindow::actionExportDXF() { #ifdef ENABLE_CGAL - current_win = this; + setCurrentOutput(); if (!this->root_N) { PRINT("Nothing to export! Try building first (press F6)."); - current_win = NULL; + clearCurrentOutput(); return; } if (this->root_N->dim != 2) { PRINT("Current top level object is not a 2D object."); - current_win = NULL; + clearCurrentOutput(); return; } @@ -1306,14 +1304,14 @@ void MainWindow::actionExportDXF() "Export DXF File", "", "DXF Files (*.dxf)"); if (stl_filename.isEmpty()) { PRINTF("No filename specified. DXF export aborted."); - current_win = NULL; + clearCurrentOutput(); return; } export_dxf(this->root_N, stl_filename, NULL); PRINTF("DXF export finished."); - current_win = NULL; + clearCurrentOutput(); #endif /* ENABLE_CGAL */ } @@ -1757,14 +1755,14 @@ void MainWindow::dragEnterEvent(QDragEnterEvent *event) void MainWindow::dropEvent(QDropEvent *event) { - current_win = this; + setCurrentOutput(); const QList urls = event->mimeData()->urls(); for (int i = 0; i < urls.size(); i++) { if (urls[i].scheme() != "file") continue; openFile(urls[i].path()); } - current_win = NULL; + clearCurrentOutput(); } void @@ -1842,3 +1840,13 @@ void MainWindow::quit() QApplication::sendEvent(QApplication::instance(), &ev); if (ev.isAccepted()) QApplication::instance()->quit(); } + +void MainWindow::setCurrentOutput() +{ + set_output_handler(&MainWindow::consoleOutput, this); +} + +void MainWindow::clearCurrentOutput() +{ + set_output_handler(NULL, NULL); +} diff --git a/src/printutils.cc b/src/printutils.cc index a75cf142..3d909c48 100644 --- a/src/printutils.cc +++ b/src/printutils.cc @@ -1,7 +1,14 @@ #include "printutils.h" -#include "MainWindow.h" QList print_messages_stack; +OutputHandlerFunc *outputhandler = NULL; +void *outputhandler_data = NULL; + +void set_output_handler(OutputHandlerFunc *newhandler, void *userdata) +{ + outputhandler = newhandler; + outputhandler_data = userdata; +} void print_messages_push() { @@ -35,10 +42,9 @@ void PRINT_NOCACHE(const QString &msg) { if (msg.isNull()) return; - if (MainWindow::current_win.isNull()) { - fprintf(stderr, "%s\n", msg.toAscii().data()); + if (!outputhandler) { + fprintf(stderr, "%s\n", msg.toUtf8().data()); } else { - MainWindow::current_win->console->append(msg); + outputhandler(msg, outputhandler_data); } } - diff --git a/src/printutils.h b/src/printutils.h index 18cef93f..7f2e8285 100644 --- a/src/printutils.h +++ b/src/printutils.h @@ -4,6 +4,12 @@ #include #include +typedef void (OutputHandlerFunc)(const QString &msg, void *userdata); +extern OutputHandlerFunc *outputhandler; +extern void *outputhandler_data; + +void set_output_handler(OutputHandlerFunc *newhandler, void *userdata); + extern QList print_messages_stack; void print_messages_push(); void print_messages_pop();