Ejemplo n.º 1
0
    def __init__(self, editBox, editorPositionToSourceLineFunc,
                 sourceLineToEditorPositionFunc):

        QWebView.__init__(self)

        self.editBox = editBox

        if not globalSettings.handleWebLinks:
            self.page().setLinkDelegationPolicy(QWebPage.DelegateExternalLinks)
            self.page().linkClicked.connect(QDesktopServices.openUrl)
        self.settings().setAttribute(
            QWebSettings.LocalContentCanAccessFileUrls, False)
        self.settings().setDefaultTextEncoding('utf-8')
        # Avoid caching of CSS
        self.settings().setObjectCacheCapacities(0, 0, 0)

        self.syncscroll = SyncScroll(self.page().mainFrame(),
                                     editorPositionToSourceLineFunc,
                                     sourceLineToEditorPositionFunc)

        # Events relevant to sync scrolling
        self.editBox.cursorPositionChanged.connect(
            self._handleCursorPositionChanged)
        self.editBox.verticalScrollBar().valueChanged.connect(
            self.syncscroll.handleEditorScrolled)
        self.editBox.resized.connect(self._handleEditorResized)

        # Scroll the preview when the mouse wheel is used to scroll
        # beyond the beginning/end of the editor
        self.editBox.scrollLimitReached.connect(self._handleWheelEvent)
Ejemplo n.º 2
0
class ReTextWebPreview(QWebView):
    def __init__(self, editBox, editorPositionToSourceLineFunc,
                 sourceLineToEditorPositionFunc):

        QWebView.__init__(self)

        self.editBox = editBox

        if not globalSettings.handleWebLinks:
            self.page().setLinkDelegationPolicy(QWebPage.DelegateExternalLinks)
            self.page().linkClicked.connect(QDesktopServices.openUrl)
        self.settings().setAttribute(
            QWebSettings.LocalContentCanAccessFileUrls, False)
        self.settings().setDefaultTextEncoding('utf-8')
        # Avoid caching of CSS
        self.settings().setObjectCacheCapacities(0, 0, 0)

        self.syncscroll = SyncScroll(self.page().mainFrame(),
                                     editorPositionToSourceLineFunc,
                                     sourceLineToEditorPositionFunc)

        # Events relevant to sync scrolling
        self.editBox.cursorPositionChanged.connect(
            self._handleCursorPositionChanged)
        self.editBox.verticalScrollBar().valueChanged.connect(
            self.syncscroll.handleEditorScrolled)
        self.editBox.resized.connect(self._handleEditorResized)

        # Scroll the preview when the mouse wheel is used to scroll
        # beyond the beginning/end of the editor
        self.editBox.scrollLimitReached.connect(self._handleWheelEvent)

    def disconnectExternalSignals(self):
        self.editBox.cursorPositionChanged.disconnect(
            self._handleCursorPositionChanged)
        self.editBox.verticalScrollBar().valueChanged.disconnect(
            self.syncscroll.handleEditorScrolled)
        self.editBox.resized.disconnect(self._handleEditorResized)

        self.editBox.scrollLimitReached.disconnect(self._handleWheelEvent)

    def _handleWheelEvent(self, event):
        """
		Use this intermediate function because it is not possible to
		disconnect a built-in method. It would generate the following error:
		  TypeError: 'builtin_function_or_method' object is not connected
		"""
        # Only pass wheelEvents on to the preview if syncscroll is
        # controlling the position of the preview
        if self.syncscroll.isActive():
            self.wheelEvent(event)

    def _handleCursorPositionChanged(self):
        editorCursorPosition = self.editBox.verticalScrollBar().value() + \
                 self.editBox.cursorRect().top()
        self.syncscroll.handleCursorPositionChanged(editorCursorPosition)

    def _handleEditorResized(self, rect):
        self.syncscroll.handleEditorResized(rect.height())
