Skip to content

Commit

Permalink
update to use latest plugin_utils framework
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinhendricks committed Mar 17, 2022
1 parent 1a2c061 commit 694f216
Show file tree
Hide file tree
Showing 6 changed files with 450 additions and 55 deletions.
4 changes: 4 additions & 0 deletions ChangeLog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
v040
- support both Qt5 qnd Qt6 using the latest plugin_utils
- properly save plugin app settings between launches

v030
- added bug fix from RbnJrg to support svg wrappers arund svg

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

**[Plugin] Bibi Reader - a version of BiBi's Epub Reader for Sigil **

Updated: June 14, 2021
Updated: March 17, 2022

Current Version: "0.3.0"
Current Version: "0.4.0"

This plugin implements an epub3 reader for the epub currently being edited in Sigil.
It uses PyQt5 and PyQtWebengine to create a browser like main window and then loads
It uses PyQt5 and PyQtWebengine or PySide6 to create a browser like main window and then loads
Bibi's Epub Reader project to implement the epub reader.

See https://github.com/satorumurmur/bibi
Expand Down
1 change: 1 addition & 0 deletions buildplugin
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ PLUGIN_FILES = ['bibi',
'bibi_license.txt',
'README.md',
'Bibi_README.md',
'plugin_utils.py',
'plugin.py',
'plugin.xml',
'plugin.svg',
Expand Down
92 changes: 41 additions & 51 deletions plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
# -*- coding: utf-8 -*-
# vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab

# Copyright 2021 Kevin B. Hendricks, Stratford Ontario Canada
# Copyright 2021 Doug Massay
# Copyright 2021-2022 Kevin B. Hendricks, Stratford Ontario Canada
# Copyright 2021-2022 Doug Massay

# This plugin's source code is available under the GNU LGPL Version 2.1 or GNU LGPL Version 3 License.
# See https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html or
Expand All @@ -14,49 +14,20 @@
import argparse
import tempfile, shutil
import inspect
from epub_utils import epub_zip_up_book_contents

try:
from PySide2.QtCore import *
from PySide2.QtWidgets import *
from PySide2.QtGui import *
from PySide2.QtWebEngineWidgets import QWebEnginePage, QWebEngineProfile, QWebEngineScript
from PySide2.QtWebEngineWidgets import QWebEngineSettings, QWebEngineView
print('Pyside2')
except:
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtWebEngineWidgets import QWebEnginePage, QWebEngineProfile, QWebEngineScript
from PyQt5.QtWebEngineWidgets import QWebEngineSettings, QWebEngineView
print('PyQt5')
from plugin_utils import QtCore, QtWidgets
from plugin_utils import QtWebEngineWidgets
from plugin_utils import QWebEnginePage, QWebEngineProfile, QWebEngineScript, QWebEngineSettings
from plugin_utils import PluginApplication, iswindows, ismacos

from epub_utils import epub_zip_up_book_contents

SCRIPT_DIR = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))

_plat = sys.platform.lower()
iswindows = 'win32' in _plat or 'win64' in _plat
ismacos = isosx = 'darwin' in _plat

def setup_highdpi(highdpi):
has_env_setting = False
env_vars = ('QT_AUTO_SCREEN_SCALE_FACTOR', 'QT_SCALE_FACTOR', 'QT_SCREEN_SCALE_FACTORS', 'QT_DEVICE_PIXEL_RATIO')
for v in env_vars:
if os.environ.get(v):
has_env_setting = True
break
if highdpi == 'on' or (highdpi == 'detect' and not has_env_setting):
QApplication.setAttribute(Qt.AA_EnableHighDpiScaling, True)
elif highdpi == 'off':
QApplication.setAttribute(Qt.AA_EnableHighDpiScaling, False)
for p in env_vars:
os.environ.pop(p, None)


class WebPage(QWebEnginePage):

def __init__(self, parent=None):
QWebEnginePage.__init__(self, parent)
def __init__(self, profile, parent=None):
QWebEnginePage.__init__(self, profile, parent)

