Add exporting to PDF and printing

This commit is contained in:
Raoul Snyman 2021-08-07 01:06:50 -07:00
parent ec5101e23d
commit cb3d15d2dd
3 changed files with 63 additions and 11 deletions

View File

@ -145,7 +145,7 @@ class ConfigureDialog(QtWidgets.QDialog):
value = self.settings.value('font')
self.editor_values['font'] = value
self.editor_font_combobox.setCurrentFont(QtGui.QFont(value))
if self.settings.value.contains('size'):
if self.settings.contains('size'):
value = self.settings.value('size')
self.editor_values['size'] = int(value)
self.editor_size.setValue(int(value))

View File

@ -127,7 +127,7 @@ class ChordProLexer(Qsci.QsciLexerCustom):
def styleText(self, start, end):
"""Style the text"""
keywords = ['chorus'] + \
keywords = ['verse', 'chorus', 'bridge'] + \
[d for t in KNOWN_DIRECTIVES for d in t] + \
['start_of_' + vt for vt in KNOWN_VERSE_TYPES] + \
['end_of_' + vt for vt in KNOWN_VERSE_TYPES]

View File

@ -1,12 +1,14 @@
# -*- coding: utf-8 -*-
from pathlib import Path
from PyQt5 import QtCore, QtGui, QtWidgets, QtWebEngineWidgets, Qsci
from PyQt5 import QtCore, QtGui, QtWidgets, QtWebEngineWidgets, Qsci, QtPrintSupport
from chordpro import Song
from chordpro.renderers.html import render
from ukatali.configuredialog import ConfigureDialog
from ukatali.lexer import ChordProLexer
CHORDPRO_FILTERS = 'ChordPro files (*.txt *.cho *.chordpro *.chopro);;All files (*)'
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
@ -173,8 +175,11 @@ 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.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)
self.preview_view.page().pdfPrintingFinished.connect(self.on_pdf_finished)
def retranslate_ui(self):
_translate = QtCore.QCoreApplication.translate
@ -232,8 +237,7 @@ class MainWindow(QtWidgets.QMainWindow):
last_directory = QtCore.QSettings().value('files/last-directory')
else:
last_directory = ''
filename = QtWidgets.QFileDialog.getOpenFileName(self, 'Open file', last_directory,
filter='All files *')
filename = QtWidgets.QFileDialog.getOpenFileName(self, 'Open file', last_directory, filter=CHORDPRO_FILTERS)
if isinstance(filename, tuple):
filename = filename[0]
if not filename:
@ -259,8 +263,7 @@ class MainWindow(QtWidgets.QMainWindow):
last_directory = QtCore.QSettings().value('files/last-directory')
else:
last_directory = ''
filename = QtWidgets.QFileDialog.getSaveFileName(self, 'Save file', last_directory,
filter='All files *')
filename = QtWidgets.QFileDialog.getSaveFileName(self, 'Save file', last_directory, filter=CHORDPRO_FILTERS)
if isinstance(filename, tuple):
filename = filename[0]
if not filename:
@ -281,14 +284,63 @@ class MainWindow(QtWidgets.QMainWindow):
settings = QtCore.QSettings()
settings.beginGroup('render')
for key in settings.allKeys():
options[key] = settings.value(key)
value = settings.value(key)
if value:
options[key] = value
settings.endGroup()
return options
def on_text_changed(self):
"""Update the preview when the text changes"""
def _render_song(self, extra_styles=None):
"""Render the current song and return the HTML"""
options = self._get_render_options()
text = self.file_editor.text()
song = Song()
song.parse(text)
html = render(song, options)
return render(song, options, extra_styles)
def on_text_changed(self):
"""Update the preview when the text changes"""
html = self._render_song()
self.preview_view.setHtml(html)
def on_export_clicked(self):
"""Export the current song to PDF"""
if self.filename:
last_directory = str(Path(self.filename).with_suffix('.pdf'))
elif QtCore.QSettings().value('files/last-directory'):
last_directory = QtCore.QSettings().value('files/last-directory')
else:
last_directory = ''
filename = QtWidgets.QFileDialog.getSaveFileName(self, 'Export to PDF', last_directory,
filter='PDF files (.pdf)*')
if isinstance(filename, tuple):
filename = filename[0]
if not filename:
return
filename = Path(filename)
QtCore.QSettings().setValue('files/last-directory', str(filename.parent))
self.preview_view.page().printToPdf(str(filename), QtGui.QPageLayout(QtGui.QPageSize(QtGui.QPageSize.Letter),
QtGui.QPageLayout.Portrait,
QtCore.QMarginsF()))
def on_pdf_finished(self, filename, is_success):
"""A slot to notify the user when the PDF is done"""
if is_success:
QtWidgets.QMessageBox.information(self, 'Export to PDF Successful',
'Successfully exported to "{}"'.format(Path(filename).name))
else:
QtWidgets.QMessageBox.critical(self, 'Error Exporting to PDF', 'There was an error while exporting to PDF')
def on_print_clicked(self):
"""Print the current song"""
def _on_print_finished(is_success):
print(is_success)
# print(QtPrintSupport.QPrinterInfo.defaultPrinterName())
# default_printer = QtPrintSupport.QPrinterInfo.defaultPrinter()
# print(default_printer)
# printer = QtPrintSupport.QPrinter()
print_dialog = QtPrintSupport.QPrintDialog(QtPrintSupport.QPrinter(), self)
if print_dialog.exec() == QtWidgets.QDialog.Accepted:
self.preview_view.page().print(print_dialog.printer(), _on_print_finished)