744 |
744 |
745 @param saveAs flag indicating to save the file with a new name |
745 @param saveAs flag indicating to save the file with a new name |
746 @type bool |
746 @type bool |
747 """ |
747 """ |
748 aw = ericApp().getObject("ViewManager").activeWindow() |
748 aw = ericApp().getObject("ViewManager").activeWindow() |
749 if not aw: |
749 if aw: |
750 return |
750 selectedItems = self.deviceFileTreeWidget.selectedItems() |
751 |
751 if selectedItems: |
752 selectedItems = self.deviceFileTreeWidget.selectedItems() |
752 filename = selectedItems[0].text(0).strip() |
753 if selectedItems: |
753 if filename.endswith("/"): |
754 filename = selectedItems[0].text(0).strip() |
754 saveAs = True |
755 if filename.endswith("/"): |
755 else: |
756 saveAs = True |
756 saveAs = True |
757 else: |
757 filename = "" |
758 saveAs = True |
758 |
759 filename = "" |
759 if saveAs: |
760 |
760 filename, ok = QInputDialog.getText( |
761 if saveAs: |
761 self, |
762 filename, ok = QInputDialog.getText( |
762 self.tr("Save File As"), |
763 self, |
763 self.tr("Enter a new name for the file:"), |
764 self.tr("Save File As"), |
764 QLineEdit.EchoMode.Normal, |
765 self.tr("Enter a new name for the file:"), |
765 filename, |
766 QLineEdit.EchoMode.Normal, |
766 ) |
767 filename, |
767 if not ok or not filename: |
768 ) |
768 return |
769 if not ok or not filename: |
769 |
770 return |
770 if not saveAs: |
771 |
771 # check editor and selected file names for an implicit 'save as' |
772 if not saveAs: |
772 editorFileName = os.path.basename( |
773 # check editor and selected file names for an implicit 'save as' |
773 FileSystemUtilities.plainFileName(aw.getFileName()) |
774 editorFileName = os.path.basename( |
774 ) |
775 FileSystemUtilities.plainFileName(aw.getFileName()) |
775 if editorFileName != filename: |
776 ) |
776 saveAs = True |
777 if editorFileName != filename: |
777 |
778 saveAs = True |
778 if saveAs and self.__isFileInList(filename, self.deviceFileTreeWidget): |
779 |
779 # ask for overwrite permission |
780 if saveAs and self.__isFileInList(filename, self.deviceFileTreeWidget): |
780 action, resultFilename = confirmOverwrite( |
781 # ask for overwrite permission |
781 filename, |
782 action, resultFilename = confirmOverwrite( |
782 self.tr("Save File As"), |
783 filename, |
783 self.tr("The given file exists already (Enter file name only)."), |
784 self.tr("Save File As"), |
784 False, |
785 self.tr("The given file exists already (Enter file name only)."), |
785 self, |
786 False, |
786 ) |
787 self, |
787 if action == "cancel": |
788 ) |
788 return |
789 if action == "cancel": |
789 elif action == "rename": |
790 return |
790 filename = os.path.basename(resultFilename) |
791 elif action == "rename": |
791 |
792 filename = os.path.basename(resultFilename) |
792 text = aw.text() |
793 |
793 if self.__repl.deviceSupportsLocalFileAccess(): |
794 text = aw.text() |
794 filename = os.path.join(self.deviceCwd.text(), filename) |
795 if self.__repl.deviceSupportsLocalFileAccess(): |
795 os.makedirs(os.path.dirname(filename), exist_ok=True) |
796 filename = os.path.join(self.deviceCwd.text(), filename) |
796 with open(filename, "w") as f: |
797 os.makedirs(os.path.dirname(filename), exist_ok=True) |
797 f.write(text) |
798 with open(filename, "w") as f: |
798 self.__newDeviceList() |
799 f.write(text) |
799 aw.setFileName(filename) |
800 self.__newDeviceList() |
800 else: |
801 aw.setFileName(filename) |
801 if not filename.startswith("/"): |
802 else: |
802 deviceCwd = self.deviceCwd.text() |
803 if not filename.startswith("/"): |
803 if deviceCwd: |
804 deviceCwd = self.deviceCwd.text() |
804 filename = ( |
805 if deviceCwd: |
805 deviceCwd + "/" + filename |
806 filename = ( |
806 if deviceCwd != "/" |
807 deviceCwd + "/" + filename |
807 else "/" + filename |
808 if deviceCwd != "/" |
808 ) |
809 else "/" + filename |
809 dirname = filename.rsplit("/", 1)[0] |
810 ) |
810 self.__fileManager.makedirs(dirname) |
811 dirname = filename.rsplit("/", 1)[0] |
811 self.__fileManager.putData(filename, text.encode("utf-8")) |
812 self.__fileManager.makedirs(dirname) |
812 aw.setFileName(FileSystemUtilities.deviceFileName(filename)) |
813 self.__fileManager.putData(filename, text.encode("utf-8")) |
813 |
814 aw.setFileName(FileSystemUtilities.deviceFileName(filename)) |
814 aw.setModified(False) |
815 |
815 aw.resetOnlineChangeTraceInfo() |
816 aw.setModified(False) |
|
817 aw.resetOnlineChangeTraceInfo() |
|
818 |
816 |
819 @pyqtSlot() |
817 @pyqtSlot() |
820 def on_saveAsButton_clicked(self): |
818 def on_saveAsButton_clicked(self): |
821 """ |
819 """ |
822 Private slot to save the current editor in a new file on the connected device. |
820 Private slot to save the current editor in a new file on the connected device. |