Skip to content

Commit

Permalink
Use aqt for PyQt5 backwards compatiblity (#293)
Browse files Browse the repository at this point in the history
* When possible use aqt instead of directly using PyQt.
* This will make morphman compatible with anki-qt5 and anki-qt6.

Motivation: #291 #286
  • Loading branch information
rameauv authored Aug 11, 2023
1 parent 6f4312f commit 680f308
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 25 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,14 @@ jobs:
if [ "${{ runner.os }}" = "Windows" ]; then
source pyenv/Scripts/activate
else
# Qt6 complains because Ubuntu is missing libEGL.so.1
if [ "${{ runner.os }}" = "Linux" ]; then
sudo apt-get update -y -qq
sudo apt-get install -y -qq libegl1
fi
source pyenv/bin/activate
fi
python -m pip install aqt==${{ matrix.anki }} anki==${{ matrix.anki }} pyqtwebengine pylint
python -m pip install aqt[qt5]==${{ matrix.anki }} aqt[qt6]==${{ matrix.anki }} anki==${{ matrix.anki }} PyQt6-WebEngine pylint
- name: Lint
shell: bash
run: |
Expand Down
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[MASTER]
ignore=deps
extension-pkg-whitelist=PyQt5
extension-pkg-whitelist=PyQt5,PyQt6

[MESSAGES CONTROL]
disable=C,R,
Expand Down
1 change: 0 additions & 1 deletion __init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from .morph.util import *
from PyQt5.QtWidgets import *
import anki.stats
from anki.hooks import wrap

Expand Down
3 changes: 1 addition & 2 deletions morph/UI/morphemizerComboBox.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

from PyQt5.QtWidgets import QComboBox
from aqt.qt import QComboBox


class MorphemizerComboBox(QComboBox):
Expand Down
3 changes: 1 addition & 2 deletions morph/customTableWidget.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from PyQt5.QtWidgets import QApplication, QTableWidget
from PyQt5.QtGui import QKeySequence
from aqt.qt import QApplication, QTableWidget, QKeySequence

class CustomTableWidget(QTableWidget):

Expand Down
4 changes: 1 addition & 3 deletions morph/manager.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# -*- coding: utf-8 -*-
import os

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from aqt.qt import *
from anki.utils import is_mac
from .UI import MorphemizerComboBox

Expand Down
4 changes: 1 addition & 3 deletions morph/preferencesDialog.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from aqt.qt import *
from anki.lang import _

from aqt.utils import tooltip
Expand Down
26 changes: 17 additions & 9 deletions morph/readability.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@
from collections import namedtuple
from contextlib import redirect_stdout, redirect_stderr

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from aqt.qt import *

isSupportingWebSockets = False
try:
from PyQt5 import QtWebSockets, QtNetwork
from PyQt6 import QtWebSockets, QtNetwork
isSupportingWebSockets = True
except:
pass
try:
from PyQt5 import QtWebSockets, QtNetwork
isSupportingWebSockets = True
except:
pass

from .morphemes import Morpheme, MorphDb, getMorphemes, altIncludesMorpheme
from .morphemizer import getAllMorphemizers
Expand Down Expand Up @@ -55,7 +59,7 @@ def kaner(to_translate, hiraganer = False):
else:
hiragana = [ord(char) for char in hiragana]
translate_table = dict(zip(hiragana, katakana))
return to_translate.translate(translate_table)
return to_translate.translate(translate_table)

def adjustReading(reading):
return kaner(reading)
Expand Down Expand Up @@ -343,7 +347,11 @@ def __init__(self, parent=None):
self.ui.migakuDictionaryTagsCheckBox.setChecked(False)
self.ui.migakuDictionaryTagsCheckBox.setEnabled(False)

self.ui.webServiceCheckBox.setChecked(cfg('Option_EnableWebService'))
if isSupportingWebSockets:
self.ui.webServiceCheckBox.setChecked(cfg('Option_EnableWebService'))
else:
self.ui.webServiceCheckBox.setChecked(False)
self.ui.webServiceCheckBox.setEnabled(False)

self.ui.buttonBox.accepted.connect(self.onAccept)
self.ui.buttonBox.rejected.connect(self.onReject)
Expand Down Expand Up @@ -432,8 +440,8 @@ def __init__(self, parent=None):
self.server = None
self.clients = set()

if cfg('Option_EnableWebService'):
self.server = QtWebSockets.QWebSocketServer('MorphMan Service', QtWebSockets.QWebSocketServer.NonSecureMode)
if cfg('Option_EnableWebService') and isSupportingWebSockets:
self.server = QtWebSockets.QWebSocketServer('MorphMan Service', QtWebSockets.QWebSocketServer.SslMode.NonSecureMode)
if self.server.listen(QtNetwork.QHostAddress.LocalHost, 9779):
self.write('Web Service Created: '+self.server.serverName()+' : '+self.server.serverAddress().toString()+':'+str(self.server.serverPort()) + '\n')
else:
Expand Down
2 changes: 1 addition & 1 deletion morph/readability_settings_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Form implementation generated from reading ui file 'morph/readability_settings.ui'
#
# Created by: PyQt5 UI code generator 5.15.7
# Created by: PyQt5 UI code generator 5.15.9
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
Expand Down
2 changes: 1 addition & 1 deletion morph/readability_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Form implementation generated from reading ui file 'morph/readability.ui'
#
# Created by: PyQt5 UI code generator 5.15.7
# Created by: PyQt5 UI code generator 5.15.9
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
Expand Down
1 change: 1 addition & 0 deletions test/fake_aqt.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from unittest.mock import MagicMock
from aqt import *

class FakeCollection:
def __init__(self, config):
Expand Down
2 changes: 1 addition & 1 deletion test/test_MorphemizerComboBox.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest

from PyQt5.QtWidgets import QApplication
from aqt.qt import QApplication
from morph.UI import MorphemizerComboBox
from morph.morphemizer import getAllMorphemizers

Expand Down

0 comments on commit 680f308

Please sign in to comment.