def _initFileEditor(self): """ Create the Editor widget based on QTextEdit """ # Font base_font = QtGui.QFont() base_font.setFamily("Courier") base_font.setStyleHint(QtGui.QFont.Monospace) base_font.setFixedPitch(True) base_font.setPointSize(10) font_metrics = QtGui.QFontMetrics(base_font) _tab_string = '' for i in range(_tab_size): _tab_string += ' ' # Main text zone textEdit = CodeEditor() textEdit.setFont(base_font) textEdit.textChanged.connect(self.updateFileState) textEdit.setReadOnly(self.readOnly) policy = textEdit.sizePolicy() policy.setHorizontalPolicy(QSizePolicy.Expanding) textEdit.setSizePolicy(policy) # tab textEdit.setTabStopWidth(font_metrics.width(_tab_string)) return textEdit
def loc_format(color, style=''): """ Returns a TextCharFormat with the proper attributes """ c = QtGui.QColor() c.setNamedColor(color) f = QtGui.QTextCharFormat() f.setForeground(c) # Bold font if 'bold' in style: f.setFontWeight(QtGui.QFont.Bold) # Italic font if 'italic' in style: f.setFontItalic(True) return f
def highlightCurrentLine(self): extraSelections = [] if not self.isReadOnly(): selection = QTextEdit.ExtraSelection() lineColor = QtGui.QColor(QtCore.Qt.yellow).lighter(160) selection.format.setBackground(lineColor) selection.format.setProperty(QtGui.QTextFormat.FullWidthSelection, True) selection.cursor = self.textCursor() selection.cursor.clearSelection() extraSelections.append(selection) self.setExtraSelections(extraSelections)
def lineNumberAreaPaintEvent(self, event): mypainter = QtGui.QPainter(self.lineNumberArea) mypainter.fillRect(event.rect(), QtCore.Qt.lightGray) block = self.firstVisibleBlock() blockNumber = block.blockNumber() top = self.blockBoundingGeometry(block).translated(self.contentOffset()).top() bottom = top + self.blockBoundingRect(block).height() # Just to make sure I use the right font height = self.fontMetrics().height() while block.isValid() and (top <= event.rect().bottom()): if block.isVisible() and (bottom >= event.rect().top()): number = str(blockNumber + 1) mypainter.setPen(QtCore.Qt.black) mypainter.drawText(0, top, self.lineNumberArea.width(), height, QtCore.Qt.AlignRight, number) block = block.next() top = bottom bottom = top + self.blockBoundingRect(block).height() blockNumber += 1
def __init__(self, parent=None, case_dir=None, readOnly=False, noOpen=False, useHighlight=True): super(QFileEditor, self).__init__(parent) self.setGeometry(50, 50, 500, 300) self.setWindowTitle("code_saturne built-in file editor") self.parent = parent self.case_dir = case_dir if self.case_dir: self.case_name = os.path.split(case_dir)[-1] self.last_dir = case_dir self.readOnly = readOnly self.readerMode = readOnly # Activate text highlight self.useHighlight = useHighlight self.opened = False self.saved = True self.dialog = QFileDialog(self) # Open file action open_img_path = ":/icons/22x22/document-open.png" icon_open = QtGui.QIcon() icon_open.addPixmap(QtGui.QPixmap(_fromUtf8(open_img_path)), QtGui.QIcon.Normal, QtGui.QIcon.Off) self.openFileAction = QAction(icon_open, "Open", self) self.openFileAction.setShortcut("Ctrl+O") self.openFileAction.setStatusTip('Open File') self.openFileAction.triggered.connect(self.openFileForAction) # New file action new_img_path = ":/icons/22x22/document-new.png" icon_new = QtGui.QIcon() icon_new.addPixmap(QtGui.QPixmap(_fromUtf8(new_img_path)), QtGui.QIcon.Normal, QtGui.QIcon.Off) self.newFileAction = QAction(icon_new, "New", self) self.newFileAction.setShortcut("Ctrl+E") self.newFileAction.setStatusTip('Create new file') self.newFileAction.triggered.connect(self.newFile) # Save action save_img_path = ":/icons/22x22/document-save.png" icon_save = QtGui.QIcon() icon_save.addPixmap(QtGui.QPixmap(_fromUtf8(save_img_path)), QtGui.QIcon.Normal, QtGui.QIcon.Off) self.saveFileAction = QAction(icon_save, "Save", self) self.saveFileAction.setShortcut("Ctrl+S") self.saveFileAction.setStatusTip('Save file') self.saveFileAction.triggered.connect(self.saveFile) # Save as action saveas_img_path = ":/icons/22x22/document-save-as.png" icon_saveas = QtGui.QIcon() icon_saveas.addPixmap(QtGui.QPixmap(_fromUtf8(saveas_img_path)), QtGui.QIcon.Normal, QtGui.QIcon.Off) self.saveFileAsAction = QAction(icon_saveas, "Save as", self) self.saveFileAsAction.setStatusTip('Save file as') self.saveFileAsAction.triggered.connect(self.saveFileAs) # Close file action close_img_path = ":/icons/22x22/process-stop.png" icon_close = QtGui.QIcon() icon_close.addPixmap(QtGui.QPixmap(_fromUtf8(close_img_path)), QtGui.QIcon.Normal, QtGui.QIcon.Off) self.closeFileAction = QAction(icon_close, "Close file", self) self.closeFileAction.setShortcut("Ctrl+Q") self.closeFileAction.setStatusTip('Close opened file') self.closeFileAction.triggered.connect(self.closeOpenedFile) # Exit editor action quit_img_path = ":/icons/22x22/system-log-out.png" icon_quit = QtGui.QIcon() icon_quit.addPixmap(QtGui.QPixmap(_fromUtf8(quit_img_path)), QtGui.QIcon.Normal, QtGui.QIcon.Off) self.quitAction = QAction(icon_quit, "Quit", self) self.quitAction.setStatusTip('Quit the editor') self.quitAction.triggered.connect(self.closeApplication) self.statusBar() # File toolbar self.toolbar = self.addToolBar("Options") self.toolbar.addAction(self.newFileAction) if not noOpen: self.toolbar.addAction(self.openFileAction) self.toolbar.addAction(self.saveFileAction) self.toolbar.addAction(self.saveFileAsAction) self.toolbar.addAction(self.closeFileAction) self.toolbar.addAction(self.quitAction) # File menu self.mainMenu = self.menuBar() self.fileMenu = self.mainMenu.addMenu('&File') self.fileMenu.addAction(self.newFileAction) if not noOpen: self.fileMenu.addAction(self.openFileAction) self.fileMenu.addAction(self.saveFileAction) self.fileMenu.addAction(self.saveFileAsAction) self.fileMenu.addAction(self.closeFileAction) self.fileMenu.addAction(self.quitAction) # Explorer self.explorer = self._initFileExplorer() self._initExplorerActions() # Editor self.textEdit = self._initFileEditor() # Settings settings = QtCore.QSettings() try: # API 2 self.restoreGeometry( settings.value("MainWindow/Geometry", QtCore.QByteArray())) self.restoreState( settings.value("MainWindow/State", QtCore.QByteArray())) except: # API 1 self.recentFiles = settings.value("RecentFiles").toStringList() self.restoreGeometry( settings.value("MainWindow/Geometry").toByteArray()) self.restoreState(settings.value("MainWindow/State").toByteArray()) # file attributes self.filename = "" self.file_extension = "" self.mainWidget = FormWidget(self, [self.explorer, self.textEdit]) self.setCentralWidget(self.mainWidget)