def __init__(self, parent, commands=None, font=None): QtGui.QWidget.__init__(self, parent) self.setAcceptDrops(True) self.commands = commands self.code = myCodeWidget(self, font=font) #set lexer manually instead of by name lexer = PyScriptLexer(commands) self.code.highlighter._lexer = lexer self.code.setMouseTracking(True) #AdvanceCodeWidget # ===================================== self.find = FindWidget(self) self.find.hide() self.replace = ReplaceWidget(self) self.replace.hide() self.replace.replace_button.setEnabled(False) self.replace.replace_all_button.setEnabled(False) self.active_find_widget = None self.previous_find_widget = None self.code.selectionChanged.connect(self._update_replace_enabled) self.find.line_edit.returnPressed.connect(self.find_next) self.find.next_button.clicked.connect(self.find_next) self.find.prev_button.clicked.connect(self.find_prev) self.replace.line_edit.returnPressed.connect(self.find_next) self.replace.line_edit.textChanged.connect( self._update_replace_all_enabled) self.replace.next_button.clicked.connect(self.find_next) self.replace.prev_button.clicked.connect(self.find_prev) self.replace.replace_button.clicked.connect(self.replace_next) self.replace.replace_all_button.clicked.connect(self.replace_all) layout = QtGui.QVBoxLayout() layout.setContentsMargins(0, 0, 0, 0) layout.setSpacing(0) layout.addWidget(self.code) layout.addWidget(self.find) layout.addWidget(self.replace) self.setLayout(layout) self.edit_color = QtGui.QColor('blue').lighter(175)
class myAdvancedCodeWidget(AdvancedCodeWidget): commands = None def __init__(self, parent, commands=None, font=None): QtGui.QWidget.__init__(self, parent) self.setAcceptDrops(True) self.commands = commands self.code = myCodeWidget(self, font=font) #set lexer manually instead of by name lexer = PyScriptLexer(commands) self.code.highlighter._lexer = lexer self.code.setMouseTracking(True) #AdvanceCodeWidget # ===================================== self.find = FindWidget(self) self.find.hide() self.replace = ReplaceWidget(self) self.replace.hide() self.replace.replace_button.setEnabled(False) self.replace.replace_all_button.setEnabled(False) self.active_find_widget = None self.previous_find_widget = None self.code.selectionChanged.connect(self._update_replace_enabled) self.find.line_edit.returnPressed.connect(self.find_next) self.find.next_button.clicked.connect(self.find_next) self.find.prev_button.clicked.connect(self.find_prev) self.replace.line_edit.returnPressed.connect(self.find_next) self.replace.line_edit.textChanged.connect( self._update_replace_all_enabled) self.replace.next_button.clicked.connect(self.find_next) self.replace.prev_button.clicked.connect(self.find_prev) self.replace.replace_button.clicked.connect(self.replace_next) self.replace.replace_all_button.clicked.connect(self.replace_all) layout = QtGui.QVBoxLayout() layout.setContentsMargins(0, 0, 0, 0) layout.setSpacing(0) layout.addWidget(self.code) layout.addWidget(self.find) layout.addWidget(self.replace) self.setLayout(layout) self.edit_color = QtGui.QColor('blue').lighter(175) # ===================================== def insert_command(self, cmd): cur = self.code.textCursor() self._insert_command(cmd, cur) def _insert_command(self, cmd, cur): text = cmd.to_string() if text: # get the indent level of the line # if line starts with a special keyword add indent block = cur.block() line = block.text() indent = max(4, self.code._get_indent_position(line)) line = line.strip() token = line.split(' ')[0] token = token.strip() if token in ('if', 'for', 'while', 'with', 'def', 'class'): indent += 4 indent = ' ' * indent cur.movePosition(QTextCursor.EndOfLine) cur.insertText('\n{}{}'.format(indent, text)) def dragEnterEvent(self, e): if e.mimeData().hasFormat('traits-ui-tabular-editor'): e.accept() else: e.ignore() def dropEvent(self, e): mime = e.mimeData() data = mime.data('traits-ui-tabular-editor') idx = int(data.split(' ')[1]) # cmd = '' cmd = self.commands.command_objects[idx] if cmd: cur = self.code.cursorForPosition(e.pos()) self._insert_command(cmd, cur) def highlight_line(self, lineno=None): if lineno is None: color = self.edit_color else: color = self.code.line_highlight_color selection = QTextEdit.ExtraSelection() selection.format.setBackground(color) selection.format.setProperty( QTextFormat.FullWidthSelection, True) selection.cursor = self.code.textCursor() if lineno is not None: doc = self.code.document() block = doc.findBlockByLineNumber(lineno - 1) pos = block.position() selection.cursor.setPosition(pos) selection.cursor.clearSelection() self.code.setExtraSelections([selection])
class myAdvancedCodeWidget(AdvancedCodeWidget): commands = None def __init__(self, parent, commands=None, font=None): QtGui.QWidget.__init__(self, parent) self.setAcceptDrops(True) self.commands = commands self.code = myCodeWidget(self, font=font) #set lexer manually instead of by name lexer = PyScriptLexer(commands) self.code.highlighter._lexer = lexer self.code.setMouseTracking(True) #AdvanceCodeWidget # ===================================== self.find = FindWidget(self) self.find.hide() self.replace = ReplaceWidget(self) self.replace.hide() self.replace.replace_button.setEnabled(False) self.replace.replace_all_button.setEnabled(False) self.active_find_widget = None self.previous_find_widget = None self.code.selectionChanged.connect(self._update_replace_enabled) self.find.line_edit.returnPressed.connect(self.find_next) self.find.next_button.clicked.connect(self.find_next) self.find.prev_button.clicked.connect(self.find_prev) self.replace.line_edit.returnPressed.connect(self.find_next) self.replace.line_edit.textChanged.connect( self._update_replace_all_enabled) self.replace.next_button.clicked.connect(self.find_next) self.replace.prev_button.clicked.connect(self.find_prev) self.replace.replace_button.clicked.connect(self.replace_next) self.replace.replace_all_button.clicked.connect(self.replace_all) layout = QtGui.QVBoxLayout() layout.setContentsMargins(0, 0, 0, 0) layout.setSpacing(0) layout.addWidget(self.code) layout.addWidget(self.find) layout.addWidget(self.replace) self.setLayout(layout) self.edit_color = QtGui.QColor('blue').lighter(175) # ===================================== def insert_command(self, cmd): cur = self.code.textCursor() self._insert_command(cmd, cur) def _insert_command(self, cmd, cur): text = cmd.to_string() if text: # get the indent level of the line # if line starts with a special keyword add indent block = cur.block() line = block.text() indent = max(4, self.code._get_indent_position(line)) line = line.strip() token = line.split(' ')[0] token = token.strip() if token in ('if', 'for', 'while', 'with', 'def', 'class'): indent += 4 indent = ' ' * indent cur.movePosition(QTextCursor.EndOfLine) cur.insertText('\n{}{}'.format(indent, text)) def dragEnterEvent(self, e): if e.mimeData().hasFormat('traits-ui-tabular-editor'): e.accept() else: e.ignore() def dropEvent(self, e): mime = e.mimeData() data = mime.data('traits-ui-tabular-editor') idx = int(data.split(' ')[1]) # cmd = '' cmd = self.commands.command_objects[idx] if cmd: cur = self.code.cursorForPosition(e.pos()) self._insert_command(cmd, cur) def highlight_line(self, lineno=None): if lineno is None: color = self.edit_color else: color = self.code.line_highlight_color selection = QTextEdit.ExtraSelection() selection.format.setBackground(color) selection.format.setProperty(QTextFormat.FullWidthSelection, True) selection.cursor = self.code.textCursor() if lineno is not None: doc = self.code.document() block = doc.findBlockByLineNumber(lineno - 1) pos = block.position() selection.cursor.setPosition(pos) selection.cursor.clearSelection() self.code.setExtraSelections([selection])