def __init__(self, callback, app=None, palette=None): super().__init__() directoryFont = QFont() self.app = app self.palette = palette directoryFont.setFamily(editor["directoryFont"]) directoryFont.setPointSize(editor["directoryFontSize"]) self.open_callback = callback self.setContextMenuPolicy(Qt.CustomContextMenu) self.customContextMenuRequested.connect(self.openMenu) self.setFont(directoryFont) self.layout = QHBoxLayout() self.model = QFileSystemModel() self.setModel(self.model) self.model.setRootPath(QDir.rootPath()) self.setMaximumWidth(300) self.setIndentation(10) self.setAnimated(True) self.newFile() self.setSortingEnabled(True) self.setWindowTitle("Dir View") self.hideColumn(1) self.resize(200, 600) self.hideColumn(2) self.confirmation = MessageBox(self) self.hideColumn(3) # self.layout.addWidget(self) self.doubleClicked.connect(self.openFile)
def __init__(self, parent=None): super().__init__(parent) self.fileName = None self.parent = parent self.debugging = False self.line = None self.column = None self.wordlist = [] self.searchtext = None self.font = QFont() self.font.setFamily("Inconsolata") self.pointSize = editor["pointSize"] self.font.setPointSize(self.pointSize) self.dialog = MessageBox(self) self.verticalScrollBar().setStyleSheet(""" background-color: transparent; """) self.horizontalScrollBar().setStyleSheet(""" background-color: transparent; """) self.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded) self.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded) self.setCaretForegroundColor(QColor("#FFFFFF")) self.setEdgeColumn(121) self.setEdgeMode(1) self.setEdgeColor(QColor("#8c8c8c")) self.setFont(self.font) self.setMarginSensitivity(1, True) self.markerDefine(QsciScintilla.RightArrow, 8) self.setMarkerBackgroundColor(QColor('#FF0000'), 8) self.indicator_number = 0 self.indicator_value = 222 self.indicator_color = QColor("#FF0000") self.draw_under_text = True # Initializing some stuff self.set_brace_colors(QColor("#98b4f9"), QColor("#edf40e"), QColor("#98b4f9"), QColor("red")) self.cursorPositionChanged.connect(self.change_col) self.textChanged.connect(self.check_lines) self.set_linenumbers(QFontMetrics(self.font)) self.setFoldMarginColors(QColor("#212121"), QColor("#212121")) self.set_indentation_settings()
def __init__(self, app, palette, editor, parent=None): super().__init__(parent) self.editor = editor self.onStart(choiceIndex) self.status = QStatusBar(self) # Initializing the main widget where text is displayed self.tab = Tabs(self.openFile, app, palette, self) self.tabsOpen = [] self.pic_opened = False self.dialog = MessageBox(self) self.setWindowIcon(QIcon('resources/Python-logo-notext.svg_.png') ) # Setting the window icon self.setWindowTitle('PyPad') # Setting the window title self.status_font = QFont(editor["statusBarFont"], editor["statusBarFontSize"]) self.os = platform.system() self.tab.tabs.currentChanged.connect(self.fileNameChange) self.search = DocumentSearch() self.openterm() self.openterminal() # self.split2Tabs() self.new() self.newProject() self.findDocument() self.openProjectF() self.open() self.save() self.saveAs() self.exit() self.thread = UpdateThread() self.thread.start() self.thread.textSignal.connect(self.check_updates) self.dir_opened = False self._dir = None self.update_progress = QProgressBar() self.update_progress.setMaximumWidth(225) self.update_progress.setStyleSheet(self.update_progress.styleSheet()) self.setCentralWidget(self.tab) self.files = None # Tracking the current file that is open self.cFileOpened = False self.update_process = QProcess() self.initUI() # Main UI
def __init__(self, parent, isReadOnly=False): super().__init__(parent) self.lineNumberArea = QLineNumberArea(self) self.blockCountChanged.connect(self.updateLineNumberAreaWidth) self.updateRequest.connect(self.updateLineNumberArea) self.cursorPositionChanged.connect(self.highlightCurrentLine) self.updateLineNumberAreaWidth(0) self.setReadOnly(isReadOnly) self.parent = parent self.font = QFont() self.size = 12 self.worldList = wordList self.dialog = MessageBox(self) self.menu_font = QFont() self.menu_font.setFamily(editor["menuFont"]) self.menu_font.setPointSize(editor["menuFontSize"]) self.font.setFamily(editor["editorFont"]) self.font.setPointSize(editor["editorFontSize"]) self.focused = None self.text = None self.replace_tabs = 4 self.setWordWrapMode(4) self.setFont(self.font) self.l = 0 self.highlightingRules = [] self.extraSelections_ = [] self.indexes = None self.setMouseTracking(True) self.completer = None self.info_process = QProcess() self.setTabStopWidth(editor["TabWidth"]) self.createStandardContextMenu() self.setWordWrapMode(QTextOption.NoWrap) self.cursorPositionChanged.connect(self.getColAndRow) self.info_process.readyReadStandardOutput.connect( self.get_pydoc_output)
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.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 __init__(self, callback, app, palette, parent=None): super().__init__() self.app = app self.parent = parent self.palette = palette self.terminal = Terminal(self, False) self.tool_layout = QVBoxLayout() self.tool_layout_bar = QHBoxLayout() self.layout = QVBoxLayout(self) # Initialize tab screen self.tabs = QTabWidget() self.events = Events() self.tabs.setStyleSheet(""" QTabWidget::pane { /* The tab widget frame */ border-top: 0.5px solid #2c2c2c; } QTabWidget::tab-bar { } /* Style the tab using the tab sub-control. Note that it reads QTabBar _not_ QTabWidget */ QTabBar::tab { background: #212121; border-bottom: 2px solid #303030; border-bottom-color: #434343; min-width: 8ex; margin-left: 10px; padding-left: 25px; padding-right: 20px; padding-top: 3px; padding-bottom: 3px; } QTabBar::tab:selected, QTabBar::tab:hover { background: #212121; } QToolTip { padding: 3px; font-family: \"Iosevka\"; font-size: 14px; color: #FFFFFF; background: #2c2c2c; } QTabBar::tab:selected { border-bottom-color: #FFFFFF; /* same as pane color */ } """) font = QFont(editor['tabFont']) font.setPointSize( editor["tabFontSize"]) # This is the tab font and font size self.tabs.setFont(font) self.status = QStatusBar(self) self.dialog = MessageBox(self) self.tabs.usesScrollButtons() self.filelist = [] self.tabSaved = False self.Console = Console( self) # This is the terminal widget and the SECOND thread self.directory = Directory(callback, self.app, self.palette) # TODO: This is top left self.directory.clearSelection() self.tabCounter = [] # Add tabs self.tab_layout = QHBoxLayout( ) # Create new layout for original tab layout self.tab_layout.addWidget(self.tabs) # Add tab widget to tab layout self.search_layout = QHBoxLayout() self.tabs.setTabsClosable(True) self.tabs.setMovable( editor['tabMovable']) # Let's you make the tabs movable if editor[ 'tabShape'] is True: # If tab shape is true then they have this rounded look self.tabs.setTabShape(1) else: self.tabs.setTabShape(0) # If false, it has this boxy look self.tabs.tabCloseRequested.connect(self.closeTab) # Build Layout self.layout.addLayout( self.tab_layout) # Adds 'TOP' layout : tab + directory self.layout.addLayout(self.search_layout) # Creating horizontal splitter self.splitterH = QSplitter(Qt.Horizontal) # Creating vertical splitter self.splitterV = QSplitter(Qt.Vertical) self.splitterV2 = QSplitter(Qt.Vertical) self.splitterV.addWidget(self.splitterH) self.layout.addWidget(self.splitterV) self.splitterV.setSizes([400, 10]) self.setLayout(self.layout) # Sets layout of QWidget self.closeShortcut = QShortcut( QKeySequence(editor["closeTabShortcut"]), self) self.closeShortcut.activated.connect(self.closeTabShortcut) self.getAllOpenTabs = QShortcut(QKeySequence("Ctrl+Shift+W"), self) self.getAllOpenTabs.activated.connect(self.getAllOpenTabsFunc) currentTab = self.tabs.currentWidget() self.layout.addLayout(self.tool_layout) self.layout.addWidget(self.splitterV) self.hideDirectory()
class Tabs(QWidget): def __init__(self, callback, app, palette, parent=None): super().__init__() self.app = app self.parent = parent self.palette = palette self.terminal = Terminal(self, False) self.tool_layout = QVBoxLayout() self.tool_layout_bar = QHBoxLayout() self.layout = QVBoxLayout(self) # Initialize tab screen self.tabs = QTabWidget() self.events = Events() self.tabs.setStyleSheet(""" QTabWidget::pane { /* The tab widget frame */ border-top: 0.5px solid #2c2c2c; } QTabWidget::tab-bar { } /* Style the tab using the tab sub-control. Note that it reads QTabBar _not_ QTabWidget */ QTabBar::tab { background: #212121; border-bottom: 2px solid #303030; border-bottom-color: #434343; min-width: 8ex; margin-left: 10px; padding-left: 25px; padding-right: 20px; padding-top: 3px; padding-bottom: 3px; } QTabBar::tab:selected, QTabBar::tab:hover { background: #212121; } QToolTip { padding: 3px; font-family: \"Iosevka\"; font-size: 14px; color: #FFFFFF; background: #2c2c2c; } QTabBar::tab:selected { border-bottom-color: #FFFFFF; /* same as pane color */ } """) font = QFont(editor['tabFont']) font.setPointSize( editor["tabFontSize"]) # This is the tab font and font size self.tabs.setFont(font) self.status = QStatusBar(self) self.dialog = MessageBox(self) self.tabs.usesScrollButtons() self.filelist = [] self.tabSaved = False self.Console = Console( self) # This is the terminal widget and the SECOND thread self.directory = Directory(callback, self.app, self.palette) # TODO: This is top left self.directory.clearSelection() self.tabCounter = [] # Add tabs self.tab_layout = QHBoxLayout( ) # Create new layout for original tab layout self.tab_layout.addWidget(self.tabs) # Add tab widget to tab layout self.search_layout = QHBoxLayout() self.tabs.setTabsClosable(True) self.tabs.setMovable( editor['tabMovable']) # Let's you make the tabs movable if editor[ 'tabShape'] is True: # If tab shape is true then they have this rounded look self.tabs.setTabShape(1) else: self.tabs.setTabShape(0) # If false, it has this boxy look self.tabs.tabCloseRequested.connect(self.closeTab) # Build Layout self.layout.addLayout( self.tab_layout) # Adds 'TOP' layout : tab + directory self.layout.addLayout(self.search_layout) # Creating horizontal splitter self.splitterH = QSplitter(Qt.Horizontal) # Creating vertical splitter self.splitterV = QSplitter(Qt.Vertical) self.splitterV2 = QSplitter(Qt.Vertical) self.splitterV.addWidget(self.splitterH) self.layout.addWidget(self.splitterV) self.splitterV.setSizes([400, 10]) self.setLayout(self.layout) # Sets layout of QWidget self.closeShortcut = QShortcut( QKeySequence(editor["closeTabShortcut"]), self) self.closeShortcut.activated.connect(self.closeTabShortcut) self.getAllOpenTabs = QShortcut(QKeySequence("Ctrl+Shift+W"), self) self.getAllOpenTabs.activated.connect(self.getAllOpenTabsFunc) currentTab = self.tabs.currentWidget() self.layout.addLayout(self.tool_layout) self.layout.addWidget(self.splitterV) self.hideDirectory() @pyqtSlot() def closeTabShortcut(self): self.index = self.tabs.currentIndex() self.closeTab(self.index) def getAllOpenTabsFunc(self): word = 'import' for tab in range(self.tabs.count()): file = self.tabs.widget(tab).fileName if file not in self.filelist: self.filelist.append(file) for file in self.filelist: openedFileContents = open(file, 'r').read() def closeTab(self, index): try: tab = self.tabs.widget(index) if tab.saved is True and tab.modified is False: tab.deleteLater() self.tabCounter.pop(index) self.filelist.pop(index) self.tabs.removeTab(index) elif tab.modified is True: self.dialog.saveMaybe(tab, self.tabCounter, self.tabs, index) except (AttributeError, IndexError) as E: try: tab.deleteLater() self.tabCounter.pop(index) self.filelist.pop(index) self.tabs.removeTab(index) except (AttributeError, IndexError) as E: print(E, " on line 175 in the file Tabs.py") def showDirectory(self): self.directory.setVisible(True) self.tab_layout.removeWidget(self.tabs) self.splitterV2.addWidget(self.directory) self.splitterV2.addWidget(self.events) self.splitterH.addWidget(self.splitterV2) self.splitterH.addWidget( self.tabs ) # Adding tabs, now the directory tree will be on the left def hideDirectory(self): self.tab_layout.removeWidget(self.directory) self.directory.setVisible(False) """ Because the root layouts are set all you have to do now is just add/remove widgets from the parent layout associated This keeps the UI order set as intended as built above when initialized. """ def hideConsole(self): self.splitterV.setSizes([0, 0]) def showConsole(self): self.splitterV.addWidget(self.terminal) self.splitterV.setSizes([400, 10]) self.terminal.clicked = False def hideFileExecuter(self): self.splitterV.setSizes([0, 0]) def showFileExecuter(self): self.splitterV.addWidget(self.Console) self.splitterV.setSizes([400, 10]) self.Console.clicked = False def currentTab(self): return self.tabs.currentWidget()
class Editor(QsciScintilla): def __init__(self, parent=None): super().__init__(parent) self.fileName = None self.parent = parent self.debugging = False self.line = None self.column = None self.wordlist = [] self.searchtext = None self.font = QFont() self.font.setFamily("Inconsolata") self.pointSize = editor["pointSize"] self.font.setPointSize(self.pointSize) self.dialog = MessageBox(self) self.verticalScrollBar().setStyleSheet(""" background-color: transparent; """) self.horizontalScrollBar().setStyleSheet(""" background-color: transparent; """) self.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded) self.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded) self.setCaretForegroundColor(QColor("#FFFFFF")) self.setEdgeColumn(121) self.setEdgeMode(1) self.setEdgeColor(QColor("#8c8c8c")) self.setFont(self.font) self.setMarginSensitivity(1, True) self.markerDefine(QsciScintilla.RightArrow, 8) self.setMarkerBackgroundColor(QColor('#FF0000'), 8) self.indicator_number = 0 self.indicator_value = 222 self.indicator_color = QColor("#FF0000") self.draw_under_text = True # Initializing some stuff self.set_brace_colors(QColor("#98b4f9"), QColor("#edf40e"), QColor("#98b4f9"), QColor("red")) self.cursorPositionChanged.connect(self.change_col) self.textChanged.connect(self.check_lines) self.set_linenumbers(QFontMetrics(self.font)) self.setFoldMarginColors(QColor("#212121"), QColor("#212121")) self.set_indentation_settings() def set_up_tooltips(self): self.setCallTipsStyle(QsciScintilla.CallTipsNoContext) self.setCallTipsVisible(0) self.setCallTipsPosition(QsciScintilla.CallTipsAboveText) self.setCallTipsBackgroundColor(QColor("#FF0000")) self.setCallTipsForegroundColor(QColor("#FF0000")) self.setCallTipsHighlightColor(QColor("#FF0000")) def set_brace_colors(self, matched_B=None, matched_F=None, unmatched_B=None, unmatched_F=None): self.setMatchedBraceBackgroundColor(matched_B) self.setMatchedBraceForegroundColor(matched_F) self.setUnmatchedBraceBackgroundColor(unmatched_B) self.setUnmatchedBraceForegroundColor(unmatched_F) self.setBraceMatching(QsciScintilla.SloppyBraceMatch) def set_linenumbers(self, fontmetrics): self.setMarginsFont(self.font) self.setMarginWidth(0, fontmetrics.width("00000")) self.setMarginLineNumbers(0, True) self.setMarginsBackgroundColor(QColor("#212121")) self.setMarginsForegroundColor(QColor("#FFFFFF")) def set_indentation_settings(self): self.setIndentationsUseTabs(False) self.setTabWidth(4) self.SendScintilla(QsciScintilla.SCI_SETUSETABS, False) self.setAutoIndent(True) self.setTabIndents(True) def check_lines(self): line_n = self.lines() for i in range(line_n): if self.lineLength(i) > 121: # TODO: Make a character format or something pass # print("Line over 121 characters on line", str(i+1)) # self.setCursorPosition(i, 120) def python_highlighter(self): self.lexer = PythonLexer() self.lexer.setFoldComments(True) self.setCaretLineVisible(True) self.setDefaultSettings(self.lexer) self.setPythonAutocomplete() self.setFold() def json_highlighter(self): lexer = QsciLexerJSON() self.setDefaultSettings(lexer) def c_highlighter(self): lexer = QsciLexerCPP() self.setDefaultSettings(lexer) def xml_highlighter(self): lexer = QsciLexerXML() self.setDefaultSettings(lexer) def html_highlighter(self): lexer = QsciLexerHTML() self.setDefaultSettings(lexer) def setDefaultSettings(self, lexer): # self.setAutoIndent(True) lexer.setFont(self.font) lexer.setColor(QColor('white'), 0) # default lexer.setColor(QColor('#6B6E6C'), PythonLexer.Comment) # = 1 lexer.setColor(QColor('#ADD4FF'), 2) # Number = 2 lexer.setColor(QColor('#38ef7d'), 3) # DoubleQuotedString lexer.setColor(QColor('#38ef7d'), 4) # SingleQuotedString lexer.setColor(QColor('#F6DC74'), 5) # Keyword lexer.setColor(QColor('#38ef7d'), 6) # TripleSingleQuotedString lexer.setColor(QColor('#38ef7d'), 7) # TripleDoubleQuotedString lexer.setColor(QColor('#74F6C3'), 8) # ClassName lexer.setColor(QColor('#FF6666'), 9) # FunctionMethodName lexer.setColor(QColor('magenta'), 10) # Operator lexer.setColor(QColor('white'), 11) # Identifier lexer.setColor(QColor('gray'), 12) # CommentBlock lexer.setColor(QColor('#a8ff78'), 13) # UnclosedString lexer.setColor(QColor('gray'), 14) # HighlightedIdentifier lexer.setColor(QColor('#FF00E7'), 15) # Decorator lexer.setFont(QFont("Iosevka", weight=QFont.Bold), 5) self.setCaretLineBackgroundColor(QColor("#3C3B3F")) self.setLexer(lexer) def setPythonAutocomplete(self): self.autocomplete = QsciAPIs(self.lexer) self.keywords = wordList for word in self.keywords: self.autocomplete.add(word) self.setAutoCompletionThreshold(2) self.setAutoCompletionSource(QsciScintilla.AcsAPIs) self.updateAutoComplete(self.parent.fileName) self.autocomplete.prepare() def setFold(self): # setup Fold Styles for classes and functions ... x = self.FoldStyle(self.FoldStyle(5)) # self.textPad.folding() if not x: self.foldAll(False) self.setFolding(x) # self.textPad.folding() def unsetFold(self): self.setFolding(0) def updateAutoComplete(self, file_path=None): for i in tokenize(file_path): for j in i: if j not in self.wordlist: self.wordlist.append(j) for word in self.wordlist: self.autocomplete.add(word) self.autocomplete.prepare() def change_col(self, line, column): # Responsible for changing the column bar. self.line = line self.column = column def check_if_func(self, word): # Checks if a word is a built in function word_array = list(word) for wo in word_array: if wo in ["{", "}", "'", '"', "[", "]", "(", ")"]: word_array.remove(wo) for w in funcList: if w == "".join(word_array): return True def check_if_error(self, word): if word in errorList: # This is the list where all possible errors are defined return True def keyReleaseEvent(self, e): if e.key() == Qt.Key_Return: try: self.updateAutoComplete(self.parent.fileName) except AttributeError as E: print(E, "on line 210 in TextEditor.py") if e.key() == Qt.Key_Backspace: pass def mousePressEvent(self, e): super().mousePressEvent(e) if QGuiApplication.queryKeyboardModifiers() == Qt.ControlModifier: word = self.wordAtLineIndex(self.getCursorPosition()[0], self.getCursorPosition()[1]) print(word) if self.check_if_func(word): url = "https://docs.python.org/3/library/functions.html#" + word self.parent.parent.openBrowser( url, word) # Runs the openBrowser function in Main class elif self.check_if_error(word): url = "https://docs.python.org/3/library/exceptions.html#" + word print(url) self.parent.parent.openBrowser(url, word) def keyPressEvent(self, e): if e.modifiers() == Qt.ControlModifier and e.key() == Qt.Key_F: text, okPressed = QInputDialog.getText(self, 'Find', 'Find what: ') self.setSelectionBackgroundColor(QColor("#6be585")) if okPressed: if text == "": text = " " self.dialog.noMatch(text) self.searchtext = text """ This is the way to implement a search function using QScintilla http://pyqt.sourceforge.net/Docs/QScintilla2/classQsciScintilla.html#a37ac2bea94eafcfa639173557a821200 """ if self.findFirst(self.searchtext, False, True, False, True, True, -1, -1, True, False): pass else: self.dialog.noMatch(self.searchtext) if e.key() == Qt.Key_F3: self.findNext() self.setSelectionBackgroundColor(QColor("#6be585")) if e.modifiers() == Qt.ControlModifier and e.key() == Qt.Key_L: self.setCursorPosition(self.line, self.column + 1) return if e.modifiers() == Qt.ControlModifier and e.key() == 77: self.setCursorPosition(self.line + 1, self.column) return if e.modifiers() == Qt.ControlModifier and e.key() == Qt.Key_J: self.setCursorPosition(self.line, self.column - 1) if e.modifiers() == Qt.ControlModifier and e.key() == Qt.Key_I: self.setCursorPosition(self.line - 1, self.column) if e.modifiers() == Qt.ControlModifier and e.key() == Qt.Key_T: self.parent.parent.realterminal() return super().keyPressEvent(e)
class Directory(QTreeView): def __init__(self, callback, app=None, palette=None): super().__init__() directoryFont = QFont() self.app = app self.palette = palette directoryFont.setFamily(editor["directoryFont"]) directoryFont.setPointSize(editor["directoryFontSize"]) self.open_callback = callback self.setContextMenuPolicy(Qt.CustomContextMenu) self.customContextMenuRequested.connect(self.openMenu) self.setFont(directoryFont) self.layout = QHBoxLayout() self.model = QFileSystemModel() self.setModel(self.model) self.model.setRootPath(QDir.rootPath()) self.setMaximumWidth(300) self.setIndentation(10) self.setAnimated(True) self.newFile() self.setSortingEnabled(True) self.setWindowTitle("Dir View") self.hideColumn(1) self.resize(200, 600) self.hideColumn(2) self.confirmation = MessageBox(self) self.hideColumn(3) # self.layout.addWidget(self) self.doubleClicked.connect(self.openFile) def newFile(self): self.newAct = QAction('New') self.newAct.setStatusTip('Create a new file') self.newAct.triggered.connect(lambda: print("new")) def deleteFile(self): print("not implementeed") def openMenu(self, position): indexes = self.selectedIndexes() if len(indexes) > 0: level = 0 index = indexes[0] while index.parent().isValid(): index = index.parent() level += 1 menu = QMenu() menu.addAction( self.newAct ) # TODO: Add more context menu stuff and make them functional menu.exec_(self.viewport().mapToGlobal(position)) def focusInEvent(self, event): # If we are focused then we change the selected item highlighting color self.focused = True self.palette.setColor(QPalette.Highlight, QColor(editor["HighlightColor"]).lighter()) self.app.setPalette(self.palette) def focusOutEvent(self, event): # If we un focus from the QTreeView then we make the highlighted item color white self.palette.setColor( QPalette.Highlight, QColor(editor["UnfocusedHighlightColor"]).lighter()) # self.clearSelection() Uncomment this if you want to remove all highlighting when unfocused self.app.setPalette(self.palette) def openDirectory(self, path): self.setRootIndex(self.model.index(path)) def openFile(self, signal): file_path = self.model.filePath(signal) self.open_callback(file_path) return file_path def keyPressEvent(self, event): key = event.key() if key == Qt.Key_Delete: try: self.fileObject = self.selectedIndexes()[0] fileName = self.model.filePath(self.fileObject) self.confirmation.run("Are you sure you want to delete ", str(fileName)) except IndexError: print("No file selected")
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.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) def onReadyReadStandardOutput(self): try: self.result = self.process.readAllStandardOutput().data().decode() except UnicodeDecodeError as E: print(E) 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 print(path) try: os.chdir( os.path.dirname(path) ) # We need to change the path to the path where the file is being ran from except Exception as E: print(E) 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()
class Editor(QPlainTextEdit): def __init__(self, parent, isReadOnly=False): super().__init__(parent) self.lineNumberArea = QLineNumberArea(self) self.blockCountChanged.connect(self.updateLineNumberAreaWidth) self.updateRequest.connect(self.updateLineNumberArea) self.cursorPositionChanged.connect(self.highlightCurrentLine) self.updateLineNumberAreaWidth(0) self.setReadOnly(isReadOnly) self.parent = parent self.font = QFont() self.size = 12 self.worldList = wordList self.dialog = MessageBox(self) self.menu_font = QFont() self.menu_font.setFamily(editor["menuFont"]) self.menu_font.setPointSize(editor["menuFontSize"]) self.font.setFamily(editor["editorFont"]) self.font.setPointSize(editor["editorFontSize"]) self.focused = None self.text = None self.replace_tabs = 4 self.setWordWrapMode(4) self.setFont(self.font) self.l = 0 self.highlightingRules = [] self.extraSelections_ = [] self.indexes = None self.setMouseTracking(True) self.completer = None self.info_process = QProcess() self.setTabStopWidth(editor["TabWidth"]) self.createStandardContextMenu() self.setWordWrapMode(QTextOption.NoWrap) self.cursorPositionChanged.connect(self.getColAndRow) self.info_process.readyReadStandardOutput.connect( self.get_pydoc_output) def getTextCursor(self): textCursor = self.textCursor() textCursorPos = textCursor.position() return textCursor, textCursorPos def getColAndRow(self): textCursor = self.getTextCursor()[0] # print("row: {} column: {}".format(textCursor.blockNumber(), textCursor.positionInBlock())) def totalLines(self): return self.blockCount() def is_modified(self): return self.document().isModified() def check(self): cursor = self.textCursor() b = cursor.block() extraSelections = [] if len(b.text()) > 120: selection = QTextEdit.ExtraSelection() # TODO: implement something using flake8 selection.format.setFontUnderline(True) selection.format.setUnderlineColor(QColor("#FF0000")) selection.format.setUnderlineStyle( QTextCharFormat.SpellCheckUnderline) selection.format.setProperty(QTextFormat.FullWidthSelection, True) selection.cursor = self.textCursor() selection.cursor.clearSelection() self.extraSelections_.append(selection) self.setExtraSelections(self.extraSelections_) font = QFont() font.setFamily("Iosevka") font.setPointSize(10) QToolTip.setFont(font) cursor = self.textCursor() current_line = cursor.positionInBlock() QToolTip.showText( QCursor.pos(), "Line too long (" + str(current_line) + "> 120) | violation on line: " + str(b.blockNumber() + 1)) def newFile(self): """This is a wrapper for the function defined in Main""" self.new_action = QAction("New") self.new_action.triggered.connect(self.parent.parent.newFile) def lineNumberAreaWidth(self): digits = 1 max_value = max(1, self.blockCount()) while max_value >= 10: max_value /= 10 digits += 1 space = 10 + self.fontMetrics().width('9') * digits return space def updateLineNumberAreaWidth(self, _): self.setViewportMargins(self.lineNumberAreaWidth(), 0, 0, 0) def updateLineNumberArea(self, rect, dy): if dy: self.lineNumberArea.scroll(0, dy) else: self.lineNumberArea.update(0, rect.y(), self.lineNumberArea.width(), rect.height()) if rect.contains(self.viewport().rect()): self.updateLineNumberAreaWidth(0) def resizeEvent(self, event): super().resizeEvent(event) cr = self.contentsRect() self.lineNumberArea.setGeometry( QRect(cr.left(), cr.top(), self.lineNumberAreaWidth(), cr.height())) def highlightCurrentLine(self): extraSelections = [] if not self.isReadOnly(): selection = QTextEdit.ExtraSelection() lineColor = QColor("#434343") selection.format.setBackground(lineColor) selection.format.setProperty(QTextFormat.FullWidthSelection, True) selection.cursor = self.textCursor() selection.cursor.clearSelection() extraSelections.append(selection) self.setExtraSelections(extraSelections) self.check() return extraSelections def lineNumberAreaPaintEvent(self, event): painter = QPainter(self.lineNumberArea) painter.fillRect(event.rect(), QColor("#303030")) 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) painter.setPen(Qt.white) painter.drawText(0, top, self.lineNumberArea.width(), height, Qt.AlignCenter, number) block = block.next() top = bottom bottom = top + self.blockBoundingRect(block).height() blockNumber += 1 def openFile(self): self.open_action = QAction("Open") self.open_action.triggered.connect(self.parent.parent.openFileFromMenu) def runFile(self): self.run_action = QAction("Run") self.run_action.triggered.connect(self.parent.parent.execute_file) def textUnderCursor(self): textCursor = self.textCursor() textCursor.select(QTextCursor.WordUnderCursor) return textCursor.selectedText() def moveCursorPosBack(self): textCursor = self.textCursor() textCursorPos = textCursor.position() textCursor.setPosition(textCursorPos - 1) self.setTextCursor(textCursor) def mouseMoveEvent(self, QMouseEvent): # font = textCursor.block().charFormat().font() # metrics = QFontMetrics(font) # # b = self.document().findBlockByLineNumber(0) # # cursor = QTextCursor(b) # # cursor.select(QTextCursor.BlockUnderCursor) # # cursor.removeSelectedText() # # height = metrics.height() + 2 # y = QMouseEvent.pos().y() #print(y, height) #print(y/height) cursor_main = self.cursorForPosition(QMouseEvent.pos()) if QApplication.queryKeyboardModifiers() == Qt.ControlModifier: cursor_main.select(QTextCursor.WordUnderCursor) text = cursor_main.selectedText() self.text = text if self.text is not None: url = "https://docs.python.org/3/library/functions.html#" + self.text word = self.text # self.parent.parent.showBrowser(url, word) if self.check_func(word): extraSelections = self.highlightCurrentLine() selection = QTextEdit.ExtraSelection() selection.format.setFontUnderline(True) selection.format.setUnderlineColor(QColor("#00d2ff")) selection.format.setForeground(QColor("#00d2ff")) selection.format.setProperty( QTextFormat.FullWidthSelection, True) selection.cursor = self.cursorForPosition( QMouseEvent.pos()) selection.cursor.select(QTextCursor.WordUnderCursor) extraSelections.append(selection) self.setExtraSelections(extraSelections) cursor = QCursor(Qt.PointingHandCursor) # tooltip = QToolTip() QToolTip.setFont( QFont(editor["ToolTipFont"], editor["ToolTipFontSize"])) word_shown = eval(word).__doc__ if len(word_shown) > 2000: word_shown = word_shown[:2000] QToolTip.showText(QCursor.pos(), "{}".format(word_shown)) QApplication.setOverrideCursor(cursor) QApplication.changeOverrideCursor(cursor) else: self.returnCursorToNormal() else: pass else: self.returnCursorToNormal() extraSelections = self.highlightCurrentLine() self.setExtraSelections(extraSelections) super().mouseMoveEvent(QMouseEvent) def returnCursorToNormal(self): cursor = QCursor(Qt.ArrowCursor) QApplication.setOverrideCursor(cursor) QApplication.changeOverrideCursor(cursor) def mousePressEvent(self, e): self.check() if QApplication.queryKeyboardModifiers() == Qt.ControlModifier: super().mousePressEvent(e) self.text = self.textUnderCursor() if self.text is not None: word = self.text # self.parent.parent.showBrowser(url, word) if self.check_func(self.textUnderCursor(), True): extraSelections = self.highlightCurrentLine() selection = QTextEdit.ExtraSelection() selection.format.setFontUnderline(True) selection.format.setUnderlineColor(QColor("#00d2ff")) selection.format.setForeground(QColor("#00d2ff")) selection.format.setProperty( QTextFormat.FullWidthSelection, True) selection.cursor = self.textCursor() selection.cursor.clearSelection() selection.cursor.select(QTextCursor.WordUnderCursor) extraSelections.append(selection) self.setExtraSelections(extraSelections) self.text = self.textUnderCursor() # cursor = QCursor(Qt.PointingHandCursor) # QApplication.setOverrideCursor(cursor) # QApplication.changeOverrideCursor(cursor) else: self.text = None else: super().mousePressEvent(e) QApplication.restoreOverrideCursor() # super().mousePressEvent(e) def check_func(self, word, clicked=False): funcs = [ "abs", "all", "any", "ascii", "bin", "bool", "breakpoint", "bytearray", "bytes", "callable", "chr", "classmethod", "compile", "complex", "delattr", "dict", "dir", "divmod", "enumerate", "eval", "exec", "filter", "float", "format", "frozenset", "getattr", "globals", "hasattr", "hash", "help", "hex", "id", "input", "int", "isinstance", "issubclass", "iter", "len", "list", "locals", "map", "max", "memoryview", "min", "next", "object", "oct", "open", "ord", "pow", "print", "property", "range", "repr", "reversed", "round", "set", "setattr", "slice", "sorted", "staticmethod", "str", "sum", "super", "tuple", "type", "vars", "zip", "__import__" ] word_array = list(word) for wo in word_array: if wo in ["{", "}", "'", '"', "[", "]", "(", ")"]: word_array.remove(wo) for w in funcs: if w == "".join(word_array): if clicked: if self.info_process.state() == 0: self.info_process.start("pydoc {}".format(word)) else: pass return True def get_pydoc_output(self): output = self.info_process.readAllStandardOutput().data().decode() self.parent.parent.open_documentation(output, self.info_process.arguments()[0]) def contextMenuEvent(self, event): menu = QMenu() """Initializing actions""" self.newFile() self.openFile() self.runFile() menu.addAction(self.new_action) menu.addAction(self.open_action) menu.addAction(self.run_action) menu.setFont(self.menu_font) menu.exec(event.globalPos()) del menu def mouseReleaseEvent(self, e): self.check() super().mouseReleaseEvent(e) def keyPressEvent(self, e): textCursor = self.textCursor() key = e.key() if e.modifiers( ) == Qt.ControlModifier and key == 16777217: # that key code stands for tab self.parent.parent.switchTabs() isSearch = (e.modifiers() == Qt.ControlModifier and e.key() == Qt.Key_F) isDeleteLine = (e.modifiers() == Qt.ControlModifier and e.key() == 16777219 ) # This is for MacOS (CMD+Delete) if isSearch: try: currentWidget = self.parent currentFile = currentWidget.fileName currentEditor = currentWidget.editor textCursor = currentEditor.textCursor() textCursorPos = textCursor.position() if currentWidget is not None: text, okPressed = QInputDialog.getText( self, 'Find', 'Find what: ') if okPressed: if text == "": text = " " self.dialog.noMatch(text) self.searchtext = text try: with open(currentFile, 'r') as file: contents = file.read() self.indexes = list(find_all(contents, text)) if len(self.indexes) == 0: self.dialog.noMatch(text) except FileNotFoundError as E: print(E, " on line 245 in the file Editor.py") except (AttributeError, UnboundLocalError) as E: print(E, " on line 228 in the file Editor.py") if isDeleteLine and platform.system( ) == "Darwin": # Check if the os is MacOS textCursor.select(QTextCursor.LineUnderCursor) textCursor.removeSelectedText() if key == Qt.Key_QuoteDbl: self.insertPlainText('"') self.moveCursorPosBack() if e.modifiers() == Qt.ControlModifier and e.key( ) == 61: # Press Ctrl+Equal key to make font bigger self.font.setPointSize(self.size + 1) self.font.setFamily(editor["editorFont"]) self.setFont(self.font) self.size += 1 if e.modifiers() == Qt.ControlModifier and e.key() == 16777217: return if e.modifiers() == Qt.ControlModifier and e.key( ) == 45: # Press Ctrl+Minus key to make font smaller self.font.setPointSize(self.size - 1) self.font.setFamily(editor["editorFont"]) self.setFont(self.font) self.size -= 1 if key == Qt.Key_F3: try: index = self.indexes[0 + self.l] currentWidget = self.parent currentFile = currentWidget.fileName currentEditor = currentWidget.editor textCursor.setPosition(index) textCursor.movePosition(textCursor.Right, textCursor.KeepAnchor, len(self.searchtext)) currentEditor.setTextCursor(textCursor) self.l += 1 except IndexError: self.l = 0 if key == 39: self.insertPlainText("'") self.moveCursorPosBack() if key == Qt.Key_BraceLeft: self.insertPlainText("}") self.moveCursorPosBack() if key == Qt.Key_BracketLeft: self.insertPlainText("]") self.moveCursorPosBack() if key == Qt.Key_ParenLeft: self.insertPlainText(")") self.moveCursorPosBack() if key == Qt.Key_ParenRight: textCursor = self.textCursor() textCursor.select(QTextCursor.WordUnderCursor) if textCursor.selectedText( ) == "()" or "()" in textCursor.selectedText(): return if key == Qt.Key_BraceRight: textCursor = self.textCursor() textCursor.select(QTextCursor.WordUnderCursor) if textCursor.selectedText == "": return if key == 16777219: if self.textUnderCursor() in ['""', '()', '[]', "''", "{}"]: textCursor = self.textCursor() textCursorPos = textCursor.position() textCursor.setPosition(textCursorPos + 1) textCursor.deletePreviousChar() self.setTextCursor(textCursor) if key not in [16777217, 16777219, 16777220]: super().keyPressEvent(e) # print(self.textUnderCursor()) if len(self.textUnderCursor()) == 3: pass return # e.accept() cursor = self.textCursor() if key == 16777217: # and self.replace_tabs: amount = 4 - self.textCursor().positionInBlock() % 4 self.insertPlainText(' ' * amount) elif key == 16777219 and cursor.selectionStart() == cursor.selectionEnd() and self.replace_tabs and \ cursor.positionInBlock(): position = cursor.positionInBlock() end = cursor.position() start = end - (position % 4) if start == end and position >= 4: start -= 4 string = self.toPlainText()[start:end] if not len(string.strip() ): # if length is 0 which is binary for false for i in range(end - start): cursor.deletePreviousChar() else: super().keyPressEvent(e) elif key == 16777220: end = cursor.position() start = end - cursor.positionInBlock() line = self.toPlainText()[start:end] indentation = len(line) - len(line.lstrip()) chars = '\t' if self.replace_tabs: chars = ' ' indentation /= self.replace_tabs if line.endswith(':'): if self.replace_tabs: indentation += 1 super().keyPressEvent(e) self.insertPlainText(chars * int(indentation)) else: super().keyPressEvent(e)