1 #pragma once
2 
3 #include "qtgettext.h"
4 #include <QMainWindow>
5 #include <QIcon>
6 #include "ui_MainWindow.h"
7 #include "UIUtils.h"
8 #include "openscad.h"
9 #include "builtincontext.h"
10 #include "module.h"
11 #include "ModuleInstantiation.h"
12 #include "Tree.h"
13 #include "memory.h"
14 #include "editor.h"
15 #include "export.h"
16 #include <vector>
17 #include <QMutex>
18 #include <QElapsedTimer>
19 #include <QTime>
20 #include <QIODevice>
21 #include "input/InputDriver.h"
22 #include "editor.h"
23 #include "tabmanager.h"
24 #include <memory>
25 
26 class MouseSelector;
27 
28 class MainWindow : public QMainWindow, public Ui::MainWindow, public InputEventHandler
29 {
30 	Q_OBJECT
31 
32 public:
33 	class Preferences *prefs;
34 
35 	QTimer *consoleUpdater;
36 	QTimer *animate_timer;
37 	int anim_step;
38 	int anim_numsteps;
39 	double anim_tval;
40 	bool anim_dumping;
41 	int anim_dump_start_step;
42 
43 	QTimer *autoReloadTimer;
44 	QTimer *waitAfterReloadTimer;
45 	QTime renderingTime;
46 	EditorInterface *customizerEditor;
47 
48 	ContextHandle<BuiltinContext> top_ctx;
49 	FileModule *root_module;      // Result of parsing
50 	FileModule *parsed_module;		// Last parse for include list
51 	ModuleInstantiation root_inst;	// Top level instance
52 	AbstractNode *absolute_root_node; // Result of tree evaluation
53 	AbstractNode *root_node;		  // Root if the root modifier (!) is used
54 	Tree tree;
55 	EditorInterface *activeEditor;
56 	TabManager *tabManager;
57 
58 #ifdef ENABLE_CGAL
59 	shared_ptr<const class Geometry> root_geom;
60 	class CGALRenderer *cgalRenderer;
61 #endif
62 #ifdef ENABLE_OPENCSG
63 	class OpenCSGRenderer *opencsgRenderer;
64 	std::unique_ptr<MouseSelector> selector;
65 #endif
66 	class ThrownTogetherRenderer *thrownTogetherRenderer;
67 
68 	QString last_compiled_doc;
69 
70 	QAction *actionRecentFile[UIUtils::maxRecentFiles];
71 	QMap<QString, QString> knownFileExtensions;
72 
73 	QLabel *versionLabel;
74 	QWidget *editorDockTitleWidget;
75 	QWidget *consoleDockTitleWidget;
76 	QWidget *parameterDockTitleWidget;
77 	QWidget *errorLogDockTitleWidget;
78 
79 	int compileErrors;
80 	int compileWarnings;
81 
82 	MainWindow(const QStringList &filenames);
83 	~MainWindow();
84 
85 private:
86 	void consoleOutputRaw(const QString& msg);
87 
88 protected:
89 	void closeEvent(QCloseEvent *event) override;
90 
91 private slots:
92 	void setTabToolBarVisible(int);
93 	void updatedAnimTval();
94 	void updatedAnimFps();
95 	void updatedAnimSteps();
96 	void updatedAnimDump(bool checked);
97 	void updateTVal();
98 	void updateUndockMode(bool undockMode);
99 	void updateReorderMode(bool reorderMode);
100 	void setFont(const QString &family, uint size);
101 	void setColorScheme(const QString &cs);
102 	void showProgress();
103 	void openCSGSettingsChanged();
104 	void consoleOutput(const Message& msgObj);
105 	void setCursor();
106 	void errorLogOutput(const Message &log_msg);
107 
108 public:
109 	static void consoleOutput(const Message &msgObj, void *userdata);
110 	static void errorLogOutput(const Message &log_msg, void *userdata);
noOutputConsole(const Message &,void *)111 	static void noOutputConsole(const Message &, void*) {};  // /dev/null
noOutputErrorLog(const Message &,void *)112 	static void noOutputErrorLog(const Message &, void*) {};  // /dev/null
113 
114 	bool fileChangedOnDisk();
115 	void parseTopLevelDocument(bool rebuildParameterWidget);
116 	void exceptionCleanup();
117 
118 private:
119 	void initActionIcon(QAction *action, const char *darkResource, const char *lightResource);
120 	void updateTemporalVariables();
121 	void updateCompileResult();
122 	void compile(bool reload, bool forcedone = false, bool rebuildParameterWidget=true);
123 	void compileCSG();
124 	bool checkEditorModified();
125 	QString dumpCSGTree(AbstractNode *root);
126 
127 	void loadViewSettings();
128 	void loadDesignSettings();
129 	void prepareCompile(const char *afterCompileSlot, bool procevents, bool preview);
130     void updateWindowSettings(bool console, bool editor, bool customizer, bool errorLog, bool editorToolbar, bool viewToolbar);
131 	void saveBackup();
132 	void writeBackup(class QFile *file);
133 	void show_examples();
134 	void setDockWidgetTitle(QDockWidget *dockWidget, QString prefix, bool topLevel);
135 	void addKeyboardShortCut(const QList<QAction *> &actions);
136 	void updateStatusBar(class ProgressWidget *progressWidget);
137 	void activateWindow(int);
138 
139   class LibraryInfoDialog* library_info_dialog;
140   class FontListDialog *font_list_dialog;
141 
142 public slots:
143 	void updateRecentFiles(EditorInterface *edt);
144 	void updateRecentFileActions();
145 	void handleFileDrop(const QUrl& url);
146 
147 private slots:
148 	void actionOpen();
149 	void actionNewWindow();
150 	void actionOpenWindow();
151 	void actionOpenRecent();
152 	void actionOpenExample();
153 	void clearRecentFiles();
154 	void actionSave();
155 	void actionSaveAs();
156 	void actionReload();
157 	void actionShowLibraryFolder();
158 	void convertTabsToSpaces();
159 
160 	void instantiateRoot();
161 	void compileDone(bool didchange);
162 	void compileEnded();
163 	void changeParameterWidget();
164 
165 private slots:
166 	void copyViewportTranslation();
167 	void copyViewportRotation();
168 	void copyViewportDistance();
169 	void copyViewportFov();
170 	void preferences();
171     void hideEditorToolbar();
172     void hide3DViewToolbar();
173 	void showLink(const QString);
174 	void showEditor();
175 	void hideEditor();
176 	void showConsole();
177 	void hideConsole();
178 	void showErrorLog();
179 	void hideErrorLog();
180 	void showParameters();
181 	void hideParameters();
182 	void on_windowActionSelectEditor_triggered();
183 	void on_windowActionSelectConsole_triggered();
184 	void on_windowActionSelectCustomizer_triggered();
185 	void on_windowActionSelectErrorLog_triggered();
186 	void on_windowActionNextWindow_triggered();
187 	void on_windowActionPreviousWindow_triggered();
188 	void on_editActionInsertTemplate_triggered();
189 
190 public slots:
191 	void hideFind();
192 	void showFind();
193 	void showFindAndReplace();
194 
195 private slots:
196 	void selectFindType(int);
197 	void findString(QString);
198 	void findNext();
199 	void findPrev();
200 	void useSelectionForFind();
201 	void replace();
202 	void replaceAll();
203 
204 	// Mac OSX FindBuffer support
205 	void findBufferChanged();
206 	void updateFindBuffer(QString);
207 	bool event(QEvent* event) override;
208 protected:
209 	bool eventFilter(QObject* obj, QEvent *event) override;
210 
211 private slots:
212 	void actionRenderPreview(bool rebuildParameterWidget=true);
213 	void csgRender();
214 	void csgReloadRender();
215 	void action3DPrint();
216 	void sendToOctoPrint();
217 	void sendToPrintService();
218 #ifdef ENABLE_CGAL
219 	void actionRender();
220 	void actionRenderDone(shared_ptr<const class Geometry>);
221 	void cgalRender();
222 #endif
223 	void actionCheckValidity();
224 	void actionDisplayAST();
225 	void actionDisplayCSGTree();
226 	void actionDisplayCSGProducts();
227 	bool canExport(unsigned int dim);
228 	void actionExport(FileFormat format, const char *type_name, const char *suffix, unsigned int dim);
229 	void actionExportSTL();
230 	void actionExport3MF();
231 	void actionExportOFF();
232 	void actionExportAMF();
233 	void actionExportDXF();
234 	void actionExportSVG();
235     void actionExportPDF();
236 	void actionExportCSG();
237 	void actionExportImage();
238 	void actionCopyViewport();
239 	void actionFlushCaches();
240 
241 public:
242 	void viewModeActionsUncheck();
243 	void setCurrentOutput();
244 	void clearCurrentOutput();
245 	void hideCurrentOutput();
246   bool isEmpty();
247 
248 	void onAxisChanged(InputEventAxisChanged *event) override;
249 	void onButtonChanged(InputEventButtonChanged *event) override;
250 
251 	void onTranslateEvent(InputEventTranslate *event) override;
252 	void onRotateEvent(InputEventRotate *event) override;
253 	void onRotate2Event(InputEventRotate2 *event) override;
254 	void onActionEvent(InputEventAction *event) override;
255 	void onZoomEvent(InputEventZoom *event) override;
256 
257 	void changedTopLevelConsole(bool);
258 	void changedTopLevelEditor(bool);
259 	void changedTopLevelErrorLog(bool);
260 
261 	QList<double> getTranslation() const;
262 	QList<double> getRotation() const;
263 
264 public slots:
265 	void actionReloadRenderPreview();
266 	void on_editorDock_visibilityChanged(bool);
267 	void on_consoleDock_visibilityChanged(bool);
268 	void on_parameterDock_visibilityChanged(bool);
269 	void on_errorLogDock_visibilityChanged(bool);
270 	void on_toolButtonCompileResultClose_clicked();
271 	void editorTopLevelChanged(bool);
272 	void consoleTopLevelChanged(bool);
273 	void parameterTopLevelChanged(bool);
274 	void errorLogTopLevelChanged(bool);
275 	void processEvents();
276 	void jumpToLine(int,int);
277 	void openFileFromPath(QString,int);
278 
279 #ifdef ENABLE_OPENCSG
280 	void viewModePreview();
281 #endif
282 #ifdef ENABLE_CGAL
283 	void viewModeSurface();
284 	void viewModeWireframe();
285 #endif
286 	void viewModeThrownTogether();
287 	void viewModeShowEdges();
288 	void viewModeShowAxes();
289 	void viewModeShowCrosshairs();
290 	void viewModeShowScaleProportional();
291 	void viewModeAnimate();
292 	void viewAngleTop();
293 	void viewAngleBottom();
294 	void viewAngleLeft();
295 	void viewAngleRight();
296 	void viewAngleFront();
297 	void viewAngleBack();
298 	void viewAngleDiagonal();
299 	void viewCenter();
300 	void viewPerspective();
301 	void viewOrthogonal();
302 	void viewTogglePerspective();
303 	void viewResetView();
304 	void viewAll();
305 	void animateUpdateDocChanged();
306 	void animateUpdate();
307 	void selectObject(QPoint coordinate);
308 	void dragEnterEvent(QDragEnterEvent *event) override;
309 	void dropEvent(QDropEvent *event) override;
310 	void helpAbout();
311 	void helpHomepage();
312 	void helpManual();
313 	void helpCheatSheet();
314 	void helpLibrary();
315 	void helpFontInfo();
316 	void quit();
317 	void checkAutoReload();
318 	void waitAfterReload();
319 	void autoReloadSet(bool);
320 
321 private:
322 	bool network_progress_func(const double permille);
323 	static void report_func(const class AbstractNode*, void *vp, int mark);
324 	static bool undockMode;
325 	static bool reorderMode;
326 	static const int tabStopWidth;
327 	static QElapsedTimer *progressThrottle;
328 
329 	shared_ptr<class CSGNode> csgRoot;		   // Result of the CSGTreeEvaluator
330 	shared_ptr<CSGNode> normalizedRoot;		  // Normalized CSG tree
331  	shared_ptr<class CSGProducts> root_products;
332 	shared_ptr<CSGProducts> highlights_products;
333 	shared_ptr<CSGProducts> background_products;
334 
335 	char const * afterCompileSlot;
336 	bool procevents;
337 	class QTemporaryFile *tempFile;
338 	class ProgressWidget *progresswidget;
339 	class CGALWorker *cgalworker;
340 	QMutex consolemutex;
341 	EditorInterface *renderedEditor; // stores pointer to editor which has been most recently rendered
342 	time_t includes_mtime;   // latest include mod time
343 	time_t deps_mtime;	  // latest dependency mod time
344 	std::unordered_map<std::string, QString> export_paths; // for each file type, where it was exported to last
345 	QString exportPath(const char *suffix); // look up the last export path and generate one if not found
346 	int last_parser_error_pos; // last highlighted error position
347 	int tabCount = 0;
348 
349 signals:
350 	void highlightError(int);
351 	void unhighlightLastError();
352 };
353 
354 class GuiLocker
355 {
356 public:
GuiLocker()357 	GuiLocker() {
358 		GuiLocker::lock();
359 	}
~GuiLocker()360 	~GuiLocker() {
361 		GuiLocker::unlock();
362 	}
isLocked()363 	static bool isLocked() { return gui_locked > 0; }
lock()364 	static void lock() {
365 		gui_locked++;
366 	}
unlock()367 	static void unlock() {
368 		gui_locked--;
369 	}
370 
371 private:
372  	static unsigned int gui_locked;
373 };
374