1# vim: ts=8:sts=8:sw=8:noexpandtab 2# 3# This file is part of ReText 4# Copyright: 2015-2021 Dmitry Shachnev 5# 6# This program is free software; you can redistribute it and/or modify 7# it under the terms of the GNU General Public License as published by 8# the Free Software Foundation; either version 2 of the License, or 9# (at your option) any later version. 10# 11# This program is distributed in the hope that it will be useful, 12# but WITHOUT ANY WARRANTY; without even the implied warranty of 13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14# GNU General Public License for more details. 15# 16# You should have received a copy of the GNU General Public License 17# along with this program. If not, see <http://www.gnu.org/licenses/>. 18 19 20from ReText import globalSettings 21from ReText.syncscroll import SyncScroll 22from ReText.preview import ReTextWebPreview 23 24from PyQt5.QtCore import QStandardPaths 25from PyQt5.QtGui import QDesktopServices, QTextDocument 26from PyQt5.QtNetwork import QNetworkDiskCache 27from PyQt5.QtWebKit import QWebSettings 28from PyQt5.QtWebKitWidgets import QWebPage, QWebView 29 30 31class ReTextWebKitPreview(ReTextWebPreview, QWebView): 32 33 def __init__(self, tab, 34 editorPositionToSourceLineFunc, 35 sourceLineToEditorPositionFunc): 36 37 QWebView.__init__(self) 38 self.tab = tab 39 40 self.syncscroll = SyncScroll(self.page().mainFrame(), 41 editorPositionToSourceLineFunc, 42 sourceLineToEditorPositionFunc) 43 ReTextWebPreview.__init__(self, tab.editBox) 44 45 self.page().setLinkDelegationPolicy(QWebPage.LinkDelegationPolicy.DelegateAllLinks) 46 self.page().linkClicked.connect(self._handleLinkClicked) 47 self.settings().setAttribute(QWebSettings.WebAttribute.LocalContentCanAccessFileUrls, False) 48 # Avoid caching of CSS 49 self.settings().setObjectCacheCapacities(0,0,0) 50 51 self.cache = QNetworkDiskCache() 52 cacheDirectory = QStandardPaths.writableLocation(QStandardPaths.StandardLocation.CacheLocation) 53 self.cache.setCacheDirectory(cacheDirectory) 54 self.page().networkAccessManager().setCache(self.cache) 55 56 def updateFontSettings(self): 57 settings = self.settings() 58 settings.setFontFamily(QWebSettings.FontFamily.StandardFont, 59 globalSettings.font.family()) 60 settings.setFontSize(QWebSettings.FontSize.DefaultFontSize, 61 globalSettings.font.pointSize()) 62 63 def _handleWheelEvent(self, event): 64 # Only pass wheelEvents on to the preview if syncscroll is 65 # controlling the position of the preview 66 if self.syncscroll.isActive(): 67 self.wheelEvent(event) 68 69 def _handleLinkClicked(self, url): 70 if url.isLocalFile(): 71 localFile = url.toLocalFile() 72 if localFile == self.tab.fileName and url.hasFragment(): 73 self.page().mainFrame().scrollToAnchor(url.fragment()) 74 return 75 if self.tab.openSourceFile(localFile): 76 return 77 if globalSettings.handleWebLinks: 78 self.load(url) 79 else: 80 QDesktopServices.openUrl(url) 81 82 def findText(self, text, flags): 83 options = QWebPage.FindFlag.FindWrapsAroundDocument 84 if flags & QTextDocument.FindFlag.FindBackward: 85 options |= QWebPage.FindFlag.FindBackward 86 if flags & QTextDocument.FindFlag.FindCaseSensitively: 87 options |= QWebPage.FindFlag.FindCaseSensitively 88 return super().findText(text, options) 89