def javaScriptConsoleMessage(self, level, msg, linenumber, source_id):
prefix = {
Expand Down Expand Up @@ -84,27 +55,43 @@ def acceptNavigationRequest(self, url, req_type, is_main_frame):
return False


class WebView(QWebEngineView):
class WebView(QtWebEngineWidgets.QWebEngineView):

def __init__(self, parent=None):
QWebEngineView.__init__(self, parent)
QtWebEngineWidgets.QWebEngineView.__init__(self, parent)
app = PluginApplication.instance()
# Plugin prefs folder
pfolder = os.path.dirname(app.bk._w.plugin_dir) + '/plugins_prefs/' + app.bk._w.plugin_name
localstorepath = pfolder + '/local-storage'
if not os.path.exists(localstorepath):
try:
os.makedirs(localstorepath, 0o700)
except FileExistsError:
# directory already exists
pass
print(localstorepath)
w = app.primaryScreen().availableGeometry().width()
self._size_hint = QtCore.QSize(int(w/3), int(w/2))
# How to get bookid to add to QWebEngineProfile name?
self._profile = QWebEngineProfile('BibiReaderSigilPluginSettings')
# Set HTTP Cache type to memory only
self._profile.setHttpCacheType(QWebEngineProfile.MemoryHttpCache)
self._page = WebPage(self._profile, self)
self.setPage(self._page)
# Set this View's page settings
s = self.settings();
s.setAttribute(QWebEngineSettings.WebAttribute.JavascriptEnabled, True)
s.setAttribute(QWebEngineSettings.WebAttribute.JavascriptCanOpenWindows, True)
s.setAttribute(QWebEngineSettings.WebAttribute.JavascriptCanAccessClipboard, True)
s.setAttribute(QWebEngineSettings.WebAttribute.LocalContentCanAccessFileUrls, True)
s.setAttribute(QWebEngineSettings.WebAttribute.AllowWindowActivationFromJavaScript, True)
w = QApplication.instance().desktop().availableGeometry(self).width()
self._size_hint = QSize(int(w/3), int(w/2))
self._page = WebPage(self)
self.setPage(self._page)

def sizeHint(self):
return self._size_hint



class MainWindow(QMainWindow):
class MainWindow(QtWidgets.QMainWindow):

# constructor
def __init__(self, query, prefs, *args, **kwargs):
Expand All @@ -129,7 +116,7 @@ def __init__(self, query, prefs, *args, **kwargs):

# build url to launch readium with
readerpath = os.path.join(SCRIPT_DIR,'bibi','index.html')
bookurl = QUrl.fromLocalFile(readerpath)
bookurl = QtCore.QUrl.fromLocalFile(readerpath)
bookurl.setQuery(self.query)
self.browser.setUrl(bookurl)

Expand All @@ -142,7 +129,7 @@ def __init__(self, query, prefs, *args, **kwargs):
def readsettings(self):
b64val = self.prefs.get('geometry', None)
if b64val:
self.restoreGeometry(QByteArray.fromBase64(QByteArray(b64val.encode('ascii'))))
self.restoreGeometry(QtCore.QByteArray.fromBase64(QtCore.QByteArray(b64val.encode('ascii'))))

# method for updating the title of the window
def update_title(self):
Expand All @@ -156,13 +143,13 @@ def done(self):
self.close()

def resizeEvent(self, ev):
QMainWindow.resizeEvent(self, ev)
QtWidgets.QMainWindow.resizeEvent(self, ev)
self.update_title()

def closeEvent(self, ev):
b64val = str(self.saveGeometry().toBase64(), 'ascii')
self.prefs['geometry'] = b64val
QMainWindow.closeEvent(self, ev)
QtWidgets.QMainWindow.closeEvent(self, ev)


# the plugin entry point
Expand Down Expand Up @@ -196,11 +183,14 @@ def run(bk):

query = 'book=' + bookdir_name + '.epub'

'''
if not ismacos:
setup_highdpi(bk._w.highdpi)
'''
icon = os.path.join(bk._w.plugin_dir, bk._w.plugin_name, 'plugin.svg')

# creating a pyQt5 application
app = QApplication(sys.argv)
# creating a python qt application
app = PluginApplication(sys.argv, bk, app_icon=icon)

# setting name to the application
app.setApplicationName("BibiReader")
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 a Bibi epub3 reader to view the current epub being edited</description>
<engine>python3.4</engine>
<version>0.3.0</version>
<version>0.4.0</version>
<autostart>true</autostart>
<autoclose>true</autoclose>
<oslist>osx,win,unx</oslist>
Expand Down
Loading

0 comments on commit 694f216

Please sign in to comment.