MicroPython eric7

Thu, 05 Sep 2024 16:09:07 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Thu, 05 Sep 2024 16:09:07 +0200
branch
eric7
changeset 10911
9cccac01156f
parent 10910
13c8626c5024
child 10912
8c71124a71cd

MicroPython
- Extended the file manager widget context menus.

docs/changelog.md file | annotate | diff | comparison | revisions
src/eric7/MicroPython/MicroPythonFileManagerWidget.py file | annotate | diff | comparison | revisions
src/eric7/MicroPython/MicroPythonWidget.py file | annotate | diff | comparison | revisions
--- a/docs/changelog.md	Wed Sep 04 09:15:41 2024 +0200
+++ b/docs/changelog.md	Thu Sep 05 16:09:07 2024 +0200
@@ -6,6 +6,7 @@
     - Added MicroPython support for RP2350 based controllers.
     - Updated the list of known CircuitPython boards for CPy 9.2.0-alpha.2351.
     - Updated the list of known UF2 capable boards.
+    - Extended the file manager widget context menus.
 
 ### Version 24.9
 - bug fixes
--- a/src/eric7/MicroPython/MicroPythonFileManagerWidget.py	Wed Sep 04 09:15:41 2024 +0200
+++ b/src/eric7/MicroPython/MicroPythonFileManagerWidget.py	Thu Sep 05 16:09:07 2024 +0200
@@ -123,6 +123,10 @@
             self.__showDeviceContextMenu
         )
 
+        ########################################################################
+        ## Context menu for the local directory tree.
+        ########################################################################
+
         self.__localMenu = QMenu(self)
         self.__localMenu.addAction(
             self.tr("Change Directory"), self.__changeLocalDirectory
@@ -134,10 +138,15 @@
             self.tr("Delete Directory Tree"), self.__deleteLocalDirectoryTree
         )
         self.__localMenu.addSeparator()
+        self.__localMenu.addAction(
+            self.tr("New File"), self.__newLocalFile
+        )
+        self.__openLocalFileAct = self.__localMenu.addAction(
+            self.tr("Open File"), self.__openLocalFile
+        )
         self.__localRenameFileAct = self.__localMenu.addAction(
             self.tr("Rename File"), self.__renameLocalFile
         )
-        self.__localMenu.addSeparator()
         self.__localDelFileAct = self.__localMenu.addAction(
             self.tr("Delete File"), self.__deleteLocalFile
         )
@@ -151,6 +160,10 @@
             self.tr("Clear Selection"), self.__clearLocalSelection
         )
 
+        ########################################################################
+        ## Context menu for the device directory tree.
+        ########################################################################
+
         self.__deviceMenu = QMenu(self)
         if not isMicrobitDeviceWithMPy:
             self.__deviceMenu.addAction(
@@ -166,10 +179,15 @@
                 self.tr("Delete Directory Tree"), self.__deleteDeviceDirectoryTree
             )
             self.__deviceMenu.addSeparator()
+        self.__deviceMenu.addAction(
+            self.tr("New File"), self.__newDeviceFile
+        )
+        self.__openDeviceFileAct = self.__deviceMenu.addAction(
+            self.tr("Open File"), self.__openDeviceFile
+        )
         self.__devRenameFileAct = self.__deviceMenu.addAction(
             self.tr("Rename File"), self.__renameDeviceFile
         )
-        self.__deviceMenu.addSeparator()
         self.__devDelFileAct = self.__deviceMenu.addAction(
             self.tr("Delete File"), self.__deleteDeviceFile
         )
@@ -1099,6 +1117,7 @@
         self.__localDelDirTreeAct.setEnabled(isDir)
         self.__localRenameFileAct.setEnabled(isFile)
         self.__localDelFileAct.setEnabled(isFile)
+        self.__openLocalFileAct.setEnabled(isFile)
 
         self.__localMenu.exec(self.localFileTreeWidget.mapToGlobal(pos))
 
@@ -1364,6 +1383,7 @@
             self.__devDelDirTreeAct.setEnabled(isDir)
         self.__devRenameFileAct.setEnabled(isFile)
         self.__devDelFileAct.setEnabled(isFile)
+        self.__openDeviceFileAct.setEnabled(isFile)
 
         self.__deviceMenu.exec(self.deviceFileTreeWidget.mapToGlobal(pos))
 
