src/eric7/QScintilla/Editor.py

branch
eric7
changeset 9853
080e060a0383
parent 9790
6b14962d9d31
child 9925
a267ac36dd69
--- a/src/eric7/QScintilla/Editor.py	Mon Mar 06 11:39:26 2023 +0100
+++ b/src/eric7/QScintilla/Editor.py	Mon Mar 06 16:13:42 2023 +0100
@@ -227,12 +227,13 @@
 
         self.dbs = dbs
         self.taskViewer = tv
-        self.setFileName(fn)
+        self.fileName = ""
         self.vm = vm
         self.filetype = filetype
         self.filetypeByFlag = False
         self.noName = ""
         self.project = ericApp().getObject("Project")
+        self.setFileName(fn)
 
         # clear some variables
         self.lastHighlight = None  # remember the last highlighted line
@@ -620,8 +621,13 @@
         @param name name of the current file
         @type str
         """
+        renamed = self.fileName != name
+
         self.fileName = name
 
+        if renamed:
+            self.vm.setEditorName(self, self.fileName)
+
         if self.fileName:
             self.__fileNameExtension = os.path.splitext(self.fileName)[1][1:].lower()
         else:
@@ -3256,7 +3262,7 @@
                 self,
                 self.tr("File Modified"),
                 self.tr("<p>The file <b>{0}</b> has unsaved changes.</p>").format(fn),
-                self.saveFile if self.isLocalFile() else None,
+                self.saveFile if not self.isRemoteFile() else None,
             )
             if res:
                 self.vm.setEditorName(self, self.fileName)
@@ -3528,11 +3534,13 @@
         @param path directory to save the file in (string)
         @return flag indicating success (boolean)
         """
-        if not saveas and (not self.isModified() or not self.isLocalFile()):
-            # do nothing if text wasn't changed or is not a local file
-            # TODO: write the file back to the MCU if isDeviceFile()
+        if not saveas and (not self.isModified() or self.isRemoteFile()):
+            # do nothing if text wasn't changed or is a remote file
             return False
 
+        if self.isDeviceFile():
+            return self.__saveDeviceFile(saveas=saveas)
+
         newName = None
         if saveas or self.fileName == "":
             saveas = True
@@ -3609,6 +3617,46 @@
         """
         return self.saveFile(True, path)
 
+    def __saveDeviceFile(self, saveas=False):
+        """
+        Private method to save the text to a file on the connected device.
+
+        @param saveas flag indicating a 'save as' action (defaults to False)
+        @type bool (optional)
+        @return flag indicating success
+        @rtype bool
+        """
+        with contextlib.suppress(KeyError):
+            mpy = ericApp().getObject("MicroPython")
+            filemanager = mpy.getFileManager()
+            if saveas:
+                fn, ok = QInputDialog.getText(
+                    self,
+                    self.tr("Save File to Device"),
+                    self.tr("Enter the complete device file path:"),
+                    QLineEdit.EchoMode.Normal,
+                    self.fileName,
+                )
+                if not ok or not fn:
+                    # aborted
+                    return False
+            else:
+                fn = self.fileName
+            # Convert the file name to device path separators ('/') and ensure,
+            # intermediate directories exist (creating them if necessary)
+            fn = fn.replace("\\", "/")
+            if "/" in fn:
+                dn = fn.rsplit("/", 1)[0]
+                filemanager.makedirs(dn.replace("device:", ""))
+            success = filemanager.writeFile(fn.replace("device:", ""), self.text())
+            if success:
+                self.setFileName(fn)
+                self.setModified(False)
+                self.resetOnlineChangeTraceInfo()
+            return success
+
+        return False
+
     def handleRenamed(self, fn):
         """
         Public slot to handle the editorRenamed signal.

eric ide

mercurial