Skip to content

Commit

Permalink
More refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dougmassay committed Jan 14, 2022
1 parent 1ac8c63 commit 2897817
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 125 deletions.
4 changes: 2 additions & 2 deletions buildplugin
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ TRANS_NAME = 'translations'
TRANS_SRC = os.path.join(SCRIPT_DIR, TRANS_NAME)
TRANS_DEST = os.path.join(SCRIPT_DIR, PLUGIN_NAME, TRANS_NAME)

PLUGIN_FILES = ['compat.py',
'dialogs.py',
PLUGIN_FILES = ['dialogs.py',
'parsing_engine.py',
'plugin.py',
'plugin.xml',
'sigil_utils.py',
'utilities.py',
'plugin.svg',
'plugin.png',]
Expand Down
41 changes: 26 additions & 15 deletions dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,61 +8,72 @@
import sys
import math

from sigil_utils import match_sigil_highdpi, match_sigil_font, match_sigil_darkmode
from sigil_utils import disable_whats_this, loadUi
from sigil_utils import load_base_qt_translations, load_plugin_translations
# from sigil_utils import loadUi # noqa

from utilities import UpdateChecker, taglist, combobox_defaults, remove_dupes
from compat import match_sigil_highdpi, match_sigil_font, match_sigil_darkmode
from compat import disable_whats_this, loadUi
from compat import loadUi # noqa
from compat import load_base_qt_translations, load_plugin_translations
from parsing_engine import MarkupParser

try:
from PySide6.QtCore import Qt, QByteArray, QCoreApplication
# from PySide6.QtCore import Signal, Slot
from PySide6.QtWidgets import QApplication, QCheckBox, QComboBox, QDialog, QDialogButtonBox
from PySide6.QtWidgets import QHBoxLayout, QLabel, QLineEdit, QMainWindow, QMessageBox, QPushButton
from PySide6.QtWidgets import QTextEdit, QVBoxLayout, QWidget
from PySide6.QtGui import QIcon, QAction
print('Pyside6')
except ImportError:
from PyQt5.QtCore import Qt, QByteArray, QCoreApplication
# from PyQt5.QtCore import pyqtSignal as Signal, pyqtSlot as Slot
from PyQt5.QtWidgets import QAction, QApplication, QCheckBox, QComboBox, QDialog, QDialogButtonBox
from PyQt5.QtWidgets import QHBoxLayout, QLabel, QLineEdit, QMainWindow, QMessageBox, QPushButton
from PyQt5.QtWidgets import QTextEdit, QVBoxLayout, QWidget
from PyQt5.QtGui import QIcon
print('PyQt5')

DEBUG = 1
if DEBUG:
if 'PySide6' in sys.modules:
print('Plugin using PySide6')
else:
print('Plugin using PyQt5')

BAIL_OUT = False
PROCESSED = False
DEBUG = 0

# Function alias used to surround translatable strings
_t = QCoreApplication.translate


def launch_gui(bk, prefs):

match_sigil_highdpi(bk)
match_sigil_font(bk)
match_sigil_highdpi(bk) # or fail gracefully
match_sigil_font(bk) # or fail gracefully

app = QApplication([])
icon = os.path.join(bk._w.plugin_dir, bk._w.plugin_name, 'plugin.svg')
app.setWindowIcon(QIcon(icon))

disable_whats_this(app)
disable_whats_this(app) # or fail gracefully

# Make plugin match Sigil's light/dark theme
match_sigil_darkmode(bk, app)
match_sigil_darkmode(bk, app) # or fail gracefully

# Load Qt Base translations, if found, for Sigil's language
qttrans = load_base_qt_translations(bk, language_override=None)
res = app.installTranslator(qttrans)
if DEBUG:
print('Qt Base Translator succesfully installed: {}'.format(res))

# Assumes that binary qm files similar <plugin_name>_es.qm
# are present in a folder named 'translations'.
plugintrans = load_plugin_translations(bk, language_override=None)
# Load plugin translations, if found, for Sigil's language
# Folder where binary '<plugin_name>_pl.qm' are found
transfolder = os.path.join(bk._w.plugin_dir, bk._w.plugin_name, 'translations')
plugintrans = load_plugin_translations(bk, transfolder, language_override=None)
res = app.installTranslator(plugintrans)
if DEBUG:
print('Plugin Translator succesfully installed: {}'.format(res))

win = guiMain(bk, prefs)
# Use exec() and not exec_() for PyQt5/PySide6 compliance
app.exec()
return win.getAbort()

Expand Down
43 changes: 30 additions & 13 deletions compat.py → sigil_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import os
import sys
import inspect

try:
from PySide6.QtCore import Qt, QTimer, qVersion, QMetaObject, QDir
Expand All @@ -18,7 +19,15 @@
from PyQt5 import uic


DEBUG = 0
SCRIPT_DIRECTORY = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))

DEBUG = 1
if DEBUG:
if 'PySide6' in sys.modules:
print('sigil_utilities using PySide6')
else:
print('sigil_utilities using PyQt5')


