Esempio n. 1
0
    def __init__(self, parent, filename='', lexer='', autosave=True):
        FocusProxy.__init__(self)
        WinterObject.__init__(self)
        self.QSCI_SUPPORT = QSCI_SUPPORT
        self.parent = parent
        self.autosave = autosave
        try:
            self.api = parent.API()
        except AttributeError:
            from baseQt import API

            self.api = API()
        self.filename = filename
        lay = QVBoxLayout()
        self.tb = QToolBar(self)
        lay.addWidget(self.tb)

        self.tb_save = QToolButton()
        #        self.tb_save.setIcon(QIcon.fromTheme('document-save', QIcon(self.api.icons['filesave'])))
        #        self.connect(self.tb_save, SIGNAL("clicked()"), self.save)
        #        self.tb.addWidget(self.tb_save)

        if QSCI_SUPPORT:
            editor = QsciScintilla(self)
            editor.setUtf8(True)
            self.lexers = {'Bash': QsciLexerBash, 'Batch': QsciLexerBatch, 'CMake': QsciLexerCMake, 'CPP': QsciLexerCPP,
                           'CSS': QsciLexerCSS, 'D': QsciLexerD, 'Diff': QsciLexerDiff, 'Fortran': QsciLexerFortran77,
                           'HTML': QsciLexerHTML, 'Lua': QsciLexerLua, 'Make': QsciLexerMakefile,
                           'Pascal': QsciLexerPascal,
                           'Perl': QsciLexerPerl, 'PostScript': QsciLexerPostScript, 'POV': QsciLexerPOV,
                           'Properties': QsciLexerProperties, 'Python': QsciLexerPython, 'Ruby': QsciLexerRuby,
                           'SQL': QsciLexerSQL, 'TCL': QsciLexerTCL, 'TeX': QsciLexerTeX,
                           'VHDL': QsciLexerVHDL, 'YAML': QsciLexerYAML, 'Plain': QsciLexerPython}

            if lexer:
                lex = self.lexers[lexer]()
                lex.setPaper(QColor(self.parent.config.options.qsci.bg_color))
                #lex.setColor(QColor(self.parent.config.options.qsci.fg_color))
                editor.setLexer(lex)

            editor.resetSelectionBackgroundColor()
            editor.resetSelectionForegroundColor()
            editor.setCaretLineBackgroundColor(QColor(self.parent.config.options.qsci.caretline_color))
            editor.setMarginsBackgroundColor(QColor(self.parent.config.options.qsci.margins_bg_color))
            editor.setMarginsForegroundColor(QColor(self.parent.config.options.qsci.margins_fg_color))
            editor.setFoldMarginColors(QColor(self.parent.config.options.qsci.foldmargin_prim_color),
                QColor(self.parent.config.options.qsci.foldmargin_sec_color))
            editor.setSelectionBackgroundColor(QColor(self.parent.config.options.qsci.selection_bg_color))
            editor.setSelectionForegroundColor(QColor(self.parent.config.options.qsci.selection_fg_color))
            editor.setFolding(self.parent.config.options.qsci.folding)
            editor.setPaper(QColor(self.parent.config.options.qsci.bg_color))
            editor.setColor(QColor(self.parent.config.options.qsci.fg_color))

            font = QFont()
            font.setFamily(self.parent.config.options.qsci.font)
            font.setFixedPitch(True)
            font.setPointSize(self.parent.config.options.qsci.font_size)
            fm = QFontMetrics(font)
            editor.setFont(font)
            if self.parent.config.options.qsci.linenumbers:
                editor.setMarginsFont(font)
                editor.setMarginWidth(0, fm.width("000") + 5)
                editor.setMarginLineNumbers(0, True)
            if self.parent.config.options.qsci.folding:
                editor.setFolding(QsciScintilla.BoxedTreeFoldStyle)
            editor.setBraceMatching(QsciScintilla.SloppyBraceMatch)
            editor.setCaretLineVisible(self.parent.config.options.qsci.caretline)




        else:
            editor = QTextEdit(self)

        self.editor = editor
        self.focused = editor
        lay.addWidget(editor)
        if filename:
            self.open(filename)

        self.setLayout(lay)

        if self.autosave:
            self.editor.focusOutEvent = self.focusOutEvent
