Mon, 05 May 2025 17:40:08 +0200
MicroPython
- Added support for IPv6 for WiFi and Ethernet enabled devices (MPy ≥ 1.24.0).
9878 | 1 | # -*- coding: utf-8 -*- |
2 | ||
11090
f5f5f5803935
Updated copyright for 2025.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
3 | # Copyright (c) 2023 - 2025 Detlev Offenbach <detlev@die-offenbachs.de> |
9878 | 4 | # |
5 | ||
6 | """ | |
7 | Module implementing a dialog to show Ethernet related status information. | |
8 | """ | |
9 | ||
11263 | 10 | from PyQt6.QtCore import Qt, pyqtSlot |
9878 | 11 | from PyQt6.QtWidgets import QDialog, QDialogButtonBox, QTreeWidgetItem |
12 | ||
11263 | 13 | from eric7.EricGui import EricPixmapCache |
14 | from eric7.SystemUtilities.NetworkUtilities import ipv6AddressScope | |
15 | ||
9878 | 16 | from .Ui_EthernetStatusDialog import Ui_EthernetStatusDialog |
17 | ||
18 | ||
19 | class EthernetStatusDialog(QDialog, Ui_EthernetStatusDialog): | |
20 | """ | |
21 | Class implementing a dialog to show Ethernet related status information. | |
22 | """ | |
23 | ||
11263 | 24 | def __init__(self, microPython, parent=None): |
9878 | 25 | """ |
26 | Constructor | |
27 | ||
11263 | 28 | @param microPython reference to the MicroPython widget |
29 | @type MicroPythonWidget | |
9878 | 30 | @param parent reference to the parent widget (defaults to None) |
31 | @type QWidget (optional) | |
32 | """ | |
33 | super().__init__(parent) | |
34 | self.setupUi(self) | |
11263 | 35 | self.setWindowFlags(Qt.WindowType.Window) |
9878 | 36 | |
37 | self.statusTree.setColumnCount(2) | |
38 | ||
11263 | 39 | self.refreshButton.setIcon(EricPixmapCache.getIcon("reload")) |
40 | self.refreshButton.clicked.connect(self.__showStatus) | |
41 | ||
42 | self.__mpy = microPython | |
43 | ||
44 | self.__showStatus() | |
45 | ||
46 | @pyqtSlot() | |
47 | def __showStatus(self): | |
48 | """ | |
49 | Private slot to show the current WiFi status. | |
50 | """ | |
51 | # clear old data | |
52 | self.statusTree.clear() | |
53 | ||
54 | # get the status | |
55 | try: | |
56 | status, addressInfo = self.__mpy.getDevice().getEthernetStatus() | |
57 | except Exception as exc: | |
58 | self.__mpy.showError("getEthernetStatus()", str(exc)) | |
59 | return | |
60 | ||
9878 | 61 | for topic, value in status: |
62 | QTreeWidgetItem(self.statusTree, [topic, str(value)]) | |
63 | ||
11263 | 64 | if addressInfo["ipv4"]: |
65 | header = self.__createHeader(self.statusTree, self.tr("IPv4")) | |
66 | QTreeWidgetItem(header, [self.tr("Address"), addressInfo["ipv4"][0]]) | |
67 | QTreeWidgetItem(header, [self.tr("Netmask"), addressInfo["ipv4"][1]]) | |
68 | QTreeWidgetItem(header, [self.tr("Gateway"), addressInfo["ipv4"][2]]) | |
69 | QTreeWidgetItem(header, [self.tr("DNS"), addressInfo["ipv4"][3]]) | |
70 | ||
71 | if addressInfo["ipv6"]: | |
72 | header = self.__createHeader(self.statusTree, self.tr("IPv6")) | |
73 | addrHeader = self.__createHeader( | |
74 | header, self.tr("Addresses"), underlined=False | |
75 | ) | |
76 | for addr in sorted(addressInfo["ipv6"]): | |
77 | QTreeWidgetItem(addrHeader, [addr.lower(), ipv6AddressScope(addr)]) | |
78 | ||
9878 | 79 | for col in range(self.statusTree.columnCount()): |
80 | self.statusTree.resizeColumnToContents(col) | |
81 | ||
82 | self.buttonBox.button(QDialogButtonBox.StandardButton.Close).setDefault(True) | |
83 | self.buttonBox.setFocus(Qt.FocusReason.OtherFocusReason) | |
11263 | 84 | |
85 | def __createHeader(self, parent, text, underlined=True): | |
86 | """ | |
87 | Private method to create a subheader item. | |
88 | ||
89 | @param parent reference to the parent item | |
90 | @type QTreeWidgetItem | |
91 | @param text text for the header item | |
92 | @type str | |
93 | @param underlined flag indicating an underlined header (defaults to True) | |
94 | @type bool (optional) | |
95 | @return reference to the created header item | |
96 | @rtype QTreeWidgetItem | |
97 | """ | |
98 | headerItem = QTreeWidgetItem(parent, [text]) | |
99 | headerItem.setExpanded(True) | |
100 | headerItem.setFirstColumnSpanned(True) | |
101 | ||
102 | if underlined: | |
103 | font = headerItem.font(0) | |
104 | font.setUnderline(True) | |
105 | headerItem.setFont(0, font) | |
106 | ||
107 | return headerItem |