_plat = sys.platform.lower()
iswindows = 'win32' in _plat or 'win64' in _plat
Expand All @@ -44,13 +53,13 @@ def setup_highdpi(highdpi):
for p in env_vars:
os.environ.pop(p, None)

# Match Sigil's highdpi setting if necessary/possible
def match_sigil_highdpi(bk):
if not (bk.launcher_version() >= 20200326): # Sigil 1.2.0
return
# macos handles highdpi in both Qt5 and Qt6
if not ismacos:
# Linux and Windows don't need to do anything
# to enable highdpi after Qt6
# Linux and Windows don't need to do anything to enable highdpi after Qt6
if tuple_version(qVersion()) < (6, 0, 0):
try:
setup_highdpi(bk._w.highdpi)
Expand All @@ -63,8 +72,9 @@ def setup_ui_font(font_str):
font.fromString(font_str)
QApplication.setFont(font)

# Match Sigil's UI font settings if possible
def match_sigil_font(bk):
# QFont::toString produces slightly different results in Qt5 vs
# QFont::toString produces slightly different results with Qt5 vs
# Qt6. Produce a string that will work with QFont::fromString
# in both Qt5 and Qt6.
if not (bk.launcher_version() >= 20200326): # Sigil 1.2.0
Expand All @@ -90,6 +100,7 @@ def match_sigil_font(bk):
pass


# Match Sigil's dark theme if possible
def match_sigil_darkmode(bk, app):
if not (bk.launcher_version() >= 20200117):
return
Expand Down Expand Up @@ -123,11 +134,13 @@ def match_sigil_darkmode(bk, app):
app.setPalette(p)


# Disable '?' window box buttons if necessary
def disable_whats_this(app):
if tuple_version(qVersion()) >= (5, 10, 0) and tuple_version(qVersion()) < (5, 15, 0):
app.setAttribute(Qt.AA_DisableWindowContextHelpButton)


# Find the location of Qt's base translations
def get_qt_translations_path(sigil_path):
isBundled = 'sigil' in sys.prefix.lower()
if DEBUG:
Expand All @@ -140,10 +153,9 @@ def get_qt_translations_path(sigil_path):
else:
return QLibraryInfo.location(QLibraryInfo.TranslationsPath)

# Install qtbase translator for standard dialogs and such.
# Use the Sigil language setting unless manually overridden.
def load_base_qt_translations(bk, language_override=None):
# print(f'Application dir: {bk._w.appdir}')
# Install qtbase translator for standard dialogs and such.
# Use the Sigil language setting unless manually overridden.
if not (bk.launcher_version() >= 20170227): # Sigil 0.9.8
return
qt_translator = QTranslator()
Expand All @@ -162,9 +174,10 @@ def load_base_qt_translations(bk, language_override=None):
return qt_translator


def load_plugin_translations(bk, language_override=None):
# Install translator for the specified plugin dialog.
# Use the Sigil language setting unless manually overridden.
# Install translator for the specified plugin dialog.
# Use the Sigil language setting unless manually overridden.
# 'folder' parameter is where the binary <plugin_name>_??.qm files are.
def load_plugin_translations(bk, folder, language_override=None):
if not (bk.launcher_version() >= 20170227): # Sigil 0.9.8
return
plugin_translator = QTranslator()
Expand All @@ -175,11 +188,13 @@ def load_plugin_translations(bk, language_override=None):
else:
qmf = '{}_{}'.format(bk._w.plugin_name.lower(), bk.sigil_ui_lang)
if DEBUG:
print('Looking for {} in {}'.format(qmf, os.path.join(bk._w.plugin_dir, bk._w.plugin_name, 'translations')))
plugin_translator.load(qmf, os.path.join(bk._w.plugin_dir, bk._w.plugin_name, 'translations'))
print('Looking for {} in {}'.format(qmf, folder))
plugin_translator.load(qmf, folder)
return plugin_translator


# Mimic the behavior of PyQt5.uic.loadUi() in PySide6
# so the same code can be used for PyQt5/PySide6.
if 'PySide6' in sys.modules:
class UiLoader(QUiLoader):
def __init__(self, baseinstance, customWidgets=None):
Expand All @@ -204,7 +219,7 @@ def createWidget(self, class_name, parent=None, name=''):
try:
widget = self.customWidgets[class_name](parent)

except (TypeError, KeyError) as e: # noqa
except (TypeError, KeyError):
raise Exception('No custom widget ' + class_name + ' found in customWidgets param of UiLoader __init__.')

if self.baseinstance:
Expand Down Expand Up @@ -233,5 +248,7 @@ def loadUi(uifile, baseinstance=None, customWidgets=None,
widget = loader.load(uifile)
QMetaObject.connectSlotsByName(widget)
return widget

# Otherwise return the standard PyQt5 loadUi object
else:
loadUi = uic.loadUi
Loading

0 comments on commit 2897817

Please sign in to comment.