MicroPythonReplWidget: streamlined the code a little bit and changed the 'disconnect' button to a 'connect/disconnect' button. micropython

Tue, 30 Jul 2019 19:44:27 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Tue, 30 Jul 2019 19:44:27 +0200
branch
micropython
changeset 7103
aea236dc8002
parent 7102
5e77aa4671e6
child 7108
4f6133a01c6a

MicroPythonReplWidget: streamlined the code a little bit and changed the 'disconnect' button to a 'connect/disconnect' button.

eric6/MicroPython/MicroPythonReplWidget.py file | annotate | diff | comparison | revisions
eric6/MicroPython/MicroPythonReplWidget.ui file | annotate | diff | comparison | revisions
--- a/eric6/MicroPython/MicroPythonReplWidget.py	Tue Jul 30 19:43:18 2019 +0200
+++ b/eric6/MicroPython/MicroPythonReplWidget.py	Tue Jul 30 19:44:27 2019 +0200
@@ -14,7 +14,8 @@
 from PyQt5.QtCore import pyqtSlot, pyqtSignal, Qt, QPoint, QEvent
 from PyQt5.QtGui import QColor, QKeySequence, QTextCursor, QBrush
 from PyQt5.QtWidgets import (
-    QWidget, QMenu, QApplication, QHBoxLayout, QSpacerItem, QSizePolicy
+    QWidget, QMenu, QApplication, QHBoxLayout, QSpacerItem, QSizePolicy,
+    QTextEdit
 )
 
 from E5Gui.E5ZoomWidget import E5ZoomWidget
@@ -135,8 +136,6 @@
 }
 
 
-# TODO: make wrapping an option for the repl edit
-# TODO: add a connect button or make the disconnect button with changing icon (see IRC)
 class MicroPythonReplWidget(QWidget, Ui_MicroPythonReplWidget):
     """
     Class implementing the MicroPython REPL widget.
@@ -175,7 +174,7 @@
         self.replButton.setIcon(UI.PixmapCache.getIcon("terminal"))
         self.filesButton.setIcon(UI.PixmapCache.getIcon("filemanager"))
         self.chartButton.setIcon(UI.PixmapCache.getIcon("chart"))
-        self.disconnectButton.setIcon(UI.PixmapCache.getIcon("disconnect"))
+        self.connectButton.setIcon(UI.PixmapCache.getIcon("linkConnect"))
         
         self.__zoomLayout = QHBoxLayout()
         spacerItem = QSpacerItem(40, 20, QSizePolicy.Expanding,
@@ -203,11 +202,6 @@
         self.__connected = False
         self.setConnected(False)
         
-        # TODO: replace these by checking the button states
-        self.__replRunning = False
-        self.__plotterRunning = False
-        self.__fileManagerRunning = False
-        
         if not HAS_QTSERIALPORT:
             self.replEdit.setHtml(self.tr(
                 "<h3>The QtSerialPort package is not available.<br/>"
@@ -270,9 +264,13 @@
         self.__colorScheme = Preferences.getMicroPython("ColorScheme")
         
         self.__font = Preferences.getEditorOtherFonts("MonospacedFont")
-        
         self.replEdit.setFontFamily(self.__font.family())
         self.replEdit.setFontPointSize(self.__font.pointSize())
+        
+        if Preferences.getMicroPython("ReplLineWrap"):
+            self.replEdit.setLineWrapMode(QTextEdit.WidgetWidth)
+        else:
+            self.replEdit.setLineWrapMode(QTextEdit.NoWrap)
     
     def commandsInterface(self):
         """
@@ -367,7 +365,16 @@
         
         self.deviceTypeComboBox.setEnabled(not connected)
         
-        self.disconnectButton.setEnabled(connected)
+        if connected:
+            self.connectButton.setIcon(
+                UI.PixmapCache.getIcon("linkDisconnect"))
+            self.connectButton.setToolTip(self.tr(
+                "Press to disconnect the current device"))
+        else:
+            self.connectButton.setIcon(
+                UI.PixmapCache.getIcon("linkConnect"))
+            self.connectButton.setToolTip(self.tr(
+                "Press to connect the selected device"))
     
     def __showNoDeviceMessage(self):
         """
@@ -386,8 +393,9 @@
     @pyqtSlot(bool)
     def on_replButton_clicked(self, checked):
         """
-        Private slot to connect to enable or disable the REPL widget connecting
-        or disconnecting from the device.
+        Private slot to connect to enable or disable the REPL widget.
+       
+        If the selected device is not connected yet, this will be done now.
         
         @param checked state of the button
         @type bool
@@ -417,23 +425,26 @@
                     # send Ctrl-C (keyboard interrupt)
                     self.__interface.write(b'\x03')
             
