def highlightCommentsOverLines(self, text, dls, dle): startExpression = QtCore.QRegExp(dls) endExpression = QtCore.QRegExp(dle) ref_state = 1 if self.previousBlockState() == ref_state: start = 0 add = 0 else: start = startExpression.indexIn(text) add = startExpression.matchedLength() while start >= 0: end = endExpression.indexIn(text, start + add) if end >= add: length = end - start + add + endExpression.matchedLength() self.setCurrentBlockState(0) else: self.setCurrentBlockState(ref_state) if has_qstring: length = text.length() - start + add else: length = len(text) - start + add self.setFormat(start, length, format_styles['comment']) start = endExpression.indexIn(text, start + length)
def highlightBlock(self, text): """ Apply the syntax highlighting """ for rule in self.highlightingRules: exp = QtCore.QRegExp(rule.pattern) index = exp.indexIn(text) while index >= 0: length = exp.matchedLength() ok_to_highlight = True if len(text) > index + length: if text[index + length] not in self.op + [' ']: ok_to_highlight = False if text[index:index + length] not in self.op + self.br: ok_to_highlight = True if ok_to_highlight: self.setFormat(index, length, rule.format) if has_qstring: index = text.indexOf(exp, index + length) else: index = text.find(exp.cap(), index + length) self.setCurrentBlockState(0) # C/C++ comments self.highlightCommentsOverLines(text, "/\\*", "\\*/")
def resizeEvent(self, event): # Handle the python2/python3 differences for super super(resizeEvent, self).resizeEvent(event) cr = self.contentsRect(); self.lineNumberArea.setGeometry(QtCore.QRect(cr.left(), cr.top(), self.lineNumberAreaWidth(), cr.height()))
def closeApplication(self): """ Close the editor """ if self.opened == True: choice = QMessageBox.question(self, 'Built-in editor', "Exit text editor?", QMessageBox.Yes | QMessageBox.No) else: choice = QMessageBox.Yes if choice == QMessageBox.Yes: self.closeOpenedFile() settings = QtCore.QSettings() settings.setValue("MainWindow/Geometry", self.saveGeometry()) self.close() else: pass
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)
def __init__(self, parent, extension): QtGui.QSyntaxHighlighter.__init__(self, parent) self.parent = parent self.highlightingRules = [] # Keywords (C or Fortran) fortran_kw = [ 'if', 'else', 'endif', 'do', 'enddo', 'end', 'implicit none', 'use', 'subroutine', 'function', 'double precision', 'real', 'integer', 'char', 'allocatable', 'allocate', 'deallocate', 'dimension', 'select case' ] c_kw = [ 'if', 'else', 'for', 'switch', 'while', '\#', 'include', 'pass', 'return', 'del', 'delete', 'assert', 'true', 'false', 'continue', 'break', 'fprintf', 'bft_printf', 'bft_printf_flush', 'bft_error', 'cs_real_t', 'cs_lnum_t', 'cs_real_3_t', 'int', 'char', 'string', 'void', 'double', 'const', 'BEGIN_C_DECLS', 'END_C_DECLS' ] py_kw = [ 'if', 'elif', 'for', 'range', 'while', 'return', 'def', 'True', 'False' ] self.kw = [] # Fortran if extension in ['f90', 'F90', 'F', 'f77']: for kw in fortran_kw: self.kw.append(kw) self.kw.append(kw.upper()) # C/C++ elif extension in ['c', 'cpp', 'cxx', 'c++']: for kw in c_kw: self.kw.append(kw) self.kw.append(kw.upper()) # Python elif extension == 'py': for kw in py_kw: self.kw.append(kw) # Operators self.op = [ '=', '==', '!=', '<', '>', '<=', '>=', '\+', '-', '\*', '/', '\%', '\*\*', '\+=', '-=', '\*=', '/=', '->', '=>', '\^', '\|', '\&', '\|\|', '\&\&' ] # Braces self.br = ['\(', '\)', '\{', '\}', '\[', '\]'] # RULES for kw in self.kw: p = QtCore.QRegExp("\\b" + kw + '\\b') rule = HighlightingRule(p, format_styles['keyword']) self.highlightingRules.append(rule) for op in self.op: p = QtCore.QRegExp(op) rule = HighlightingRule(p, format_styles['operator']) self.highlightingRules.append(rule) for br in self.br: p = QtCore.QRegExp(br) rule = HighlightingRule(p, format_styles['brace']) self.highlightingRules.append(rule) # strings ps = QtCore.QRegExp('"[^"\\]*(\\.[^"\\]*)*"') rs = HighlightingRule(ps, format_styles['string']) self.highlightingRules.append(rs) # comments pc = QtCore.QRegExp('//[^\n]*') rc = HighlightingRule(pc, format_styles['comment']) self.highlightingRules.append(rc) pcf = QtCore.QRegExp('![^\n]*') rcf = HighlightingRule(pcf, format_styles['comment']) self.highlightingRules.append(rcf) # numerals pn1 = QtCore.QRegExp('[+-]?[0-9]+[lL]?') rn1 = HighlightingRule(pn1, format_styles['expression']) self.highlightingRules.append(rn1) pn2 = QtCore.QRegExp('[+-]?0[xX][0-9A-Fa-f]+[lL]?') rn2 = HighlightingRule(pn2, format_styles['expression']) self.highlightingRules.append(rn2) pn3 = QtCore.QRegExp('[+-]?[0-9]+(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?') rn3 = HighlightingRule(pn3, format_styles['expression']) self.highlightingRules.append(rn3)
def sizeHint(self): return QtCore.QSize(self.editor.lineNumberAreaWidth(), 0)
def __init__(self, parent): QtGui.QSyntaxHighlighter.__init__(self, parent) self.parent = parent self.highlightingRules = [] # Keywords (C or Fortran) self.kw = [ 'if', 'else', 'endif', '\#', 'include', 'void', 'int', 'integer', 'double', 'const', 'fprintf', 'bft_printf', 'bft_printf_flush', 'cs_real_t', 'subroutine', 'function', 'def', 'double precision', 'use', 'implicit none', 'allocatable', 'dimension', 'string', 'float', 'allocate', 'deallocate', 'char', 'for', 'while', 'assert', 'continue', 'break', 'switch', 'del', 'pass', 'return', 'true', 'false' ] # Operators self.op = [ '=', '==', '!=', '<', '>', '<=', '>=', '\+', '-', '\*', '/', '\%', '\*\*', '\+=', '-=', '\*=', '/=', '\^', '\|', '\&', '\|\|', '\&\&' ] # Braces self.br = ['\(', '\)', '\{', '\}', '\[', '\]'] # RULES for kw in self.kw: p = QtCore.QRegExp("\\b" + kw + '\\b') rule = HighlightingRule(p, format_styles['keyword']) self.highlightingRules.append(rule) for op in self.op: p = QtCore.QRegExp(op) rule = HighlightingRule(p, format_styles['operator']) self.highlightingRules.append(rule) for br in self.br: p = QtCore.QRegExp(br) rule = HighlightingRule(p, format_styles['brace']) self.highlightingRules.append(rule) # strings ps = QtCore.QRegExp('"[^"\\]*(\\.[^"\\]*)*"') rs = HighlightingRule(ps, format_styles['string']) self.highlightingRules.append(rs) # comments pc = QtCore.QRegExp('//[^\n]*') rc = HighlightingRule(pc, format_styles['comment']) self.highlightingRules.append(rc) pcf = QtCore.QRegExp('![^\n]*') rcf = HighlightingRule(pcf, format_styles['comment']) self.highlightingRules.append(rcf) # numerals pn1 = QtCore.QRegExp('[+-]?[0-9]+[lL]?') rn1 = HighlightingRule(pn1, format_styles['expression']) self.highlightingRules.append(rn1) pn2 = QtCore.QRegExp('[+-]?0[xX][0-9A-Fa-f]+[lL]?') rn2 = HighlightingRule(pn2, format_styles['expression']) self.highlightingRules.append(rn2) pn3 = QtCore.QRegExp('[+-]?[0-9]+(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?') rn3 = HighlightingRule(pn3, format_styles['expression']) self.highlightingRules.append(rn3)