def add_menu(path, text, icon, handle): window = Gui.getMainWindow() bar = window.menuBar() menu = bar for name in path: menu = find_or_add(menu, name) for action in menu.actions(): if action.text() == text: menu.removeAction(action) break action = QtGui.QAction(QtGui.QIcon(icon), text, window.menuBar()) menu.addAction(action) action.triggered.connect(handle)
def __init__(self, pattern, color, bold=False, background=None): self.format = QtGui.QTextCharFormat() self.format.setForeground(self.hex2color(color)) if bold: self.format.setFontWeight(QtGui.QFont.Bold) if background: self.format.setBackground(self.hex2color(background)) self.format.setProperty(QtGui.QTextFormat.FullWidthSelection, True) self.pattern = pattern
def __init__(self, name, file_name, highlighter=PythonSyntaxHighlighter, tab_size=4): self.form = QtGui.QDialog() self.form.setWindowTitle(tr("Edit: {}").format(str(file_name.name))) self.editor = CodeEditor() self.editor.appendPlainText(file_name.read_text()) self.highlighter = highlighter(self.editor.document()) self.error_message = QtGui.QLabel() self.file_name = file_name self.name = name self.tab_size = tab_size layout = QtGui.QVBoxLayout(self.form) layout.addWidget(self.editor) layout.addWidget(self.error_message)
def highlightCurrentLine(self): extra_selections = [] if not self.isReadOnly(): selection = QtGui.QTextEdit.ExtraSelection() line_color = QtGui.QColor(QtCore.Qt.yellow).lighter(200) selection.format.setBackground(line_color) selection.format.setProperty(QtGui.QTextFormat.FullWidthSelection, True) selection.cursor = self.textCursor() selection.cursor.clearSelection() extra_selections.append(selection) self.setExtraSelections(extra_selections)
def compile(self): self.error_message.setText("") editor = self.editor source = self.get_source() try: compile(source, self.file_name, "exec") return True except SyntaxError as e: block = editor.document().findBlockByLineNumber(e.lineno - 1) editor.moveCursor(QtGui.QTextCursor.End) editor.setTextCursor(QtGui.QTextCursor(block)) self.error_message.setText("Line {0}: {1}".format(e.lineno, e.msg)) return False
def lineNumberAreaPaintEvent(self, event): painter = QtGui.QPainter(self.lineNumberArea) painter.fillRect(event.rect(), QtGui.Qt.lightGray) block = self.firstVisibleBlock() block_num = block.blockNumber() top = int( self.blockBoundingGeometry(block).translated( self.contentOffset()).top()) bottom = top + int(self.blockBoundingRect(block).height()) while block.isValid() and top <= event.rect().bottom(): if block.isVisible() and bottom >= event.rect().top(): number = str(block_num + 1) painter.setPen(QtGui.Qt.black) painter.drawText(0, top, self.lineNumberArea.width(), self.fontMetrics().height(), QtGui.Qt.AlignRight, number) block = block.next() top = bottom bottom = top + int(self.blockBoundingRect(block).height()) block_num += 1
def sizeHint(self): return QtGui.QSize(self.editor.lineNumberAreaWidth(), 0)