-            self.__replRunning = True
             self.__device.setRepl(True)
             self.replEdit.setFocus(Qt.OtherFocusReason)
         else:
             self.__interface.dataReceived.disconnect(self.__processData)
-            if not self.__plotterRunning and not self.__fileManagerRunning:
+            if (not self.chartButton.isChecked() and
+                    not self.filesButton.isChecked()):
                 self.__disconnectFromDevice()
-            self.__replRunning = False
             self.__device.setRepl(False)
         self.replButton.setChecked(checked)
     
     @pyqtSlot()
-    def on_disconnectButton_clicked(self):
+    def on_connectButton_clicked(self):
+        """
+        Private slot to connect to the selected device or disconnect from the
+        currently connected device.
         """
-        Private slot to disconnect from the currently connected device.
-        """
-        self.__disconnectFromDevice()
+        if self.__connected:
+            self.__disconnectFromDevice()
+        else:
+            self.__connectToDevice()
     
     @pyqtSlot()
     def __clear(self):
@@ -781,6 +792,9 @@
         """
         Private slot to execute the script of the active editor on the
         selected device.
+        
+        If the REPL is not active yet, it will be activated, which might cause
+        an unconnected device to be connected.
         """
         if not self.__device:
             self.__showNoDeviceMessage()
@@ -812,10 +826,10 @@
                         """ {0}</p>""").format(reason))
             return
         
-        if not self.__replRunning:
-            # switch on the REPL
+        if not self.replButton.isChecked():
+            # activate on the REPL
             self.on_replButton_clicked(True)
-        if self.__replRunning:
+        if self.replButton.isChecked():
             self.__device.runScript(script)
     
     @pyqtSlot()
@@ -855,6 +869,8 @@
         """
         Private slot to open a chart view to plot data received from the
         connected device.
+       
+        If the selected device is not connected yet, this will be done now.
         
         @param checked state of the button
         @type bool
@@ -896,7 +912,6 @@
                     # send Ctrl-C (keyboard interrupt)
                     self.__interface.write(b'\x03')
             
-            self.__plotterRunning = True
             self.__device.setPlotter(True)
         else:
             if self.__chartWidget.isDirty():
@@ -914,10 +929,10 @@
             self.__chartWidget.dataFlood.disconnect(
                 self.handleDataFlood)
             
-            if not self.__replRunning and not self.__fileManagerRunning:
+            if (not self.replButton.isChecked() and
+                    not self.filesButton.isChecked()):
                 self.__disconnectFromDevice()
             
-            self.__plotterRunning = False
             self.__device.setPlotter(False)
             self.__ui.removeSideWidget(self.__chartWidget)
             
@@ -931,13 +946,15 @@
         """
         Public slot handling a data flood from the device.
         """
-        self.on_disconnectButton_clicked()
+        self.on_connectButton_clicked()
         self.__device.handleDataFlood()
     
     @pyqtSlot(bool)
     def on_filesButton_clicked(self, checked):
         """
         Private slot to open a file manager window to the connected device.
+       
+        If the selected device is not connected yet, this will be done now.
         
         @param checked state of the button
         @type bool
@@ -968,14 +985,17 @@
             self.__ui.showSideWidget(self.__fileManagerWidget)
 
             self.__device.setFileManager(True)
-            self.__fileManagerRunning = True
             
             self.__fileManagerWidget.start()
         else:
             self.__fileManagerWidget.stop()
-            self.__ui.removeSideWidget(self.__fileManagerWidget)
+            
+            if (not self.replButton.isChecked() and
+                    not self.chartButton.isChecked()):
+                self.__disconnectFromDevice()
             
             self.__device.setFileManager(False)
-            self.__fileManagerRunning = False
+            self.__ui.removeSideWidget(self.__fileManagerWidget)
+            
             self.__fileManagerWidget.deleteLater()
             self.__fileManagerWidget = None
--- a/eric6/MicroPython/MicroPythonReplWidget.ui	Tue Jul 30 19:43:18 2019 +0200
+++ b/eric6/MicroPython/MicroPythonReplWidget.ui	Tue Jul 30 19:44:27 2019 +0200
@@ -145,9 +145,9 @@
       </spacer>
      </item>
      <item>
-      <widget class="QToolButton" name="disconnectButton">
+      <widget class="QToolButton" name="connectButton">
        <property name="toolTip">
-        <string>Press to disconnect the selected device</string>
+        <string>Press to connect the selected device</string>
        </property>
       </widget>
      </item>
@@ -191,7 +191,7 @@
   <tabstop>replButton</tabstop>
   <tabstop>filesButton</tabstop>
   <tabstop>chartButton</tabstop>
-  <tabstop>disconnectButton</tabstop>
+  <tabstop>connectButton</tabstop>
   <tabstop>replEdit</tabstop>
  </tabstops>
  <resources/>

eric ide

mercurial