class Console(QWidget): errorSignal = pyqtSignal(str) outputSignal = pyqtSignal(str) def __init__(self): super().__init__() self.editor = Editor(self) self.editor.setReadOnly(True) self.custom = Customize() self.font = QFont() self.numbers = NumberBar(self.editor, index=self.custom.index) self.dialog = MessageBox() # self.font.setFamily(editor["editorFont"]) self.font.setFamily("Iosevka") self.terminateButton = QPushButton() self.terminateButton.setIcon(QIcon("resources/square.png")) self.terminateButton.clicked.connect(self.terminate) self.font.setPointSize(12) self.layout = QHBoxLayout() self.layout.addWidget(self.numbers) self.layout.addWidget(self.editor, 1) self.layout.addWidget(self.terminateButton) self.setLayout(self.layout) self.output = None self.setFocusPolicy(Qt.StrongFocus) self.error = None self.finished = False self.editor.setFont(self.font) self.process = QProcess() self.state = None self.process.readyReadStandardError.connect( self.onReadyReadStandardError) self.process.readyReadStandardOutput.connect( self.onReadyReadStandardOutput) def onReadyReadStandardError(self): try: self.error = self.process.readAllStandardError().data().decode() self.editor.appendPlainText(self.error) self.errorSignal.emit(self.error) if self.error == "": pass else: self.error = self.error.split(os.linesep)[-2] self.dialog.helpword = str(self.error) self.dialog.getHelp() except IndexError as E: print(E) def onReadyReadStandardOutput(self): try: self.result = self.process.readAllStandardOutput().data().decode() except UnicodeDecodeError as E: print(E) self.editor.appendPlainText(self.result.strip("\n")) self.state = self.process.state() self.outputSignal.emit(self.result) def ifFinished(self, exitCode, exitStatus): self.finished = True def run(self, command): """Executes a system command.""" # clear previous text self.editor.clear() # self.editor.setPlainText("[" + str(getpass.getuser()) + "@" + str( socket.gethostname()) + "]" + #" ~/" + str(os.path.basename(os.getcwd())) + " >$") if self.process.state() == 1 or self.process.state() == 2: self.process.kill() self.editor.setPlainText("Process already started, terminating") else: self.process.start(command) def terminate(self): if self.process.state() == 2: self.process.kill()
class Content(QWidget): def __init__(self, text, fileName, baseName, themeIndex, parent, window): super().__init__() self.editor = Editor(self) self.text = text self.window = window # This should be the Main class self.parent = parent self.status_bar = QStatusBar(self) self.wordlist = wordList self.fileName = fileName self.baseName = baseName self.temporary = 0 self.font = QFont() self.custom = Customize() self.font.setFamily(editor["editorFont"]) self.font.setPointSize(editor["editorFontSize"]) self.tabSize = editor["TabWidth"] self.editor.textChanged.connect(self.changeSaved) self.custom = Customize() self.saved = True self.editor.setPlainText(str(text)) self.test = QLabel(self) self.editor.cursorPositionChanged.connect(self.change_col) self.test.setText(str(platform.system())) if self.baseName.endswith(".py"): self.highlighter = PyHighlighter(self.editor.document(), index=self.custom.index) self.tokenize_file() self.test.setText(str(platform.system()) + " " + "Python 3") else: pass self.main_layout = QVBoxLayout(self) self.hbox = QHBoxLayout() self.status_bar_layout = QHBoxLayout() self.status_bar = QStatusBar(self) # Create a widget for the line numbers self.hbox.addWidget(self.editor) self.status_bar_layout.addWidget(self.status_bar) self.status_bar_layout.addWidget(self.test) self.main_layout.addLayout(self.hbox) self.main_layout.addLayout(self.status_bar_layout) self.completer = Completer(self.wordlist) self.moveCursorRight = QShortcut( QKeySequence(editor["moveCursorRight"]), self) self.moveCursorLeft = QShortcut(QKeySequence(editor["moveCursorLeft"]), self) self.selectAllBeforeCursor = QShortcut( QKeySequence(editor["selectAllWordsBeforeCursor"]), self) self.moveUp = QShortcut(QKeySequence(editor["moveCursorUp"]), self) self.moveDown = QShortcut(QKeySequence(editor["moveCursorDown"]), self) self.moveDown.activated.connect(self.moveCursorDown) self.moveUp.activated.connect(self.moveCursorUp) self.selectAllBeforeCursor.activated.connect(self.selectBeforeCursor) self.moveCursorRight.activated.connect(self.moveCursorRightFunc) self.moveCursorLeft.activated.connect(self.moveCursorLeftFunc) self.editor.textChanged.connect(self.changeSaved) self.setCompleter(self.completer) def change_col(self): cursor = self.editor.textCursor() current_row = cursor.blockNumber() + 1 current_line = cursor.positionInBlock() self.status_bar.showMessage( "Line: " + str(current_row) + " Column: " + str(current_line) + " Total: " + str(self.editor.get_linenumbers()) + " lines " + "Size: " + str(self.get_size(self.editor.toPlainText())) + " KiB") def get_size(self, input): return round(len(input.encode("utf-8")) / 1000) def tokenize_file(self): for i in tokenize(self.fileName): for j in i: if j not in self.wordlist: self.wordlist.append(j) self.completer = Completer(self.wordlist) self.setCompleter(self.completer) def getTextCursor(self): textCursor = self.editor.textCursor() textCursorPos = textCursor.position() return textCursor, textCursorPos def changeSaved(self): self.modified = self.editor.document().isModified() try: if self.modified: self.window.setWindowTitle("PyPad ~ " + str(self.baseName) + " [UNSAVED]") else: pass except NameError as E: print(E) def moveCursorRightFunc(self): textCursor, textCursorPos = self.getTextCursor() textCursor.setPosition(textCursorPos + 1) self.editor.setTextCursor(textCursor) def moveCursorUp(self): textCursor, textCursorPos = self.getTextCursor() textCursor.movePosition(textCursor.Up) self.editor.setTextCursor(textCursor) def moveCursorDown(self): textCursor, textCursorPos = self.getTextCursor() textCursor.movePosition(textCursor.Down) self.editor.setTextCursor(textCursor) def moveCursorLeftFunc(self): textCursor, textCursorPos = self.getTextCursor() textCursor.setPosition(textCursorPos - 1) self.editor.setTextCursor(textCursor) def selectBeforeCursor(self): textCursor, textCursorPos = self.getTextCursor() text = self.textUnderCursor() textCursor.movePosition(textCursor.StartOfWord, textCursor.KeepAnchor, len(text)) self.editor.setTextCursor(textCursor) def setCompleter(self, completer): self.completer.setWidget(self) completer.setCompletionMode(QCompleter.PopupCompletion) completer.setCaseSensitivity(Qt.CaseInsensitive) self.completer = completer self.completer.insertText.connect(self.insertCompletion) def insertCompletion(self, completion): textCursor = self.editor.textCursor() extra = (len(completion) - len(self.completer.completionPrefix())) textCursor.movePosition(QTextCursor.Left) textCursor.movePosition(QTextCursor.EndOfWord) textCursor.insertText(completion[-extra:]) if completion.endswith("()"): cursorPos = textCursor.position() textCursor.setPosition(cursorPos - 1) self.editor.setTextCursor(textCursor) def textUnderCursor(self): textCursor = self.editor.textCursor() textCursor.select(QTextCursor.WordUnderCursor) return textCursor.selectedText() def focusInEvent(self, event): if self.completer: self.completer.setWidget(self) QPlainTextEdit.focusInEvent(self, event) def keyPressEvent(self, event): if self.completer and self.completer.popup() and self.completer.popup( ).isVisible(): if event.key() in (Qt.Key_Enter, Qt.Key_Return, Qt.Key_Escape, Qt.Key_Tab, Qt.Key_Backtab): event.ignore() return isShortcut = (event.modifiers() == Qt.ControlModifier and event.key() == Qt.Key_Space) if not self.completer or not isShortcut: QPlainTextEdit.keyPressEvent(self.editor, event) completionPrefix = self.textUnderCursor() if not isShortcut: if self.completer.popup(): self.completer.popup().hide() return self.completer.setCompletionPrefix(completionPrefix) popup = self.completer.popup() popup.setFont(self.font) popup.setCurrentIndex(self.completer.completionModel().index(0, 0)) cr = self.editor.cursorRect() cr.translate(QPoint(10, 10)) cr.setWidth \ (self.completer.popup().sizeHintForColumn(0) + self.completer.popup().verticalScrollBar().sizeHint().width()) self.completer.complete(cr)
class Console(QWidget): errorSignal = pyqtSignal(str) outputSignal = pyqtSignal(str) def __init__(self, parent=None): super().__init__() self.parent = parent self.pressed = False self.font = QFont() self.dialog = MessageBox(self) # self.font.setFamily(editor["editorFont"]) self.font.setFamily("Iosevka") self.font.setPointSize(12) self.layout = QVBoxLayout() self.setLayout(self.layout) self.output = None self.setFocusPolicy(Qt.StrongFocus) self.error = None self.finished = False self.clicked = False self.process = QProcess() self.state = None self.process.readyReadStandardError.connect(self.onReadyReadStandardError) self.process.readyReadStandardOutput.connect(self.onReadyReadStandardOutput) self.add() # Add items to the layout def ispressed(self): return self.pressed def added(self): self.pressed = True def remove(self): self.parent.hideFileExecuter() self.clicked = True def hideTerminalClicked(self): return self.clicked def onReadyReadStandardError(self): try: self.error = self.process.readAllStandardError().data().decode() self.editor.appendPlainText(self.error) self.errorSignal.emit(self.error) if self.error == "": pass else: self.error = self.error.split(os.linesep)[-2] self.dialog.helpword = str(self.error) self.dialog.getHelp(self.parent.parent) except IndexError as E: print(E, " on line 70 in the file Console.py") def onReadyReadStandardOutput(self): try: self.result = self.process.readAllStandardOutput().data().decode() except UnicodeDecodeError as E: print(E, " on line 76 in the file Console.py") try: self.editor.appendPlainText(self.result.strip("\n")) self.state = self.process.state() except RuntimeError: pass self.outputSignal.emit(self.result) def ifFinished(self, exitCode, exitStatus): self.finished = True def add(self): """Executes a system command.""" # clear previous text self.added() self.button = QPushButton("Hide terminal") self.button.setFont(QFont("Iosevka", 11)) self.button.setStyleSheet(""" height: 20; background-color: #212121; """) self.terminateButton = QPushButton(" Stop") self.terminateButton.setIcon(QIcon("resources/square.png")) self.terminateButton.setFont(QFont("Iosevka", 11)) self.terminateButton.clicked.connect(self.terminate) self.button.setFixedWidth(120) self.h_layout = QHBoxLayout() self.editor = Editor(self) self.editor.setReadOnly(True) self.editor.setFont(self.font) self.layout.addWidget(self.button) self.layout.addWidget(self.editor) self.layout.addWidget(self.terminateButton) self.button.clicked.connect(self.remove) def run(self, command, path): # Takes in the command and the path of the file os.chdir(os.path.dirname(path)) # We need to change the path to the path where the file is being ran from self.editor.clear() if self.process.state() == 1 or self.process.state() == 2: self.process.kill() self.editor.setPlainText("Process already started, terminating") else: self.process.start(command) def terminate(self): if self.process.state() == 2: self.process.kill()