@@ -1572,3 +1592,104 @@
         """
         for item in self.deviceFileTreeWidget.selectedItems()[:]:
             item.setSelected(False)
+
+    ############################################################################
+    ## Methods for the MicroPython window variant.
+    ############################################################################
+
+    @pyqtSlot()
+    def __newLocalFile(self, localDevice=False):
+        """
+        Private slot to create a new local file.
+
+        @param localDevice flag indicating device access via local file system
+        @type bool
+        """
+        cwdWidget = self.deviceCwd if localDevice else self.localCwd
+        fileTreeWidget = (
+            self.deviceFileTreeWidget if localDevice else self.localFileTreeWidget
+        )
+
+        if fileTreeWidget.selectedItems():
+            localItem = fileTreeWidget.selectedItems()[0]
+            defaultPath = localItem.data(0, Qt.ItemDataRole.UserRole)
+            if not os.path.isdir(defaultPath):
+                defaultPath = os.path.dirname(defaultPath)
+                localItem = localItem.parent()
+        else:
+            defaultPath = cwdWidget.text()
+            localItem = None
+
+        filePath, ok = QInputDialog.getText(
+            self,
+            self.tr("New File"),
+            self.tr("Enter file name:"),
+            QLineEdit.EchoMode.Normal,
+        )
+        if ok and filePath:
+            filePath = os.path.join(defaultPath, filePath)
+            try:
+                with open(filePath, "w") as f:
+                    f.close()
+                if localItem:
+                    self.__listLocalFiles(localDevice=localDevice, parentItem=localItem)
+                else:
+                    self.__listLocalFiles(
+                        dirname=cwdWidget.text(), localDevice=localDevice
+                    )
+            except OSError as exc:
+                EricMessageBox.critical(
+                    self,
+                    self.tr("New File"),
+                    self.tr(
+                        """<p>The file <b>{0}</b> could not be"""
+                        """ created.</p><p>Reason: {1}</p>"""
+                    ).format(filePath, str(exc)),
+                )
+
+    @pyqtSlot()
+    def __openLocalFile(self):
+        """
+        Private slot to open the selected local file in an editor window.
+        """
+        self.on_localFileTreeWidget_itemActivated(
+            self.localFileTreeWidget.selectedItems()[0], 0
+        )
+
+    @pyqtSlot()
+    def __newDeviceFile(self):
+        """
+        Private slot to create a new file on the connected device.
+        """
+        # TODO: not implemented yet
+        if self.__repl.deviceSupportsLocalFileAccess():
+            self.__newLocalFile(True)
+        else:
+            selectedItems = self.deviceFileTreeWidget.selectedItems()
+            if selectedItems:
+                item = selectedItems[0]
+                defaultPath = (
+                    item.data(0, Qt.ItemDataRole.UserRole)
+                    if item.text(0).endswith("/")
+                    else os.path.dirname(item.data(0, Qt.ItemDataRole.UserRole))
+                )
+            else:
+                defaultPath = self.deviceCwd.text()
+            fileName, ok = QInputDialog.getText(
+                self,
+                self.tr("New File"),
+                self.tr("Enter file name:"),
+                QLineEdit.EchoMode.Normal,
+                defaultPath,
+            )
+            if ok and fileName:
+                self.__fileManager.writeFile(fileName, "")
+
+    @pyqtSlot()
+    def __openDeviceFile(self):
+        """
+        Private slot to open the selected device file in an editor window.
+        """
+        self.on_deviceFileTreeWidget_itemActivated(
+            self.deviceFileTreeWidget.selectedItems()[0], 0
+        )
--- a/src/eric7/MicroPython/MicroPythonWidget.py	Wed Sep 04 09:15:41 2024 +0200
+++ b/src/eric7/MicroPython/MicroPythonWidget.py	Thu Sep 05 16:09:07 2024 +0200
@@ -97,7 +97,8 @@
         super().__init__(parent)
         self.setupUi(self)
 
-        self.layout().setContentsMargins(0, 3, 0, 0)
+        if not forMPyWindow:
+            self.layout().setContentsMargins(0, 3, 0, 0)
 
         self.__ui = parent
         self.__forMPyWindow = forMPyWindow
@@ -134,6 +135,14 @@
         self.chartButton.setIcon(EricPixmapCache.getIcon("chart"))
         self.connectButton.setIcon(EricPixmapCache.getIcon("linkConnect"))
 
+        for button in (
+            self.runButton,
+            self.replButton,
+            self.filesButton,
+            self.chartButton,
+        ):
+            button.setEnabled(False)
+
         self.__fileManager = None
         self.__fileManagerWidget = None
         self.__chartWidget = None
@@ -573,6 +582,8 @@
                 self.on_filesButton_clicked(False)
             if self.chartButton.isChecked():
                 self.on_chartButton_clicked(False)
+            if self.runButton.isChecked():
+                self.on_runButton_clicked(False)
         else:
             with EricOverrideCursor():
                 self.__connectToDevice(withAutostart=True)
@@ -915,7 +926,7 @@
                 if self.__connected:
                     self.__fileManager = MicroPythonFileManager(self.__device, self)
                     self.__fileManagerWidget = MicroPythonFileManagerWidget(
-                        self.__fileManager, self
+                        self.__fileManager, parent=self
                     )
 
                     self.__ui.addSideWidget(

eric ide

mercurial