Ejemplo n.º 3
0
class ReTextWebPreview(QWebView):

	def __init__(self, editBox,
	             editorPositionToSourceLineFunc,
	             sourceLineToEditorPositionFunc):

		QWebView.__init__(self)

		self.editBox = editBox

		if not globalSettings.handleWebLinks:
			self.page().setLinkDelegationPolicy(QWebPage.DelegateExternalLinks)
			self.page().linkClicked.connect(QDesktopServices.openUrl)
		self.settings().setAttribute(QWebSettings.LocalContentCanAccessFileUrls, False)
		self.settings().setDefaultTextEncoding('utf-8')
		# Avoid caching of CSS
		self.settings().setObjectCacheCapacities(0,0,0)

		self.syncscroll = SyncScroll(self.page().mainFrame(),
					     editorPositionToSourceLineFunc,
					     sourceLineToEditorPositionFunc)

		# Events relevant to sync scrolling
		self.editBox.cursorPositionChanged.connect(self._handleCursorPositionChanged)
		self.editBox.verticalScrollBar().valueChanged.connect(self.syncscroll.handleEditorScrolled)
		self.editBox.resized.connect(self._handleEditorResized)

		# Scroll the preview when the mouse wheel is used to scroll
		# beyond the beginning/end of the editor
		self.editBox.scrollLimitReached.connect(self._handleWheelEvent)

	def disconnectExternalSignals(self):
		self.editBox.cursorPositionChanged.disconnect(self._handleCursorPositionChanged)
		self.editBox.verticalScrollBar().valueChanged.disconnect(self.syncscroll.handleEditorScrolled)
		self.editBox.resized.disconnect(self._handleEditorResized)

		self.editBox.scrollLimitReached.disconnect(self._handleWheelEvent)

	def _handleWheelEvent(self, event):
		"""
		Use this intermediate function because it is not possible to
		disconnect a built-in method. It would generate the following error:
		  TypeError: 'builtin_function_or_method' object is not connected
		"""
		# Only pass wheelEvents on to the preview if syncscroll is
		# controlling the position of the preview
		if self.syncscroll.isActive():
			self.wheelEvent(event)

	def _handleCursorPositionChanged(self):
		editorCursorPosition = self.editBox.verticalScrollBar().value() + \
				       self.editBox.cursorRect().top()
		self.syncscroll.handleCursorPositionChanged(editorCursorPosition)

	def _handleEditorResized(self, rect):
		self.syncscroll.handleEditorResized(rect.height())
Ejemplo n.º 4
0
    def __init__(self, tab, editorPositionToSourceLineFunc,
                 sourceLineToEditorPositionFunc):

        QWebEngineView.__init__(self, parent=tab)
        webPage = ReTextWebEnginePage(self, tab)
        self.setPage(webPage)

        self.syncscroll = SyncScroll(webPage, editorPositionToSourceLineFunc,
                                     sourceLineToEditorPositionFunc)
        ReTextWebPreview.__init__(self, tab.editBox)
Ejemplo n.º 5
0
    def __init__(self, tab, editorPositionToSourceLineFunc,
                 sourceLineToEditorPositionFunc):

        QWebEngineView.__init__(self, parent=tab)
        webPage = ReTextWebEnginePage(self, tab)
        self.setPage(webPage)

        self.syncscroll = SyncScroll(webPage, editorPositionToSourceLineFunc,
                                     sourceLineToEditorPositionFunc)
        ReTextWebPreview.__init__(self, tab.editBox)

        settings = self.settings()
        settings.setAttribute(QWebEngineSettings.LocalContentCanAccessFileUrls,
                              False)
Ejemplo n.º 6
0
	def __init__(self, editBox,
	             editorPositionToSourceLineFunc,
	             sourceLineToEditorPositionFunc):

		QWebView.__init__(self)

		self.editBox = editBox

		if not globalSettings.handleWebLinks:
			self.page().setLinkDelegationPolicy(QWebPage.DelegateExternalLinks)
			self.page().linkClicked.connect(QDesktopServices.openUrl)
		self.settings().setAttribute(QWebSettings.LocalContentCanAccessFileUrls, False)
		self.settings().setDefaultTextEncoding('utf-8')
		# Avoid caching of CSS
		self.settings().setObjectCacheCapacities(0,0,0)

		self.syncscroll = SyncScroll(self.page().mainFrame(),
					     editorPositionToSourceLineFunc,
					     sourceLineToEditorPositionFunc)

		# Events relevant to sync scrolling
		self.editBox.cursorPositionChanged.connect(self._handleCursorPositionChanged)
		self.editBox.verticalScrollBar().valueChanged.connect(self.syncscroll.handleEditorScrolled)
		self.editBox.resized.connect(self._handleEditorResized)

		# Scroll the preview when the mouse wheel is used to scroll
		# beyond the beginning/end of the editor
		self.editBox.scrollLimitReached.connect(self._handleWheelEvent)
