diff --git a/plugins/extensions/pykrita/plugin/plugins/scripter/fontscombobox.py b/plugins/extensions/pykrita/plugin/plugins/scripter/fontscombobox.py new file mode 100644 --- /dev/null +++ b/plugins/extensions/pykrita/plugin/plugins/scripter/fontscombobox.py @@ -0,0 +1,28 @@ +from PyQt5.QtWidgets import * +from PyQt5.QtGui import * +from PyQt5.QtCore import * + +class FontsComboBox(QComboBox): + + def __init__(self, editor, parent=None): + super(FontsComboBox, self).__init__(parent) + + self.editor = editor + + _fontDataBase = QFontDatabase() + + self.addItems(_fontDataBase.families()) + self.setCurrentIndex(self.findText(self.editor.font)) + + com = QCompleter() + com.setCaseSensitivity(Qt.CaseInsensitive) + com.setCompletionMode(QCompleter.PopupCompletion) + + # Style sheet to set false on combobox-popup + self.setStyleSheet("QComboBox { combobox-popup: 0; }") + self.setMaxVisibleItems(10) + self.setCompleter(com) + self.currentIndexChanged.connect(self._currentIndexChanged) + + def _currentIndexChanged(self, index): + self.editor.font = self.itemText(index) diff --git a/plugins/extensions/pykrita/plugin/plugins/scripter/pythoneditor.py b/plugins/extensions/pykrita/plugin/plugins/scripter/pythoneditor.py --- a/plugins/extensions/pykrita/plugin/plugins/scripter/pythoneditor.py +++ b/plugins/extensions/pykrita/plugin/plugins/scripter/pythoneditor.py @@ -20,6 +20,7 @@ self.updateLineNumberAreaWidth() self.highlightCurrentLine() + self.font = "Monospace" def lineNumberAreaWidth(self): """The lineNumberAreaWidth is the quatity of decimal places in blockCount""" @@ -98,3 +99,12 @@ self.zoomIn() else: super(CodeEditor, self).wheelEvent(e) + + @property + def font(self): + return self._font + + @font.setter + def font(self, font): + self._font = font + self.setFont(QFont(font, 10)) diff --git a/plugins/extensions/pykrita/plugin/plugins/scripter/scripter.py b/plugins/extensions/pykrita/plugin/plugins/scripter/scripter.py --- a/plugins/extensions/pykrita/plugin/plugins/scripter/scripter.py +++ b/plugins/extensions/pykrita/plugin/plugins/scripter/scripter.py @@ -56,9 +56,6 @@ dialog = QDialog() dialog.setWindowModality(Qt.NonModal) self.editor = pythoneditor.CodeEditor() - f = QFont("monospace", 10, QFont.Normal) - f.setFixedPitch(True) - self.editor.document().setDefaultFont(f) self.highlight = syntax.PythonHighlighter(self.editor.document(), syntaxstyles.DefaultSyntaxStyle()) vbox = QVBoxLayout(dialog) vbox.addWidget(self.editor) diff --git a/plugins/extensions/pykrita/plugin/plugins/scripter/settingsdialog.py b/plugins/extensions/pykrita/plugin/plugins/scripter/settingsdialog.py --- a/plugins/extensions/pykrita/plugin/plugins/scripter/settingsdialog.py +++ b/plugins/extensions/pykrita/plugin/plugins/scripter/settingsdialog.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- from PyQt5.QtWidgets import * -from scripter import syntaxstylescombobox +from scripter import syntaxstylescombobox, fontscombobox class SettingsDialog(QDialog): @@ -12,5 +12,4 @@ self.scripter = scripter self.mainLayout = QFormLayout(self) self.mainLayout.addRow('Syntax Highlither', syntaxstylescombobox.SyntaxStylesComboBox(scripter.highlight)) - self.mainLayout.addRow('Fonts', QComboBox()) - self.mainLayout.addRow('BackGoud Color', QComboBox()) + self.mainLayout.addRow('Fonts', fontscombobox.FontsComboBox(scripter.editor)) diff --git a/plugins/extensions/pykrita/plugin/plugins/scripter/syntax.py b/plugins/extensions/pykrita/plugin/plugins/scripter/syntax.py --- a/plugins/extensions/pykrita/plugin/plugins/scripter/syntax.py +++ b/plugins/extensions/pykrita/plugin/plugins/scripter/syntax.py @@ -36,6 +36,7 @@ braces = [ '\{', '\}', '\(', '\)', '\[', '\]', ] + def __init__(self, document, syntaxStyle): QSyntaxHighlighter.__init__(self, document) @@ -45,60 +46,59 @@ # Multi-line strings (expression, flag, style) # FIXME: The triple-quotes in these two lines will mess up the # syntax highlighting from this point onward - self.tri_single = (QRegExp("'''"), 1, self.syntaxStyle['string2']) - self.tri_double = (QRegExp('"""'), 2, self.syntaxStyle['string2']) + self.tri_single = (QRegExp(r"""'''(?!")"""), 1, 'string2') + self.tri_double = (QRegExp(r'''"""(?!')'''), 2, 'string2') rules = [] # Keyword, operator, and brace rules - rules += [(r'\b%s\b' % w, 0, self.syntaxStyle['keyword']) + rules += [(r'\b%s\b' % w, 0, 'keyword') for w in PythonHighlighter.keywords] - rules += [(r'%s' % o, 0, self.syntaxStyle['operator']) + rules += [(r'%s' % o, 0, 'operator') for o in PythonHighlighter.operators] - rules += [(r'%s' % b, 0, self.syntaxStyle['brace']) + rules += [(r'%s' % b, 0, 'brace') for b in PythonHighlighter.braces] # All other rules rules += [ # 'self' - (r'\bself\b', 0, self.syntaxStyle['self']), + (r'\bself\b', 0, 'self'), # Double-quoted string, possibly containing escape sequences - (r'"[^"\\]*(\\.[^"\\]*)*"', 0, self.syntaxStyle['string']), + (r'"[^"\\]*(\\.[^"\\]*)*"', 0, 'string'), # Single-quoted string, possibly containing escape sequences - (r"'[^'\\]*(\\.[^'\\]*)*'", 0, self.syntaxStyle['string']), + (r"'[^'\\]*(\\.[^'\\]*)*'", 0, 'string'), # 'def' followed by an identifier - (r'\bdef\b\s*(\w+)', 1, self.syntaxStyle['defclass']), + (r'\bdef\b\s*(\w+)', 1, 'defclass'), # 'class' followed by an identifier - (r'\bclass\b\s*(\w+)', 1, self.syntaxStyle['defclass']), + (r'\bclass\b\s*(\w+)', 1, 'defclass'), # From '#' until a newline - (r'#[^\n]*', 0, self.syntaxStyle['comment']), + (r'#[^\n]*', 0, 'comment'), # Numeric literals - (r'\b[+-]?[0-9]+[lL]?\b', 0, self.syntaxStyle['numbers']), - (r'\b[+-]?0[xX][0-9A-Fa-f]+[lL]?\b', 0, self.syntaxStyle['numbers']), - (r'\b[+-]?[0-9]+(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\b', 0, self.syntaxStyle['numbers']), + (r'\b[+-]?[0-9]+[lL]?\b', 0, 'numbers'), + (r'\b[+-]?0[xX][0-9A-Fa-f]+[lL]?\b', 0, 'numbers'), + (r'\b[+-]?[0-9]+(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\b', 0, 'numbers'), ] # Build a QRegExp for each pattern - self.rules = [(QRegExp(pat), index, fmt) - for (pat, index, fmt) in rules] + self.rules = [(QRegExp(pat), index, identifier) + for (pat, index, identifier) in rules] def highlightBlock(self, text): - """Apply syntax highlighting to the given block of text. - """ + """Apply syntax highlighting to the given block of text.""" # Do other syntax formatting - for expression, nth, format in self.rules: + for expression, nth, identifier in self.rules: index = expression.indexIn(text, 0) while index >= 0: # We actually want the index of the nth match index = expression.pos(nth) length = len(expression.cap(nth)) - self.setFormat(index, length, format) + self.setFormat(index, length, self.syntaxStyle[identifier]) index = expression.indexIn(text, index + length) self.setCurrentBlockState(0) @@ -139,7 +139,7 @@ self.setCurrentBlockState(in_state) length = len(text) - start + add # Apply formatting - self.setFormat(start, length, style) + self.setFormat(start, length, self.syntaxStyle[style]) # Look for the next match start = delimiter.indexIn(text, start + length) @@ -154,4 +154,3 @@ def setSyntaxStyle(self, syntaxStyle): self.syntaxStyle = syntaxStyle - PythonHighlighter(self.document, self.syntaxStyle) diff --git a/plugins/extensions/pykrita/plugin/plugins/scripter/syntaxstylescombobox.py b/plugins/extensions/pykrita/plugin/plugins/scripter/syntaxstylescombobox.py --- a/plugins/extensions/pykrita/plugin/plugins/scripter/syntaxstylescombobox.py +++ b/plugins/extensions/pykrita/plugin/plugins/scripter/syntaxstylescombobox.py @@ -2,7 +2,6 @@ from PyQt5.QtWidgets import * from scripter import syntaxstyles -import importlib class SyntaxStylesComboBox(QComboBox):