Skip to content

Commit

Permalink
Use aqt for PyQt5 backwards compatiblity (#293) (#297)
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
Co-authored-by: rameauv <[email protected]>
  • Loading branch information
landonepps and rameauv authored Aug 13, 2023
1 parent e597ae3 commit 206ff99
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 22 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
fi
source pyenv/bin/activate
fi
python -m pip install aqt==${{ matrix.anki }} anki==${{ matrix.anki }} PyQt6-WebEngine 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 All @@ -55,3 +55,4 @@ jobs:
export QT_QPA_PLATFORM=minimal
export PYTHONPATH=./
python test.py
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=PyQt6
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,4 +1,3 @@
from PyQt6.QtWidgets import *
from aqt.reviewer import Reviewer
from aqt.utils import tooltip
from aqt import gui_hooks
Expand Down
4 changes: 1 addition & 3 deletions morph/UI/morphemizerComboBox.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

from PyQt6.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 PyQt6.QtWidgets import QApplication, QTableWidget
from PyQt6.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 PyQt6.QtCore import *
from PyQt6.QtGui import *
from PyQt6.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 PyQt6.QtCore import *
from PyQt6.QtWidgets import *
from PyQt6.QtGui import *
from aqt.qt import *
from anki.lang import _

from aqt.utils import tooltip
Expand Down
22 changes: 15 additions & 7 deletions morph/readability.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@
from contextlib import redirect_stdout, redirect_stderr
from functools import partial

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

isSupportingWebSockets = False
try:
from PyQt6 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 @@ -361,7 +365,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 @@ -451,7 +459,7 @@ def __init__(self, parent=None):
self.server = None
self.clients = set()

if cfg('Option_EnableWebService'):
if cfg('Option_EnableWebService') and isSupportingWebSockets:
self.server = QtWebSockets.QWebSocketServer('MorphMan Service', QtWebSockets.QWebSocketServer.SslMode.NonSecureMode)
if self.server.listen(QtNetwork.QHostAddress.SpecialAddress.LocalHost, 9779):
self.write('Web Service Created: '+self.server.serverName()+' : '+self.server.serverAddress().toString()+':'+str(self.server.serverPort()) + '\n')
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 PyQt6.QtWidgets import QApplication
from aqt.qt import QApplication
from morph.UI import MorphemizerComboBox
from morph.morphemizer import getAllMorphemizers

Expand Down

0 comments on commit 206ff99

Please sign in to comment.