eric6/MicroPython/MicroPythonReplWidget.py

branch
micropython
changeset 7108
4f6133a01c6a
parent 7103
aea236dc8002
child 7111
62191d1aeeed
diff -r aea236dc8002 -r 4f6133a01c6a eric6/MicroPython/MicroPythonReplWidget.py
--- a/eric6/MicroPython/MicroPythonReplWidget.py	Tue Jul 30 19:44:27 2019 +0200
+++ b/eric6/MicroPython/MicroPythonReplWidget.py	Wed Jul 31 20:41:39 2019 +0200
@@ -15,7 +15,7 @@
 from PyQt5.QtGui import QColor, QKeySequence, QTextCursor, QBrush
 from PyQt5.QtWidgets import (
     QWidget, QMenu, QApplication, QHBoxLayout, QSpacerItem, QSizePolicy,
-    QTextEdit
+    QTextEdit, QToolButton
 )
 
 from E5Gui.E5ZoomWidget import E5ZoomWidget
@@ -163,6 +163,20 @@
         
         self.__ui = parent
         
+        self.__superMenu = QMenu(self)
+        self.__superMenu.aboutToShow.connect(self.__aboutToShowSuperMenu)
+        
+        self.menuButton.setObjectName(
+            "micropython_supermenu_button")
+        self.menuButton.setIcon(UI.PixmapCache.getIcon("superMenu"))
+        self.menuButton.setToolTip(self.tr("pip Menu"))
+        self.menuButton.setPopupMode(QToolButton.InstantPopup)
+        self.menuButton.setToolButtonStyle(Qt.ToolButtonIconOnly)
+        self.menuButton.setFocusPolicy(Qt.NoFocus)
+        self.menuButton.setAutoRaise(True)
+        self.menuButton.setShowMenuInside(True)
+        self.menuButton.setMenu(self.__superMenu)
+        
         self.deviceIconLabel.setPixmap(MicroPythonDevices.getDeviceIcon(
             "", False))
         
@@ -345,9 +359,6 @@
         menu.addAction(self.tr("Copy"), self.replEdit.copy, copyKeys)
         menu.addAction(self.tr("Paste"), self.__paste, pasteKeys)
         menu.addSeparator()
-        if self.__device is not None:
-            # allow device interface to add specific context menu entries
-            self.__device.addActions(menu)
         menu.exec_(self.replEdit.mapToGlobal(pos))
     
     def setConnected(self, connected):
@@ -376,6 +387,15 @@
             self.connectButton.setToolTip(self.tr(
                 "Press to connect the selected device"))
     
+    def isConnected(self):
+        """
+        Public method to get the connection state.
+        
+        @return connection state
+        @rtype bool
+        """
+        return self.__connected
+    
     def __showNoDeviceMessage(self):
         """
         Private method to show a message dialog indicating a missing device.
@@ -748,9 +768,9 @@
             self.replEdit.zoomIn(value - self.__currentZoom)
         self.__currentZoom = value
     
-    def __getCurrentPort(self):
+    def getCurrentPort(self):
         """
-        Private method to determine the port path of the selected device.
+        Public method to determine the port path of the selected device.
         
         @return path of the port of the selected device
         @rtype str
@@ -770,7 +790,7 @@
         """
         Private method to connect to the selected device.
         """
-        port = self.__getCurrentPort()
+        port = self.getCurrentPort()
         if self.__interface.connectToDevice(port):
             self.setConnected(True)
         else:
@@ -999,3 +1019,149 @@
             
             self.__fileManagerWidget.deleteLater()
             self.__fileManagerWidget = None