Ejemplo n.º 7
0
class ReTextWebEnginePreview(ReTextWebPreview, QWebEngineView):
    def __init__(self, tab, editorPositionToSourceLineFunc,
                 sourceLineToEditorPositionFunc):

        QWebEngineView.__init__(self, parent=tab)
        webPage = ReTextWebEnginePage(self, tab)
        self.setPage(webPage)

        self.syncscroll = SyncScroll(webPage, editorPositionToSourceLineFunc,
                                     sourceLineToEditorPositionFunc)
        ReTextWebPreview.__init__(self, tab.editBox)

        settings = self.settings()
        settings.setAttribute(QWebEngineSettings.LocalContentCanAccessFileUrls,
                              False)

    def updateFontSettings(self):
        settings = self.settings()
        settings.setFontFamily(QWebEngineSettings.StandardFont,
                               globalSettings.font.family())
        # settings.setFontSize(QWebEngineSettings.DefaultFontSize,
        #                      globalSettings.font.pointSize())
        settings.setFontSize(QWebEngineSettings.DefaultFontSize, 15)

    def setHtml(self, html, baseUrl):
        # A hack to prevent WebEngine from stealing the focus
        self.setEnabled(False)
        QWebEngineView.setHtml(self, html, baseUrl)
        self.setEnabled(True)

    def _handleWheelEvent(self, event):
        # Only pass wheelEvents on to the preview if syncscroll is
        # controlling the position of the preview
        if self.syncscroll.isActive():
            QGuiApplication.sendEvent(self.focusProxy(), event)
Ejemplo n.º 8
0
class ReTextWebKitPreview(ReTextWebPreview, QWebView):
    def __init__(self, tab, editorPositionToSourceLineFunc,
                 sourceLineToEditorPositionFunc):

        QWebView.__init__(self)
        self.tab = tab

        self.syncscroll = SyncScroll(self.page().mainFrame(),
                                     editorPositionToSourceLineFunc,
                                     sourceLineToEditorPositionFunc)
        ReTextWebPreview.__init__(self, tab.editBox)

        self.page().setLinkDelegationPolicy(
            QWebPage.LinkDelegationPolicy.DelegateAllLinks)
        self.page().linkClicked.connect(self._handleLinkClicked)
        self.settings().setAttribute(
            QWebSettings.WebAttribute.LocalContentCanAccessFileUrls, False)
        # Avoid caching of CSS
        self.settings().setObjectCacheCapacities(0, 0, 0)

        self.cache = QNetworkDiskCache()
        cacheDirectory = QStandardPaths.writableLocation(
            QStandardPaths.StandardLocation.CacheLocation)
        self.cache.setCacheDirectory(cacheDirectory)
        self.page().networkAccessManager().setCache(self.cache)

    def updateFontSettings(self):
        settings = self.settings()
        settings.setFontFamily(QWebSettings.FontFamily.StandardFont,
                               globalSettings.font.family())
        settings.setFontSize(QWebSettings.FontSize.DefaultFontSize,
                             globalSettings.font.pointSize())

    def _handleWheelEvent(self, event):
        # Only pass wheelEvents on to the preview if syncscroll is
        # controlling the position of the preview
        if self.syncscroll.isActive():
            self.wheelEvent(event)

    def _handleLinkClicked(self, url):
        if url.isLocalFile():
            localFile = url.toLocalFile()
            if localFile == self.tab.fileName and url.hasFragment():
                self.page().mainFrame().scrollToAnchor(url.fragment())
                return
            if self.tab.openSourceFile(localFile):
                return
        if globalSettings.handleWebLinks:
            self.load(url)
        else:
            QDesktopServices.openUrl(url)

    def findText(self, text, flags):
        options = QWebPage.FindFlag.FindWrapsAroundDocument
        if flags & QTextDocument.FindFlag.FindBackward:
            options |= QWebPage.FindFlag.FindBackward
        if flags & QTextDocument.FindFlag.FindCaseSensitively:
            options |= QWebPage.FindFlag.FindCaseSensitively
        return super().findText(text, options)
