From e0c829b00d5af75d28abb362e2da3ab8870445a1 Mon Sep 17 00:00:00 2001 From: pohmelie Date: Wed, 31 Dec 2014 22:25:39 +0300 Subject: [PATCH 1/4] added pyqt5 --- ghost/ghost.py | 50 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 11 deletions(-) mode change 100755 => 100644 ghost/ghost.py diff --git a/ghost/ghost.py b/ghost/ghost.py old mode 100755 new mode 100644 index 9d465a5..228588c --- a/ghost/ghost.py +++ b/ghost/ghost.py @@ -22,14 +22,14 @@ long = int -bindings = ["PySide", "PyQt4"] +bindings = ["PySide", "PyQt4", "PyQt5"] binding = None for name in bindings: try: binding = __import__(name) - if name == 'PyQt4': + if name.startswith('PyQt'): import sip sip.setapi('QVariant', 2) @@ -66,14 +66,33 @@ def _import(name): QtDebugMsg = QtCore.QtDebugMsg QtFatalMsg = QtCore.QtFatalMsg QtWarningMsg = QtCore.QtWarningMsg -qInstallMsgHandler = QtCore.qInstallMsgHandler +if name == "PyQt5": + + qInstallMsgHandler = QtCore.qInstallMessageHandler + +else: + + qInstallMsgHandler = QtCore.qInstallMsgHandler QtGui = _import("QtGui") -QApplication = QtGui.QApplication -QImage = QtGui.QImage -QPainter = QtGui.QPainter -QPrinter = QtGui.QPrinter -QRegion = QtGui.QRegion +if name == "PyQt5": + + QtWidgets = _import("QtWidgets") + QtPrintSupport = _import("QtPrintSupport") + + QApplication = QtWidgets.QApplication + QImage = QtGui.QImage + QPainter = QtGui.QPainter + QPrinter = QtPrintSupport.QPrinter + QRegion = QtGui.QRegion + +else: + + QApplication = QtGui.QApplication + QImage = QtGui.QImage + QPainter = QtGui.QPainter + QPrinter = QtGui.QPrinter + QRegion = QtGui.QRegion QtNetwork = _import("QtNetwork") QNetworkRequest = QtNetwork.QNetworkRequest @@ -85,7 +104,16 @@ def _import(name): QSsl = QtNetwork.QSsl QtWebKit = _import('QtWebKit') +if name == "PyQt5": + + QtWebKitWidgets = _import("QtWebKitWidgets") + QWebPage = QtWebKitWidgets.QWebPage + QWebView = QtWebKitWidgets.QWebView + +else: + QWebPage = QtWebKit.QWebPage + QWebView = QtWebKit.QWebView default_user_agent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.2 " +\ "(KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2" @@ -115,7 +143,7 @@ def __call__(self, msgType, msg): getattr(self.logger, levels[msgType])(msg) -class GhostWebPage(QtWebKit.QWebPage): +class GhostWebPage(QWebPage): """Overrides QtWebKit.QWebPage in order to intercept some graphical behaviours like alert(), confirm(). Also intercepts client side console.log(). @@ -419,7 +447,7 @@ def __init__( self.main_frame = self.page.mainFrame() - class GhostQWebView(QtWebKit.QWebView): + class GhostQWebView(QWebView): def sizeHint(self): return QSize(*viewport_size) @@ -572,7 +600,7 @@ def print_to_pdf( printer.setFullPage(True) printer.setOutputFileName(path) if self.webview is None: - self.webview = QtWebKit.QWebView() + self.webview = QWebView() self.webview.setPage(self.page) self.webview.setZoomFactor(zoom_factor) self.webview.print_(printer) From 596ddbff6920b9ff349d974aba4d88dc0230969d Mon Sep 17 00:00:00 2001 From: pohmelie Date: Fri, 2 Jan 2015 13:42:31 +0300 Subject: [PATCH 2/4] QtMessageHandler have three arguments in qt5 --- ghost/ghost.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ghost/ghost.py b/ghost/ghost.py index 228588c..8bc8e42 100644 --- a/ghost/ghost.py +++ b/ghost/ghost.py @@ -133,7 +133,8 @@ class QTMessageProxy(object): def __init__(self, logger): self.logger = logger - def __call__(self, msgType, msg): + def __call__(self, *args): + msgType, msg = args[0], args[-1] levels = { QtDebugMsg: 'debug', QtWarningMsg: 'warn', From cf785fe123bd2a6ba4aa63bbc54f9dc916c7b427 Mon Sep 17 00:00:00 2001 From: pohmelie Date: Fri, 2 Jan 2015 13:51:37 +0300 Subject: [PATCH 3/4] When using threads child should be in same as parent thread. So we can't use ghost in multithreaded app when using "main" app. --- ghost/ghost.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ghost/ghost.py b/ghost/ghost.py index 8bc8e42..3dee23b 100644 --- a/ghost/ghost.py +++ b/ghost/ghost.py @@ -151,7 +151,8 @@ class GhostWebPage(QWebPage): """ def __init__(self, app, ghost): self.ghost = ghost - super(GhostWebPage, self).__init__(app) + # super(GhostWebPage, self).__init__(app) + super(GhostWebPage, self).__init__() def chooseFile(self, frame, suggested_file=None): filename = Ghost._upload_file From e515c77ec4858fcbf0127b9c9042529ee1aa9228 Mon Sep 17 00:00:00 2001 From: pohmelie Date: Fri, 2 Jan 2015 13:52:45 +0300 Subject: [PATCH 4/4] pyqt5 requires error message --- ghost/ghost.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ghost/ghost.py b/ghost/ghost.py index 3dee23b..82a3cf9 100644 --- a/ghost/ghost.py +++ b/ghost/ghost.py @@ -345,7 +345,7 @@ def __init__( network_access_manager_class=NetworkAccessManager, ): if not binding: - raise Exception("Ghost.py requires PySide or PyQt4") + raise Exception("Ghost.py requires PySide, PyQt4 or PyQt5") self.id = str(uuid.uuid4())