+    
+    ##################################################################
+    ## Super Menu related methods below
+    ##################################################################
+    
+    def __aboutToShowSuperMenu(self):
+        """
+        Private slot to populate the Super Menu before showing it.
+        """
+        self.__superMenu.clear()
+        
+        act = self.__superMenu.addAction(
+            self.tr("Show Version"), self.__showDeviceVersion)
+        act.setEnabled(self.__connected)
+        act = self.__superMenu.addAction(
+            self.tr("Show Implementation"), self.__showImplementation)
+        act.setEnabled(self.__connected)
+        self.__superMenu.addSeparator()
+        act = self.__superMenu.addAction(
+            self.tr("Synchronize Time"), self.__synchronizeTime)
+        act.setEnabled(self.__connected)
+        act = self.__superMenu.addAction(
+            self.tr("Show Time"), self.__showDeviceTime)
+        act.setEnabled(self.__connected)
+        self.__superMenu.addSeparator()
+        if self.__device:
+            self.__device.addDeviceMenuEntries(self.__superMenu)
+    
+    @pyqtSlot()
+    def __showDeviceVersion(self):
+        """
+        Private slot to show some version info about MicroPython of the device.
+        """
+        try:
+            versionInfo = self.__interface.version()
+            if versionInfo:
+                msg = self.tr(
+                    "<h3>Device Version Information</h3>"
+                )
+                msg += "<table>"
+                for key, value in versionInfo.items():
+                    msg += "<tr><td><b>{0}</b></td><td>{1}</td></tr>".format(
+                        key.capitalize(), value)
+                msg += "</table>"
+            else:
+                msg = self.tr("No version information available.")
+            
+            E5MessageBox.information(
+                self,
+                self.tr("Device Version Information"),
+                msg)
+        except Exception as exc:
+            self.__showError("version()", str(exc))
+    
+    @pyqtSlot()
+    def __showImplementation(self):
+        """
+        Private slot to show some implementation related information.
+        """
+        try:
+            impInfo = self.__interface.getImplementation()
+            if impInfo["name"] == "micropython":
+                name = "MicroPython"
+            elif impInfo["name"] == "circuitpython":
+                name = "CircuitPython"
+            elif impInfo["name"] == "unknown":
+                name = self.tr("unknown")
+            else:
+                name = impInfo["name"]
+            if impInfo["version"] == "unknown":
+                version = self.tr("unknown")
+            else:
+                version = impInfo["version"]
+            
+            E5MessageBox.information(
+                self,
+                self.tr("Device Implementation Information"),
+                self.tr(
+                    "<h3>Device Implementation Information</h3>"
+                    "<p>This device contains <b>{0} {1}</b>.</p>"
+                ).format(name, version)
+            )
+        except Exception as exc:
+            self.__showError("getImplementation()", str(exc))
+    
+    @pyqtSlot()
+    def __synchronizeTime(self):
+        """
+        Private slot to set the time of the connected device to the local
+        computer's time.
+        """
+        try:
+            self.__interface.syncTime()
+            
+            E5MessageBox.information(
+                self,
+                self.tr("Synchronize Time"),
+                self.tr("The time of the connected device was synchronized"
+                        " with the local time."))
+        except Exception as exc:
+            self.__showError("syncTime()", str(exc))
+    
+    @pyqtSlot()
+    def __showDeviceTime(self):
+        """
+        Private slot to show the date and time of the connected device.
+        """
+        try:
+            dateTimeString = self.__interface.getTime()
+            try:
+                date, time = dateTimeString.strip().split(None, 1)
+                msg = self.tr(
+                    "<h3>Device Date and Time</h3>"
+                    "<table>"
+                    "<tr><td><b>Date</b></td><td>{0}</td></tr>"
+                    "<tr><td><b>Time</b></td><td>{1}</td></tr>"
+                    "</table>"
+                ).format(date, time)
+            except ValueError:
+                msg = self.tr(
+                    "<h3>Device Date and Time</h3>"
+                    "<p>{0}</p>"
+                ).format(dateTimeString.strip())
+            
+            E5MessageBox.information(
+                self,
+                self.tr("Device Date and Time"),
+                msg)
+        except Exception as exc:
+            self.__showError("getTime()", str(exc))
+    
+    def __showError(self, method, error):
+        """
+        Private method to show some error message.
+        
+        @param method name of the method the error occured in
+        @type str
+        @param error error message
+        @type str
+        """
+        E5MessageBox.warning(
+            self,
+            self.tr("Error handling device"),
+            self.tr("<p>There was an error communicating with the connected"
+                    " device.</p><p>Method: {0}</p><p>Message: {1}</p>")
+            .format(method, error))

eric ide

mercurial