eric6/MicroPython/MicroPythonReplWidget.py

branch
micropython
changeset 7108
4f6133a01c6a
parent 7103
aea236dc8002
child 7111
62191d1aeeed
equal deleted inserted replaced
7103:aea236dc8002 7108:4f6133a01c6a
13 13
14 from PyQt5.QtCore import pyqtSlot, pyqtSignal, Qt, QPoint, QEvent 14 from PyQt5.QtCore import pyqtSlot, pyqtSignal, Qt, QPoint, QEvent
15 from PyQt5.QtGui import QColor, QKeySequence, QTextCursor, QBrush 15 from PyQt5.QtGui import QColor, QKeySequence, QTextCursor, QBrush
16 from PyQt5.QtWidgets import ( 16 from PyQt5.QtWidgets import (
17 QWidget, QMenu, QApplication, QHBoxLayout, QSpacerItem, QSizePolicy, 17 QWidget, QMenu, QApplication, QHBoxLayout, QSpacerItem, QSizePolicy,
18 QTextEdit 18 QTextEdit, QToolButton
19 ) 19 )
20 20
21 from E5Gui.E5ZoomWidget import E5ZoomWidget 21 from E5Gui.E5ZoomWidget import E5ZoomWidget
22 from E5Gui import E5MessageBox, E5FileDialog 22 from E5Gui import E5MessageBox, E5FileDialog
23 from E5Gui.E5Application import e5App 23 from E5Gui.E5Application import e5App
161 super(MicroPythonReplWidget, self).__init__(parent) 161 super(MicroPythonReplWidget, self).__init__(parent)
162 self.setupUi(self) 162 self.setupUi(self)
163 163
164 self.__ui = parent 164 self.__ui = parent
165 165
166 self.__superMenu = QMenu(self)
167 self.__superMenu.aboutToShow.connect(self.__aboutToShowSuperMenu)
168
169 self.menuButton.setObjectName(
170 "micropython_supermenu_button")
171 self.menuButton.setIcon(UI.PixmapCache.getIcon("superMenu"))
172 self.menuButton.setToolTip(self.tr("pip Menu"))
173 self.menuButton.setPopupMode(QToolButton.InstantPopup)
174 self.menuButton.setToolButtonStyle(Qt.ToolButtonIconOnly)
175 self.menuButton.setFocusPolicy(Qt.NoFocus)
176 self.menuButton.setAutoRaise(True)
177 self.menuButton.setShowMenuInside(True)
178 self.menuButton.setMenu(self.__superMenu)
179
166 self.deviceIconLabel.setPixmap(MicroPythonDevices.getDeviceIcon( 180 self.deviceIconLabel.setPixmap(MicroPythonDevices.getDeviceIcon(
167 "", False)) 181 "", False))
168 182
169 self.openButton.setIcon(UI.PixmapCache.getIcon("open")) 183 self.openButton.setIcon(UI.PixmapCache.getIcon("open"))
170 self.saveButton.setIcon(UI.PixmapCache.getIcon("fileSaveAs")) 184 self.saveButton.setIcon(UI.PixmapCache.getIcon("fileSaveAs"))
343 menu.addAction(self.tr("Clear"), self.__clear) 357 menu.addAction(self.tr("Clear"), self.__clear)
344 menu.addSeparator() 358 menu.addSeparator()
345 menu.addAction(self.tr("Copy"), self.replEdit.copy, copyKeys) 359 menu.addAction(self.tr("Copy"), self.replEdit.copy, copyKeys)
346 menu.addAction(self.tr("Paste"), self.__paste, pasteKeys) 360 menu.addAction(self.tr("Paste"), self.__paste, pasteKeys)
347 menu.addSeparator() 361 menu.addSeparator()
348 if self.__device is not None:
349 # allow device interface to add specific context menu entries
350 self.__device.addActions(menu)
351 menu.exec_(self.replEdit.mapToGlobal(pos)) 362 menu.exec_(self.replEdit.mapToGlobal(pos))
352 363
353 def setConnected(self, connected): 364 def setConnected(self, connected):
354 """ 365 """
355 Public method to set the connection status LED. 366 Public method to set the connection status LED.
373 else: 384 else:
374 self.connectButton.setIcon( 385 self.connectButton.setIcon(
375 UI.PixmapCache.getIcon("linkConnect")) 386 UI.PixmapCache.getIcon("linkConnect"))
376 self.connectButton.setToolTip(self.tr( 387 self.connectButton.setToolTip(self.tr(
377 "Press to connect the selected device")) 388 "Press to connect the selected device"))
389
390 def isConnected(self):
391 """
392 Public method to get the connection state.
393
394 @return connection state
395 @rtype bool
396 """
397 return self.__connected
378 398
379 def __showNoDeviceMessage(self): 399 def __showNoDeviceMessage(self):
380 """ 400 """
381 Private method to show a message dialog indicating a missing device. 401 Private method to show a message dialog indicating a missing device.
382 """ 402 """
746 self.replEdit.zoomOut(self.__currentZoom - value) 766 self.replEdit.zoomOut(self.__currentZoom - value)
747 elif value > self.__currentZoom: 767 elif value > self.__currentZoom:
748 self.replEdit.zoomIn(value - self.__currentZoom) 768 self.replEdit.zoomIn(value - self.__currentZoom)
749 self.__currentZoom = value 769 self.__currentZoom = value
750 770
751 def __getCurrentPort(self): 771 def getCurrentPort(self):
752 """ 772 """
753 Private method to determine the port path of the selected device. 773 Public method to determine the port path of the selected device.
754 774
755 @return path of the port of the selected device 775 @return path of the port of the selected device
756 @rtype str 776 @rtype str
757 """ 777 """
758 portName = self.deviceTypeComboBox.itemData( 778 portName = self.deviceTypeComboBox.itemData(
768 788
769 def __connectToDevice(self): 789 def __connectToDevice(self):
770 """ 790 """
771 Private method to connect to the selected device. 791 Private method to connect to the selected device.
772 """ 792 """
773 port = self.__getCurrentPort() 793 port = self.getCurrentPort()
774 if self.__interface.connectToDevice(port): 794 if self.__interface.connectToDevice(port):
775 self.setConnected(True) 795 self.setConnected(True)
776 else: 796 else:
777 E5MessageBox.warning( 797 E5MessageBox.warning(
778 self, 798 self,
997 self.__device.setFileManager(False) 1017 self.__device.setFileManager(False)
998 self.__ui.removeSideWidget(self.__fileManagerWidget) 1018 self.__ui.removeSideWidget(self.__fileManagerWidget)
999 1019
1000 self.__fileManagerWidget.deleteLater() 1020 self.__fileManagerWidget.deleteLater()
1001 self.__fileManagerWidget = None 1021 self.__fileManagerWidget = None
1022
1023 ##################################################################
1024 ## Super Menu related methods below
1025 ##################################################################
1026
1027 def __aboutToShowSuperMenu(self):
1028 """
1029 Private slot to populate the Super Menu before showing it.
1030 """
1031 self.__superMenu.clear()
1032
1033 act = self.__superMenu.addAction(
1034 self.tr("Show Version"), self.__showDeviceVersion)
1035 act.setEnabled(self.__connected)
1036 act = self.__superMenu.addAction(
1037 self.tr("Show Implementation"), self.__showImplementation)
1038 act.setEnabled(self.__connected)
1039 self.__superMenu.addSeparator()
1040 act = self.__superMenu.addAction(
1041 self.tr("Synchronize Time"), self.__synchronizeTime)
1042 act.setEnabled(self.__connected)
1043 act = self.__superMenu.addAction(
1044 self.tr("Show Time"), self.__showDeviceTime)
1045 act.setEnabled(self.__connected)
1046 self.__superMenu.addSeparator()
1047 if self.__device:
1048 self.__device.addDeviceMenuEntries(self.__superMenu)
1049
1050 @pyqtSlot()
1051 def __showDeviceVersion(self):
1052 """
1053 Private slot to show some version info about MicroPython of the device.
1054 """
1055 try:
1056 versionInfo = self.__interface.version()
1057 if versionInfo:
1058 msg = self.tr(
1059 "<h3>Device Version Information</h3>"
1060 )
1061 msg += "<table>"
1062 for key, value in versionInfo.items():
1063 msg += "<tr><td><b>{0}</b></td><td>{1}</td></tr>".format(
1064 key.capitalize(), value)
1065 msg += "</table>"
1066 else:
1067 msg = self.tr("No version information available.")
1068
1069 E5MessageBox.information(
1070 self,
1071 self.tr("Device Version Information"),
1072 msg)
1073 except Exception as exc:
1074 self.__showError("version()", str(exc))
1075
1076 @pyqtSlot()
1077 def __showImplementation(self):
1078 """
1079 Private slot to show some implementation related information.
1080 """
1081 try:
1082 impInfo = self.__interface.getImplementation()
1083 if impInfo["name"] == "micropython":
1084 name = "MicroPython"
1085 elif impInfo["name"] == "circuitpython":
1086 name = "CircuitPython"
1087 elif impInfo["name"] == "unknown":
1088 name = self.tr("unknown")
1089 else:
1090 name = impInfo["name"]
1091 if impInfo["version"] == "unknown":
1092 version = self.tr("unknown")
1093 else:
1094 version = impInfo["version"]
1095
1096 E5MessageBox.information(
1097 self,
1098 self.tr("Device Implementation Information"),
1099 self.tr(
1100 "<h3>Device Implementation Information</h3>"
1101 "<p>This device contains <b>{0} {1}</b>.</p>"
1102 ).format(name, version)
1103 )
1104 except Exception as exc:
1105 self.__showError("getImplementation()", str(exc))
1106
1107 @pyqtSlot()
1108 def __synchronizeTime(self):
1109 """
1110 Private slot to set the time of the connected device to the local
1111 computer's time.
1112 """
1113 try:
1114 self.__interface.syncTime()
1115
1116 E5MessageBox.information(
1117 self,
1118 self.tr("Synchronize Time"),
1119 self.tr("The time of the connected device was synchronized"
1120 " with the local time."))
1121 except Exception as exc:
1122 self.__showError("syncTime()", str(exc))
1123
1124 @pyqtSlot()
1125 def __showDeviceTime(self):
1126 """
1127 Private slot to show the date and time of the connected device.
1128 """
1129 try:
1130 dateTimeString = self.__interface.getTime()
1131 try:
1132 date, time = dateTimeString.strip().split(None, 1)
1133 msg = self.tr(
1134 "<h3>Device Date and Time</h3>"
1135 "<table>"
1136 "<tr><td><b>Date</b></td><td>{0}</td></tr>"
1137 "<tr><td><b>Time</b></td><td>{1}</td></tr>"
1138 "</table>"
1139 ).format(date, time)
1140 except ValueError:
1141 msg = self.tr(
1142 "<h3>Device Date and Time</h3>"
1143 "<p>{0}</p>"
1144 ).format(dateTimeString.strip())
1145
1146 E5MessageBox.information(
1147 self,
1148 self.tr("Device Date and Time"),
1149 msg)
1150 except Exception as exc:
1151 self.__showError("getTime()", str(exc))
1152
1153 def __showError(self, method, error):
1154 """
1155 Private method to show some error message.
1156
1157 @param method name of the method the error occured in
1158 @type str
1159 @param error error message
1160 @type str
1161 """
1162 E5MessageBox.warning(
1163 self,
1164 self.tr("Error handling device"),
1165 self.tr("<p>There was an error communicating with the connected"
1166 " device.</p><p>Method: {0}</p><p>Message: {1}</p>")
1167 .format(method, error))

eric ide

mercurial