From 42df7c9d815e79c4fe30676f03cec15b8202f2cd Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Tue, 31 Aug 2021 14:32:51 -0700 Subject: [PATCH] Add exporting to HTML --- src/ukatali/mainwindow.py | 59 +++++++++++++++++++++++++++++++-------- 1 file changed, 48 insertions(+), 11 deletions(-) diff --git a/src/ukatali/mainwindow.py b/src/ukatali/mainwindow.py index 22e0c05..ecd479f 100644 --- a/src/ukatali/mainwindow.py +++ b/src/ukatali/mainwindow.py @@ -96,10 +96,14 @@ class MainWindow(QtWidgets.QMainWindow): icon = QtGui.QIcon.fromTheme('document-print') self.print_action.setIcon(icon) self.print_action.setObjectName('print_action') - self.export_action = QtWidgets.QAction(self) - icon = QtGui.QIcon.fromTheme('document-export') - self.export_action.setIcon(icon) - self.export_action.setObjectName('export_action') + self.export_pdf_action = QtWidgets.QAction(self) + icon = QtGui.QIcon.fromTheme('application-pdf') + self.export_pdf_action.setIcon(icon) + self.export_pdf_action.setObjectName('export_pdf_action') + self.export_html_action = QtWidgets.QAction(self) + icon = QtGui.QIcon.fromTheme('text-html') + self.export_html_action.setIcon(icon) + self.export_html_action.setObjectName('export_html_action') self.undo_action = QtWidgets.QAction(self) icon = QtGui.QIcon.fromTheme('edit-undo') self.undo_action.setIcon(icon) @@ -137,7 +141,10 @@ class MainWindow(QtWidgets.QMainWindow): self.file_menu.addAction(self.save_action) self.file_menu.addAction(self.save_as_action) self.file_menu.addSeparator() - self.file_menu.addAction(self.export_action) + self.export_menu = self.file_menu.addMenu('') + self.export_menu.setObjectName('export_menu') + self.export_menu.addAction(self.export_pdf_action) + self.export_menu.addAction(self.export_html_action) self.file_menu.addAction(self.print_action) self.file_menu.addSeparator() self.file_menu.addAction(self.exit_action) @@ -157,7 +164,7 @@ class MainWindow(QtWidgets.QMainWindow): self.toolbar.addAction(self.open_action) self.toolbar.addAction(self.save_action) self.toolbar.addSeparator() - self.toolbar.addAction(self.export_action) + self.toolbar.addAction(self.export_pdf_action) self.toolbar.addAction(self.print_action) self.toolbar.addSeparator() self.toolbar.addAction(self.undo_action) @@ -179,7 +186,8 @@ class MainWindow(QtWidgets.QMainWindow): self.open_action.triggered.connect(self.on_open_clicked) self.save_action.triggered.connect(self.on_save_clicked) self.save_as_action.triggered.connect(self.on_save_as_clicked) - self.export_action.triggered.connect(self.on_export_clicked) + self.export_pdf_action.triggered.connect(self.on_export_pdf_clicked) + self.export_html_action.triggered.connect(self.on_export_html_clicked) self.print_action.triggered.connect(self.on_print_clicked) self.configure_action.triggered.connect(self.on_configure_clicked) self.file_editor.textChanged.connect(self.on_text_changed) @@ -209,9 +217,12 @@ class MainWindow(QtWidgets.QMainWindow): self.print_action.setText(_translate('MainWindow', '&Print')) self.print_action.setToolTip(_translate('MainWindow', 'Print the current ChordPro file')) self.print_action.setShortcut(_translate('MainWindow', 'Ctrl+P')) - self.export_action.setText(_translate('MainWindow', '&Export...')) - self.export_action.setToolTip(_translate('MainWindow', 'Export the current file as a different format')) - self.export_action.setShortcut(_translate('MainWindow', 'Ctrl+E')) + self.export_menu.setTitle(_translate('MainWindow', '&Export')) + self.export_pdf_action.setText(_translate('MainWindow', 'Export to &PDF...')) + self.export_pdf_action.setToolTip(_translate('MainWindow', 'Export the current file as a PDF file')) + self.export_pdf_action.setShortcut(_translate('MainWindow', 'Ctrl+E')) + self.export_html_action.setText(_translate('MainWindow', 'Export to &HTML...')) + self.export_html_action.setToolTip(_translate('MainWindow', 'Export the current file as an HTML file')) self.undo_action.setText(_translate('MainWindow', '&Undo')) self.undo_action.setToolTip(_translate('MainWindow', 'Undo the last edit')) self.undo_action.setShortcut(_translate('MainWindow', 'Ctrl+Z')) @@ -316,7 +327,7 @@ class MainWindow(QtWidgets.QMainWindow): html = self._render_song() self.preview_view.setHtml(html) - def on_export_clicked(self): + def on_export_pdf_clicked(self): """Export the current song to PDF""" if self.filename: last_directory = str(Path(self.filename).with_suffix('.pdf')) @@ -347,6 +358,32 @@ class MainWindow(QtWidgets.QMainWindow): # Export to PDF self.preview_view.page().printToPdf(str(filename), page_layout) + def on_export_html_clicked(self): + """Export the current song to HTML""" + if self.filename: + last_directory = str(Path(self.filename).with_suffix('.html')) + elif self.settings.value('files/last-directory'): + last_directory = self.settings.value('files/last-directory') + else: + last_directory = '' + filename = QtWidgets.QFileDialog.getSaveFileName(self, 'Export to HTML', last_directory, + filter='HTML files (.html)*') + if isinstance(filename, tuple): + filename = filename[0] + if not filename: + return + filename = Path(filename) + self.settings.setValue('files/last-directory', str(filename.parent)) + # Export to HTML + try: + with filename.open('w') as html_file: + html_file.write(self._render_song()) + QtWidgets.QMessageBox.information(self, 'Export to HTML Successful', + 'Successfully exported to "{}"'.format(filename.name)) + except Exception as e: + QtWidgets.QMessageBox.critical(self, 'Error Exporting to HTML', + 'There was an error while exporting to HTML:\n{e}'.format(e=e)) + def on_pdf_finished(self, filename, is_success): """A slot to notify the user when the PDF is done""" if is_success: