def __init__(self, parent = None): """ Constructor @param parent parent widget of this window (QWidget) """ QWebPage.__init__(self, parent) self.__lastRequest = None self.__lastRequestType = QWebPage.NavigationTypeOther self.__proxy = NetworkAccessManagerProxy(self) self.__proxy.setWebPage(self) self.__proxy.setPrimaryNetworkAccessManager( Helpviewer.HelpWindow.HelpWindow.networkAccessManager()) self.setNetworkAccessManager(self.__proxy)
class HelpWebPage(QWebPage): """ Class implementing an enhanced web page. """ def __init__(self, parent = None): """ Constructor @param parent parent widget of this window (QWidget) """ QWebPage.__init__(self, parent) self.__lastRequest = None self.__lastRequestType = QWebPage.NavigationTypeOther self.__proxy = NetworkAccessManagerProxy(self) self.__proxy.setWebPage(self) self.__proxy.setPrimaryNetworkAccessManager( Helpviewer.HelpWindow.HelpWindow.networkAccessManager()) self.setNetworkAccessManager(self.__proxy) def acceptNavigationRequest(self, frame, request, type_): """ Protected method to determine, if a request may be accepted. @param frame reference to the frame sending the request (QWebFrame) @param request reference to the request object (QNetworkRequest) @param type_ type of the navigation request (QWebPage.NavigationType) @return flag indicating acceptance (boolean) """ self.__lastRequest = request self.__lastRequestType = type_ scheme = request.url().scheme() if scheme in ["mailto", "ftp"]: QDesktopServices.openUrl(request.url()) return False return QWebPage.acceptNavigationRequest(self, frame, request, type_) def populateNetworkRequest(self, request): """ Public method to add data to a network request. @param request reference to the network request object (QNetworkRequest) """ try: request.setAttribute(QNetworkRequest.User + 100, QVariant(self)) request.setAttribute(QNetworkRequest.User + 101, QVariant(self.__lastRequestType)) except TypeError: pass def pageAttributeId(self): """ Public method to get the attribute id of the page attribute. @return attribute id of the page attribute (integer) """ return QNetworkRequest.User + 100 def supportsExtension(self, extension): """ Public method to check the support for an extension. @param extension extension to test for (QWebPage.Extension) @return flag indicating the support of extension (boolean) """ try: if extension == QWebPage.ErrorPageExtension: return True except AttributeError: pass return QWebPage.supportsExtension(self, extension) def extension(self, extension, option, output): """ Public method to implement a specific extension. @param extension extension to be executed (QWebPage.Extension) @param option provides input to the extension (QWebPage.ExtensionOption) @param output stores the output results (QWebPage.ExtensionReturn) @return flag indicating a successful call of the extension (boolean) """ try: if extension == QWebPage.ErrorPageExtension: info = sip.cast(option, QWebPage.ErrorPageExtensionOption) if info.error == 102: # this is something of a hack; hopefully it will work in the future return False if info.domain == QWebPage.QtNetwork and \ info.error == QNetworkReply.OperationCanceledError and \ info.errorString == "eric4:No Error": return False errorPage = sip.cast(output, QWebPage.ErrorPageExtensionReturn) html = QString(notFoundPage_html) urlString = QString.fromUtf8(info.url.toEncoded()) title = self.trUtf8("Error loading page: %1").arg(urlString) pixmap = qApp.style()\ .standardIcon(QStyle.SP_MessageBoxWarning, None, self.parent())\ .pixmap(32, 32) imageBuffer = QBuffer() imageBuffer.open(QIODevice.ReadWrite) if pixmap.save(imageBuffer, "PNG"): html = html.replace("IMAGE_BINARY_DATA_HERE", QString(imageBuffer.buffer().toBase64())) errorPage.content = html.arg( title, info.errorString, self.trUtf8("When connecting to: %1.").arg(urlString), self.trUtf8("Check the address for errors such as " "<b>ww</b>.example.org instead of " "<b>www</b>.example.org"), self.trUtf8("If the address is correct, try checking the network " "connection."), self.trUtf8("If your computer or network is protected by a firewall " "or proxy, make sure that the browser is permitted to " "access the network.") ).toUtf8() return True except AttributeError: pass return QWebPage.extension(self, extension, option, output) def userAgentForUrl(self, url): """ Protected method to determine the user agent for the given URL. @param url URL to determine user agent for (QUrl) @return user agent string (string) """ agentList = QWebPage.userAgentForUrl(self, url).split(" ") agentList[0] = "Mozilla/9.0" return agentList.join(" ")