src/eric7/MicroPython/WifiDialogs/WifiNetworksWindow.py

branch
mpy_network
changeset 9781
3112f77f722b
child 9789
d8e0ab86ddca
equal deleted inserted replaced
9779:8d3c7c991085 9781:3112f77f722b
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2023 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a dialog showing the available WiFi networks.
8 """
9
10 from PyQt6.QtCore import Qt, QTimer, pyqtSlot
11 from PyQt6.QtWidgets import QHeaderView, QTreeWidgetItem, QWidget
12
13 from eric7.EricGui.EricOverrideCursor import EricOverrideCursor
14 from eric7.EricWidgets import EricMessageBox
15
16 from .Ui_WifiNetworksWindow import Ui_WifiNetworksWindow
17
18
19 class WifiNetworksWindow(QWidget, Ui_WifiNetworksWindow):
20 """
21 Class implementing a dialog showing the available WiFi networks.
22 """
23
24 def __init__(self, device, parent=None):
25 """
26 Constructor
27
28 @param device reference to the connected device
29 @type BaseDevice
30 @param parent reference to the parent widget (defaults to None)
31 @type QWidget (optional)
32 """
33 super().__init__(parent)
34 self.setupUi(self)
35
36 windowFlags = self.windowFlags()
37 windowFlags |= Qt.WindowType.Window
38 windowFlags |= Qt.WindowType.WindowContextHelpButtonHint
39 self.setWindowFlags(windowFlags)
40
41 self.__device = device
42
43 self.__scanTimer = QTimer(self)
44 self.__scanTimer.timeout.connect(self.scanNetworks)
45
46 self.scanButton.clicked.connect(self.scanNetworks)
47
48 self.networkList.sortByColumn(0, Qt.SortOrder.AscendingOrder)
49
50 def scanNetworks(self):
51 """
52 Private method to ask the device for a network scan and display the result.
53 """
54 self.networkList.clear()
55 self.statusLabel.clear()
56
57 if not self.periodicCheckBox.isChecked():
58 self.scanButton.setEnabled(False)
59 with EricOverrideCursor():
60 networks, error = self.__device.scanNetworks()
61 if not self.periodicCheckBox.isChecked():
62 self.scanButton.setEnabled(True)
63
64 if error:
65 EricMessageBox.warning(
66 self,
67 self.tr("Scan WiFi Networks"),
68 self.tr(
69 """<p>The scan for available WiFi networks failed.</p>"""
70 """<p>Reason: {0}</p>"""),
71 )
72 if self.periodicCheckBox.isChecked():
73 self.periodicCheckBox.setChecked(False)
74
75 else:
76 self.statusLabel.setText(
77 self.tr("<p>Detected <b>%n</b> network(s).</p>", "", len(networks))
78 )
79 for network in networks:
80 itm = QTreeWidgetItem(
81 self.networkList,
82 [
83 network[0],
84 str(network[2]),
85 network[1],
86 str(network[3]),
87 network[4]
88 ],
89 )
90 itm.setTextAlignment(1, Qt.AlignmentFlag.AlignHCenter)
91 itm.setTextAlignment(2, Qt.AlignmentFlag.AlignHCenter)
92 itm.setTextAlignment(3, Qt.AlignmentFlag.AlignHCenter)
93
94 self.__resizeColumns()
95 self.__resort()
96
97 def __resort(self):
98 """
99 Private method to resort the networks list.
100 """
101 self.networkList.sortItems(
102 self.networkList.sortColumn(),
103 self.networkList.header().sortIndicatorOrder(),
104 )
105
106 def __resizeColumns(self):
107 """
108 Private method to resize the columns of the result list.
109 """
110 self.networkList.header().resizeSections(
111 QHeaderView.ResizeMode.ResizeToContents
112 )
113 self.networkList.header().setStretchLastSection(True)
114
115 def closeEvent(self, evt):
116 """
117 Public method to handle a window close event.
118
119 @param evt reference to the close event
120 @type QCloseEvent
121 """
122 self.__scanTimer.stop()
123
124 @pyqtSlot(bool)
125 def on_periodicCheckBox_toggled(self, checked):
126 """
127 Private slot handling the selection of a periodic scan.
128
129 @param checked flag indicating a periodic scan
130 @type bool
131 """
132 self.scanButton.setEnabled(not checked)
133 if checked:
134 self.__scanTimer.setInterval(self.intervalSpinBox.value() * 1000)
135 self.__scanTimer.start()
136 else:
137 self.__scanTimer.stop()
138
139 @pyqtSlot(int)
140 def on_intervalSpinBox_valueChanged(self, interval):
141 """
142 Private slot handling a change of the periodic scan interval.
143
144 @param interval periodic scan interval
145 @type int
146 """
147 if self.periodicCheckBox.isChecked():
148 self.__scanTimer.setInterval(interval* 1000)

eric ide

mercurial