WebBrowser/WebBrowserView.py

branch
QtWebEngine
changeset 4846
960e5e18894b
parent 4826
aa752e6e9eba
child 4857
8dba5fb92f12
equal deleted inserted replaced
4845:2d22ff71c005 4846:960e5e18894b
107 self.__firstLoad = False 107 self.__firstLoad = False
108 108
109 self.__currentZoom = 100 109 self.__currentZoom = 100
110 self.__zoomLevels = WebBrowserView.ZoomLevels[:] 110 self.__zoomLevels = WebBrowserView.ZoomLevels[:]
111 111
112 ## self.__mw.zoomTextOnlyChanged.connect(self.__applyZoom)
113
114 ## self.page().setLinkDelegationPolicy(QWebPage.DelegateAllLinks) 112 ## self.page().setLinkDelegationPolicy(QWebPage.DelegateAllLinks)
115 ## self.linkClicked.connect(self.setSource) 113 ## self.linkClicked.connect(self.setSource)
116 ## 114 ##
117 self.iconUrlChanged.connect(self.__iconUrlChanged) 115 self.iconUrlChanged.connect(self.__iconUrlChanged)
118 self.urlChanged.connect(self.__urlChanged) 116 self.urlChanged.connect(self.__urlChanged)
200 @return reference to the main window 198 @return reference to the main window
201 @rtype WebBrowserWindow 199 @rtype WebBrowserWindow
202 """ 200 """
203 return self.__mw 201 return self.__mw
204 202
205 def load(self, urlOrRequest): 203 def load(self, url):
206 """ 204 """
207 Public method to load a web site. 205 Public method to load a web site.
208 206
209 @param urlOrRequest URL or request object 207 @param url URL to be loaded
210 @type QUrl or LoadRequest 208 @type QUrl or LoadRequest
211 """ 209 """
212 if isinstance(urlOrRequest, QUrl): 210 ## if isinstance(urlOrRequest, QUrl):
213 super(WebBrowserView, self).load(urlOrRequest) 211 super(WebBrowserView, self).load(url)
214 212
215 if not self.__firstLoad: 213 if not self.__firstLoad:
216 self.__firstLoad = True 214 self.__firstLoad = True
217 WebInspector.pushView(self) 215 WebInspector.pushView(self)
218 elif isinstance(urlOrRequest, LoadRequest): 216 ## elif isinstance(urlOrRequest, LoadRequest):
219 reqUrl = urlOrRequest.url() 217 ## reqUrl = urlOrRequest.url()
220 if reqUrl.isEmpty(): 218 ## if reqUrl.isEmpty():
221 return 219 ## return
222 220 ##
223 if reqUrl.scheme() == "javascript": 221 ## if reqUrl.scheme() == "javascript":
224 script = reqUrl.toString()[11:] 222 ## script = reqUrl.toString()[11:]
225 # check if the javascript script is percent encode 223 ## # check if the javascript script is percent encode
226 # i.e. it contains '%' characters 224 ## # i.e. it contains '%' characters
227 if '%' in script: 225 ## if '%' in script:
228 script = QUrl.fromPercentEncoding( 226 ## script = QUrl.fromPercentEncoding(
229 QByteArray(script.encode("utf-8"))) 227 ## QByteArray(script.encode("utf-8")))
230 self.page().runJavaScript(script) 228 ## self.page().runJavaScript(script)
231 return 229 ## return
232 230 ##
233 if self.__isUrlValid(reqUrl): 231 ## if self.__isUrlValid(reqUrl):
234 self.loadRequest(urlOrRequest) 232 ## self.loadRequest(urlOrRequest)
235 return 233 ## return
236 234 ##
237 # ensure proper loading of hosts without a '.' 235 ## # ensure proper loading of hosts without a '.'
238 if not reqUrl.isEmpty() and \ 236 ## if not reqUrl.isEmpty() and \
239 reqUrl.scheme() and \ 237 ## reqUrl.scheme() and \
240 not WebBrowserTools.containsSpace(reqUrl.path()) and \ 238 ## not WebBrowserTools.containsSpace(reqUrl.path()) and \
241 '.' not in reqUrl.path(): 239 ## '.' not in reqUrl.path():
242 u = QUrl("http://" + reqUrl.path()) 240 ## u = QUrl("http://" + reqUrl.path())
243 if u.isValid(): 241 ## if u.isValid():
244 info = QHostInfo.fromName(u.path()) 242 ## info = QHostInfo.fromName(u.path())
245 if info.error() == QHostInfo.NoError: 243 ## if info.error() == QHostInfo.NoError:
246 req = LoadRequest(urlOrRequest) 244 ## req = LoadRequest(urlOrRequest)
247 req.setUrl(u) 245 ## req.setUrl(u)
248 self.loadRequest(req) 246 ## self.loadRequest(req)
249 return 247 ## return
250 248 ##
251 def loadRequest(self, req): 249 ## def loadRequest(self, req):
252 """ 250 ## """
253 Public method to load a page via a load request object. 251 ## Public method to load a page via a load request object.
254 252 ##
255 @param req loaf request object 253 ## @param req loaf request object
256 @type LoadRequest 254 ## @type LoadRequest
257 """ 255 ## """
258 if req.Operation == LoadRequestOperations.GetOperation: 256 ## if req.Operation == LoadRequestOperations.GetOperation:
259 self.load(req.url()) 257 ## self.load(req.url())
260 else: 258 ## else:
261 self.page().runJavaScript( 259 ## self.page().runJavaScript(
262 Scripts.sendPostData(req.url(), req.data())) 260 ## Scripts.sendPostData(req.url(), req.data()))
263 261
264 # TODO: eliminate requestData, add param to get rid of __ctrlPressed 262 # TODO: eliminate requestData, add param to get rid of __ctrlPressed
265 def setSource(self, name, requestData=None): 263 def setSource(self, name, requestData=None):
266 """ 264 """
267 Public method used to set the source to be displayed. 265 Public method used to set the source to be displayed.
464 for index in range(len(self.__zoomLevels)): 462 for index in range(len(self.__zoomLevels)):
465 if zoom <= self.__zoomLevels[index]: 463 if zoom <= self.__zoomLevels[index]:
466 break 464 break
467 return index 465 return index
468 466
469 def __applyZoom(self):
470 """
471 Private slot to apply the current zoom factor.
472 """
473 self.setZoomValue(self.__currentZoom)
474
475 def setZoomValue(self, value, saveValue=True): 467 def setZoomValue(self, value, saveValue=True):
476 """ 468 """
477 Public method to set the zoom value. 469 Public method to set the zoom value.
478 470
479 @param value zoom value (integer) 471 @param value zoom value (integer)
502 """ 494 """
503 Public slot to zoom into the page. 495 Public slot to zoom into the page.
504 """ 496 """
505 index = self.__levelForZoom(self.__currentZoom) 497 index = self.__levelForZoom(self.__currentZoom)
506 if index < len(self.__zoomLevels) - 1: 498 if index < len(self.__zoomLevels) - 1:
507 self.__currentZoom = self.__zoomLevels[index + 1] 499 self.setZoomValue(self.__zoomLevels[index + 1])
508 self.__applyZoom()
509 500
510 def zoomOut(self): 501 def zoomOut(self):
511 """ 502 """
512 Public slot to zoom out of the page. 503 Public slot to zoom out of the page.
513 """ 504 """
514 index = self.__levelForZoom(self.__currentZoom) 505 index = self.__levelForZoom(self.__currentZoom)
515 if index > 0: 506 if index > 0:
516 self.__currentZoom = self.__zoomLevels[index - 1] 507 self.setZoomValue(self.__zoomLevels[index - 1])
517 self.__applyZoom()
518 508
519 def zoomReset(self): 509 def zoomReset(self):
520 """ 510 """
521 Public method to reset the zoom factor. 511 Public method to reset the zoom factor.
522 """ 512 """
523 index = self.__levelForZoom(WebBrowserView.ZoomLevelDefault) 513 index = self.__levelForZoom(WebBrowserView.ZoomLevelDefault)
524 self.__currentZoom = self.__zoomLevels[index] 514 self.setZoomValue(self.__zoomLevels[index])
525 self.__applyZoom()
526 515
527 def hasSelection(self): 516 def hasSelection(self):
528 """ 517 """
529 Public method to determine, if there is some text selected. 518 Public method to determine, if there is some text selected.
530 519
1407 if pinch: 1396 if pinch:
1408 if pinch.state() == Qt.GestureStarted: 1397 if pinch.state() == Qt.GestureStarted:
1409 pinch.setScaleFactor(self.__currentZoom / 100.0) 1398 pinch.setScaleFactor(self.__currentZoom / 100.0)
1410 else: 1399 else:
1411 scaleFactor = pinch.scaleFactor() 1400 scaleFactor = pinch.scaleFactor()
1412 self.__currentZoom = int(scaleFactor * 100) 1401 self.setZoomValue(int(scaleFactor * 100))
1413 self.__applyZoom()
1414 evt.accept() 1402 evt.accept()
1415 1403
1416 def eventFilter(self, obj, evt): 1404 def eventFilter(self, obj, evt):
1417 """ 1405 """
1418 Public method to process event for other objects. 1406 Public method to process event for other objects.

eric ide

mercurial