def saveFileAs(self): try: active_tab = self.tab.tabs.currentWidget() if active_tab is not None: active_index = self.tab.tabs.currentIndex() options = QFileDialog.Options() name = QFileDialog.getSaveFileName( self, 'Save File', '', 'All Files (*);;Python Files (*.py);;Text Files (*.txt)', options=options) fileName = name[0] with open(fileName, "w+") as saveFile: active_tab.saved = True active_tab.modified = False self.tabsOpen.append(fileName) try: baseName = os.path.basename(fileName) except AttributeError: print("All tabs closed") saveFile.write(active_tab.editor.toPlainText()) text = active_tab.editor.toPlainText() newTab = Content(str(text), fileName, baseName, self) newTab.ready = True self.tab.tabs.removeTab( active_index ) # When user changes the tab name we make sure we delete the old one index = self.tab.tabs.addTab( newTab, newTab.baseName) # And add the new one! self.tab.tabs.setTabToolTip(index, str(newTab.fileName)) self.tab.tabs.setCurrentIndex(index) newActiveTab = self.tab.tabs.currentWidget() newActiveTab.editor.setFont(self.font) newActiveTab.editor.setFocus() saveFile.close() self.setWindowTitle("PyPad ~ " + str(active_tab.baseName) + " [SAVED]") else: print("No file opened") except FileNotFoundError: print("File dialog closed")
def newFile(self): text = "" if self._dir: base_file_name = "Untitled_file_" + str(random.randint( 1, 100)) + ".py" fileName = str(self._dir) + "/" + base_file_name else: base_file_name = "Untitled_file_" + str(random.randint( 1, 100)) + ".py" current = os.getcwd() fileName = current + "/" + base_file_name self.pyFileOpened = True # Creates a new blank file file = Content(text, fileName, base_file_name, self) self.tab.splitterH.addWidget( self.tab.tabs ) # Adding tabs, now the directory tree will be on the left self.tab.tabCounter.append(file.fileName) self.tab.setLayout(self.tab.layout) # Finally we set the layout index = self.tab.tabs.addTab( file, file.baseName ) # addTab method returns an index for the tab that was added self.tab.tabs.setTabToolTip(index, str(file.fileName)) self.tab.tabs.setCurrentIndex( index) # Setting focus to the new tab that we created widget = self.tab.tabs.currentWidget()
def open_documentation(self, data, word): """ Opens documentation for a built in function """ data = data.replace("|", "") index = self.tab.tabs.addTab( Content(data, os.getcwd() + "/" + str(word) + ".doc", str(word) + ".doc", self, True), str(word)) self.tab.tabs.setCurrentIndex(index)
def openFile(self, filename): try: for index, tabName in enumerate(self.tab.tabCounter): with open(filename, 'r+') as file_o: if filename[-3:] in ['gif', 'png', 'jpg', 'bmp' ] or filename[-4:] in ['jpeg']: self.pic_opened = True else: self.pic_opened = False try: text = file_o.read() except UnicodeDecodeError as E: text = str(E) basename = os.path.basename(filename) if not self.pic_opened: tab = Content(text, filename, basename, self) tab.saved = True tab.modified = False else: tab = Image(filename, basename) if tabName == tab.baseName: self.tab.tabs.removeTab(index) self.tab.tabCounter.remove(tab.baseName) try: with open(filename, 'r+') as file_o: try: if self.pic_opened is not True: text = file_o.read() else: text = None except (FileNotFoundError, UnicodeDecodeError, AttributeError) as E: text = str(E) except FileNotFoundError: with open(filename, 'w+') as newFileCreated: text = newFileCreated.read() basename = os.path.basename(filename) if self.pic_opened is True: tab = Image(filename, basename) else: tab = Content(text, filename, basename, self) # Creating a tab object *IMPORTANT* tab.saved = True tab.modified = False self.tab.tabCounter.append(tab.baseName) dirPath = os.path.dirname(filename) self.files = filename self.tabsOpen.append(self.files) index = self.tab.tabs.addTab( tab, tab.baseName ) # This is the index which we will use to set the current self.tab.tabs.setTabToolTip(index, str(tab.fileName)) if not self.dir_opened: # If a project isnt opened then we open a directory everytime we open a file self.tab.directory.openDirectory(dirPath) self.tab.showDirectory() else: pass self.tab.setLayout(self.tab.layout) # Finally we set the layout update_previous_file(filename) self.tab.tabs.setCurrentIndex( index) # Setting the index so we could find the current widget self.currentTab = self.tab.tabs.currentWidget() if self.pic_opened is not True: self.currentTab.editor.setFont(self.font) # Setting the font self.currentTab.editor.setFocus( ) # Setting focus to the tab after we open it self.pic_opened = False except (IsADirectoryError, AttributeError, UnboundLocalError, PermissionError) as E: print(E, " on line 346 in the file main.py")