[Eric] Help about the code of Eric

projetmbc projetmbc at club-internet.fr
Sat Jul 11 09:48:11 BST 2009


Hello,
I finally decide to try understand your code about LexerPygment so as to 
define a custom lexer via Qsci.QsciLexerCustom. Here is the code I've made.

The problem with that lexer is that it doesn't colorize in red the line 
which begins by #. Do you see why ?

=====================================================
class myLexer(Qsci.QsciLexerCustom):
    def description(self, style):
        if style == 0:
            return 'ID_DEFAULT'
        if style == 1:
            return 'ID_COMMENT'
        return ''

    def defaultColor(self, style):
        if style==ID_DEFAULT:
            return QtGui.QColor(0,0,255)
        if style==ID_COMMENT:
            return QtGui.QColor(255,0,0)
        return QtGui.QColor(0,0,0)

    def styleText(self, start, end):
# Taken from the source code of Eric.
        self.editor.startStyling(start)
        if self.editor.text()[0] == '#':
            self.editor.setStyling(end, ID_COMMENT)
            self.editor.startStyling(start+end)
        else:
            self.editor.setStyling(end+1, ID_DEFAULT)
=====================================================

Can you help to find the mistakes I've made ?

Best regards.
Christophe.

-------------- next part --------------
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'C:\Documents and Settings\Christophe\Mes documents\2,pyBaNaMa\DebuterAvecPythonEtPyQT\CodesProjets\04-Proj4_MiniEditHTML\05-TestColorSyntax(WebKit)\window_LecteurCodePython_HTML.ui     '
#
# Created: Thu Aug 28 12:37:43 2008
#      by: PyQt4 UI code generator 4.4.3
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