Ejemplo n.º 9
0
	def __init__(self, tab,
	             editorPositionToSourceLineFunc,
	             sourceLineToEditorPositionFunc):

		QWebView.__init__(self)
		self.tab = tab

		self.syncscroll = SyncScroll(self.page().mainFrame(),
		                             editorPositionToSourceLineFunc,
		                             sourceLineToEditorPositionFunc)
		ReTextWebPreview.__init__(self, tab.editBox)

		self.page().setLinkDelegationPolicy(QWebPage.DelegateAllLinks)
		self.page().linkClicked.connect(self._handleLinkClicked)
		self.settings().setAttribute(QWebSettings.LocalContentCanAccessFileUrls, False)
		# Avoid caching of CSS
		self.settings().setObjectCacheCapacities(0,0,0)
Ejemplo n.º 10
0
class ReTextWebEnginePreview(ReTextWebPreview, QWebEngineView):
    def __init__(self, tab, editorPositionToSourceLineFunc,
                 sourceLineToEditorPositionFunc):

        QWebEngineView.__init__(self, parent=tab)
        webPage = ReTextWebEnginePage(self, tab)
        self.setPage(webPage)

        self.syncscroll = SyncScroll(webPage, editorPositionToSourceLineFunc,
                                     sourceLineToEditorPositionFunc)
        ReTextWebPreview.__init__(self, tab.editBox)

    def updateFontSettings(self):
        settings = self.settings()
        settings.setFontFamily(QWebEngineSettings.FontFamily.StandardFont,
                               globalSettings.font.family())
        settings.setFontSize(QWebEngineSettings.FontSize.DefaultFontSize,
                             globalSettings.font.pointSize())

    def setHtml(self, html, baseUrl):
        # A hack to prevent WebEngine from stealing the focus
        self.setEnabled(False)
        super().setHtml(html, baseUrl)
        self.setEnabled(True)

    def _handleWheelEvent(self, event):
        # Only pass wheelEvents on to the preview if syncscroll is
        # controlling the position of the preview
        if self.syncscroll.isActive():
            QGuiApplication.sendEvent(self.focusProxy(), event)

    def event(self, event):
        # Work-around https://bugreports.qt.io/browse/QTBUG-43602
        if event.type() == QEvent.Type.ChildAdded:
            event.child().installEventFilter(self)
        elif event.type() == QEvent.Type.ChildRemoved:
            event.child().removeEventFilter(self)
        return super().event(event)

    def eventFilter(self, object, event):
        if event.type() == QEvent.Type.Wheel:
            if QGuiApplication.keyboardModifiers(
            ) == Qt.KeyboardModifier.ControlModifier:
                self.wheelEvent(event)
                return True
        return False

    def findText(self, text, flags):
        options = QWebEnginePage.FindFlags()
        if flags & QTextDocument.FindFlag.FindBackward:
            options |= QWebEnginePage.FindFlag.FindBackward
        if flags & QTextDocument.FindFlag.FindCaseSensitively:
            options |= QWebEnginePage.FindFlag.FindCaseSensitively
        super().findText(text, options)
        return True
Ejemplo n.º 11
0
    def __init__(self, tab, editorPositionToSourceLineFunc,
                 sourceLineToEditorPositionFunc):

        QWebView.__init__(self)
        self.tab = tab

        self.syncscroll = SyncScroll(self.page().mainFrame(),
                                     editorPositionToSourceLineFunc,
                                     sourceLineToEditorPositionFunc)
        ReTextWebPreview.__init__(self, tab.editBox)

        self.page().setLinkDelegationPolicy(QWebPage.DelegateAllLinks)
        self.page().linkClicked.connect(self._handleLinkClicked)
        self.settings().setAttribute(
            QWebSettings.LocalContentCanAccessFileUrls, False)
        # Avoid caching of CSS
        self.settings().setObjectCacheCapacities(0, 0, 0)

        self.cache = QNetworkDiskCache()
        cacheDirectory = QStandardPaths.writableLocation(
            QStandardPaths.CacheLocation)
        self.cache.setCacheDirectory(cacheDirectory)
        self.page().networkAccessManager().setCache(self.cache)
