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

pyqt5 #205

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open

pyqt5 #205

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
58 changes: 44 additions & 14 deletions ghost/ghost.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -105,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',
Expand All @@ -115,14 +144,15 @@ 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().
"""
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
Expand Down Expand Up @@ -315,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())

Expand Down Expand Up @@ -419,7 +449,7 @@ def __init__(

self.main_frame = self.page.mainFrame()

class GhostQWebView(QtWebKit.QWebView):
class GhostQWebView(QWebView):
def sizeHint(self):
return QSize(*viewport_size)

Expand Down Expand Up @@ -572,7 +602,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)
Expand Down