eric6/MicroPython/MicroPythonWidget.py

changeset 8143
2c730d5fd177
parent 8139
418c2d9a767d
child 8218
7c09585bd960
equal deleted inserted replaced
8141:27f636beebad 8143:2c730d5fd177
187 connection for further processing 187 connection for further processing
188 """ 188 """
189 ZoomMin = -10 189 ZoomMin = -10
190 ZoomMax = 20 190 ZoomMax = 20
191 191
192 DeviceTypeRole = Qt.UserRole 192 DeviceTypeRole = Qt.ItemDataRole.UserRole
193 DeviceBoardRole = Qt.UserRole + 1 193 DeviceBoardRole = Qt.ItemDataRole.UserRole + 1
194 DevicePortRole = Qt.UserRole + 2 194 DevicePortRole = Qt.ItemDataRole.UserRole + 2
195 DeviceVidRole = Qt.UserRole + 3 195 DeviceVidRole = Qt.ItemDataRole.UserRole + 3
196 DevicePidRole = Qt.UserRole + 4 196 DevicePidRole = Qt.ItemDataRole.UserRole + 4
197 197
198 dataReceived = pyqtSignal(bytes) 198 dataReceived = pyqtSignal(bytes)
199 199
200 ManualMarker = "<manual>" 200 ManualMarker = "<manual>"
201 201
216 216
217 self.menuButton.setObjectName( 217 self.menuButton.setObjectName(
218 "micropython_supermenu_button") 218 "micropython_supermenu_button")
219 self.menuButton.setIcon(UI.PixmapCache.getIcon("superMenu")) 219 self.menuButton.setIcon(UI.PixmapCache.getIcon("superMenu"))
220 self.menuButton.setToolTip(self.tr("MicroPython Menu")) 220 self.menuButton.setToolTip(self.tr("MicroPython Menu"))
221 self.menuButton.setPopupMode(QToolButton.InstantPopup) 221 self.menuButton.setPopupMode(
222 self.menuButton.setToolButtonStyle(Qt.ToolButtonIconOnly) 222 QToolButton.ToolButtonPopupMode.InstantPopup)
223 self.menuButton.setFocusPolicy(Qt.NoFocus) 223 self.menuButton.setToolButtonStyle(
224 Qt.ToolButtonStyle.ToolButtonIconOnly)
225 self.menuButton.setFocusPolicy(Qt.FocusPolicy.NoFocus)
224 self.menuButton.setAutoRaise(True) 226 self.menuButton.setAutoRaise(True)
225 self.menuButton.setShowMenuInside(True) 227 self.menuButton.setShowMenuInside(True)
226 self.menuButton.setMenu(self.__superMenu) 228 self.menuButton.setMenu(self.__superMenu)
227 229
228 self.deviceIconLabel.setPixmap(MicroPythonDevices.getDeviceIcon( 230 self.deviceIconLabel.setPixmap(MicroPythonDevices.getDeviceIcon(
237 self.filesButton.setIcon(UI.PixmapCache.getIcon("filemanager")) 239 self.filesButton.setIcon(UI.PixmapCache.getIcon("filemanager"))
238 self.chartButton.setIcon(UI.PixmapCache.getIcon("chart")) 240 self.chartButton.setIcon(UI.PixmapCache.getIcon("chart"))
239 self.connectButton.setIcon(UI.PixmapCache.getIcon("linkConnect")) 241 self.connectButton.setIcon(UI.PixmapCache.getIcon("linkConnect"))
240 242
241 self.__zoomLayout = QHBoxLayout() 243 self.__zoomLayout = QHBoxLayout()
242 spacerItem = QSpacerItem(40, 20, QSizePolicy.Expanding, 244 spacerItem = QSpacerItem(40, 20, QSizePolicy.Policy.Expanding,
243 QSizePolicy.Minimum) 245 QSizePolicy.Policy.Minimum)
244 self.__zoomLayout.addSpacerItem(spacerItem) 246 self.__zoomLayout.addSpacerItem(spacerItem)
245 247
246 self.__zoom0 = self.replEdit.fontPointSize() 248 self.__zoom0 = self.replEdit.fontPointSize()
247 self.__zoomWidget = E5ZoomWidget( 249 self.__zoomWidget = E5ZoomWidget(
248 UI.PixmapCache.getPixmap("zoomOut"), 250 UI.PixmapCache.getPixmap("zoomOut"),
355 unknownMessage = "" 357 unknownMessage = ""
356 358
357 self.deviceInfoLabel.setText(supportedMessage + unknownMessage) 359 self.deviceInfoLabel.setText(supportedMessage + unknownMessage)
358 360
359 index = self.deviceTypeComboBox.findText(currentDevice, 361 index = self.deviceTypeComboBox.findText(currentDevice,
360 Qt.MatchExactly) 362 Qt.MatchFlag.MatchExactly)
361 if index == -1: 363 if index == -1:
362 # entry is no longer present 364 # entry is no longer present
363 index = 0 365 index = 0
364 if self.__connected: 366 if self.__connected:
365 # we are still connected, so disconnect 367 # we are still connected, so disconnect
425 self.__font = Preferences.getEditorOtherFonts("MonospacedFont") 427 self.__font = Preferences.getEditorOtherFonts("MonospacedFont")
426 self.replEdit.setFontFamily(self.__font.family()) 428 self.replEdit.setFontFamily(self.__font.family())
427 self.replEdit.setFontPointSize(self.__font.pointSize()) 429 self.replEdit.setFontPointSize(self.__font.pointSize())
428 430
429 if Preferences.getMicroPython("ReplLineWrap"): 431 if Preferences.getMicroPython("ReplLineWrap"):
430 self.replEdit.setLineWrapMode(QTextEdit.WidgetWidth) 432 self.replEdit.setLineWrapMode(QTextEdit.LineWrapMode.WidgetWidth)
431 else: 433 else:
432 self.replEdit.setLineWrapMode(QTextEdit.NoWrap) 434 self.replEdit.setLineWrapMode(QTextEdit.LineWrapMode.NoWrap)
433 435
434 if self.__chartWidget is not None: 436 if self.__chartWidget is not None:
435 self.__chartWidget.preferencesChanged() 437 self.__chartWidget.preferencesChanged()
436 438
437 def commandsInterface(self): 439 def commandsInterface(self):
521 523
522 @param pos position to show the menu at 524 @param pos position to show the menu at
523 @type QPoint 525 @type QPoint
524 """ 526 """
525 if Globals.isMacPlatform(): 527 if Globals.isMacPlatform():
526 copyKeys = QKeySequence(Qt.CTRL + Qt.Key_C) 528 copyKeys = QKeySequence(Qt.Modifier.CTRL + Qt.Key.Key_C)
527 pasteKeys = QKeySequence(Qt.CTRL + Qt.Key_V) 529 pasteKeys = QKeySequence(Qt.Modifier.CTRL + Qt.Key.Key_V)
528 else: 530 else:
529 copyKeys = QKeySequence(Qt.CTRL + Qt.SHIFT + Qt.Key_C) 531 copyKeys = QKeySequence(
530 pasteKeys = QKeySequence(Qt.CTRL + Qt.SHIFT + Qt.Key_V) 532 Qt.Modifier.CTRL + Qt.Modifier.SHIFT + Qt.Key.Key_C)
533 pasteKeys = QKeySequence(
534 Qt.Modifier.CTRL + Qt.Modifier.SHIFT + Qt.Key.Key_V)
531 menu = QMenu(self) 535 menu = QMenu(self)
532 menu.addAction(self.tr("Clear"), self.__clear) 536 menu.addAction(self.tr("Clear"), self.__clear)
533 menu.addSeparator() 537 menu.addSeparator()
534 menu.addAction(self.tr("Copy"), self.replEdit.copy, copyKeys) 538 menu.addAction(self.tr("Copy"), self.replEdit.copy, copyKeys)
535 menu.addAction(self.tr("Paste"), self.__paste, pasteKeys) 539 menu.addAction(self.tr("Paste"), self.__paste, pasteKeys)
619 self.__interface.write(b'\x02') 623 self.__interface.write(b'\x02')
620 # send Ctrl-C (keyboard interrupt) 624 # send Ctrl-C (keyboard interrupt)
621 self.__interface.write(b'\x03') 625 self.__interface.write(b'\x03')
622 626
623 self.__device.setRepl(True) 627 self.__device.setRepl(True)
624 self.replEdit.setFocus(Qt.OtherFocusReason) 628 self.replEdit.setFocus(Qt.FocusReason.OtherFocusReason)
625 else: 629 else:
626 self.__interface.dataReceived.disconnect(self.__processData) 630 self.__interface.dataReceived.disconnect(self.__processData)
627 if (not self.chartButton.isChecked() and 631 if (not self.chartButton.isChecked() and
628 not self.filesButton.isChecked()): 632 not self.filesButton.isChecked()):
629 self.__disconnectFromDevice() 633 self.__disconnectFromDevice()
685 @param evt reference to the event object 689 @param evt reference to the event object
686 @type QEvent 690 @type QEvent
687 @return flag to indicate that the event was handled 691 @return flag to indicate that the event was handled
688 @rtype bool 692 @rtype bool
689 """ 693 """
690 if obj is self.replEdit and evt.type() == QEvent.KeyPress: 694 if obj is self.replEdit and evt.type() == QEvent.Type.KeyPress:
691 # handle the key press event on behalve of the REPL pane 695 # handle the key press event on behalve of the REPL pane
692 key = evt.key() 696 key = evt.key()
693 msg = bytes(evt.text(), 'utf8') 697 msg = bytes(evt.text(), 'utf8')
694 if key == Qt.Key_Backspace: 698 if key == Qt.Key.Key_Backspace:
695 msg = b'\b' 699 msg = b'\b'
696 elif key == Qt.Key_Delete: 700 elif key == Qt.Key.Key_Delete:
697 msg = b'\x1B[\x33\x7E' 701 msg = b'\x1B[\x33\x7E'
698 elif key == Qt.Key_Up: 702 elif key == Qt.Key.Key_Up:
699 msg = b'\x1B[A' 703 msg = b'\x1B[A'
700 elif key == Qt.Key_Down: 704 elif key == Qt.Key.Key_Down:
701 msg = b'\x1B[B' 705 msg = b'\x1B[B'
702 elif key == Qt.Key_Right: 706 elif key == Qt.Key.Key_Right:
703 msg = b'\x1B[C' 707 msg = b'\x1B[C'
704 elif key == Qt.Key_Left: 708 elif key == Qt.Key.Key_Left:
705 msg = b'\x1B[D' 709 msg = b'\x1B[D'
706 elif key == Qt.Key_Home: 710 elif key == Qt.Key.Key_Home:
707 msg = b'\x1B[H' 711 msg = b'\x1B[H'
708 elif key == Qt.Key_End: 712 elif key == Qt.Key.Key_End:
709 msg = b'\x1B[F' 713 msg = b'\x1B[F'
710 elif ((Globals.isMacPlatform() and 714 elif ((Globals.isMacPlatform() and
711 evt.modifiers() == Qt.MetaModifier) or 715 evt.modifiers() == Qt.KeyboardModifier.MetaModifier) or
712 (not Globals.isMacPlatform() and 716 (not Globals.isMacPlatform() and
713 evt.modifiers() == Qt.ControlModifier)): 717 evt.modifiers() == Qt.KeyboardModifier.ControlModifier)):
714 if Qt.Key_A <= key <= Qt.Key_Z: 718 if Qt.Key.Key_A <= key <= Qt.Key.Key_Z:
715 # devices treat an input of \x01 as Ctrl+A, etc. 719 # devices treat an input of \x01 as Ctrl+A, etc.
716 msg = bytes([1 + key - Qt.Key_A]) 720 msg = bytes([1 + key - Qt.Key.Key_A])
717 elif ( 721 elif (
718 evt.modifiers() == (Qt.ControlModifier | Qt.ShiftModifier) or 722 evt.modifiers() == (
723 Qt.KeyboardModifier.ControlModifier |
724 Qt.KeyboardModifier.ShiftModifier) or
719 (Globals.isMacPlatform() and 725 (Globals.isMacPlatform() and
720 evt.modifiers() == Qt.ControlModifier) 726 evt.modifiers() == Qt.KeyboardModifier.ControlModifier)
721 ): 727 ):
722 if key == Qt.Key_C: 728 if key == Qt.Key.Key_C:
723 self.replEdit.copy() 729 self.replEdit.copy()
724 msg = b'' 730 msg = b''
725 elif key == Qt.Key_V: 731 elif key == Qt.Key.Key_V:
726 self.__paste() 732 self.__paste()
727 msg = b'' 733 msg = b''
728 elif key in (Qt.Key_Return, Qt.Key_Enter): 734 elif key in (Qt.Key.Key_Return, Qt.Key.Key_Enter):
729 tc = self.replEdit.textCursor() 735 tc = self.replEdit.textCursor()
730 tc.movePosition(QTextCursor.EndOfLine) 736 tc.movePosition(QTextCursor.MoveOperation.EndOfLine)
731 self.replEdit.setTextCursor(tc) 737 self.replEdit.setTextCursor(tc)
732 self.__interface.isConnected() and self.__interface.write(msg) 738 self.__interface.isConnected() and self.__interface.write(msg)
733 return True 739 return True
734 else: 740 else:
735 # standard event processing 741 # standard event processing
738 def __replEditMouseReleaseEvent(self, evt): 744 def __replEditMouseReleaseEvent(self, evt):
739 """ 745 """
740 Private method handling mouse release events for the replEdit widget. 746 Private method handling mouse release events for the replEdit widget.
741 747
742 Note: this is a hack because QTextEdit does not allow filtering of 748 Note: this is a hack because QTextEdit does not allow filtering of
743 QEvent.MouseButtonRelease. To make middle button paste work, we had 749 QEvent.Type.MouseButtonRelease. To make middle button paste work, we
744 to intercept the protected event method (some kind of reimplementing 750 had to intercept the protected event method (some kind of
745 it). 751 reimplementing it).
746 752
747 @param evt reference to the event object 753 @param evt reference to the event object
748 @type QMouseEvent 754 @type QMouseEvent
749 """ 755 """
750 if evt.button() == Qt.MiddleButton: 756 if evt.button() == Qt.MouseButton.MiddleButton:
751 self.__paste(mode=QClipboard.Mode.Selection) 757 self.__paste(mode=QClipboard.Mode.Selection)
752 msg = b'' 758 msg = b''
753 if self.__interface.isConnected(): 759 if self.__interface.isConnected():
754 self.__interface.write(msg) 760 self.__interface.write(msg)
755 evt.accept() 761 evt.accept()
763 @param data bytes received from the device 769 @param data bytes received from the device
764 @type bytes 770 @type bytes
765 """ 771 """
766 tc = self.replEdit.textCursor() 772 tc = self.replEdit.textCursor()
767 # the text cursor must be on the last line 773 # the text cursor must be on the last line
768 while tc.movePosition(QTextCursor.Down): 774 while tc.movePosition(QTextCursor.MoveOperation.Down):
769 pass 775 pass
770 776
771 # set the font 777 # set the font
772 charFormat = tc.charFormat() 778 charFormat = tc.charFormat()
773 charFormat.setFontFamily(self.__font.family()) 779 charFormat.setFontFamily(self.__font.family())
775 tc.setCharFormat(charFormat) 781 tc.setCharFormat(charFormat)
776 782
777 index = 0 783 index = 0
778 while index < len(data): 784 while index < len(data):
779 if data[index] == 8: # \b 785 if data[index] == 8: # \b
780 tc.movePosition(QTextCursor.Left) 786 tc.movePosition(QTextCursor.MoveOperation.Left)
781 self.replEdit.setTextCursor(tc) 787 self.replEdit.setTextCursor(tc)
782 elif data[index] in (4, 13): # EOT, \r 788 elif data[index] in (4, 13): # EOT, \r
783 pass 789 pass
784 elif (len(data) > index + 1 and 790 elif (len(data) > index + 1 and
785 data[index] == 27 and 791 data[index] == 27 and
798 count = 1 804 count = 1
799 else: 805 else:
800 count = int(match.group("count")) 806 count = int(match.group("count"))
801 807
802 if action == "A": # up 808 if action == "A": # up
803 tc.movePosition(QTextCursor.Up, n=count) 809 tc.movePosition(QTextCursor.MoveOperation.Up,
810 n=count)
804 self.replEdit.setTextCursor(tc) 811 self.replEdit.setTextCursor(tc)
805 elif action == "B": # down 812 elif action == "B": # down
806 tc.movePosition(QTextCursor.Down, n=count) 813 tc.movePosition(QTextCursor.MoveOperation.Down,
814 n=count)
807 self.replEdit.setTextCursor(tc) 815 self.replEdit.setTextCursor(tc)
808 elif action == "C": # right 816 elif action == "C": # right
809 tc.movePosition(QTextCursor.Right, n=count) 817 tc.movePosition(QTextCursor.MoveOperation.Right,
818 n=count)
810 self.replEdit.setTextCursor(tc) 819 self.replEdit.setTextCursor(tc)
811 elif action == "D": # left 820 elif action == "D": # left
812 tc.movePosition(QTextCursor.Left, n=count) 821 tc.movePosition(QTextCursor.MoveOperation.Left,
822 n=count)
813 self.replEdit.setTextCursor(tc) 823 self.replEdit.setTextCursor(tc)
814 elif action == "K": # delete things 824 elif action == "K": # delete things
815 if match.group("count") in ("", "0"): 825 if match.group("count") in ("", "0"):
816 # delete to end of line 826 # delete to end of line
817 tc.movePosition(QTextCursor.EndOfLine, 827 tc.movePosition(
818 mode=QTextCursor.KeepAnchor) 828 QTextCursor.MoveOperation.EndOfLine,
829 mode=QTextCursor.MoveMode.KeepAnchor)
819 tc.removeSelectedText() 830 tc.removeSelectedText()
820 self.replEdit.setTextCursor(tc) 831 self.replEdit.setTextCursor(tc)
821 elif match.group("count") == "1": 832 elif match.group("count") == "1":
822 # delete to beinning of line 833 # delete to beinning of line
823 tc.movePosition(QTextCursor.StartOfLine, 834 tc.movePosition(
824 mode=QTextCursor.KeepAnchor) 835 QTextCursor.MoveOperation.StartOfLine,
836 mode=QTextCursor.MoveMode.KeepAnchor)
825 tc.removeSelectedText() 837 tc.removeSelectedText()
826 self.replEdit.setTextCursor(tc) 838 self.replEdit.setTextCursor(tc)
827 elif match.group("count") == "2": 839 elif match.group("count") == "2":
828 # delete whole line 840 # delete whole line
829 tc.movePosition(QTextCursor.EndOfLine) 841 tc.movePosition(
830 tc.movePosition(QTextCursor.StartOfLine, 842 QTextCursor.MoveOperation.EndOfLine)
831 mode=QTextCursor.KeepAnchor) 843 tc.movePosition(
844 QTextCursor.MoveOperation.StartOfLine,
845 mode=QTextCursor.MoveMode.KeepAnchor)
832 tc.removeSelectedText() 846 tc.removeSelectedText()
833 self.replEdit.setTextCursor(tc) 847 self.replEdit.setTextCursor(tc)
834 elif action == "m": 848 elif action == "m":
835 self.__setCharFormat(match.group(0)[:-1].split(";"), 849 self.__setCharFormat(match.group(0)[:-1].split(";"),
836 tc) 850 tc)
1027 from .ConnectionSelectionDialog import ConnectionSelectionDialog 1041 from .ConnectionSelectionDialog import ConnectionSelectionDialog
1028 with E5OverridenCursor(): 1042 with E5OverridenCursor():
1029 dlg = ConnectionSelectionDialog( 1043 dlg = ConnectionSelectionDialog(
1030 self.__unknownPorts, self.__lastPort, self.__lastDeviceType 1044 self.__unknownPorts, self.__lastPort, self.__lastDeviceType
1031 ) 1045 )
1032 if dlg.exec() == QDialog.Accepted: 1046 if dlg.exec() == QDialog.DialogCode.Accepted:
1033 vid, pid, port, deviceType = dlg.getData() 1047 vid, pid, port, deviceType = dlg.getData()
1034 1048
1035 self.deviceIconLabel.setPixmap( 1049 self.deviceIconLabel.setPixmap(
1036 MicroPythonDevices.getDeviceIcon(deviceType, False)) 1050 MicroPythonDevices.getDeviceIcon(deviceType, False))
1037 self.__device = MicroPythonDevices.getDevice( 1051 self.__device = MicroPythonDevices.getDevice(
1740 from .IgnoredDevicesDialog import IgnoredDevicesDialog 1754 from .IgnoredDevicesDialog import IgnoredDevicesDialog
1741 1755
1742 dlg = IgnoredDevicesDialog( 1756 dlg = IgnoredDevicesDialog(
1743 Preferences.getMicroPython("IgnoredUnknownDevices"), 1757 Preferences.getMicroPython("IgnoredUnknownDevices"),
1744 self) 1758 self)
1745 if dlg.exec() == QDialog.Accepted: 1759 if dlg.exec() == QDialog.DialogCode.Accepted:
1746 ignoredDevices = dlg.getDevices() 1760 ignoredDevices = dlg.getDevices()
1747 Preferences.setMicroPython("IgnoredUnknownDevices", 1761 Preferences.setMicroPython("IgnoredUnknownDevices",
1748 ignoredDevices) 1762 ignoredDevices)
1749 1763
1750 @pyqtSlot() 1764 @pyqtSlot()
1779 [d[2] for d in devices], 1793 [d[2] for d in devices],
1780 title=self.tr("Add Unknown Devices"), 1794 title=self.tr("Add Unknown Devices"),
1781 message=self.tr("Select the devices to be added:"), 1795 message=self.tr("Select the devices to be added:"),
1782 checkBoxSelection=True 1796 checkBoxSelection=True
1783 ) 1797 )
1784 if sdlg.exec() == QDialog.Accepted: 1798 if sdlg.exec() == QDialog.DialogCode.Accepted:
1785 selectedDevices = sdlg.getSelection() 1799 selectedDevices = sdlg.getSelection()
1786 else: 1800 else:
1787 selectedDevices = devices[0][2] 1801 selectedDevices = devices[0][2]
1788 1802
1789 if selectedDevices: 1803 if selectedDevices:
1790 manualDevices = Preferences.getMicroPython("ManualDevices") 1804 manualDevices = Preferences.getMicroPython("ManualDevices")
1791 for vid, pid, description in devices: 1805 for vid, pid, description in devices:
1792 if description in selectedDevices: 1806 if description in selectedDevices:
1793 dlg = AddEditDevicesDialog(vid, pid, description) 1807 dlg = AddEditDevicesDialog(vid, pid, description)
1794 if dlg.exec() == QDialog.Accepted: 1808 if dlg.exec() == QDialog.DialogCode.Accepted:
1795 manualDevices.append(dlg.getDeviceDict()) 1809 manualDevices.append(dlg.getDeviceDict())
1796 Preferences.setMicroPython("ManualDevices", manualDevices) 1810 Preferences.setMicroPython("ManualDevices", manualDevices)
1797 1811
1798 # rescan the ports 1812 # rescan the ports
1799 self.__populateDeviceTypeComboBox() 1813 self.__populateDeviceTypeComboBox()

eric ide

mercurial