89 lines
2.6 KiB
Python
89 lines
2.6 KiB
Python
import time
|
|
from PyQt6.QtWidgets import (
|
|
QWidget,
|
|
QVBoxLayout,
|
|
QLabel,
|
|
QRadioButton,
|
|
QPushButton,
|
|
QButtonGroup,
|
|
)
|
|
from PyQt6.QtCore import Qt, QTimer
|
|
|
|
from logger import logger
|
|
|
|
class ChangeRobot(QWidget):
|
|
def __init__(self, robots, updateRobot, status):
|
|
super().__init__()
|
|
self.robots = robots or []
|
|
self.updateRobot = updateRobot
|
|
self.status = status
|
|
|
|
self.initUI()
|
|
self.old_status = None
|
|
self.counter = 0
|
|
|
|
self.timer = QTimer(self)
|
|
self.timer.timeout.connect(self.timerCallback)
|
|
|
|
def initUI(self):
|
|
self.layout = QVBoxLayout()
|
|
|
|
self.robotsLabel = QLabel("Выберите робота")
|
|
self.layout.addWidget(self.robotsLabel)
|
|
|
|
self.buttonGroup = QButtonGroup(self)
|
|
self.robotsRadio = []
|
|
|
|
for i, r in enumerate(self.robots):
|
|
radioButton = QRadioButton(r["name"])
|
|
self.buttonGroup.addButton(radioButton, i)
|
|
self.robotsRadio.append(radioButton)
|
|
self.layout.addWidget(radioButton)
|
|
|
|
self.connectButton = QPushButton("Соединить")
|
|
self.connectButton.clicked.connect(self.connectRobot)
|
|
self.layout.addWidget(self.connectButton)
|
|
|
|
self.setLayout(self.layout)
|
|
self.updateConnectButtonText()
|
|
|
|
def updateConnectButtonText(self):
|
|
radioState = True
|
|
logger.info(self.status())
|
|
if self.status() == "not_connected":
|
|
self.connectButton.setText("Соединить")
|
|
elif self.status() == "connected":
|
|
self.connectButton.setText("Отключить")
|
|
radioState = False
|
|
|
|
for radioButton in self.robotsRadio:
|
|
radioButton.setEnabled(radioState)
|
|
|
|
def timerCallback(self):
|
|
new_status = self.status()
|
|
self.counter += 1
|
|
if new_status == self.old_status and self.counter <= 10:
|
|
self.connectButton.setText(self.connectButton.text() + ".")
|
|
else:
|
|
self.old_status = None
|
|
self.timer.stop()
|
|
self.updateConnectButtonText()
|
|
|
|
def connectRobot(self):
|
|
self.updateConnectButtonText()
|
|
selectedId = self.buttonGroup.checkedId()
|
|
if selectedId == -1:
|
|
self.connectButton.setText("Выберите робота")
|
|
return
|
|
|
|
self.old_status = self.status()
|
|
self.timer.start(500)
|
|
time.sleep(0.3)
|
|
self.updateRobot(self.robots[selectedId])
|
|
|
|
def paintEvent(self, event):
|
|
p = self.palette()
|
|
p.setColor(self.backgroundRole(), Qt.GlobalColor.lightGray)
|
|
self.setPalette(p)
|
|
super().paintEvent(event)
|