-
Notifications
You must be signed in to change notification settings - Fork 0
/
PluginDialog.py
197 lines (164 loc) · 7.59 KB
/
PluginDialog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
PluginDialog.py
MIT License (c) Faure Systems <dev at faure dot systems>
Dialog to control PluginProps app running on Raspberry.
"""
import os, re
from PluginSettingsDialog import PluginSettingsDialog
from AppletDialog import AppletDialog
from LedWidget import LedWidget
from PyQt5.QtGui import QIcon, QPalette
from PyQt5.QtCore import Qt, pyqtSignal, pyqtSlot, QSize, QPoint, QSettings
from PyQt5.QtWidgets import QHBoxLayout, QVBoxLayout, QFrame
from PyQt5.QtWidgets import QTextEdit, QPlainTextEdit, QPushButton, QComboBox
class PluginDialog(AppletDialog):
aboutToClose = pyqtSignal()
publishMessage = pyqtSignal(str, str)
switchLed = pyqtSignal(str, str)
# __________________________________________________________________
def __init__(self, title, icon, logger):
super().__init__(title, icon, logger)
# always on top sometimes doesn't work
self.setAttribute(Qt.WA_AlwaysStackOnTop)
self.setWindowFlags(self.windowFlags()
& ~Qt.WindowContextHelpButtonHint | Qt.WindowStaysOnTopHint)
self._props_inbox = False
self._room_admin = False
# __________________________________________________________________
def _buildUi(self):
self._options = {}
if os.path.isfile('definitions.ini'):
definitions = QSettings('definitions.ini', QSettings.IniFormat)
for group in definitions.childGroups():
definitions.beginGroup(group)
if group == "options":
for key in definitions.childKeys():
self._options[key] = definitions.value(key)
definitions.endGroup()
main_layout = QVBoxLayout()
main_layout.setSpacing(12)
self._led = LedWidget(self.tr("Arduino Echo"), QSize(40, 20))
self._led.setRedAsBold(True)
self._led.setRedAsRed(True)
self._led.switchOn('gray')
settings_button = QPushButton()
settings_button.setIcon(QIcon("./images/settings.svg"))
settings_button.setFlat(True)
settings_button.setToolTip(self.tr("Configuration"))
settings_button.setIconSize(QSize(16, 16))
settings_button.setFixedSize(QSize(24, 24))
header_layout = QHBoxLayout()
header_layout.addWidget(self._led)
header_layout.addWidget(settings_button, Qt.AlignRight)
main_layout.addLayout(header_layout)
self._selectionComboBox = QComboBox()
self.loadMessages();
main_layout.addWidget(self._selectionComboBox)
self._props_messages_input = QPlainTextEdit()
self._props_messages_input.setFrameShape(QFrame.NoFrame)
self._props_messages_input.setCursorWidth(8)
main_layout.addWidget(self._props_messages_input)
buttons_layout = QHBoxLayout()
main_layout.addLayout(buttons_layout)
send_button = QPushButton(self.tr("Send"))
buttons_layout.addWidget(send_button)
clear_button = QPushButton(self.tr("Clear"))
buttons_layout.addWidget(clear_button)
self._props_messages_display = QTextEdit()
self._props_messages_display.setReadOnly(True)
main_layout.addWidget(self._props_messages_display)
main_layout.addStretch(0)
self.setLayout(main_layout)
self._selectionComboBox.activated.connect(self.onSelectionBox)
clear_button.pressed.connect(self.onClearButton)
send_button.pressed.connect(self.onSendButton)
settings_button.pressed.connect(self.settings)
self.switchLed.connect(self._led.switchOn)
# __________________________________________________________________
@pyqtSlot(str, str)
def onMessage(self, topic, message):
if topic == self._room_admin and not self._props_inbox:
self._logger.warning(
"{0} : {1}".format(self.tr("Room administrator topic is not defined, message ignored"), "---"))
else:
if message == 'yes':
self._props_messages_display.setVisible(True)
elif message == 'no':
self._props_messages_display.setVisible(False)
else:
self._logger.warning(
"{0} : {1}".format(message, self.tr("unexpected message from Room administrator topic")))
# __________________________________________________________________
@pyqtSlot(str)
def onPropsMessage(self, message):
if message.startswith("DISCONNECTED"):
self._led.switchOn('yellow')
else:
if self._led.color() != 'green':
self._led.switchOn('green')
self._props_messages_display.append(message)
# __________________________________________________________________
@pyqtSlot()
def onClearButton(self):
if not self._props_inbox:
self._logger.warning(
"{0} : {1}".format(self.tr("Echo props inbox is not defined, message ignored"), "---"))
else:
self.publishMessage.emit(self._props_inbox, "echo:---")
self._props_messages_input.clear()
self._selectionComboBox.setCurrentIndex(0)
# __________________________________________________________________
@pyqtSlot()
def onSendButton(self):
message = self._props_messages_input.toPlainText().strip()
if not self._props_inbox:
self._logger.warning(
"{0} : {1}".format(self.tr("Echo props inbox is not defined, message ignored"), message))
else:
if len(message) == 0:
message = "---"
self.publishMessage.emit(self._props_inbox, "echo:" + message)
self._props_messages_input.clear()
self._selectionComboBox.setCurrentIndex(0)
# __________________________________________________________________
@pyqtSlot('int')
def onSelectionBox(self, index):
self._props_messages_input.setPlainText(self._selectionComboBox.itemData(index))
# __________________________________________________________________
def setPropsInboxTopic(self, inbox):
self._props_inbox = inbox
# __________________________________________________________________
def setRoomAdminTopic(self, topic):
self._room_admin = topic
# __________________________________________________________________
def loadMessages(self):
settings = QSettings("settings.ini", QSettings.IniFormat);
settings.setIniCodec("UTF-8");
settings.beginGroup("Parameters")
lang = settings.value('param', 'en')
settings.endGroup()
predefined_messages = {}
if lang == 'fr':
predefined_messages["Charger un message..."] = None
predefined_messages['Bonjour le Monde'] = 'Bonjour le Monde'
predefined_messages['Salut les copains'] = 'Salut les copains!\nComment allez-vous?'
else:
predefined_messages["Load message..."] = None
predefined_messages['Hello World'] = 'Hello World'
predefined_messages['Hello folks'] = 'Hello folks!\nHow are you doing?'
self._selectionComboBox.clear()
for key in predefined_messages:
self._selectionComboBox.addItem(key, predefined_messages[key])
# __________________________________________________________________
def closeEvent(self, e):
self.aboutToClose.emit()
# __________________________________________________________________
@pyqtSlot()
def settings(self):
dlg = PluginSettingsDialog(self._logger)
dlg.setModal(True)
dlg.move(self.pos() + QPoint(20, 20))
dlg.exec()
self.loadMessages()