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

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

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

import sys

from qtext import QextScintillaLexer, QextScintilla
from qt import *

import Preferences

class PreferencesLexer(QextScintillaLexer):
    """ 
    Subclass of QextScintillaLexer to implement preferences specific lexer methods.
    """
    def __init__(self, language, parent=None, name=None):
        QextScintillaLexer.__init__(self, parent, name)
        
        # instantiate a lexer object for the given language
        if language == 'Python':
            from QScintilla.LexerPython import LexerPython
            self.lex = LexerPython(self)
            self.lex.setAutoIndentStyle(QextScintilla.AiMaintain)
        elif language == 'C++':
            from QScintilla.LexerCPP import LexerCPP
            self.lex = LexerCPP(self)
            self.lex.setAutoIndentStyle(QextScintilla.AiOpening | QextScintilla.AiClosing)
        elif language == 'C#':
            from QScintilla.LexerCSharp import LexerCSharp
            self.lex = LexerCSharp(self)
            self.lex.setAutoIndentStyle(QextScintilla.AiOpening | QextScintilla.AiClosing)
        elif language == 'IDL':
            from QScintilla.LexerIDL import LexerIDL
            self.lex = LexerIDL(self)
            self.lex.setAutoIndentStyle(QextScintilla.AiOpening | QextScintilla.AiClosing)
        elif language == 'Java':
            from QScintilla.LexerJava import LexerJava
            self.lex = LexerJava(self)
            self.lex.setAutoIndentStyle(QextScintilla.AiOpening | QextScintilla.AiClosing)
        elif language == 'JavaScript':
            from QScintilla.LexerJavaScript import LexerJavaScript
            self.lex = LexerJavaScript(self)
            self.lex.setAutoIndentStyle(QextScintilla.AiOpening | QextScintilla.AiClosing)
        elif language == 'HTML':
            from QScintilla.LexerHTML import LexerHTML
            self.lex = LexerHTML(self)
            self.lex.setAutoIndentStyle(QextScintilla.AiMaintain)
        elif language == 'SQL':
            from QScintilla.LexerSQL import LexerSQL
            self.lex = LexerSQL(self)
            self.lex.setAutoIndentStyle(QextScintilla.AiMaintain)
        else:
            raise RuntimeError, \
                'Unsupported Lexer Language: %s' % language
                
        self.language = self.lex.language()
        
        # define the local store
        self.colours = {}
        self.papers = {}
        self.eolFills = {}
        self.fonts = {}
        self.ind2style = {}
        self.styles = QStringList()
        
        # fill local store with default values from lexer
        # and built up styles list and conversion table from index to style
        index = 0
        for i in range(128):
            desc = self.lex.description(i)
            if not desc.isEmpty():
                self.styles.append(desc)
                
                self.colours[i] = self.lex.color(i)
                self.papers[i] = self.lex.paper(i)
                self.eolFills[i] = self.lex.eolFill(i)
                self.fonts[i] = self.lex.font(i)
                
                self.ind2style[index] = i
                index += 1
            
        # read the last stored values from preferences file
        self.readSettings(Preferences.Prefs.settings, "/eric3/Scintilla")
        
    def color(self, style):
        """
        Reimplemented method to get the colour of a style.
        
        Arguments
        
            style -- the style number (int)
            
        Returns
        
            colour
        """
        return self.colours[style]
        
    def setColor(self, c, style):
        """
        Reimplemented method to set the colour for a style.
        
        Arguments
        
            c -- coulor (int)
            
            style -- the style number (int)
        """
        self.colours[style] = QColor(c)
        
    def paper(self, style):
        """
        Reimplemented method to get the background for a style.
        
        Arguments
        
            style -- the style number (int)
            
        Returns
        
            colour
        """
        return self.papers[style]
        
    def setPaper(self, c, style):
        """
        Reimplemented method to set the background for a style.
        
        Arguments
        
            c -- coulor (int)
            
            style -- the style number (int)
        """
        self.papers[style] = QColor(c)
        
    def eolFill(self, style):
        """
        Reimplemented method to get the eolFill flag for a style.
        
        Arguments
        
            style -- the style number (int)
            
        Returns
        
            eolFill flag
        """
        return self.eolFills[style]
        
    def setEolFill(self, eolfill, style):
        """
        Reimplemented method to set the eolFill flag for a style.
        
        Arguments
        
            eolfill -- eolFill flag (boolean)
            
            style -- the style number (int)
        """
        self.eolFills[style] = eolfill
        
    def font(self, style):
        """
        Reimplemented method to get the font for a style.
        
        Arguments
        
            style -- the style number (int)
            
        Returns
        
            font
        """
        return self.fonts[style]
        
    def setFont(self, f, style):
        """
        Reimplemented method to set the font for a style.
        
        Arguments
        
            f -- font
            
            style -- the style number (int)
        """
        self.fonts[style] = QFont(f)
        
    def language(self):
        """
        Public method to get the lexers programming language.
        
        Returns
        
            language
        """
        return self.language
        
    def lexer(self):
        """
        Public method to get a reference to the underlying lexer object.
        
        Returns
        
            reference to the lexer
        """
        return self.lex
        
    def description(self, style):
        """
        Public method to get a descriptive string for a style.
        
        Arguments
        
            style -- the style number (int)
            
        Returns
        
            description of the style (QString)
        """
        return self.lex.description(style)
        
# some test code
if __name__ == '__main__':
    lex = PreferencesLexer('Python')
    print "lexer done"
    lex.writeSettings(Preferences.Prefs.settings, '/eric3/Scintilla')
    print "written"
