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)