def findPath(self, index): path = self.sender().model().filePath(index) if os.path.isfile(path): global name FormWidget.filepath.append(path) name = os.path.basename(path) for i in range(self.tab.count()): if name == self.tab.tabText(i): self.tab.removeTab(i) break newEdit = QsciScintilla() self.checkExtensionToHighlight(path, newEdit) newEdit.setFont(QFont('Times', 10)) newEdit.setMarginType(0, QsciScintilla.NumberMargin) newEdit.setMarginWidth(0, '00000000') try: newEdit.setText(open(path).read()) newEdit.setBraceMatching(QsciScintilla.SloppyBraceMatch) newEdit.setAutoCompletionCaseSensitivity(False) newEdit.setAutoCompletionReplaceWord(False) newEdit.setAutoCompletionSource(QsciScintilla.AcsDocument) newEdit.setAutoCompletionThreshold(1) self.tab.insertTab(0, newEdit, QIcon('icon.png'), os.path.basename(path)) self.tab.setCurrentIndex(0) except: QMessageBox.warning(self, "Warning", "Unsupported file type", QMessageBox.Ok)
def openFile(self): files = QFileDialog.getOpenFileNames(self, "Open", FormWidget.path) if files[0]: FormWidget.filepath = files[0] FormWidget.path = os.path.dirname(files[0][0]) self.form_widget.tree.setRootIndex( self.form_widget.model.index(FormWidget.path)) for i in files[0]: n = os.path.basename(i) for j in range(self.form_widget.tab.count()): if n == self.form_widget.tab.tabText(j): self.form_widget.tab.removeTab(j) break newEdit = QsciScintilla() newEdit.setFont(QFont('Times', 10)) newEdit.setMarginType(0, QsciScintilla.NumberMargin) newEdit.setMarginWidth(0, '00000000') self.form_widget.checkExtensionToHighlight(i, newEdit) try: newEdit.setText(open(i).read()) newEdit.setBraceMatching(QsciScintilla.SloppyBraceMatch) newEdit.setAutoCompletionCaseSensitivity(False) newEdit.setAutoCompletionReplaceWord(False) newEdit.setAutoCompletionSource(QsciScintilla.AcsDocument) newEdit.setAutoCompletionThreshold(1) self.form_widget.tab.insertTab(0, newEdit, QIcon('icon.png'), n) self.form_widget.tab.setCurrentIndex(0) except: QMessageBox.warning(self, 'Warning', 'Unsupported file type', QMessageBox.Ok)
def newFile(self): text, ok = QInputDialog.getText(self, 'New', 'Enter File name:') if ok: newEdit = QsciScintilla() newEdit.setMarginType(0, QsciScintilla.NumberMargin) newEdit.setMarginWidth(0, '00000000') newEdit.setBraceMatching(QsciScintilla.SloppyBraceMatch) newEdit.setAutoCompletionCaseSensitivity(False) newEdit.setAutoCompletionReplaceWord(False) newEdit.setAutoCompletionSource(QsciScintilla.AcsDocument) newEdit.setAutoCompletionThreshold(1) self.form_widget.checkExtensionToHighlight(text, newEdit) newEdit.setFont(QFont('Times', 10)) self.form_widget.tab.insertTab(0, newEdit, QIcon('icon.png'), text) self.form_widget.tab.setCurrentIndex(0)
class highlightEditor(QMainWindow): """ 语法高亮代码框 """ def __init__(self, parent=None, lineNumberOn=False): super(highlightEditor, self).__init__() self.editor = QsciScintilla(parent) font = QFont() font.setFamily("Consolas") font.setPointSize(12) font.setFixedPitch(True) self.editor.setFont(font) self.editor.setObjectName("editor") self.editor.setUtf8(True) self.editor.setMarginsFont(font) if lineNumberOn: self.editor.setMarginWidth( 0, len(str(len(self.editor.text().split('\n')))) * 20) self.editor.setMarginLineNumbers(0, lineNumberOn) self.editor.setBraceMatching(QsciScintilla.StrictBraceMatch) self.editor.setIndentationsUseTabs(True) self.editor.setIndentationWidth(4) self.editor.setTabIndents(True) self.editor.setAutoIndent(True) self.editor.setBackspaceUnindents(True) self.editor.setTabWidth(4) self.editor.setCaretLineVisible(True) self.editor.setCaretLineBackgroundColor(QColor('#DCDCDC')) self.editor.setIndentationGuides(True) self.editor.setFolding(QsciScintilla.PlainFoldStyle) self.editor.setMarginWidth(2, 12) self.editor.markerDefine(QsciScintilla.Minus, QsciScintilla.SC_MARKNUM_FOLDEROPEN) self.editor.markerDefine(QsciScintilla.Plus, QsciScintilla.SC_MARKNUM_FOLDER) self.editor.markerDefine(QsciScintilla.Minus, QsciScintilla.SC_MARKNUM_FOLDEROPENMID) self.editor.markerDefine(QsciScintilla.Plus, QsciScintilla.SC_MARKNUM_FOLDEREND) self.editor.setMarkerBackgroundColor( QColor("#FFFFFF"), QsciScintilla.SC_MARKNUM_FOLDEREND) self.editor.setMarkerForegroundColor( QColor("#272727"), QsciScintilla.SC_MARKNUM_FOLDEREND) self.editor.setMarkerBackgroundColor( QColor("#FFFFFF"), QsciScintilla.SC_MARKNUM_FOLDEROPENMID) self.editor.setMarkerForegroundColor( QColor("#272727"), QsciScintilla.SC_MARKNUM_FOLDEROPENMID) self.editor.setAutoCompletionSource(QsciScintilla.AcsAll) self.editor.setAutoCompletionCaseSensitivity(True) self.editor.setAutoCompletionReplaceWord(False) self.editor.setAutoCompletionThreshold(1) self.editor.setAutoCompletionUseSingle(QsciScintilla.AcusExplicit) self.lexer = highlight(self.editor) self.editor.setLexer(self.lexer) self.__api = QsciAPIs(self.lexer) autocompletions = keyword.kwlist + [] for ac in autocompletions: self.__api.add(ac) self.__api.prepare() self.editor.autoCompleteFromAll() self.editor.textChanged.connect(self.changed) def changed(self): self.editor.setMarginWidth( 0, len(str(len(self.editor.text().split('\n')))) * 20)
def __init__(self): super().__init__() self.form_widget = FormWidget() self.setCentralWidget(self.form_widget) self.menubar = self.menuBar() self.toolbar = self.addToolBar("toolbar") self.file = self.menubar.addMenu("&File") self.edit = self.menubar.addMenu("&Edit") self.view = self.menubar.addMenu("&View") self.help = self.menubar.addMenu("&Help") self.helpOpen = QAction('Open') self.helpOpen.setShortcut('Ctrl+H') self.help.addAction(self.helpOpen) self.helpOpen.triggered.connect(lambda: os.startfile('help.pdf')) self.new = QAction(QIcon('newFile.png'), "New") self.new.setShortcut('Ctrl+N') self.open = QAction(QIcon('openFile.png'), "Open") self.open.setShortcut('Ctrl+O') self.save = QAction(QIcon('saveFile.png'), "Save") self.save.setShortcut('Ctrl+s') self.saveas = QAction(QIcon('saveasFile.png'), "Save As") self.print = QAction(QIcon('printFile.ico'), 'Print') self.print.setShortcut('Ctrl+P') self.exit = QAction('Exit') self.exit.setShortcut('Ctrl+Q') self.terminal = QAction(QIcon('terminal.png'), 'CMD') self.terminal.setShortcut('alt+r') self.file.addAction(self.new) self.file.addAction(self.open) self.file.addAction(self.save) self.file.addAction(self.saveas) self.file.addAction(self.print) self.file.addAction(self.exit) self.toolbar.addAction(self.new) self.toolbar.addAction(self.open) self.toolbar.addAction(self.save) self.toolbar.addAction(self.saveas) self.toolbar.addAction(self.print) self.toolbar.addAction(self.terminal) self.new.triggered.connect(self.newFile) self.open.triggered.connect(self.openFile) self.save.triggered.connect(self.saveFile) self.saveas.triggered.connect(self.saveasFile) self.print.triggered.connect(self.printFile) self.exit.triggered.connect(self.close) self.terminal.triggered.connect(self.Opencmd) self.undo = QAction('Undo') self.undo.setShortcut('Ctrl+Z') self.cut = QAction('Cut') self.cut.setShortcut('Ctrl+X') self.copy = QAction('Copy') self.copy.setShortcut('Ctrl+C') self.paste = QAction('Paste') self.paste.setShortcut('Ctrl+V') self.delete = QAction('Delete') self.delete.setShortcut('Delete') self.goto = QAction('Find') self.goto.setShortcut('Ctrl+F') self.find_and_replace = QAction('Replace') self.find_and_replace.setShortcut('Ctrl+R') self.edit.addAction(self.undo) self.edit.addAction(self.cut) self.edit.addAction(self.copy) self.edit.addAction(self.paste) self.edit.addAction(self.delete) self.edit.addAction(self.goto) self.edit.addAction(self.find_and_replace) self.undo.triggered.connect(self.undoText) self.cut.triggered.connect(self.cutText) self.copy.triggered.connect(self.copyText) self.paste.triggered.connect(self.pasteText) self.delete.triggered.connect(self.deleteText) self.goto.triggered.connect(self.findText) self.find_and_replace.triggered.connect(self.replaceText) self.Mtoolbar = QMenu('ToolBar') self.Mexplorer = QMenu("Explorer") self.Mnumbering = QMenu("Numbering") self.reset = QAction('Reset') self.reset.triggered.connect(self.resetWindow) self.view.addMenu(self.Mtoolbar) self.view.addMenu(self.Mexplorer) self.view.addMenu(self.Mnumbering) self.view.addAction(self.reset) self.thide = QAction("Hide") self.tshow = QAction("Show") self.ehide = QAction("Hide") self.eshow = QAction("Show") self.nhide = QAction("Hide") self.nshow = QAction("Show") self.Mtoolbar.addAction(self.thide) self.Mtoolbar.addAction(self.tshow) self.Mexplorer.addAction(self.ehide) self.Mexplorer.addAction(self.eshow) self.Mnumbering.addAction(self.nhide) self.Mnumbering.addAction(self.nshow) self.thide.triggered.connect(self.hide) self.tshow.triggered.connect(self.show) self.ehide.triggered.connect(self.hide) self.eshow.triggered.connect(self.show) self.nhide.triggered.connect(self.hide) self.nshow.triggered.connect(self.show) self.setWindowTitle("TextPad") self.setWindowIcon(QIcon('icon.png')) newEdit = QsciScintilla(self) newEdit.setFont(QFont('Times', 10)) newEdit.setBraceMatching(QsciScintilla.SloppyBraceMatch) newEdit.setAutoCompletionCaseSensitivity(False) newEdit.setAutoCompletionReplaceWord(False) newEdit.setAutoCompletionSource(QsciScintilla.AcsDocument) newEdit.setAutoCompletionThreshold(1) self.form_widget.tab.addTab(newEdit, QIcon('icon.png'), "Untitled") self.form_widget.tab.currentWidget().setFocus() self.showMaximized() self.show()