eric7/HelpViewer/HelpViewerImpl_qtb.py

Mon, 11 Oct 2021 19:59:45 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Mon, 11 Oct 2021 19:59:45 +0200
branch
eric7
changeset 8680
85503ff2fce9
parent 8679
fd172973428e
child 8681
6285e8374d99
permissions
-rw-r--r--

Continued implementing the embedded help viewer widget.

# -*- coding: utf-8 -*-

# Copyright (c) 2021 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing the QTextBrowser based help viewer class.
"""

from PyQt6.QtCore import Qt, QByteArray, QUrl
from PyQt6.QtGui import QDesktopServices,  QImage
from PyQt6.QtWidgets import QTextBrowser

from .HelpViewerImpl import HelpViewerImpl


class HelpViewerImpl_qtb(HelpViewerImpl, QTextBrowser):
    """
    Class implementing the QTextBrowser based help viewer class.
    """
    def __init__(self, engine, parent=None):
        """
        Constructor
        
        @param engine reference to the help engine
        @type QHelpEngine
        @param parent reference to the parent widget
        @type QWidget
        """
        QTextBrowser.__init__(self,  parent=parent)
        HelpViewerImpl.__init__(self, engine)
        
        self.sourceChanged.connect(self.titleChanged)
    
    def setUrl(self, url):
        """
        Public method to set the URL of the document to be shown.
        
        @param url source of the document
        @type QUrl
        """
        self.setSource(url)
    
    def doSetSource(self, url, type):
        """
        Public method to load the data and show it.
        
        @param url DESCRIPTION
        @type TYPE
        @param type DESCRIPTION
        @type TYPE
        """
        data = self.getData(url)
        if data is None:
            QDesktopServices.openUrl(url)
            return
        
        if url != QUrl("about:blank"):
            super().doSetSource(url, type)
        
        self.setHtml(data)
        self.sourceChanged.emit(url)
        self.loadFinished.emit(True)
    
    def title(self):
        """
        Public method get the page title.
        
        @return page title
        @rtype str
        """
        return self.documentTitle()
    
    def loadResource(self, type_,  name):
        """
        Public method to load data of the specified type from the resource with
        the given name.
        
        @param type_ resource type
        @type int
        @param name resource name
        @type QUrl
        """
        ba = QByteArray()
        
        if type_ < 4:     # QTextDocument.ResourceType.MarkdownResource
            url = self._engine.findFile(name)
            ba = self._engine.fileData(url)
            if url.toString().lower().endswith(".svg"):
                image = QImage()
                image.loadFromData(ba, "svg")
                if not image.isNull():
                    return image
        
        return ba
    
    def mousePressEvent(self, evt):
        """
        Protected method called by a mouse press event.
        
        @param evt reference to the mouse event
        @type QMouseEvent
        """
        if evt.button() == Qt.MouseButton.XButton1:
            self.backward()
        elif evt.button() == Qt.MouseButton.XButton2:
            self.forward()
        else:
            super().mousePressEvent(evt)
    
    def gotoHistory(self, index):
        """
        Public method to step through the history.
        
        @param index history index (<0 backward, >0 forward)
        @type int
        """
        if index < 0:
            # backward
            for ind in range(-index):
                self.backward()
        else:
            # forward
            for ind in range(index):
                self.forward()

eric ide

mercurial