Preferences/PreferencesLexer.py

Mon, 11 Mar 2019 19:50:50 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Mon, 11 Mar 2019 19:50:50 +0100
branch
sub_styles
changeset 6854
f4dd76230eea
parent 6853
0922aa829e5e
child 6855
4d80c8cc99a1
permissions
-rw-r--r--

PreferencesLexer, EditorHighlightingPage: got rid of the 'styles' and 'ind2style' lists (that was very old style programming).

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

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

"""
Module implementing a special QScintilla lexer to handle the preferences.
"""

from __future__ import unicode_literals

from PyQt5.QtCore import QCoreApplication
from PyQt5.Qsci import QsciLexer, QsciScintillaBase

import Preferences
import Globals


class PreferencesLexerError(Exception):
    """
    Class defining a special error for the PreferencesLexer class.
    """
    def __init__(self):
        """
        Constructor
        """
        self._errorMessage = QCoreApplication.translate(
            "PreferencesLexerError",
            "Unspecific PreferencesLexer error.")
        
    def __repr__(self):
        """
        Special method returning a representation of the exception.
        
        @return string representing the error message
        @rtype str
        """
        return repr(self._errorMessage)
        
    def __str__(self):
        """
        Special method returning a string representation of the exception.
        
        @return string representing the error message
        @rtype str
        """
        return self._errorMessage


class PreferencesLexerLanguageError(PreferencesLexerError):
    """
    Class defining a special error for the PreferencesLexer class.
    """
    def __init__(self, language):
        """
        Constructor
        
        @param language lexer language
        @rtype str
        """
        PreferencesLexerError.__init__(self)
        self._errorMessage = QCoreApplication.translate(
            "PreferencesLexerError",
            'Unsupported Lexer Language: {0}').format(language)


class PreferencesLexer(QsciLexer):
    """
    Subclass of QsciLexer to implement preferences specific lexer methods and
    delegate some methods to the real lexer object.
    """
    def __init__(self, language, parent=None):
        """
        Constructor
        
        @param language language of the lexer
        @type str
        @param parent parent widget of this lexer (QWidget)
        @exception PreferencesLexerLanguageError raised to indicate an invalid
            lexer language
        """
        super(PreferencesLexer, self).__init__(parent)
        
        # These default font families are taken from QScintilla
        if Globals.isWindowsPlatform():
            self.__defaultFontFamily = "Courier New"
        elif Globals.isMacPlatform():
            self.__defaultFontFamily = "Courier"
        else:
            self.__defaultFontFamily = "Bitstream Vera Sans Mono"
        
        # instantiate a lexer object for the given language
        import QScintilla.Lexers
        self.__lex = QScintilla.Lexers.getLexer(language)
        if self.__lex is None:
            raise PreferencesLexerLanguageError(language)
        
        # read the last stored values from preferences file
        self.__lex.readSettings(Preferences.Prefs.settings, "Scintilla")
        # TODO: add substyles
    
    def writeSettings(self):
        """
        Public method to write the lexer settings.
        """
        self.__lex.writeSettings(Preferences.Prefs.settings, "Scintilla")
        # TODO: add substyles
    
    def getStyles(self):
        """
        Public method to get a list of all supported styles.
        
        @return list of tuples each containing the description of the style,
            style number and sub-style number (or -1 for no sub-style)
        """
        styles = []
        
        for i in range(QsciScintillaBase.STYLE_MAX):
            desc = self.__lex.description(i)
            if desc:
                styles.append((desc, i, -1))
        # TODO: add substyles
        
        return styles
    
    def defaultColor(self, style):
        """
        Public method to get the default color of a style.
        
        @param style style number
        @type int
        @return default color
        @rtype QColor
        """
        return self.__lex.defaultColor(style)
    
    def color(self, style):
        """
        Public method to get the color of a style.
        
        @param style style number
        @type int
        @return color
        @rtype QColor
        """
        return self.__lex.color(style)
    
    def setColor(self, c, style):
        """
        Public method to set the color for a style.
        
        @param c color
        @type QColor
        @param style style number
        @type int
        """
        self.__lex.setColor(c, style)
    
    def defaultPaper(self, style):
        """
        Public method to get the default background for a style.
        
        @param style style number
        @type int
        @return default background color
        @rtype QColor
        """
        return self.__lex.defaultPaper(style)
    
    def paper(self, style):
        """
        Public method to get the background for a style.
        
        @param style the style number
        @type int
        @return background color
        @rtype QColor
        """
        return self.__lex.paper(style)
    
    def setPaper(self, c, style):
        """
        Public method to set the background for a style.
        
        @param c background color
        @type QColor
        @param style style number
        @type int
        """
        self.__lex.setPaper(c, style)
    
    def defaulEolFill(self, style):
        """
        Public method to get the default eolFill flag for a style.
        
        @param style style number
        @type int
        @return default eolFill flag
        @rtype bool
        """
        return self.__lex.defaultEolFill(style)
    
    def eolFill(self, style):
        """
        Public method to get the eolFill flag for a style.
        
        @param style style number
        @type int
        @return eolFill flag
        @rtype bool
        """
        return self.__lex.eolFill(style)
    
    def setEolFill(self, eolfill, style):
        """
        Public method to set the eolFill flag for a style.
        
        @param eolfill eolFill flag
        @type bool
        @param style style number
        @type int
        """
        self.__lex.setEolFill(eolfill, style)
    
    def defaultFont(self, style):
        """
        Public method to get the default font for a style.
        
        @param style style number
        @type int
        @return default font
        @rtype QFont
        """
        return self.__lex.defaultFont(style)
    
    def font(self, style):
        """
        Public method to get the font for a style.
        
        @param style style number
        @type int
        @return font
        @rtype QFont
        """
        return self.__lex.font(style)
    
    def setFont(self, f, style):
        """
        Public method to set the font for a style.
        
        @param f font
        @type QFont
        @param style style number
        @type int
        """
        self.__lex.setFont(f, style)
    
    def language(self):
        """
        Public method to get the lexers programming language.
        
        @return lexer programming language
        @rtype str
        """
        return self.__lex.language()
        
    def description(self, style):
        """
        Public method to get a descriptive string for a style.
        
        @param style style number
        @type int
        @return description of the style
        @rtype str
        """
        return self.__lex.description(style)

eric ide

mercurial