Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port to QT5 (squashed) #69

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions ldoce5viewer/qtgui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
import codecs
import os.path

from PyQt4.QtGui import QIcon
from PyQt5.QtGui import QIcon

from .utils.singleapp import SingleApplication
from .utils.error import StdErrWrapper, MyStreamHandler
from .config import get_config


# set a dummy function if QLineEdit doesn't have setPlaceholderText
from PyQt4.QtGui import QLineEdit
from PyQt5.QtWidgets import QLineEdit
if not hasattr(QLineEdit, 'setPlaceholderText'):
def _dummySetPlaceholderText(self, *args, **kwargs):
pass
Expand Down Expand Up @@ -107,7 +107,7 @@ def messageHandler(msg):
# On Windows-ja
if app.font().family() == u'MS UI Gothic':
cand = (('Segoe UI', None), ('Meiryo UI', None), ('Tahoma', 8))
from PyQt4.QtGui import QFont
from PyQt5.QtGui import QFont
for name, point in cand:
ps = app.font().pointSize()
if point is None:
Expand Down
4 changes: 2 additions & 2 deletions ldoce5viewer/qtgui/access.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import os.path
import traceback

from PyQt4.QtCore import (
from PyQt5.QtCore import (
Qt, Q_ARG, QMetaObject, QIODevice, QTimer,)
from PyQt4.QtNetwork import (
from PyQt5.QtNetwork import (
QNetworkAccessManager, QNetworkReply, QNetworkRequest,)

from .. import __version__
Expand Down
24 changes: 14 additions & 10 deletions ldoce5viewer/qtgui/advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

from operator import itemgetter

from PyQt4.QtCore import Qt, QUrl
from PyQt4.QtGui import (
QAction, QDialog, QKeySequence, QIcon, QTreeWidgetItem,)
from PyQt5.QtCore import Qt, QUrl, QUrlQuery
from PyQt5.QtGui import (
QKeySequence, QIcon)
from PyQt5.QtWidgets import (
QAction, QDialog, QTreeWidgetItem,)

from ..ldoce5 import advtree
from ..utils.compat import range
Expand Down Expand Up @@ -194,11 +196,13 @@ def _render_header(title, mode, phrase, filters):

for (name, spec) in modes:
href = QUrl('search:///')
urlquery = QUrlQuery()
if phrase:
href.addQueryItem('phrase', phrase)
urlquery.addQueryItem('phrase', phrase)
if filters:
href.addQueryItem('filters', filters)
href.addQueryItem('mode', name)
urlquery.addQueryItem('filters', filters)
urlquery.addQueryItem('mode', name)
href.setQuery(urlquery)
if name != mode:
r.append(
'<li><a href="{href}">{title}</a></li>\n'.format(
Expand Down Expand Up @@ -276,10 +280,10 @@ def _render_hwdphr(items, mode):


def search_and_render(url, fulltext_hp, fulltext_de):
query = dict((k, v) for (k, v) in url.queryItems())
mode = query.get('mode', None)
phrase = query.get('phrase', None)
filters = query.get('filters', None)
query = QUrlQuery(url)
mode = query.queryItemValue('mode')
phrase = query.queryItemValue('phrase')
filters = query.queryItemValue('filters')

r = []
if mode in MODE_DICT:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import logging

from PyQt4.QtCore import (
from PyQt5.QtCore import (
QObject, QThread, QMutex, QWaitCondition, pyqtSignal)

_logger = logging.getLogger(__name__)
Expand Down
2 changes: 1 addition & 1 deletion ldoce5viewer/qtgui/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
except ImportError:
import pickle

from PyQt4.QtCore import QReadWriteLock
from PyQt5.QtCore import QReadWriteLock


__config = None
Expand Down
5 changes: 3 additions & 2 deletions ldoce5viewer/qtgui/indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
from struct import Struct
import traceback

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import lxml.etree as et

from .. import __version__
Expand Down
34 changes: 22 additions & 12 deletions ldoce5viewer/qtgui/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@
except ImportError:
objc = None

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtNetwork import *
from PyQt4.QtWebKit import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtNetwork import *
from PyQt5.QtWebKit import *
from PyQt5.QtWebKitWidgets import *
from PyQt5.QtWidgets import *
from PyQt5.QtPrintSupport import *

from .. import fulltext
from .. import incremental
Expand All @@ -36,7 +39,7 @@
from .advanced import AdvancedSearchDialog
from .config import get_config
from .access import MyNetworkAccessManager, _load_static_data
from .async import AsyncFTSearcher
from .async_ import AsyncFTSearcher
from .utils.soundplayer import create_soundplayer
from .indexer import IndexerDialog
from .ui.custom import ToolButton, LineEdit
Expand Down Expand Up @@ -556,9 +559,13 @@ def finished():
def downloadSelectedAudio(self):
path = self._ui.webView.audioUrlToDownload.path()
def showSaveDialog(data):
filename = QFileDialog.getSaveFileName(self, u'Save mp3', '', u'MP3 Files (*.mp3)')
filename = QFileDialog.getSaveFileName(self, u'Save mp3', \
'', u'MP3 Files (*.mp3)')
if type(filename) is tuple:
filename = filename[0]

if filename != '':
file = open(filename, "w")
file = open(filename, "wb")
file.write(data)
file.close()
self._getAudioData(path, showSaveDialog)
Expand All @@ -568,7 +575,8 @@ def _onWebViewLinkClicked(self, url):
if scheme == 'audio':
self._playbackAudio(url.path())
elif scheme == 'lookup':
query = dict((k, v) for (k, v) in url.queryItems())
urlQuery = QUrlQuery(url)
query = dict(urlQuery.queryItems())
if 'q' in query:
q = query['q'].replace('+', ' ')
self._ui.lineEditSearch.setText(q)
Expand Down Expand Up @@ -682,13 +690,15 @@ def fullSearch(self, phrase, filters, mode=None, only_web=False):
self._timerUpdateIndex.start(0)

if self._fts_hwdphr and self._fts_defexa:
url = QUrl("search:///")
urlquery = QUrlQuery()
if phrase:
url.addQueryItem("phrase", phrase)
urlquery.addQueryItem("phrase", phrase)
if filters:
url.addQueryItem("filters", filters)
urlquery.addQueryItem("filters", filters)
if mode:
url.addQueryItem("mode", mode)
urlquery.addQueryItem("mode", mode)
url = QUrl("search:///")
url.setQuery(urlquery)
self._ui.webView.load(url)


Expand Down
2 changes: 1 addition & 1 deletion ldoce5viewer/qtgui/resources/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
FILES := $(wildcard *.png) $(wildcard *.svg) $(wildcard */*.png)

__init__.py: resource.qrc $(FILES)
pyrcc4 -py3 $< -o $@
pyrcc5 $< -o $@

.PHONY: clean
clean:
Expand Down
2 changes: 1 addition & 1 deletion ldoce5viewer/qtgui/ui/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ __init__.py:
touch $@

%.py: %.ui
pyuic4 $< -o $@
pyuic5 $< -o $@

.PYONY: clean
clean:
Expand Down
10 changes: 6 additions & 4 deletions ldoce5viewer/qtgui/ui/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import sys

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWebKit import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebKitWidgets import *
from ...utils.text import ellipsis


Expand Down Expand Up @@ -285,7 +287,7 @@ def mouseReleaseEvent(self, event):

def wheelEvent(self, event):
if event.modifiers() & Qt.ControlModifier:
self.wheelWithCtrl.emit(event.delta())
self.wheelWithCtrl.emit(event.pixelDelta())
return
super(WebView, self).wheelEvent(event)

Expand Down
2 changes: 1 addition & 1 deletion ldoce5viewer/qtgui/ui/main.ui
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@
<customwidget>
<class>QWebInspector</class>
<extends>QWidget</extends>
<header>PyQt4/QtWebKit</header>
<header>PyQt5/QtWebKitWidgets</header>
</customwidget>
<customwidget>
<class>HtmlListWidget</class>
Expand Down
4 changes: 2 additions & 2 deletions ldoce5viewer/qtgui/utils/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

from logging import StreamHandler

from PyQt4.QtCore import QMutex, QObject, pyqtSignal
from PyQt4.QtGui import QPlainTextEdit
from PyQt5.QtCore import QMutex, QObject, pyqtSignal
from PyQt5.QtWidgets import QPlainTextEdit


class MyStreamHandler(StreamHandler):
Expand Down
2 changes: 1 addition & 1 deletion ldoce5viewer/qtgui/utils/fontfallback.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import re

from PyQt4.QtGui import QFont
from PyQt5.QtGui import QFont


_DEFAULT_FONT_NAMES = frozenset((b'sans-serif', b'serif', b'monospace'))
Expand Down
6 changes: 3 additions & 3 deletions ldoce5viewer/qtgui/utils/singleapp.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'''This module prevent you from running two instances of the app'''


from PyQt4.QtCore import pyqtSignal, QIODevice
from PyQt4.QtGui import QApplication
from PyQt4.QtNetwork import QLocalSocket, QLocalServer
from PyQt5.QtCore import pyqtSignal, QIODevice
from PyQt5.QtWidgets import QApplication
from PyQt5.QtNetwork import QLocalSocket, QLocalServer


class SingleApplication(QApplication):
Expand Down
38 changes: 35 additions & 3 deletions ldoce5viewer/qtgui/utils/soundplayer.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import sys
import os
import abc
from tempfile import NamedTemporaryFile
from tempfile import NamedTemporaryFile, mkdtemp
import logging

from PyQt4.QtCore import *
from PyQt5.QtCore import *
from ...utils.compat import range

_logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -50,11 +50,18 @@

# Qt-Phonon
try:
from PyQt4.phonon import Phonon
from PyQt5.phonon import Phonon
except ImportError:
Phonon = None


# Qt-Multimedia
try:
import PyQt5.QtMultimedia as QtMultimedia
except ImportError:
QtMultimedia = None


class Backend(object):
__metaclass__ = abc.ABCMeta
def __init__(self, parent, temp_dir):
Expand Down Expand Up @@ -254,6 +261,29 @@ def close(self):
self._clean_tmp()


class QtMultimediaBackend(Backend):
def __init__(self, parent, temp_dir):
self._player = QtMultimedia.QMediaPlayer()
self._tmpdir = mkdtemp()

def _play(self):
url = QUrl.fromLocalFile(self._path)
content = QtMultimedia.QMediaContent(url)
self._player.setMedia(content)
self._player.play()

def play(self, data):
self._player.stop()
with NamedTemporaryFile(mode='w+b', prefix='',
suffix='.tmp.mp3', dir=self._tmpdir, delete=False) as f:
f.write(data)
self._path = f.name
QTimer.singleShot(0, self._play)

def close(self):
self._player.stop()


class AppKitBackend(Backend):
def __init__(self, parent, temp_dir):
self._sound = None
Expand Down Expand Up @@ -281,6 +311,8 @@ def create_soundplayer(parent, temp_dir):
backends.append(WinMCIBackend)
if Phonon:
backends.append(PhononBackend)
if QtMultimedia:
backends.append(QtMultimediaBackend)
if Gst:
backends.append(GstreamerBackend)
if gst:
Expand Down