Ejemplo n.º 12
0
    def __init__(self, tab,
                 editorPositionToSourceLineFunc,
                 sourceLineToEditorPositionFunc):

        QWebEngineView.__init__(self, parent=tab)
        webPage = ReTextWebEnginePage(self, tab)
        self.setPage(webPage)

        self.syncscroll = SyncScroll(webPage,
                                     editorPositionToSourceLineFunc,
                                     sourceLineToEditorPositionFunc)
        ReTextWebPreview.__init__(self, tab.editBox)

        settings = self.settings()
        settings.setAttribute(QWebEngineSettings.LocalContentCanAccessFileUrls,
                              False)
Ejemplo n.º 13
0
class ReTextWebKitPreview(ReTextWebPreview, QWebView):

	def __init__(self, tab,
	             editorPositionToSourceLineFunc,
	             sourceLineToEditorPositionFunc):

		QWebView.__init__(self)
		self.tab = tab

		self.syncscroll = SyncScroll(self.page().mainFrame(),
		                             editorPositionToSourceLineFunc,
		                             sourceLineToEditorPositionFunc)
		ReTextWebPreview.__init__(self, tab.editBox)

		self.page().setLinkDelegationPolicy(QWebPage.DelegateAllLinks)
		self.page().linkClicked.connect(self._handleLinkClicked)
		self.settings().setAttribute(QWebSettings.LocalContentCanAccessFileUrls, False)
		# Avoid caching of CSS
		self.settings().setObjectCacheCapacities(0,0,0)

	def updateFontSettings(self):
		settings = self.settings()
		settings.setFontFamily(QWebSettings.StandardFont,
		                       globalSettings.font.family())
		settings.setFontSize(QWebSettings.DefaultFontSize,
		                     globalSettings.font.pointSize())

	def _handleWheelEvent(self, event):
		# Only pass wheelEvents on to the preview if syncscroll is
		# controlling the position of the preview
		if self.syncscroll.isActive():
			self.wheelEvent(event)

	def _handleLinkClicked(self, url):
		if url.isLocalFile():
			localFile = url.toLocalFile()
			if localFile == self.tab.fileName and url.hasFragment():
				self.page().mainFrame().scrollToAnchor(url.fragment())
				return
			if self.tab.openSourceFile(localFile):
				return
		if globalSettings.handleWebLinks:
			self.load(url)
		else:
			QDesktopServices.openUrl(url)
Ejemplo n.º 14
0
class ReTextWebEnginePreview(ReTextWebPreview, QWebEngineView):

    def __init__(self, tab,
                 editorPositionToSourceLineFunc,
                 sourceLineToEditorPositionFunc):

        QWebEngineView.__init__(self, parent=tab)
        webPage = ReTextWebEnginePage(self, tab)
        self.setPage(webPage)

        self.syncscroll = SyncScroll(webPage,
                                     editorPositionToSourceLineFunc,
                                     sourceLineToEditorPositionFunc)
        ReTextWebPreview.__init__(self, tab.editBox)

        settings = self.settings()
        settings.setAttribute(QWebEngineSettings.LocalContentCanAccessFileUrls,
                              False)

    def updateFontSettings(self):
        settings = self.settings()
        settings.setFontFamily(QWebEngineSettings.StandardFont,
                               globalSettings.font.family())
        settings.setFontSize(QWebEngineSettings.DefaultFontSize,
                             globalSettings.font.pointSize())

    def setHtml(self, html, baseUrl):
        # A hack to prevent WebEngine from stealing the focus
        self.setEnabled(False)
        QWebEngineView.setHtml(self, html, baseUrl)
        self.setEnabled(True)

    def _handleWheelEvent(self, event):
        # Only pass wheelEvents on to the preview if syncscroll is
        # controlling the position of the preview
        if self.syncscroll.isActive():
            QGuiApplication.sendEvent(self.focusProxy(), event)