class Ui_window_LecteurCodePython_HTML(object):
    def setupUi(self, window_LecteurCodePython_HTML):
        window_LecteurCodePython_HTML.setObjectName("window_LecteurCodePython_HTML")
        window_LecteurCodePython_HTML.resize(516, 397)
        self.centralwidget = QtGui.QWidget(window_LecteurCodePython_HTML)
        self.centralwidget.setObjectName("centralwidget")
        self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget)
        self.verticalLayout.setObjectName("verticalLayout")
        self.textScintilla_Principal = Qsci.QsciScintilla(self.centralwidget)
        self.textScintilla_Principal.setObjectName("textScintilla_Principal")
        self.verticalLayout.addWidget(self.textScintilla_Principal)
        window_LecteurCodePython_HTML.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(window_LecteurCodePython_HTML)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 516, 21))
        self.menubar.setObjectName("menubar")
        self.menuFichier = QtGui.QMenu(self.menubar)
        self.menuFichier.setObjectName("menuFichier")
        window_LecteurCodePython_HTML.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(window_LecteurCodePython_HTML)
        self.statusbar.setObjectName("statusbar")
        window_LecteurCodePython_HTML.setStatusBar(self.statusbar)
        self.actionOuvrir = QtGui.QAction(window_LecteurCodePython_HTML)
        self.actionOuvrir.setObjectName("actionOuvrir")
        self.actionEnregistrer = QtGui.QAction(window_LecteurCodePython_HTML)
        self.actionEnregistrer.setObjectName("actionEnregistrer")
        self.actionExporterHTML = QtGui.QAction(window_LecteurCodePython_HTML)
        self.actionExporterHTML.setObjectName("actionExporterHTML")
        self.menuFichier.addAction(self.actionOuvrir)
        self.menuFichier.addAction(self.actionEnregistrer)
        self.menuFichier.addSeparator()
        self.menuFichier.addAction(self.actionExporterHTML)
        self.menubar.addAction(self.menuFichier.menuAction())

        self.retranslateUi(window_LecteurCodePython_HTML)
        QtCore.QMetaObject.connectSlotsByName(window_LecteurCodePython_HTML)

    def retranslateUi(self, window_LecteurCodePython_HTML):
        window_LecteurCodePython_HTML.setWindowTitle(QtGui.QApplication.translate("window_LecteurCodePython_HTML", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
        self.menuFichier.setTitle(QtGui.QApplication.translate("window_LecteurCodePython_HTML", "Actions", None, QtGui.QApplication.UnicodeUTF8))
        self.actionOuvrir.setText(QtGui.QApplication.translate("window_LecteurCodePython_HTML", "Ouvrir", None, QtGui.QApplication.UnicodeUTF8))
        self.actionOuvrir.setShortcut(QtGui.QApplication.translate("window_LecteurCodePython_HTML", "Ctrl+O", None, QtGui.QApplication.UnicodeUTF8))
        self.actionEnregistrer.setText(QtGui.QApplication.translate("window_LecteurCodePython_HTML", "Rechercher", None, QtGui.QApplication.UnicodeUTF8))
        self.actionEnregistrer.setShortcut(QtGui.QApplication.translate("window_LecteurCodePython_HTML", "Ctrl+S", None, QtGui.QApplication.UnicodeUTF8))
        self.actionExporterHTML.setText(QtGui.QApplication.translate("window_LecteurCodePython_HTML", "Exporter", None, QtGui.QApplication.UnicodeUTF8))
        self.actionExporterHTML.setShortcut(QtGui.QApplication.translate("window_LecteurCodePython_HTML", "Ctrl+E", None, QtGui.QApplication.UnicodeUTF8))

from PyQt4 import Qsci

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    window_LecteurCodePython_HTML = QtGui.QMainWindow()
    ui = Ui_window_LecteurCodePython_HTML()
    ui.setupUi(window_LecteurCodePython_HTML)
    window_LecteurCodePython_HTML.show()
    sys.exit(app.exec_())

-------------- next part --------------
# -*- coding: utf-8 -*-
#!/usr/bin/env python

import sys
from PyQt4 import QtCore, QtGui, Qsci
from window_LecteurCodePython_HTML import Ui_window_LecteurCodePython_HTML

ID_DEFAULT = 0
ID_COMMENT = 1

class myLexer(Qsci.QsciLexerCustom):
# Struggle for a lexer.
    def description(self, style):
# WARNING ! This method must be re-implemented by a sub-class.
        if style == 0:
            return 'ID_DEFAULT'
        if style == 1:
            return 'ID_COMMENT'
        return ''

    def defaultColor(self, style):
        if style==ID_DEFAULT:
            return QtGui.QColor(0,0,255)
        if style==ID_COMMENT: 
            return QtGui.QColor(255,0,0)
        return QtGui.QColor(0,0,0)

    def styleText(self, start, end):
# Taken from the source code of Eric.
        self.editor.startStyling(start)
        if self.editor.text()[0] == '#':
            self.editor.setStyling(end, ID_COMMENT)
            self.editor.startStyling(start+end)
        else:
            self.editor.setStyling(end+1, ID_DEFAULT)


class window_LecteurCodePython_HTML(QtGui.QMainWindow, Ui_window_LecteurCodePython_HTML):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        Ui_window_LecteurCodePython_HTML.__init__(self)
        self.setupUi(self)

        font = QtGui.QFont()
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setFixedPitch(True)
        font.setPointSize(10)
        fm = QtGui.QFontMetrics(font)

        self.textScintilla_Principal.setFont(font)
        self.textScintilla_Principal.setMarginsFont(font)
        self.textScintilla_Principal.setMarginLineNumbers(1,True)
        self.textScintilla_Principal.setMarginWidth(1, fm.width( "00000" ) + 5)

        self.textScintilla_Principal.setFolding(Qsci.QsciScintilla.BoxedTreeFoldStyle)
        self.textScintilla_Principal.setWrapMode(Qsci.QsciScintilla.WrapMode(Qsci.QsciScintillaBase.SC_WRAP_WORD))
        self.textScintilla_Principal.setWrapVisualFlags(Qsci.QsciScintilla.WrapVisualFlag(Qsci.QsciScintilla.WrapFlagByBorder), Qsci.QsciScintilla.WrapVisualFlag(Qsci.QsciScintilla.WrapFlagNone), 0)  

        self.textScintilla_Principal.setLexer(myLexer())
        textFichier= """# A red line
/* This must
be
a fold */
       
Nothing special for the moment  except that blue word."""
        self.textScintilla_Principal.setText(textFichier)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    LecteurCodePython = window_LecteurCodePython_HTML()
    LecteurCodePython.show()
    sys.exit(app.exec_())


More information about the Eric mailing list