Skip to content

Commit

Permalink
update to latest plugin_utils.py
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinhendricks committed Aug 2, 2023
1 parent cd6c466 commit 5521f8f
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 29 deletions.
3 changes: 3 additions & 0 deletions ChangeLog.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<author>Sigil</author>
<description>Provide an epub3 reader to view the current epub being edited</description>
<engine>python3.4</engine>
<version>0.4.0</version>
<version>0.4.1</version>
<autostart>true</autostart>
<autoclose>true</autoclose>
<oslist>osx,win,unx</oslist>
Expand Down
93 changes: 65 additions & 28 deletions plugin_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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!
Expand Down Expand Up @@ -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):

Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand Down Expand Up @@ -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))

Expand Down

0 comments on commit 5521f8f

Please sign in to comment.