Esempio n. 2
0
class WinterEditor(FocusProxy, WinterObject):
    def __init__(self, parent, filename='', lexer='', autosave=True):
        FocusProxy.__init__(self)
        WinterObject.__init__(self)
        self.QSCI_SUPPORT = QSCI_SUPPORT
        self.parent = parent
        self.autosave = autosave
        try:
            self.api = parent.API()
        except AttributeError:
            from baseQt import API

            self.api = API()
        self.filename = filename
        lay = QVBoxLayout()
        self.tb = QToolBar(self)
        lay.addWidget(self.tb)

        self.tb_save = QToolButton()
        #        self.tb_save.setIcon(QIcon.fromTheme('document-save', QIcon(self.api.icons['filesave'])))
        #        self.connect(self.tb_save, SIGNAL("clicked()"), self.save)
        #        self.tb.addWidget(self.tb_save)

        if QSCI_SUPPORT:
            editor = QsciScintilla(self)
            editor.setUtf8(True)
            self.lexers = {'Bash': QsciLexerBash, 'Batch': QsciLexerBatch, 'CMake': QsciLexerCMake, 'CPP': QsciLexerCPP,
                           'CSS': QsciLexerCSS, 'D': QsciLexerD, 'Diff': QsciLexerDiff, 'Fortran': QsciLexerFortran77,
                           'HTML': QsciLexerHTML, 'Lua': QsciLexerLua, 'Make': QsciLexerMakefile,
                           'Pascal': QsciLexerPascal,
                           'Perl': QsciLexerPerl, 'PostScript': QsciLexerPostScript, 'POV': QsciLexerPOV,
                           'Properties': QsciLexerProperties, 'Python': QsciLexerPython, 'Ruby': QsciLexerRuby,
                           'SQL': QsciLexerSQL, 'TCL': QsciLexerTCL, 'TeX': QsciLexerTeX,
                           'VHDL': QsciLexerVHDL, 'YAML': QsciLexerYAML, 'Plain': QsciLexerPython}

            if lexer:
                lex = self.lexers[lexer]()
                lex.setPaper(QColor(self.parent.config.options.qsci.bg_color))
                #lex.setColor(QColor(self.parent.config.options.qsci.fg_color))
                editor.setLexer(lex)

            editor.resetSelectionBackgroundColor()
            editor.resetSelectionForegroundColor()
            editor.setCaretLineBackgroundColor(QColor(self.parent.config.options.qsci.caretline_color))
            editor.setMarginsBackgroundColor(QColor(self.parent.config.options.qsci.margins_bg_color))
            editor.setMarginsForegroundColor(QColor(self.parent.config.options.qsci.margins_fg_color))
            editor.setFoldMarginColors(QColor(self.parent.config.options.qsci.foldmargin_prim_color),
                QColor(self.parent.config.options.qsci.foldmargin_sec_color))
            editor.setSelectionBackgroundColor(QColor(self.parent.config.options.qsci.selection_bg_color))
            editor.setSelectionForegroundColor(QColor(self.parent.config.options.qsci.selection_fg_color))
            editor.setFolding(self.parent.config.options.qsci.folding)
            editor.setPaper(QColor(self.parent.config.options.qsci.bg_color))
            editor.setColor(QColor(self.parent.config.options.qsci.fg_color))

            font = QFont()
            font.setFamily(self.parent.config.options.qsci.font)
            font.setFixedPitch(True)
            font.setPointSize(self.parent.config.options.qsci.font_size)
            fm = QFontMetrics(font)
            editor.setFont(font)
            if self.parent.config.options.qsci.linenumbers:
                editor.setMarginsFont(font)
                editor.setMarginWidth(0, fm.width("000") + 5)
                editor.setMarginLineNumbers(0, True)
            if self.parent.config.options.qsci.folding:
                editor.setFolding(QsciScintilla.BoxedTreeFoldStyle)
            editor.setBraceMatching(QsciScintilla.SloppyBraceMatch)
            editor.setCaretLineVisible(self.parent.config.options.qsci.caretline)




        else:
            editor = QTextEdit(self)

        self.editor = editor
        self.focused = editor
        lay.addWidget(editor)
        if filename:
            self.open(filename)

        self.setLayout(lay)

        if self.autosave:
            self.editor.focusOutEvent = self.focusOutEvent

    def open(self, filename):
        self.filename = filename
        try:
            self.editor.setText(getFileContent(filename))
        except IOError:
            open(filename, "w").write('')

    def focusOutEvent(self, event):
        self.save()

    def WFind(self, text):
        return self.findFirst(text, False, False, False, True)

    def WFindNext(self):
        return self.findNext()

    def WFindPrev(self):
        return self.findPrev()

    def _afterAppInit(self):
        if QSCI_SUPPORT:
            self.tb.addAction(self.api.ex('createAction')('document-save', 'Save', self.save))
            self.tb.addAction(self.api.ex('createAction')('edit-undo', 'Undo', self.editor.undo))
            self.tb.addAction(self.api.ex('createAction')('edit-redo', 'Undo', self.editor.redo))
            self.tb.addWidget(QWidget().setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding))
            self.tb.addWidget(QLabel('Search:  '))
            self.tb.addWidget(WinterSearch(self.editor))


    def save(self):
        try:
            f = open(self.filename, 'w')
            f.write(self.editor.text().toUtf8())
            f.close()
            self.onSave()
        except Exception as e:
            self.api.error(e)
            self.parent.statusBar.showMessage('Saving unseccessfull')

    def onSave(self):
        self.parent.statusBar.showMessage('%s saved' % self.filename)

    def text(self):
        return self.editor.text()