|
|
|
@ -1,10 +1,9 @@
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
import os
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
from PyQt5 import QtCore, QtGui, QtWidgets, QtWebEngineWidgets, Qsci, QtPrintSupport
|
|
|
|
|
from chordpro import Song
|
|
|
|
|
from chordpro.renderers.html import render, get_options
|
|
|
|
|
from igitar import Song
|
|
|
|
|
from igitar.renderers.html import render, get_options
|
|
|
|
|
|
|
|
|
|
from ukatali.configuredialog import ConfigureDialog
|
|
|
|
|
from ukatali.lexer import ChordProLexer
|
|
|
|
@ -19,6 +18,7 @@ class MainWindow(QtWidgets.QMainWindow):
|
|
|
|
|
self.setup_ui()
|
|
|
|
|
self.settings = QtCore.QSettings()
|
|
|
|
|
self.filename = None
|
|
|
|
|
self.is_lyrics_mode = False
|
|
|
|
|
self.configure_dialog = ConfigureDialog(self)
|
|
|
|
|
|
|
|
|
|
def setup_ui(self):
|
|
|
|
@ -67,6 +67,10 @@ class MainWindow(QtWidgets.QMainWindow):
|
|
|
|
|
self.preview_layout.setContentsMargins(0, 0, 0, 0)
|
|
|
|
|
self.preview_layout.setSpacing(0)
|
|
|
|
|
self.preview_layout.setObjectName('preview_layout')
|
|
|
|
|
self.preview_toolbar = QtWidgets.QToolBar(self.preview_dock_contents)
|
|
|
|
|
self.preview_toolbar.setMovable(False)
|
|
|
|
|
self.preview_toolbar.setObjectName('preview_toolbar')
|
|
|
|
|
self.preview_layout.addWidget(self.preview_toolbar)
|
|
|
|
|
self.preview_view = QtWebEngineWidgets.QWebEngineView(self.preview_dock_contents)
|
|
|
|
|
self.preview_view.setUrl(QtCore.QUrl('about:blank'))
|
|
|
|
|
self.preview_view.setObjectName('preview_view')
|
|
|
|
@ -74,6 +78,7 @@ class MainWindow(QtWidgets.QMainWindow):
|
|
|
|
|
self.preview_dock.setWidget(self.preview_dock_contents)
|
|
|
|
|
self.addDockWidget(QtCore.Qt.DockWidgetArea(2), self.preview_dock)
|
|
|
|
|
self.toolbar = QtWidgets.QToolBar(self)
|
|
|
|
|
self.toolbar.setMovable(False)
|
|
|
|
|
self.toolbar.setObjectName('toolbar')
|
|
|
|
|
self.addToolBar(QtCore.Qt.TopToolBarArea, self.toolbar)
|
|
|
|
|
self.new_action = QtWidgets.QAction(self)
|
|
|
|
@ -117,25 +122,25 @@ class MainWindow(QtWidgets.QMainWindow):
|
|
|
|
|
self.cut_action.setIcon(icon)
|
|
|
|
|
self.cut_action.setObjectName('cut_action')
|
|
|
|
|
self.copy_action = QtWidgets.QAction(self)
|
|
|
|
|
icon = QtGui.QIcon.fromTheme('edit-copy')
|
|
|
|
|
self.copy_action.setIcon(icon)
|
|
|
|
|
self.copy_action.setIcon(QtGui.QIcon.fromTheme('edit-copy'))
|
|
|
|
|
self.copy_action.setObjectName('copy_action')
|
|
|
|
|
self.paste_action = QtWidgets.QAction(self)
|
|
|
|
|
icon = QtGui.QIcon.fromTheme('edit-paste')
|
|
|
|
|
self.paste_action.setIcon(icon)
|
|
|
|
|
self.paste_action.setIcon(QtGui.QIcon.fromTheme('edit-paste'))
|
|
|
|
|
self.paste_action.setObjectName('paste_action')
|
|
|
|
|
self.configure_action = QtWidgets.QAction(self)
|
|
|
|
|
icon = QtGui.QIcon.fromTheme('configure')
|
|
|
|
|
self.configure_action.setIcon(icon)
|
|
|
|
|
self.configure_action.setIcon(QtGui.QIcon.fromTheme('configure'))
|
|
|
|
|
self.configure_action.setObjectName('configure_action')
|
|
|
|
|
self.about_action = QtWidgets.QAction(self)
|
|
|
|
|
icon = QtGui.QIcon.fromTheme('help-about')
|
|
|
|
|
self.about_action.setIcon(icon)
|
|
|
|
|
self.about_action.setIcon(QtGui.QIcon.fromTheme('help-about'))
|
|
|
|
|
self.about_action.setObjectName('about_action')
|
|
|
|
|
self.exit_action = QtWidgets.QAction(self)
|
|
|
|
|
icon = QtGui.QIcon.fromTheme('application-exit')
|
|
|
|
|
self.exit_action.setIcon(icon)
|
|
|
|
|
self.exit_action.setIcon(QtGui.QIcon.fromTheme('application-exit'))
|
|
|
|
|
self.exit_action.setObjectName('exit_action')
|
|
|
|
|
self.hide_chords_action = QtWidgets.QAction(self)
|
|
|
|
|
self.hide_chords_action.setCheckable(True)
|
|
|
|
|
self.hide_chords_action.setChecked(False)
|
|
|
|
|
self.hide_chords_action.setIcon(QtGui.QIcon.fromTheme('view-hidden'))
|
|
|
|
|
self.hide_chords_action.setObjectName('hide_chords_action')
|
|
|
|
|
self.file_menu.addAction(self.new_action)
|
|
|
|
|
self.file_menu.addAction(self.open_action)
|
|
|
|
|
self.file_menu.addAction(self.save_action)
|
|
|
|
@ -175,6 +180,7 @@ class MainWindow(QtWidgets.QMainWindow):
|
|
|
|
|
self.toolbar.addAction(self.paste_action)
|
|
|
|
|
self.toolbar.addSeparator()
|
|
|
|
|
self.toolbar.addAction(self.configure_action)
|
|
|
|
|
self.preview_toolbar.addAction(self.hide_chords_action)
|
|
|
|
|
|
|
|
|
|
self.retranslate_ui()
|
|
|
|
|
self.exit_action.triggered.connect(self.close)
|
|
|
|
@ -193,6 +199,7 @@ class MainWindow(QtWidgets.QMainWindow):
|
|
|
|
|
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)
|
|
|
|
|
self.hide_chords_action.triggered.connect(self.on_hide_chords_clicked)
|
|
|
|
|
|
|
|
|
|
def retranslate_ui(self):
|
|
|
|
|
_translate = QtCore.QCoreApplication.translate
|
|
|
|
@ -246,6 +253,7 @@ class MainWindow(QtWidgets.QMainWindow):
|
|
|
|
|
self.exit_action.setText(_translate('MainWindow', 'E&xit'))
|
|
|
|
|
self.exit_action.setToolTip(_translate('MainWindow', 'Quit Ukatali'))
|
|
|
|
|
self.exit_action.setShortcut(_translate('MainWindow', 'Alt+F4'))
|
|
|
|
|
self.hide_chords_action.setText(_translate('MainWindow', 'Hide Chords'))
|
|
|
|
|
|
|
|
|
|
def on_new_clicked(self):
|
|
|
|
|
"""Start a new file"""
|
|
|
|
@ -311,7 +319,7 @@ class MainWindow(QtWidgets.QMainWindow):
|
|
|
|
|
if self.configure_dialog.exec() == QtWidgets.QDialog.Accepted and self.file_editor.text():
|
|
|
|
|
self.on_text_changed()
|
|
|
|
|
|
|
|
|
|
def _get_render_options(self):
|
|
|
|
|
def _get_render_options(self, is_lyrics_mode=False):
|
|
|
|
|
"""Get all the render options from the settings"""
|
|
|
|
|
options = {}
|
|
|
|
|
self.settings.beginGroup('render')
|
|
|
|
@ -325,26 +333,29 @@ class MainWindow(QtWidgets.QMainWindow):
|
|
|
|
|
elif details['type'] is bool:
|
|
|
|
|
value = coerce_bool(value)
|
|
|
|
|
options[name] = value
|
|
|
|
|
if is_lyrics_mode and 'indent' in name:
|
|
|
|
|
options[name] = 0
|
|
|
|
|
elif is_lyrics_mode and 'is_hidden' in name:
|
|
|
|
|
options[name] = True
|
|
|
|
|
self.settings.endGroup()
|
|
|
|
|
return options
|
|
|
|
|
|
|
|
|
|
def _render_song(self, extra_styles=None):
|
|
|
|
|
"""Render the current song and return the HTML"""
|
|
|
|
|
options = self._get_render_options()
|
|
|
|
|
options = self._get_render_options(self.is_lyrics_mode)
|
|
|
|
|
text = self.file_editor.text()
|
|
|
|
|
song = Song()
|
|
|
|
|
song.parse(text)
|
|
|
|
|
if song.metadata.get('copyright'):
|
|
|
|
|
extra_styles = extra_styles or ''
|
|
|
|
|
extra_styles += os.linesep
|
|
|
|
|
extra_styles += '@page {{ @bottom-left {{ content: "{}"; }} }}'.format(song.metadata.get('copyright'))
|
|
|
|
|
return render(song, options, extra_styles)
|
|
|
|
|
try:
|
|
|
|
|
song.parse(text)
|
|
|
|
|
return render(song, options, extra_styles)
|
|
|
|
|
except Exception:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
def on_text_changed(self):
|
|
|
|
|
"""Update the preview when the text changes"""
|
|
|
|
|
# self.setWindowModified(True)
|
|
|
|
|
html = self._render_song()
|
|
|
|
|
self.preview_view.setHtml(html)
|
|
|
|
|
if html:
|
|
|
|
|
self.preview_view.setHtml(html)
|
|
|
|
|
|
|
|
|
|
def on_export_pdf_clicked(self):
|
|
|
|
|
"""Export the current song to PDF"""
|
|
|
|
@ -372,7 +383,7 @@ class MainWindow(QtWidgets.QMainWindow):
|
|
|
|
|
margin_top = float(self.settings.value('margin_top', 20))
|
|
|
|
|
margin_bottom = float(self.settings.value('margin_bottom', 20))
|
|
|
|
|
self.settings.endGroup()
|
|
|
|
|
margins = QtCore.QMarginsF(QtCore.QMargins(margin_left, margin_top, margin_right, margin_bottom))
|
|
|
|
|
margins = QtCore.QMarginsF(margin_left, margin_top, margin_right, margin_bottom)
|
|
|
|
|
page_layout = QtGui.QPageLayout(page_size, orientation, margins, unit)
|
|
|
|
|
# Export to PDF
|
|
|
|
|
self.preview_view.page().printToPdf(str(filename), page_layout)
|
|
|
|
@ -396,9 +407,14 @@ class MainWindow(QtWidgets.QMainWindow):
|
|
|
|
|
# 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))
|
|
|
|
|
html = self._render_song()
|
|
|
|
|
if html:
|
|
|
|
|
html_file.write(html)
|
|
|
|
|
QtWidgets.QMessageBox.information(self, 'Export to HTML Successful',
|
|
|
|
|
'Successfully exported to "{}"'.format(filename.name))
|
|
|
|
|
else:
|
|
|
|
|
QtWidgets.QMessageBox.critical(self, 'Error Exporting to HTML',
|
|
|
|
|
'There was an error while exporting to HTML')
|
|
|
|
|
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))
|
|
|
|
@ -411,16 +427,18 @@ class MainWindow(QtWidgets.QMainWindow):
|
|
|
|
|
else:
|
|
|
|
|
QtWidgets.QMessageBox.critical(self, 'Error Exporting to PDF', 'There was an error while exporting to PDF')
|
|
|
|
|
|
|
|
|
|
def on_print_finished(self, is_success):
|
|
|
|
|
print(is_success)
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
printer = QtPrintSupport.QPrinter(QtPrintSupport.QPrinter.HighResolution)
|
|
|
|
|
print_dialog = QtPrintSupport.QPrintDialog(printer, self.preview_view.page().view())
|
|
|
|
|
if print_dialog.exec() == QtWidgets.QDialog.Accepted:
|
|
|
|
|
self.preview_view.page().print(printer, _on_print_finished)
|
|
|
|
|
self.preview_view.page().print(printer, self.on_print_finished)
|
|
|
|
|
|
|
|
|
|
def on_hide_chords_clicked(self, is_checked):
|
|
|
|
|
"""Hide the chords in the preview window"""
|
|
|
|
|
self.is_lyrics_mode = is_checked
|
|
|
|
|
if self.file_editor.text():
|
|
|
|
|
self.on_text_changed()
|
|
|
|
|