eric7/HelpViewer/HelpViewerImpl.py

Tue, 12 Oct 2021 19:54:03 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Tue, 12 Oct 2021 19:54:03 +0200
branch
eric7
changeset 8681
6285e8374d99
parent 8680
85503ff2fce9
child 8683
e8a907801549
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 help viewer base class.
"""

from PyQt6.QtCore import pyqtSignal, QCoreApplication

AboutBlank = QCoreApplication.translate(
    "HelpViewer",
    "<html>"
    "<head><title>about:blank</title></head>"
    "<body></body>"
    "</html>")

PageNotFound = QCoreApplication.translate(
    "HelpViewer",
    """<html>"""
    """<head><title>Error 404...</title></head>"""
    """<body><div align="center"><br><br>"""
    """<h1>The page could not be found</h1><br>"""
    """<h3>'{0}'</h3></div></body>"""
    """</html>""")


class HelpViewerImpl:
    """
    Class implementing the help viewer base class.
    
    This is the base class of help viewer implementations and defines the
    interface. Als subclasses must implement the these methods.
    """
    titleChanged = pyqtSignal()
    loadFinished = pyqtSignal(bool)
    zoomChanged = pyqtSignal()
    
    def __init__(self, engine):
        """
        Constructor
        
        @param engine reference to the help engine
        @type QHelpEngine
        """
        self._engine = engine
    
    def setUrl(self, url):
        """
        Public method to set the URL of the document to be shown.
        
        @param url source of the document
        @type QUrl
        @exception RuntimeError raised when not implemented
        """
        raise RuntimeError("Not implemented")
    
    def getData(self, url):
        """
        Public method to get the data to be shown.
        
        @param url URL to be loaded
        @type QUrl
        @return data to be shown
        @rtype str
        """
        scheme = url.scheme()
        if scheme == "about":
            if url.toString() == "about:blank":
                return AboutBlank
            else:
                return PageNotFound.format(url.toString())
        elif scheme in ("file", ""):
            filePath = url.toLocalFile()
            try:
                with open(filePath, "r", encoding="utf-8") as f:
                    htmlText = f.read()
                return htmlText
            except OSError:
                return PageNotFound.format(url.toString())
        elif scheme == "qthelp":
            if self._engine.findFile(url).isValid():
                data = bytes(self._engine.fileData(url)).decode("utf-8")
                if not data:
                    data = PageNotFound.format(url.toString())
                return data
            else:
                return PageNotFound.format(url.toString())
        else:
            # None is an indicator that we cannot handle the request
            return None
    
    def title(self):
        """
        Public method get the page title.
        
        @return page title
        @rtype str
        @exception RuntimeError raised when not implemented
        """
        raise RuntimeError("Not implemented")
        return ""
    
    def gotoHistory(self, index):
        """
        Public method to step through the history.
        
        @param index history index (<0 backward, >0 forward)
        @type int
        @exception RuntimeError raised when not implemented
        """
        raise RuntimeError("Not implemented")
    
    def scaleUp(self):
        """
        Public method to zoom in.
        
        @exception RuntimeError raised when not implemented
        """
        raise RuntimeError("Not implemented")
    
    def scaleDown(self):
        """
        Public method to zoom out.
        
        @exception RuntimeError raised when not implemented
        """
        raise RuntimeError("Not implemented")
    
    def resetScale(self):
        """
        Public method to reset the zoom level.
        
        @exception RuntimeError raised when not implemented
        """
        raise RuntimeError("Not implemented")
    
    def scale(self):
        """
        Public method to get the zoom level.
        
        @return current zoom level
        @rtype int
        @exception RuntimeError raised when not implemented
        """
        raise RuntimeError("Not implemented")
        return 0
    
    def isScaleUpAvailable(self):
        """
        Public method to check, if the max. zoom level is reached.
        
        @return flag indicating scale up is available
        @rtype bool
        @exception RuntimeError raised when not implemented
        """
        raise RuntimeError("Not implemented")
        return False
    
    def isScaleDownAvailable(self):
        """
        Public method to check, if the min. zoom level is reached.
        
        @return flag indicating scale down is available
        @rtype bool
        @exception RuntimeError raised when not implemented
        """
        raise RuntimeError("Not implemented")
        return False

eric ide

mercurial