diff --git a/tikz_editor/controllers/app.py b/tikz_editor/controllers/app.py index e6af06c..1e33732 100644 --- a/tikz_editor/controllers/app.py +++ b/tikz_editor/controllers/app.py @@ -62,6 +62,7 @@ def _createActions(self): self.actions[actions.SAVE] = ActionFactory.createAction(self, "Save", "Save the document", QKeySequence.Save, self.save) self.actions[actions.SAVE_ALL] = ActionFactory.createAction(self, "Save All", "Save all documents", None, self.saveAll) self.actions[actions.SAVE_AS] = ActionFactory.createAction(self, "Save As...", "Save the document as...", QKeySequence.SaveAs, self.saveAs) + self.actions[actions.EXPORT] = ActionFactory.createAction(self, "Export", "Export document as PDF", None, self.export) self.actions[actions.UNDO] = ActionFactory.createAction(self, "Undo", "Undo last action", QKeySequence.Undo, self.undo) def _createMenuBar(self): @@ -84,6 +85,7 @@ def _createFileMenu(self): self.actions[actions.SAVE], self.actions[actions.SAVE_AS], self.actions[actions.SAVE_ALL], + self.actions[actions.EXPORT], None, self.actions[actions.PREVIEW], None, @@ -274,6 +276,15 @@ def saveAs(self): except Exception as e: Dialogs.showError(e) + def export(self): + try: + if self.documentHasFocus(): + self.focused_document.export() + else: + QApplication.beep() + except Exception as e: + Dialogs.showError(e) + def undo(self): self._doActionOnFocusedWidget("undo") diff --git a/tikz_editor/controllers/document.py b/tikz_editor/controllers/document.py index 617c078..9b94657 100644 --- a/tikz_editor/controllers/document.py +++ b/tikz_editor/controllers/document.py @@ -16,6 +16,8 @@ from PyQt5.QtCore import * from tikz_editor.tools import File +from tikz_editor.tools import TemporaryDirectory +from tikz_editor.tools.latex2image import Converter class DocumentController(QObject): @@ -84,6 +86,11 @@ def saveAs(self): self.model.file_path = file_path self.model.save() + def export(self): + file_path = File.showSaveFileDialog(self.view, "%s.pdf" % self.model.title) + if file_path: + self.model.export(file_path, self.preview_controller.latex2image_converter) + def _getDocumentFileName(self): file_name = self.model.file_path if not file_name: diff --git a/tikz_editor/globals/actions.py b/tikz_editor/globals/actions.py index 2ef05c1..cc3c3b8 100644 --- a/tikz_editor/globals/actions.py +++ b/tikz_editor/globals/actions.py @@ -31,6 +31,7 @@ SAVE = "save" SAVE_ALL = "saveAll" SAVE_AS = "saveAs" +EXPORT = "export" SHOW_ERRORS = "showErrors" SHOW_LOGS = "showLogs" SHOW_PREAMBLE = "showPreamble" diff --git a/tikz_editor/models/document.py b/tikz_editor/models/document.py index 177907b..21e869d 100644 --- a/tikz_editor/models/document.py +++ b/tikz_editor/models/document.py @@ -13,6 +13,8 @@ # You should have received a copy of the GNU General Public License along # with this program. If not, see . +import shutil + from PyQt5.QtCore import * from PyQt5.QtGui import * @@ -76,6 +78,20 @@ def save(self): except Exception as e: raise DocumentError("The document cannot be saved: %s" % str(e)) + def export(self, file_path, converter): + """ + To export the document as PDF, the source is converted using the + latex2image converter. + """ + try: + template = Preferences.getPreviewTemplate() + source = documentIO.buildFileContentFromDocument(template, self) + converter.convertLatexToImage(source) + converter.waitForFinished() + shutil.copyfile(converter.pdf_file_path, file_path) + except Exception as e: + raise DocumentError("The document cannot be exported: %s" % str(e)) + @property def file_path(self): return self._file_path diff --git a/tikz_editor/tools/latex2image/converter.py b/tikz_editor/tools/latex2image/converter.py index 091f6dc..2776e06 100644 --- a/tikz_editor/tools/latex2image/converter.py +++ b/tikz_editor/tools/latex2image/converter.py @@ -76,6 +76,10 @@ def _initProcesses(self): self._convert_process.error.connect(self._pdfToImageConversionError) self._convert_process.finished.connect(self._pdfToImageConversionFinished) + def waitForFinished(self): + self._latex_process.waitForFinished() + self._convert_process.waitForFinished() + def stopConversion(self): self._stopping_conversion = True self._killProcesses()