From 5521f8f692aefb511d61e5f9f1cea003e458943f Mon Sep 17 00:00:00 2001 From: Kevin Hendricks Date: Wed, 2 Aug 2023 17:23:51 -0400 Subject: [PATCH] update to latest plugin_utils.py --- ChangeLog.txt | 3 ++ plugin.xml | 2 +- plugin_utils.py | 93 ++++++++++++++++++++++++++++++++++--------------- 3 files changed, 69 insertions(+), 29 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index 16f2457..157c3dd 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -1,3 +1,6 @@ +v041 + - update to the very latest version of plugin_utils.py + v040 - support both Qt5 qnd Qt6 using the latest plugin_utils - properly save plugin app settings between launches diff --git a/plugin.xml b/plugin.xml index 3a69ad5..696b085 100755 --- a/plugin.xml +++ b/plugin.xml @@ -5,7 +5,7 @@ Sigil Provide an epub3 reader to view the current epub being edited python3.4 -0.4.0 +0.4.1 true true osx,win,unx diff --git a/plugin_utils.py b/plugin_utils.py index afe0128..5c9a871 100644 --- a/plugin_utils.py +++ b/plugin_utils.py @@ -38,17 +38,44 @@ if SIGIL_QT_MAJOR_VERSION == 6: from PySide6 import QtCore, QtGui, QtNetwork, QtPrintSupport, QtSvg, QtWebChannel, QtWidgets # noqa: F401 - from PySide6 import QtWebEngineCore, QtWebEngineWidgets # noqa: F401 - from PySide6.QtWebEngineCore import QWebEnginePage, QWebEngineProfile, QWebEngineScript, QWebEngineSettings # noqa: F401 + # Plugins that don't use QtWebEngine shouldn't fail when external Pythons + # don't have PySide6 installed. Bundled Pythons will always have PySide6 + # installed startting with Qt6 releases. + try: + from PySide6 import QtWebEngineCore, QtWebEngineWidgets # noqa: F401 + from PySide6.QtWebEngineCore import QWebEnginePage, QWebEngineProfile, QWebEngineScript, QWebEngineSettings # noqa: F401 + except ImportError: + print('QtWebEngine PySide6 Python bindings not found.') + print('If this plugin needs QtWebEngine, make sure those bindings are installed.') + pass + else: + if DEBUG: + print('QtWebEngine PySide6 Python bindings found.') + from PySide6.QtCore import Qt, Signal, Slot, qVersion # noqa: F401 - from PySide6.QtGui import QAction # noqa: F401 + from PySide6.QtGui import QAction, QActionGroup # noqa: F401 from PySide6.QtUiTools import QUiLoader # noqa: F401 elif SIGIL_QT_MAJOR_VERSION == 5: - from PyQt5 import QtCore, QtGui, QtNetwork, QtPrintSupport, QtSvg, QtWebChannel, QtWidgets # noqa: F401 - from PyQt5 import QtWebEngineCore, QtWebEngineWidgets # noqa: F401 - from PyQt5.QtWebEngineWidgets import QWebEnginePage, QWebEngineProfile, QWebEngineScript, QWebEngineSettings # noqa: F401 + from PyQt5 import QtCore, QtGui, QtNetwork, QtPrintSupport, QtSvg, QtWidgets # noqa: F401 + # Plugins that don't use QtWebEngine shouldn't fail when external Pythons + # Don't have PyQt5 installed. And Sigil versions before PyQtWebEngine was added (Pre-1.6) + # should be able to run plugins that use this script, but don't use QtWebEngine. + try: + from PyQt5 import QtWebEngineCore, QtWebEngineWidgets # noqa: F401 + from PyQt5.QtWebEngineWidgets import QWebEnginePage, QWebEngineProfile, QWebEngineScript, QWebEngineSettings # noqa: F401 + # WebChannel binding not added until Sigil 1.6 + from PyQt5 import QtWebChannel # noqa: F401 + except ImportError: + print('QtWebEngine PyQt5 Python bindings not found.') + print('If this plugin needs QtWebEngine make sure those bindings are installed') + print('(or use Sigil 1.6 or newer, which has it bundled).') + pass + else: + if DEBUG: + print('QtWebEngine PyQt5 Python bindings found.') + from PyQt5.QtCore import Qt, pyqtSignal as Signal, pyqtSlot as Slot, qVersion # noqa: F401 - from PyQt5.QtWidgets import QAction # noqa: F401 + from PyQt5.QtWidgets import QAction, QActionGroup # noqa: F401 from PyQt5 import uic # noqa: F401 @@ -71,6 +98,15 @@ ismacos = isosx = 'darwin' in _plat +''' PyQt5 translations don't like bytestrings + and PySide6 doesn't like utf-8 ''' +def trans_enc(s): + if 'PySide6' in sys.modules: + return s + else: + return s.encode('utf-8') + + ''' Return a tuple of a version string for easy comparison''' def tuple_version(v): # No alpha characters in version strings allowed here! @@ -138,7 +174,7 @@ def convertWeights(weight, inverted=False, shift=False): class PluginApplication(QtWidgets.QApplication): def __init__(self, args, bk, app_icon=None, match_fonts=True, match_highdpi=True, match_dark_palette=False, - match_whats_this=True, dont_use_native_menubars=True, + match_whats_this=True, dont_use_native_menubars=False, load_qtbase_translations=True, load_qtplugin_translations=True, plugin_trans_folder=None): @@ -226,25 +262,25 @@ def match_sigil_darkmode(self): p = QtGui.QPalette() sigil_colors = self.bk.color dark_color = QtGui.QColor(sigil_colors("Window")) - disabled_color = QtGui.QColor(127,127,127) + disabled_color = QtGui.QColor(127, 127, 127) dark_link_color = QtGui.QColor(108, 180, 238) text_color = QtGui.QColor(sigil_colors("Text")) - p.setColor(p.Window, dark_color) - p.setColor(p.WindowText, text_color) - p.setColor(p.Base, QtGui.QColor(sigil_colors("Base"))) - p.setColor(p.AlternateBase, dark_color) - p.setColor(p.ToolTipBase, dark_color) - p.setColor(p.ToolTipText, text_color) - p.setColor(p.Text, text_color) - p.setColor(p.Disabled, p.Text, disabled_color) - p.setColor(p.Button, dark_color) - p.setColor(p.ButtonText, text_color) - p.setColor(p.Disabled, p.ButtonText, disabled_color) - p.setColor(p.BrightText, Qt.red) - p.setColor(p.Link, dark_link_color) - p.setColor(p.Highlight, QtGui.QColor(sigil_colors("Highlight"))) - p.setColor(p.HighlightedText, QtGui.QColor(sigil_colors("HighlightedText"))) - p.setColor(p.Disabled, p.HighlightedText, disabled_color) + p.setColor(QtGui.QPalette.Window, dark_color) + p.setColor(QtGui.QPalette.WindowText, text_color) + p.setColor(QtGui.QPalette.Base, QtGui.QColor(sigil_colors("Base"))) + p.setColor(QtGui.QPalette.AlternateBase, dark_color) + p.setColor(QtGui.QPalette.ToolTipBase, dark_color) + p.setColor(QtGui.QPalette.ToolTipText, text_color) + p.setColor(QtGui.QPalette.Text, text_color) + p.setColor(QtGui.QPalette.Disabled, QtGui.QPalette.Text, disabled_color) + p.setColor(QtGui.QPalette.Button, dark_color) + p.setColor(QtGui.QPalette.ButtonText, text_color) + p.setColor(QtGui.QPalette.Disabled, QtGui.QPalette.ButtonText, disabled_color) + p.setColor(QtGui.QPalette.BrightText, Qt.red) + p.setColor(QtGui.QPalette.Link, dark_link_color) + p.setColor(QtGui.QPalette.Highlight, QtGui.QColor(sigil_colors("Highlight"))) + p.setColor(QtGui.QPalette.HighlightedText, QtGui.QColor(sigil_colors("HighlightedText"))) + p.setColor(QtGui.QPalette.Disabled, QtGui.QPalette.HighlightedText, disabled_color) self.setStyle(QtWidgets.QStyleFactory.create("Fusion")) self.setPalette(p) @@ -259,7 +295,7 @@ def _setup_ui_font_(self, font_lst): font.setWeight(convertWeights(int(font_lst[4]), inverted=True)) if DEBUG: - print(f'Font Weight: {font.weight()}') + print('Font Weight: {}'.format(font.weight())) self.instance().setFont(font) if DEBUG: @@ -386,8 +422,9 @@ def loadUi(uifile, baseinstance=None, customWidgets=None, # If it's in another directory, however, you may need to set the # PYSIDE_LOADUI_CWD environment variable to the resource's directory first. # Best practice is not to define icons in the .ui file. Do it at the app level. - if os.environ('PYSIDE_LOADUI_CWD') is not None: - loader.setWorkingDirectory(os.environ('PYSIDE_LOADUI_CWD')) + e = os.environ.get('PYSIDE_LOADUI_CWD', None) + if e is not None: + loader.setWorkingDirectory(e) else: loader.setWorkingDirectory(QtCore.QDir(SCRIPT_DIRECTORY))