src/eric7/MicroPython/BluetoothDialogs/BluetoothController.py

branch
mpy_network
changeset 9855
c9244db5566a
child 9857
0122ae72618d
equal deleted inserted replaced
9854:c1e298e5c588 9855:c9244db5566a
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2023 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the Bluetooth related functionality.
8 """
9
10 from PyQt6.QtCore import QObject, pyqtSlot
11 from PyQt6.QtWidgets import QMenu
12
13 from eric7.EricWidgets import EricMessageBox
14
15
16 class BluetoothController(QObject):
17 """
18 Class implementing the Bluetooth related functionality.
19 """
20
21 def __init__(self, microPython, parent=None):
22 """
23 Constructor
24
25 @param microPython reference to the MicroPython widget
26 @type MicroPythonWidgep
27 @param parent reference to the parent object (defaults to None)
28 @type QObject (optional)
29 """
30 super().__init__(parent)
31
32 self.__mpy = microPython
33
34 def createMenu(self, menu):
35 """
36 Public method to create the Bluetooth submenu.
37
38 @param menu reference to the parent menu
39 @type QMenu
40 @return reference to the created menu
41 @rtype QMenu
42 """
43 btMenu = QMenu(self.tr("Bluetooth Functions"), menu)
44 btMenu.setTearOffEnabled(True)
45 btMenu.addAction(self.tr("Show Bluetooth Status"), self.__showBtStatus)
46 btMenu.addSeparator()
47 btMenu.addSeparator()
48 btMenu.addAction(
49 self.tr("Activate Bluetooth Interface"),
50 lambda: self.__activateInterface(),
51 )
52 btMenu.addAction(
53 self.tr("Deactivate Bluetooth Interface"),
54 lambda: self.__deactivateInterface(),
55 )
56
57 # add device specific entries (if there are any)
58 self.__mpy.getDevice().addDeviceBluetoothEntries(btMenu)
59
60 return btMenu
61
62 @pyqtSlot()
63 def __showBtStatus(self):
64 """
65 Private slot to show the status and some parameters of the Bluetooth interface.
66 """
67 from .BluetoothStatusDialog import BluetoothStatusDialog
68
69 try:
70 status = self.__mpy.getDevice().getBluetoothStatus()
71 # status is a list of user labels and associated values
72
73 dlg = BluetoothStatusDialog(status, self.__mpy)
74 dlg.exec()
75 except Exception as exc:
76 self.__mpy.showError("getBluetoothStatus()", str(exc))
77
78 @pyqtSlot()
79 def __activateInterface(self):
80 """
81 Private slot to activate the Bluetooth interface.
82 """
83 try:
84 status = self.__mpy.getDevice().activateBluetoothInterface()
85 if status:
86 EricMessageBox.information(
87 None,
88 self.tr("Activate Bluetooth Interface"),
89 self.tr("""Bluetooth was activated successfully."""),
90 )
91 else:
92 EricMessageBox.warning(
93 None,
94 self.tr("Activate Bluetooth Interface"),
95 self.tr("""Bluetooth could not be activated."""),
96 )
97 except Exception as exc:
98 self.__mpy.showError("activateBluetoothInterface()", str(exc))
99
100 @pyqtSlot()
101 def __deactivateInterface(self):
102 """
103 Private slot to deactivate the Bluetooth interface.
104 """
105 try:
106 status = self.__mpy.getDevice().deactivateBluetoothInterface()
107 if not status:
108 EricMessageBox.information(
109 None,
110 self.tr("Deactivate Bluetooth Interface"),
111 self.tr("""Bluetooth was deactivated successfully."""),
112 )
113 else:
114 EricMessageBox.warning(
115 None,
116 self.tr("Deactivate Bluetooth Interface"),
117 self.tr("""Bluetooth could not be deactivated."""),
118 )
119 except Exception as exc:
120 self.__mpy.showError("deactivateBluetoothInterface()", str(exc))

eric ide

mercurial